package com.zcloud.primeport.command;

import com.alibaba.cola.exception.BizException;
import com.alibaba.fastjson.JSON;
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.DateUtil;
import com.zcloud.gbscommon.utils.UuidUtil;
import com.zcloud.primeport.domain.enums.AuditEnum;
import com.zcloud.primeport.domain.enums.VehicleBelongTypeEnum;
import com.zcloud.primeport.domain.gateway.VehicleApplyGateway;
import com.zcloud.primeport.domain.gateway.VehicleAuditGateway;
import com.zcloud.primeport.domain.gateway.VehicleMessageGateway;
import com.zcloud.primeport.domain.model.VehicleApplyE;
import com.zcloud.primeport.domain.model.VehicleAuditE;
import com.zcloud.primeport.domain.model.VehicleMessageE;
import com.zcloud.primeport.dto.VehicleApplyAddCmd;
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.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * web-app
 *
 * @Author makejava
 * @Date 2026-03-05 11:33:23
 */
@Component
@AllArgsConstructor
public class VehicleApplyAddExe {
    private final VehicleApplyGateway vehicleApplyGateway;
    private final VehicleAuditGateway vehicleAuditGateway;
    private final VehicleMessageGateway vehicleMessageGateway;
    private final TodoListEventPusherUtil todoListEventPusherUtil;
    @DubboReference
    private MessageFacade messageFacade;
    @Transactional(rollbackFor = Exception.class)
    public VehicleApplyE execute(VehicleApplyAddCmd cmd) {
        VehicleApplyE examTypeE = new VehicleApplyE();
        BeanUtils.copyProperties(cmd, examTypeE);
        examTypeE.setStatusFlag(2);
        examTypeE.setBlockedFlag(2);
        examTypeE.setMkmjPermission(2);
        examTypeE.setAuditFlag(AuditEnum.WAITING.getCode());
//        examTypeE.setApplyTime(DateUtil.date2Str(new Date()));
        // 股份、分公司个人车辆
        if (cmd.getVehicleBelongType().equals(VehicleBelongTypeEnum.JGD_PRI_VEHICLES.getCode())
                || cmd.getVehicleBelongType().equals(VehicleBelongTypeEnum.FGS_PRI_VEHICLES.getCode())) {
            examTypeE.setMkmjPermission(1);
            if (cmd.getEmployeeVehicleUserId() == null) {
                SSOUser user = AuthContext.getCurrentUser();
                if (user != null) {
                    examTypeE.setVehicleCorpId(user.getTenantId());
                    examTypeE.setVehicleCorpName(user.getTenantName());
                    examTypeE.setVehicleDepartmentId(user.getOrgId());
                    examTypeE.setVehicleDepartmentName(user.getOrgName());
                    examTypeE.setEmployeeVehicleUserId(user.getUserId());
                    examTypeE.setEmployeeVehicleUserName(user.getName());
                }
            }
        }
        if (cmd.getVehicleBelongType().equals(VehicleBelongTypeEnum.XGF_VEHICLES.getCode())){
            // 相关方车辆
            SSOUser user = AuthContext.getCurrentUser();
            examTypeE.setVehicleCorpId(user.getTenantId());
            examTypeE.setVehicleCorpName(user.getTenantName());
        }
        if (cmd.getVehicleBelongType().equals(VehicleBelongTypeEnum.TEMP_VEHICLES.getCode())){
            // 临时
            examTypeE.setEmployeeVehicleUserName(cmd.getDrivingUserName());
            examTypeE.setLsUserIdcard(Base64.encodeToString(cmd.getLsUserIdcard()));
        }
        VehicleApplyE add = vehicleApplyGateway.add(examTypeE);
        VehicleAuditE build = VehicleAuditE.builder().vehicleApplyId(examTypeE.getId()).batchState(1).batchType(1)
                .auditUserId(examTypeE.getAuditUserId())
                .auditUserName(examTypeE.getAuditUserName())
                .auditDeptId(examTypeE.getAuditDeptId()).auditDeptName(examTypeE.getAuditDeptName()).auditCorpId(examTypeE.getAuditCorpId()).auditCorpName(examTypeE.getAuditCorpName()).auditStatus(AuditEnum.WAITING.getCode()).changeAfter(JSON.toJSONString(examTypeE)).build();
        // 添加审批信息
        vehicleAuditGateway.add(build);
        TodoListAddEvent todoListAddEvent = new TodoListAddEvent();
        todoListAddEvent.setForeignKey(build.getId());
        todoListAddEvent.setForeignSubsidiaryKey(build.getId());
        todoListAddEvent.setTitle("您有一条口门审核，请及时进行审核。");
        todoListAddEvent.setContent("待审核");
        todoListAddEvent.setReceiveUser(cmd.getAuditUserId());
        todoListAddEvent.setPcFlag(1);
        todoListAddEvent.setAppFlag(1);
        todoListEventPusherUtil.sendMessageAddEvent(todoListAddEvent);
        //
        // 发送待审核消息
        MessageSendCmd messageSendCmd = new MessageSendCmd();
        messageSendCmd.setBusinessId(UuidUtil.get32UUID());
        MessageTargetCmd messageTargetCmd = new MessageTargetCmd();
        messageTargetCmd.setUserId(cmd.getAuditUserId());
        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);
//        todoListEventPusherUtil.sendMessageAddEvent(build.getSendEventObj());

        return add;
    }
}

