package com.chushang.inspection.project.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.chushang.common.core.exception.utils.AssertUtil;
import com.chushang.common.mybatis.page.CommonParam;
import com.chushang.common.mybatis.utils.PageResult;
import com.chushang.common.mybatis.utils.WrapperUtils;
import com.chushang.datascope.annotation.DataScope;
import com.chushang.inspection.project.dto.AuditDTO;
import com.chushang.inspection.project.dto.PageAuditDTO;
import com.chushang.inspection.project.po.WrkProjectContract;
import com.chushang.security.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chushang.inspection.project.po.WrkAudit;
import com.chushang.inspection.project.mapper.WrkAuditMapper;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @auther: zhao
 * @date: 2024/6/15 10:51
 */
@Slf4j
@Service
public class WrkAuditService extends ServiceImpl<WrkAuditMapper, WrkAudit> implements IService<WrkAudit> {

    @Resource
    WrkProjectContractService contractService;
    @Resource
    WrkProjectService wrkProjectService;


    /**
     * 合同 或者 项目 审核
     * 合同 只有 初审
     */
    @Transactional
    public void audit(Long contractId, AuditDTO audit) {
        // 去查询合同信息, 需要确保合同存在,
        // 合同信息
        WrkProjectContract contract = contractService.getById(contractId);
        AssertUtil.invalidate(ObjectUtil.isEmpty(contract), "待审核合同为空");
        Long projectId = contract.getProjectId();
        // 当前审核状态
        Integer currAuditStatus = contract.getAuditStatus();
        // 审核状态
        Integer auditStatus = audit.getAuditStatus();
        // 待 初审
        if (1 == currAuditStatus) {
            AssertUtil.invalidate(3 != auditStatus && 5 != auditStatus, "待初审时, 审核状态应当为初审通过或者初审驳回");
        }
        // 合同撤销
        saveAudit(contractId, 1, audit.getAuditStatus(), audit.getRemark());
        // 审核之后同步修改 项目状态
        wrkProjectService.updateAuditStatus(projectId, auditStatus);
    }


    /**
     * 获取 合同以及合同对应的 审核列表
     */
    public WrkProjectContract getContractInfo(Long contractId) {
        // 合同信息
        WrkProjectContract contract = contractService.getById(contractId);
        if (null != contract) {
            PageAuditDTO pageAuditDTO = new PageAuditDTO();
            pageAuditDTO.setThirdIds(Collections.singletonList(contractId));
            contract.setAudits(allList(pageAuditDTO));
        }
        return contract;
    }

    @DataScope
    public List<WrkAudit> allList(PageAuditDTO auditDTO) {
        CommonParam commonParam = CommonParam.buildAllRequest();
        LambdaQueryWrapper<WrkAudit> auditSql = WrapperUtils.builder(auditDTO, commonParam);
        return this.list(auditSql);
    }

    @Transactional
    public void revoke(Long contractId) {
        WrkProjectContract contract = contractService.getById(contractId);
        AssertUtil.invalidate(ObjectUtil.isEmpty(contract), "无法撤销, 当前合同不存在");

        Integer auditStatus = contract.getAuditStatus();
        AssertUtil.invalidate(3 == auditStatus, "无法撤销, 当前审核已通过");
        // 合同撤销
        saveAudit(contractId, 1, 7, "");
    }

    public void saveAudit(Long thirdId, Integer thirdType, Integer auditStatus, String remark){
        WrkAudit wrkAudit = new WrkAudit();
        wrkAudit.setThirdId(thirdId);
        wrkAudit.setThirdType(thirdType);
        wrkAudit.setAuditStatus(auditStatus);
        wrkAudit.setAuditName(SecurityUtils.getUsername());
        wrkAudit.setAuditUserId(SecurityUtils.getUserId());
        wrkAudit.setRemark(remark);
        wrkAudit.setAuditTime(LocalDateTime.now());
        // 同时需要更新 合同内的审核状态
        save(wrkAudit);
        // 此处需要判断 审核状态
        /**
         * 未提交	0
         * 待初审	1
         * 待复审	2
         * 初审通过	3
         * 复审通过	4
         * 初审驳回	5
         * 复审驳回	6
         * 已撤销	7
         */
//        if (Objects.equals(wrkAudit.getThirdType(), 0)) {
//            // 项目审核
////            projectService.updateState(byId.getThirdId(), auditStatus);
//        } else {
        // 合同审核
        contractService.updateAuditStatus(wrkAudit.getThirdId(), auditStatus);
//        }
    }
}
