package com.ningdatech.pmapi.sys.manage;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.ningdatech.pmapi.common.constant.BizConst;
import com.ningdatech.pmapi.fiscal.entity.CompanyFiscalCode;
import com.ningdatech.pmapi.fiscal.service.ICompanyFiscalCodeService;
import com.ningdatech.pmapi.signature.entity.CompanySignature;
import com.ningdatech.pmapi.signature.service.ICompanySignatureService;
import com.ningdatech.pmapi.sys.contants.UserGuidanceContant;
import com.ningdatech.pmapi.sys.model.vo.UserGuidanceDetailVO;
import com.ningdatech.pmapi.sys.model.vo.UserGuidanceVO;
import com.ningdatech.pmapi.user.security.auth.model.UserInfoDetails;
import com.wflow.bean.entity.WflowOrgModels;
import com.wflow.enums.ProcessDefTypeEnum;
import com.wflow.service.OrgProcdefService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * @Classname UserGuidanceManage
 * @Description
 * @Date 2023/8/8 13:49
 * @Author PoffyZhang
 */
@Component
@Slf4j
@AllArgsConstructor
public class UserGuidanceManage {

    private final OrgProcdefService orgProcdefService;

    private final ICompanyFiscalCodeService fiscalCodeService;

    private final ICompanySignatureService signatureService;

    /**
     * 查询用户引导 详情
     * @param user
     * @return
     */
    public UserGuidanceVO detailUserGuidance(UserInfoDetails user) {
        UserGuidanceVO vo = new UserGuidanceVO();
        //单位code
        String empPosUnitCode = user.getEmpPosUnitCode();
        vo.setEmpPosUnitCode(empPosUnitCode);
        //用户信息
        String username = user.getUsername();
        vo.setUsername(username);
        Long userId = user.getUserId();
        vo.setUserId(userId);

        //查询引导详情
        List<UserGuidanceDetailVO> details = Lists.newArrayList();
        UserGuidanceDetailVO orgModelGuidance = new UserGuidanceDetailVO();
        orgModelGuidance.setName(UserGuidanceContant.OrgModel.GUIDANCE_NAME);
        orgModelGuidance.setRemark(UserGuidanceContant.OrgModel.GUIDANCE_REMARK);

        Integer finishedNum = 0;
        //1.去查询 用户所在单位 有没有单位默认流程配置 并且process 不能为空
        WflowOrgModels orgModel = orgProcdefService.getOne(Wrappers.lambdaQuery(WflowOrgModels.class)
                .eq(WflowOrgModels::getOrgCode, empPosUnitCode)
                .eq(WflowOrgModels::getType, ProcessDefTypeEnum.DEFAULT.name())
                .eq(WflowOrgModels::getIsDelete, Boolean.FALSE)
                .last(BizConst.LIMIT_1));
        if(Objects.isNull(orgModel)){
            orgModelGuidance.setPath(UserGuidanceContant.OrgModel.GUIDANCE_PATH1);
            orgModelGuidance.setIsFinish(Boolean.FALSE);
        }else{
            orgModelGuidance.setPath(UserGuidanceContant.OrgModel.GUIDANCE_PATH2 + orgModel.getProcessDefId());
            //判断process是否为空
            if(StringUtils.isNotBlank(orgModel.getProcess()) &&
                !UserGuidanceContant.OrgModel.EMPTY_PROCESS.equals(orgModel.getProcess())){
                orgModelGuidance.setIsFinish(Boolean.TRUE);
                finishedNum ++;
            }else{
                orgModelGuidance.setIsFinish(Boolean.FALSE);
            }
        }
        details.add(orgModelGuidance);

        //2.去查询 财政编码 印章编码
        UserGuidanceDetailVO fiscalCodeGuidance = new UserGuidanceDetailVO();
        fiscalCodeGuidance.setName(UserGuidanceContant.FiscalCode.GUIDANCE_NAME);
        fiscalCodeGuidance.setRemark(UserGuidanceContant.FiscalCode.GUIDANCE_REMARK);
        fiscalCodeGuidance.setPath(UserGuidanceContant.FiscalCode.GUIDANCE_PATH);
        CompanyFiscalCode fiscalCode = fiscalCodeService.getOne(Wrappers.lambdaQuery(CompanyFiscalCode.class)
                .eq(CompanyFiscalCode::getOrganizationCode, empPosUnitCode)
                .last(BizConst.LIMIT_1));
        if(Objects.isNull(fiscalCode)){
            fiscalCodeGuidance.setIsFinish(Boolean.FALSE);
        }else{
            //判断fiscalCode是否为空
            if(StringUtils.isNotBlank(fiscalCode.getFiscalCode())){
                fiscalCodeGuidance.setIsFinish(Boolean.TRUE);
                finishedNum ++;
            }else{
                fiscalCodeGuidance.setIsFinish(Boolean.FALSE);
            }
        }
        details.add(fiscalCodeGuidance);

        UserGuidanceDetailVO signatureGuidance = new UserGuidanceDetailVO();
        signatureGuidance.setName(UserGuidanceContant.Signature.GUIDANCE_NAME);
        signatureGuidance.setRemark(UserGuidanceContant.Signature.GUIDANCE_REMARK);
        signatureGuidance.setPath(UserGuidanceContant.Signature.GUIDANCE_PATH);
        CompanySignature signature = signatureService.getOne(Wrappers.lambdaQuery(CompanySignature.class)
                .eq(CompanySignature::getOrganizationCode, empPosUnitCode)
                .last(BizConst.LIMIT_1));
        if(Objects.isNull(signature)){
            signatureGuidance.setIsFinish(Boolean.FALSE);
        }else{
            //判断印章编码是否为空
            if(StringUtils.isNotBlank(signature.getSealSn())){
                signatureGuidance.setIsFinish(Boolean.TRUE);
                finishedNum ++;
            }else{
                signatureGuidance.setIsFinish(Boolean.FALSE);
            }
        }
        details.add(signatureGuidance);

        if(UserGuidanceContant.ALL_FINISHED_NUM.equals(finishedNum)){
            vo.setIsAllFinished(Boolean.TRUE);
        }
        vo.setDetails(details);
        return vo;
    }
}
