package com.zcloud.primeport.command.query;

import com.alibaba.cola.dto.PageResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jjb.saas.framework.auth.utils.AuthContext;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import com.zcloud.primeport.command.convertor.VehicleAuditCoConvertor;
import com.zcloud.primeport.dto.VehicleAuditPageQry;
import com.zcloud.primeport.dto.clientobject.VehicleAuditCO;
import com.zcloud.primeport.persistence.dataobject.VehicleApplyDO;
import com.zcloud.primeport.persistence.dataobject.VehicleAuditDO;
import com.zcloud.primeport.persistence.repository.VehicleApplyRepository;
import com.zcloud.primeport.persistence.repository.VehicleAuditRepository;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;


/**
 * web-app
 *
 * @Author makejava
 * @Date 2026-03-05 11:33:25
 */
@Component
@AllArgsConstructor
public class VehicleAuditQueryExe {
    private final VehicleAuditRepository vehicleAuditRepository;
    private final VehicleApplyRepository vehicleApplyRepository;
    private final VehicleAuditCoConvertor vehicleAuditCoConvertor;

    /**
     * 分页
     *
     * @param vehicleAuditPageQry
     * @return
     */
    public PageResponse<VehicleAuditCO> execute(VehicleAuditPageQry vehicleAuditPageQry) {
        Map<String, Object> parmas = PageQueryHelper.toHashMap(vehicleAuditPageQry);
        PageResponse<VehicleAuditDO> pageResponse = vehicleAuditRepository.listPage(parmas);
        List<VehicleAuditCO> examCenterCOS = vehicleAuditCoConvertor.converDOsToCOs(pageResponse.getData());

        for (VehicleAuditCO item : examCenterCOS) {
            if (item.getApplyUserName() == null || item.getApplyUserName().isEmpty()) {
                item.setApplyUserName(item.getEmployeeVehicleUserName());
            }
        }

        Long userId = AuthContext.getUserId();
        if (!examCenterCOS.isEmpty()) {
            examCenterCOS.forEach(item -> {
                item.setCurrentUserCanAudit(2);
                if (item.getAuditStatus() == 1) {
                    if (item.getAuditUserId().equals(userId)) {
                        item.setCurrentUserCanAudit(1);
                    }
                }
            });
        }

        return PageResponse.of(examCenterCOS, pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());
    }

    public PageResponse<VehicleAuditCO> carApprovalLogByvehicleApplyIdList(VehicleAuditPageQry qry) {
        Map<String, Object> parmas = PageQueryHelper.toHashMap(qry);
        PageResponse<VehicleAuditDO> vehicleAuditDOPageResponse = vehicleAuditRepository.carApprovalLogByvehicleApplyIdList(parmas);
        List<VehicleAuditCO> vehicleAuditCOS = vehicleAuditCoConvertor.converDOsToCOs(vehicleAuditDOPageResponse.getData());
        return PageResponse.of(vehicleAuditCOS, vehicleAuditDOPageResponse.getTotalCount(), vehicleAuditDOPageResponse.getPageSize(), vehicleAuditDOPageResponse.getPageIndex());
    }

    public VehicleAuditCO getInfoById(Long id) {

        VehicleAuditDO vehicleAudit = vehicleAuditRepository.getById(id);

        VehicleApplyDO vehicleApplyDO = vehicleApplyRepository.getById(vehicleAudit.getVehicleApplyId());

        VehicleAuditCO vehicleAuditCO = vehicleAuditCoConvertor.converDOToCO(vehicleAudit);
        vehicleAuditCO.setMkmjId(vehicleApplyDO.getMkmjId());
        vehicleAuditCO.setMkmjName(vehicleApplyDO.getMkmjName());

        return vehicleAuditCO;
    }
}

