package com.friday.tooldriver.service.rpa.supplier;

import com.friday.base.common.response.DubboResult;
import com.friday.operation.api.keepalive.constant.taieoc.TaieOcConstants;
import com.friday.operation.api.keepalive.dto.smartrpa.MachineLicenseAcquireReqDto;
import com.friday.operation.api.keepalive.dto.smartrpa.MachineLicenseRespDto;
import com.friday.tooldriver.ServiceUTBase;
import com.friday.tooldriver.po.taieoc.MachineLicensePo;
import com.friday.tooldriver.service.rpa.supplier.smartrpa.LicenseManager;
import com.friday.tooldriver.utils.DESUtils;
import com.friday.tooldriver.utils.SM2Utils;
import com.google.gson.Gson;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class LicenseManagerTest extends ServiceUTBase {


    @Autowired
    private LicenseManager licenseManager;

    /**
     * 旧版license生成
     * 对应生成4条license数据，直接入库
     */
    @Test
    public void createNewLicense() {
        MachineLicenseAcquireReqDto reqDto = new MachineLicenseAcquireReqDto();
        reqDto.setMachineCode("SRQYXZPA9HJZP0T1M71S2QG2NKNVSHSWAZ0QJYHJQTCSK6X1C7KG");
        reqDto.setUser("mayux");
//        designLocal
        reqDto.setLicenseType("local");
        reqDto.setMachineType("design");
        System.out.println("执行入参：" + reqDto);
        DubboResult<MachineLicenseRespDto> designLocal = licenseManager.createLicense(reqDto);

//        design remote
        reqDto.setLicenseType("remote");
        System.out.println("执行入参：" + reqDto);
        DubboResult<MachineLicenseRespDto> designRemote = licenseManager.createLicense(reqDto);

//        executor remote
        reqDto.setMachineType("executor");
        System.out.println("执行入参：" + reqDto);
        DubboResult<MachineLicenseRespDto> executorRemote = licenseManager.createLicense(reqDto);

//        executor local
        reqDto.setLicenseType("local");
        System.out.println("执行入参：" + reqDto);
        DubboResult<MachineLicenseRespDto> executorLocal = licenseManager.createLicense(reqDto);
        Assert.assertNotNull(1);
    }


    /**
     * 202406xx
     * 更新后的license生成
     *
     */
    @Test
    public void createLicenseAll() {

        String strRobotNum = "15";
        String endTime = "2024-10-11 17:20:00";

//        下属数字员工个数
        String miyao = DESUtils.encrypt(strRobotNum, DESUtils.SECRET_KEY);
        String decrypt = DESUtils.decrypt(miyao, DESUtils.SECRET_KEY);

//        日期license
        MachineLicenseAcquireReqDto reqDto = new MachineLicenseAcquireReqDto();
        reqDto.setMachineCode("Q3NC9VQP5ZQKDSPEK5BEAXNBXEP58BAXYHSJNTN4ZZX8TP1XYBGG");
        reqDto.setUser("user");

        reqDto.setLicenseType("Q3NC9VQP5ZQKDSPEK5BEAXNBXEP58BAXYHSJNTN4ZZX8TP1XYBGGuser");
        reqDto.setMachineType("ALL");


        System.out.println("执行入参：" + reqDto);
        MachineLicensePo licenseAAA = createLicenseAAA(reqDto, endTime);
        String resultLicense = licenseAAA.getLicense() + "%%&&" + miyao;
        System.out.println("执行许可：" + resultLicense);


    }

    @Value("${license.expire.date}")
    private Integer licenseExpireDate;
    private static final Gson gson = new Gson();

    @Transactional(rollbackFor = Exception.class)
    public MachineLicensePo createLicenseAAA(MachineLicenseAcquireReqDto reqDto, String expireTime) {
        try {
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.YEAR, licenseExpireDate);
//          时间戳毫秒设置为零，对应处理入库时间预防 秒级四舍五入情况，导致入库时间+1s，导致rpa登录校验失败
            cal.set(Calendar.MILLISECOND, 0); //毫秒
            SimpleDateFormat sdf = new SimpleDateFormat(TaieOcConstants.DATE_LONG_FORMAT);
            // 生成license信息
            Date endTime = cal.getTime();
            String license = SM2Utils.generateLicense(reqDto.getLicenseType(), reqDto.getMachineType()
                    , expireTime, reqDto.getParamsMap());

            // license申请信息落库
            MachineLicensePo licensePo = MachineLicensePo.builder()
                    .license(license)
                    .machineKey(reqDto.getMachineCode() + reqDto.getUser())
                    .machineType(reqDto.getMachineType())
                    .licenseType(reqDto.getLicenseType())
                    .requestParam(reqDto.getParamsMap() == null ? TaieOcConstants.EMPTY_STRING : gson.toJson(reqDto.getParamsMap()))
                    .startTime(Calendar.getInstance().getTime())
                    .endTime(endTime)
                    .build();
//            machineLicenseMapper.insert(licensePo);
//            log.info("入库许可数据信息：" + licensePo);
            return licensePo;
        } catch (Exception e) {
//            log.error(e.getMessage(), e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//            log.info(TaieOcManageCode.LICENSE_AQUIRE.getCode()+ TaieOcManageCode.LICENSE_AQUIRE.getMessage());
            return null;
        }

    }
}