package com.zcloud.primeport.command;

import com.alibaba.cola.exception.BizException;
import com.zcloud.gbscommon.utils.DateUtil;
import com.zcloud.gbscommon.utils.Tools;
import com.zcloud.primeport.domain.config.MessageTemplateConfig;
import com.zcloud.primeport.domain.gateway.MkmjGateGateway;
import com.zcloud.primeport.domain.gateway.PersonApplyGateway;
import com.zcloud.primeport.domain.gateway.XgfApplyPersonGateway;
import com.zcloud.primeport.domain.model.PersonApplyE;
import com.zcloud.primeport.domain.model.XgfApplyPersonE;
import com.zcloud.primeport.dto.PersonApplyUpdateCmd;
import com.zcloud.primeport.dto.PersonAuthCmd;
import com.zcloud.primeport.dto.XgfPersonApplyAuditCmd;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * web-app
 *
 * @Author makejava
 * @Date 2026-03-05 11:33:23
 */
@Component
@AllArgsConstructor
public class PersonApplyUpdateExe {
    private final PersonApplyGateway personApplyGateway;
    private final XgfApplyPersonGateway xgfApplyPersonGateway;
    private final MessageNoticeExe messageNoticeExe;
    private final MessageTemplateConfig messageTemplateConfig;

    @Transactional(rollbackFor = Exception.class)
    public void execute(PersonApplyUpdateCmd personApplyUpdateCmd) {
        PersonApplyE personApplyE = new PersonApplyE();
        BeanUtils.copyProperties(personApplyUpdateCmd, personApplyE);
        boolean res = personApplyGateway.update(personApplyE);
        if (!res) {
            throw new BizException("修改失败");
        }
    }
    @Transactional(rollbackFor = Exception.class)

    public void xgfPersonAudit(XgfPersonApplyAuditCmd cmd) throws Exception {
        XgfApplyPersonE build = XgfApplyPersonE.builder()
                .auditFlag(cmd.getAuditFlag())
                .auditTime(DateUtil.date2Str(new Date()))
                .reasonsRefusal(cmd.getReasonsRefusal()).build();
        build.setId(cmd.getId());
        personApplyGateway.xgfPersonAudit(build);

        // 完成待办
        messageNoticeExe.sendMessageCompleteEvent(build.getId());

        XgfApplyPersonE xgfApplyPersonE = personApplyGateway.getXgfApplyPersonById(build.getId());

        // 发送通过消息通知
        List <PersonApplyE> userApplyList = personApplyGateway.personListByxgfApplyPersonId(build.getId());
        if (userApplyList != null && !userApplyList.isEmpty()) {
            for (PersonApplyE item : userApplyList) {
                if (item.getEmployeePersonUserId() != null) {
                    Map <String, Object> sendParams = new HashMap <>();
                    if (xgfApplyPersonE != null && xgfApplyPersonE.getProjectName() != null) {
                        sendParams.put("projectName", item.getEmployeePersonUserName());
                    }

                    String sourceCode = "";
                    if(cmd.getAuditFlag().equals(2)) {
                        // 通过
                        sourceCode = messageTemplateConfig.getFirstLevelPersonReceiveAudit();
                    } else {
                        // 驳回
                        sourceCode = messageTemplateConfig.getFirstLevelPersonReceiveAuditReject();
                    }
                    messageNoticeExe.sendMessageNotice(item.getEmployeePersonUserId(), messageTemplateConfig.getFirstLevelApplyReceiveCarAudit(), sendParams);
                }
            }
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void authorization(Long applyPersonId, Long XgfApplyPersonId, Integer mkmjPermission, String gate) {
        PersonApplyE personApplyE = new PersonApplyE();
        personApplyE.setId(applyPersonId);
        personApplyE.setMkmjPermission(mkmjPermission);
        personApplyGateway.update(personApplyE);
        XgfApplyPersonE xgfApplyPersonE = new XgfApplyPersonE();
        xgfApplyPersonE.setId(XgfApplyPersonId);
        if (Tools.notEmpty(gate)) {
            xgfApplyPersonE.setGateLevelAuthArea(gate);
        }
        xgfApplyPersonGateway.update(xgfApplyPersonE);
    }

    @Transactional(rollbackFor = Exception.class)
    public void xgfPersonAuthorization(PersonAuthCmd cmd) {
        PersonApplyE personApplyE = new PersonApplyE();
        personApplyE.setId(cmd.getApplyPersonId());
        personApplyE.setMkmjPermission(cmd.getMkmjPermission());
        personApplyGateway.update(personApplyE);
    }
}

