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.zcloud.gbscommon.todolistmq.TodoListEventPusherUtil;
import com.zcloud.gbscommon.todolistmq.event.TodoListAddEvent;
import com.zcloud.gbscommon.utils.DateUtil;
import com.zcloud.primeport.domain.config.MessageTemplateConfig;
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.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 MessageNoticeExe messageNoticeExe;
    private final MessageTemplateConfig messageTemplateConfig;

    @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.setExpiredExpiration("2");
        examTypeE.setAuditFlag(AuditEnum.WAITING.getCode());
        // 股份、分公司个人车辆
        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());
            examTypeE.setVehicleDepartmentId(user.getOrgId());
            examTypeE.setVehicleDepartmentName(user.getOrgName());
            examTypeE.setEmployeeVehicleUserId(user.getUserId());
            examTypeE.setEmployeeVehicleUserName(user.getName());
        }
        // 临时
        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);

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

        return add;
    }
}

