package com.zcloud.primeport.command;

import com.alibaba.cola.exception.BizException;
import com.jjb.saas.message.client.message.facede.MessageFacade;
import com.jjb.saas.message.client.message.request.MessageSendCmd;
import com.jjb.saas.message.client.message.request.MessageTargetCmd;
import com.zcloud.gbscommon.todolistmq.TodoListEventPusherUtil;
import com.zcloud.gbscommon.todolistmq.event.TodoListCompleteEvent;
import com.zcloud.gbscommon.utils.DateUtil;
import com.zcloud.gbscommon.utils.UuidUtil;
import com.zcloud.primeport.domain.gateway.ClosedAreaFgsPersonAreaGateway;
import com.zcloud.primeport.domain.gateway.ClosedAreaFgsPersonAuthGateway;
import com.zcloud.primeport.domain.gateway.ClosedAreaPersonApplyGateway;
import com.zcloud.primeport.domain.model.ClosedAreaFgsPersonAreaE;
import com.zcloud.primeport.domain.model.ClosedAreaFgsPersonAuthE;
import com.zcloud.primeport.domain.model.ClosedAreaPersonApplyE;
import com.zcloud.primeport.dto.ClosedAreaPersonApplyAuditCmd;
import com.zcloud.primeport.dto.ClosedAreaPersonApplyAuthCmd;
import com.zcloud.primeport.dto.ClosedAreaPersonApplyUpdateCmd;
import lombok.AllArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


/**
 * web-app
 *
 * @Author dearLin
 * @Date 2026-03-19 13:59:16
 */
@Component
@AllArgsConstructor
public class ClosedAreaPersonApplyUpdateExe {
    private final ClosedAreaPersonApplyGateway closedAreaPersonApplyGateway;
    private final ClosedAreaFgsPersonAuthGateway areaFgsPersonAuthGateway;
    private final ClosedAreaFgsPersonAreaGateway areaFgsPersonAreaGateway;
    private final TodoListEventPusherUtil todoListEventPusherUtil;
    @DubboReference
    private MessageFacade messageFacade;
    @Transactional(rollbackFor = Exception.class)
    public void execute(ClosedAreaPersonApplyUpdateCmd closedAreaPersonApplyUpdateCmd) {
        ClosedAreaPersonApplyE closedAreaPersonApplyE = new ClosedAreaPersonApplyE();
        BeanUtils.copyProperties(closedAreaPersonApplyUpdateCmd, closedAreaPersonApplyE);
        boolean res = closedAreaPersonApplyGateway.update(closedAreaPersonApplyE);
        if (!res) {
            throw new BizException("修改失败");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void auditPersonApply(ClosedAreaPersonApplyAuditCmd auditCmd) {
        ClosedAreaPersonApplyE closedAreaPersonApplyE = new ClosedAreaPersonApplyE();
        closedAreaPersonApplyE.setId(auditCmd.getId());
        closedAreaPersonApplyE.setAuditFlag(auditCmd.getAuditFlag());
        closedAreaPersonApplyE.setRefusalReason(auditCmd.getRefusalReason());
        closedAreaPersonApplyE.setAuditTime(DateUtil.date2Str(new Date()));
        closedAreaPersonApplyGateway.update(closedAreaPersonApplyE);
        TodoListCompleteEvent todoListCompleteEvent = new TodoListCompleteEvent();
        todoListCompleteEvent.setForeignSubsidiaryKey(auditCmd.getId());
        todoListEventPusherUtil.sendMessageCompleteEvent(todoListCompleteEvent);
        //封闭区人员审核通过
        MessageSendCmd messageSendCmd = new MessageSendCmd();
        messageSendCmd.setBusinessId(UuidUtil.get32UUID());
        MessageTargetCmd messageTargetCmd = new MessageTargetCmd();
        messageTargetCmd.setUserId(auditCmd.getId());
        messageSendCmd.setTargetCmd(messageTargetCmd);
        messageSendCmd.setNeedTokenEnum(false);
        Map<String, Object> sendParams = new HashMap<String, Object>();
        messageSendCmd.setSourceCode("MS000119");
        sendParams.put("HazardDeferralApplicationNumber", 1);
        sendParams.put("ReformHazardNumber", 1);
        messageSendCmd.setParams(sendParams);
        messageFacade.send(messageSendCmd);
    }

    @Transactional(rollbackFor = Exception.class)
    public void authPerson(ClosedAreaPersonApplyAuthCmd cmd) {
        areaFgsPersonAuthGateway.removeByUserId(cmd.getUserId());
        ClosedAreaFgsPersonAuthE closedAreaFgsPersonAuthE = new ClosedAreaFgsPersonAuthE();
        closedAreaFgsPersonAuthE.setUserId(cmd.getUserId());
        closedAreaFgsPersonAuthE.setCorpId(cmd.getCorpId());
        closedAreaFgsPersonAuthE.setMkmjPermission(cmd.getMkmjPermission());
        closedAreaFgsPersonAuthE.setDepartmentId(cmd.getDepartmentId());
        areaFgsPersonAuthGateway.addOrUpdate(closedAreaFgsPersonAuthE);
        areaFgsPersonAreaGateway.removeByFgspersonAuthId(cmd.getUserId());
        ArrayList<ClosedAreaFgsPersonAreaE> objects = new ArrayList<>();
        List<ClosedAreaPersonApplyAuthCmd.FgsPersonArea> areaList = cmd.getAreaList();
        for (ClosedAreaPersonApplyAuthCmd.FgsPersonArea fgsPersonArea : areaList) {
            ClosedAreaFgsPersonAreaE closedAreaFgsPersonAreaE = new ClosedAreaFgsPersonAreaE();
            closedAreaFgsPersonAreaE.setClosedAreaId(fgsPersonArea.getClosedAreaId());
            closedAreaFgsPersonAreaE.setClosedAreaName(fgsPersonArea.getClosedAreaName());
            closedAreaFgsPersonAreaE.setFgsPersonAuthId(fgsPersonArea.getFgsPersonAuthId());
            objects.add(closedAreaFgsPersonAreaE);
        }
        areaFgsPersonAreaGateway.saveBatch(objects);
    }
}

