package com.zcloud.primeport.command;

import com.alibaba.cola.dto.SingleResponse;
import com.jjb.saas.framework.auth.model.SSOUser;
import com.jjb.saas.framework.auth.utils.AuthContext;
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.TodoListAddEvent;
import com.zcloud.gbscommon.utils.UuidUtil;
import com.zcloud.gbscommon.zclouduser.facade.ZcloudUserFacade;
import com.zcloud.gbscommon.zclouduser.response.ZcloudUserCo;
import com.zcloud.primeport.domain.enums.AuditEnum;
import com.zcloud.primeport.domain.enums.CloseCarBelongTypeEnum;
import com.zcloud.primeport.domain.gateway.ClosedAreaCarApplyGateway;
import com.zcloud.primeport.domain.model.ClosedAreaCarApplyE;
import com.zcloud.primeport.dto.ClosedAreaCarApplyAddCmd;
import com.alibaba.cola.exception.BizException;
import jodd.util.Base64;
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.HashMap;
import java.util.Map;


/**
 * web-app
 *
 * @Author dearLin
 * @Date 2026-03-20 10:07:13
 */
@Component
@AllArgsConstructor
public class ClosedAreaCarApplyAddExe {
    private final ClosedAreaCarApplyGateway closedAreaCarApplyGateway;
    private final TodoListEventPusherUtil todoListEventPusherUtil;
    @DubboReference
    private ZcloudUserFacade zcloudUserFacade;
    @DubboReference
    private MessageFacade messageFacade;
    @Transactional(rollbackFor = Exception.class)
    public Long execute(ClosedAreaCarApplyAddCmd cmd) {
        ClosedAreaCarApplyE closedAreaCarApplyE = new ClosedAreaCarApplyE();
        BeanUtils.copyProperties(cmd, closedAreaCarApplyE);
        closedAreaCarApplyE.setAuditFlag(AuditEnum.WAITING.getCode());
        if (!cmd.getCarBelongType().equals(CloseCarBelongTypeEnum.TEMPORARY_VEHICLES.getCode())) {
            SingleResponse<ZcloudUserCo> infoByUserId = zcloudUserFacade.getInfoByUserId(AuthContext.getUserId());
            ZcloudUserCo userData = infoByUserId.getData();
            SSOUser currentUser = AuthContext.getCurrentUser();
            if (cmd.getCarBelongType().equals(CloseCarBelongTypeEnum.FGS_INSIDE_VEHICLES.getCode())) {
                closedAreaCarApplyE.setJurisdictionalCorpId(currentUser.getTenantId());
                closedAreaCarApplyE.setJurisdictionalCorpName(currentUser.getTenantName());
            }
            closedAreaCarApplyE.setApplyPersonCorpId(currentUser.getTenantId());
            closedAreaCarApplyE.setApplyPersonCorpName(currentUser.getTenantName());
            closedAreaCarApplyE.setApplyPersonDepartmentId(currentUser.getOrgId());
            closedAreaCarApplyE.setApplyPersonDepartmentName(currentUser.getOrgName());
            closedAreaCarApplyE.setApplyPersonUserId(currentUser.getUserId());
            closedAreaCarApplyE.setApplyPersonUserName(userData.getName());
            closedAreaCarApplyE.setUserPhone(userData.getPhone());
            closedAreaCarApplyE.setUserCard(userData.getUserIdCard());
            closedAreaCarApplyE.setUserFaceUrl(userData.getUserAvatarUrl());
        } else {
            closedAreaCarApplyE.setUserCard(Base64.encodeToString(cmd.getUserCard()));
        }
        boolean res = false;
        try {
            res = closedAreaCarApplyGateway.add(closedAreaCarApplyE);
            TodoListAddEvent todoListAddEvent = new TodoListAddEvent();
            todoListAddEvent.setForeignKey(closedAreaCarApplyE.getId());
            todoListAddEvent.setForeignSubsidiaryKey(closedAreaCarApplyE.getId());
            todoListAddEvent.setTitle("您有一条封闭区域审核，请及时进行审核。");
            todoListAddEvent.setContent("待审核");
            todoListAddEvent.setReceiveUser(cmd.getAuditPersonUserId());
            todoListAddEvent.setPcFlag(1);
            todoListAddEvent.setAppFlag(1);
            todoListEventPusherUtil.sendMessageAddEvent(todoListAddEvent);
            //封闭区域车辆新增审批
            MessageSendCmd messageSendCmd = new MessageSendCmd();
            messageSendCmd.setBusinessId(UuidUtil.get32UUID());
            MessageTargetCmd messageTargetCmd = new MessageTargetCmd();
            messageTargetCmd.setUserId(cmd.getAuditPersonUserId());
            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);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if (!res) {
            throw new BizException("保存失败");
        }
        return closedAreaCarApplyE.getId();
    }
}

