package com.friday.tooldriver.controller.external;

import com.friday.base.common.response.DubboResult;
import com.friday.base.web.response.ResponseAspect;
import com.friday.operation.api.keepalive.dto.smartrpa.LicenseCreateDevDto;
import com.friday.operation.api.keepalive.dto.smartrpa.MachineLicenseAcquireReqDto;
import com.friday.operation.api.keepalive.dto.smartrpa.MachineLicenseAuthUpdateDto;
import com.friday.operation.api.keepalive.dto.smartrpa.MachineLicenseInfoReq;
import com.friday.operation.api.tooldriver.service.ILicense;
import com.friday.tooldriver.po.taieoc.MachineLicensePo;
import com.friday.tooldriver.utils.DESUtils;
import com.friday.tooldriver.utils.SM2Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 */

@RestController
@RequestMapping(value = "/driver/machine/license")
@Slf4j
public class RobotRpaLicenseController {

    @Autowired
    private ResponseAspect responseAspect;

    @Autowired
    private ILicense licenseManager;


    @RequestMapping(value = "/getRobotLicenseInfo", method = RequestMethod.POST)
    public Object getRobotLicenseInfo(@RequestBody MachineLicenseInfoReq req) {
        return licenseManager.getRobotLicenseInfo(req);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object updateMachineLicense(@RequestBody MachineLicenseAuthUpdateDto req) {
        DubboResult<Integer> result = licenseManager.updateMachineLicense(req);
        return this.responseAspect.response(result);
    }

    @RequestMapping(value = "/checkRobotLicense", method = RequestMethod.POST)
    public Object checkRobotLicense(@RequestBody MachineLicenseAuthUpdateDto dto) {

        return licenseManager.checkRobotLicenseTime(dto);
    }


    @RequestMapping(value = "/createLicenseForDev", method = RequestMethod.POST)
    public DubboResult<String> createLicense(@RequestBody LicenseCreateDevDto dto) {
        String strRobotNum = dto.getRobotNum();
        String endTime = dto.getEndTime();
        MachineLicenseAcquireReqDto reqDto = new MachineLicenseAcquireReqDto();
        reqDto.setMachineCode(dto.getMachineKey());
        reqDto.setUser(dto.getUserName());
//        code+user
        reqDto.setLicenseType(dto.getLicenseType());
        reqDto.setMachineType(dto.getMachineType());
//        下属数字员工个数
        String miyao = DESUtils.encrypt(strRobotNum, DESUtils.SECRET_KEY);
//        String decrypt = DESUtils.decrypt(miyao, DESUtils.SECRET_KEY);
        System.out.println("执行入参：" + reqDto);
        MachineLicensePo licenseAAA = createLicenseAAA(reqDto, endTime);
        String resultLicense = licenseAAA.getLicense() + "%%&&" + miyao;
        System.out.println("执行许可：" + resultLicense);
        return DubboResult.newSuccess("生成许可：", resultLicense);
    }

    @Transactional(rollbackFor = Exception.class)
    public MachineLicensePo createLicenseAAA(MachineLicenseAcquireReqDto reqDto, String expireTime) {
        try {

            String license = SM2Utils.generateLicense(reqDto.getLicenseType(), reqDto.getMachineType()
                    , expireTime, reqDto.getParamsMap());
            MachineLicensePo licensePo = MachineLicensePo.builder()
                    .license(license)
                    .build();
            return licensePo;
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return null;
        }

    }
}