package com.zcloud.primeport.command;

import com.alibaba.cola.exception.BizException;
import com.alibaba.fastjson.JSON;
import com.zcloud.gbscommon.todolistmq.TodoListEventPusherUtil;
import com.zcloud.gbscommon.utils.DateUtil;
import com.zcloud.primeport.domain.enums.AuditEnum;
import com.zcloud.primeport.domain.gateway.VehicleApplyGateway;
import com.zcloud.primeport.domain.gateway.VehicleAuditGateway;
import com.zcloud.primeport.domain.model.VehicleApplyE;
import com.zcloud.primeport.domain.model.VehicleAuditE;
import com.zcloud.primeport.dto.VehicleApplyInspectUpdateCmd;
import com.zcloud.primeport.dto.VehicleApplyUpdateCmd;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


/**
 * web-app
 *
 * @Author makejava
 * @Date 2026-03-05 11:33:24
 */
@Component
@AllArgsConstructor
public class VehicleApplyUpdateExe {
    private final VehicleApplyGateway vehicleApplyGateway;
    private final VehicleAuditGateway vehicleAuditGateway;
    private final TodoListEventPusherUtil todoListEventPusherUtil;

    @Transactional(rollbackFor = Exception.class)
    public void execute(VehicleApplyUpdateCmd vehicleApplyUpdateCmd) {
        VehicleApplyE vehicleApplyE = new VehicleApplyE();
        VehicleApplyE oldV = vehicleApplyGateway.getById(vehicleApplyUpdateCmd.getId());
        BeanUtils.copyProperties(vehicleApplyUpdateCmd, vehicleApplyE);
        vehicleApplyE.setAuditFlag(AuditEnum.WAITING.getCode());
//        vehicleApplyE.setApplyTime(DateUtil.date2Str(new Date()));
        boolean res = vehicleApplyGateway.update(vehicleApplyE);
//        VehicleApplyE nweV = vehicleApplyGateway.getById(vehicleApplyUpdateCmd.getId());
//        nweV.setAuditCorpId();
//        VehicleApplyE newV = vehicleApplyGateway.getById(vehicleApplyUpdateCmd.getId());
        vehicleApplyE.setVehicleType(oldV.getVehicleType());
        vehicleApplyE.setLicenceNo(oldV.getLicenceNo());
        vehicleApplyE.setVehicleBelongType(oldV.getVehicleBelongType());
        VehicleAuditE build = VehicleAuditE.builder()
                .vehicleApplyId(vehicleApplyE.getId())
                .batchState(2).batchType(2)
                .auditUserId(vehicleApplyE.getAuditUserId())
                .auditUserName(vehicleApplyE.getAuditUserName())
                .auditDeptId(vehicleApplyE.getAuditDeptId())
                .auditDeptName(vehicleApplyE.getAuditDeptName())
                .auditCorpId(vehicleApplyE.getAuditCorpId())
                .auditCorpName(vehicleApplyE.getAuditCorpName())
                .auditStatus(AuditEnum.WAITING.getCode())
                .changeAfter(JSON.toJSONString(vehicleApplyE))
                .changeBefore(JSON.toJSONString(oldV)).build();
        // 添加审批信息
        vehicleAuditGateway.add(build);
        // 发送待审核待办
//        todoListEventPusherUtil.sendMessageAddEvent(build.getSendEventObj());
        if (!res) {
            throw new BizException("修改失败");
        }
    }

    public void inspectCarEdit(VehicleApplyInspectUpdateCmd cmd) {
        VehicleApplyE vehicleApplyE = new VehicleApplyE();
        vehicleApplyE.setId(cmd.getId());
        vehicleApplyE.setVehicleType(cmd.getVehicleType());
        vehicleApplyE.setVehicleTypeName(cmd.getVehicleTypeName());
        vehicleApplyE.setLicenceNo(cmd.getLicenceNo());
        vehicleApplyE.setLicenceTypeName(cmd.getLicenceTypeName());
        vehicleApplyE.setLicenceType(cmd.getLicenceType());
        vehicleApplyE.setRemarks(cmd.getRemarks());
        vehicleApplyGateway.update(vehicleApplyE);
    }
}

