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.zcloud.gbscommon.todolistmq.TodoListEventPusherUtil;
import com.zcloud.gbscommon.todolistmq.event.TodoListAddEvent;
import com.zcloud.gbscommon.zclouduser.facade.ZcloudUserFacade;
import com.zcloud.gbscommon.zclouduser.response.ZcloudUserCo;
import com.zcloud.primeport.domain.config.MessageTemplateConfig;
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 MessageNoticeExe messageNoticeExe;
    private final MessageTemplateConfig messageTemplateConfig;
    @DubboReference
    private ZcloudUserFacade zcloudUserFacade;

    @Transactional(rollbackFor = Exception.class)
    public Long execute(ClosedAreaCarApplyAddCmd cmd) {
        ClosedAreaCarApplyE closedAreaCarApplyE = new ClosedAreaCarApplyE();
        BeanUtils.copyProperties(cmd, closedAreaCarApplyE);
        closedAreaCarApplyE.setAuditFlag(AuditEnum.WAITING.getCode());
        closedAreaCarApplyE.setExpiredExpiration("2");
        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 = closedAreaCarApplyGateway.add(closedAreaCarApplyE);
        if (!res) {
            throw new BizException("保存失败");
        }

        // 发送消息通知 和 待办
        if (cmd.getAuditPersonUserId() != null) {
            // 发送消息提醒
            Map <String, Object> sendParams = new HashMap <>();
            messageNoticeExe.sendMessageNotice(cmd.getAuditPersonUserId(), messageTemplateConfig.getClosedAreaApplyReceiveAudit(), sendParams);
            // 发送待办
            messageNoticeExe.sendMessageTodoEvent(cmd.getAuditPersonUserId(), "您有一条封闭区域审核，请及时进行审核。", "待审核", closedAreaCarApplyE.getId(), closedAreaCarApplyE.getId(), 1, 1);
        }

        return closedAreaCarApplyE.getId();
    }
}

