package com.hz.pm.api.provincial;

import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hz.pm.api.AppTests;
import com.hz.pm.api.common.model.constant.ProjectDeclareConst;
import com.hz.pm.api.common.model.constant.RegionConst;
import com.hz.pm.api.projectdeclared.converter.ApplicationConverter;
import com.hz.pm.api.projectdeclared.manage.PrequalificationDeclaredProjectManage;
import com.hz.pm.api.projectlib.model.entity.Project;
import com.hz.pm.api.projectlib.model.entity.ProjectApplication;
import com.hz.pm.api.projectlib.model.enumeration.ProjectStatusEnum;
import com.hz.pm.api.projectlib.service.IProjectApplicationService;
import com.hz.pm.api.projectlib.service.IProjectService;
import com.hz.pm.api.provincial.service.IJoinReviewProvincialBureauService;
import com.hz.pm.api.staging.contants.StagingContant;
import com.hz.pm.api.staging.model.entity.ProjectStaging;
import com.hz.pm.api.staging.service.IProjectStagingService;
import com.ningdatech.file.service.FileService;
import com.wflow.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Classname Test
 * @Description
 * @Date 2023/3/2 16:14
 * @Author PoffyZhang
 */
@Slf4j
public class Test extends AppTests {//

    @Autowired
    private PrequalificationDeclaredProjectManage prequalificationDeclaredProjectManage;

    @Autowired
    private IProjectService projectService;

    @Autowired
    private IProjectApplicationService applicationService;

    @Autowired
    private FileService fileService;

    @Autowired
    private IJoinReviewProvincialBureauService joinReviewProvincialBureauService;

    @Autowired
    private IProjectStagingService projectStagingService;

    @org.junit.Test
    public void test() throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {
//        Long timeStamp = System.currentTimeMillis()/1000;
        Long timeStamp = 1677750055L;
        String key = "7196317343a64e67895dc0375c098fe7";
        String url = "/api/v1/foreign/importantProView";
        String methed = "POST";
        String bytesToSign = methed + "\n" + url + "\n" + timeStamp + "\n" + key;

        log.info("" + bytesToSign);

        String secret = "75152a97f20e4c4c854dc6301cf72ad4";
        String str = SecureUtil.hmacSha256(secret).digestBase64(bytesToSign, false);
        log.info("" + str);
    }

    @org.junit.Test
    public void Test3333() {
        String url = "http://devplat.oss-cn-hangzhou.aliyuncs.com/2023/04/13/5b2e1ef734e14437ba350ef97202c5b7.pdf?Expires=1681367401&OSSAccessKeyId=LTAI4GL7uypycnBjiRn55rMG&Signature=5PbIhpHNZvA2o8xYy8Ujw9Ibhl8%3D";
        String s = "Expires";
        System.out.println(url.replaceAll("&?" + s + "=[^&]*&", StringUtils.EMPTY));
    }

    @Value("${spring.profiles.active}")
    private String active;

    @org.junit.Test
    public void Test4444() {
        // 对接省级联审的接口
        Project p = projectService.getById(448);

        List<ProjectApplication> applications = applicationService.list(Wrappers.lambdaQuery(ProjectApplication.class)
                .eq(ProjectApplication::getProjectId, p.getId()));
        if (!joinReviewProvincialBureauService.pushImportProject(
                ApplicationConverter.convertProject(p, applications, fileService, active))) {
            throw new BusinessException("提交省级部门联审失败");
        }
        System.out.println("成功");
    }

    @org.junit.Test
    public void test5() {
        //1. 定时取 项目暂存表的数据 去进行状态继续流转
        List<ProjectStaging> stagingList = projectStagingService.list(Wrappers.lambdaQuery(ProjectStaging.class)
                .eq(ProjectStaging::getDead, Boolean.FALSE)
                .le(ProjectStaging::getNextTime, LocalDateTime.now())
                .le(ProjectStaging::getRetryTimes, StagingContant.Retry.MAX_RETRY_TIMES)
                .orderByAsc(ProjectStaging::getProjectId));

        log.info("需要状态流转的项目 size:{} :{}", stagingList.size(), JSON.toJSONString(stagingList));
    }

    @org.junit.Test
    public void test6() {
        List<Project> projectList = projectService.list(Wrappers.lambdaQuery(Project.class)
                .eq(Project::getStage, ProjectStatusEnum.NOT_APPROVED.getCode())
                .eq(Project::getStatus, ProjectStatusEnum.JOINT_REVIEW_BY_PROVINCIAL_DEPARTMENTS.getCode())
                .ge(Project::getDeclareAmount, ProjectDeclareConst.Number.DECLARE_AMOUNT_JUDGEMENT)
                .eq(Project::getAreaCode, RegionConst.RC_HZ)
                .orderByAsc(Project::getCreateOn));

        log.info("需要去查询省级联审结果的项目 size:{}", projectList.size());
    }
}
