package com.zcloud.primeport.command.query;

import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.SingleResponse;
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.VehicleApplyCoConvertor;
import com.zcloud.primeport.dto.VehicleApplyPageQry;
import com.zcloud.primeport.dto.clientobject.AppCountCO;
import com.zcloud.primeport.dto.clientobject.FgsVehicleCountCo;
import com.zcloud.primeport.dto.clientobject.VehicleApplyCO;
import com.zcloud.primeport.persistence.dataobject.AppCountDTO;
import com.zcloud.primeport.persistence.dataobject.FgsVehicleCountDto;
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.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * web-app
 *
 * @Author makejava
 * @Date 2026-03-05 11:33:24
 */
@Component
@AllArgsConstructor
public class VehicleApplyQueryExe {
    private final VehicleApplyRepository vehicleApplyRepository;
    private final VehicleAuditRepository vehicleAuditRepository;
    private final VehicleApplyCoConvertor vehicleApplyCoConvertor;

    /**
     * 分页
     *
     * @param vehicleApplyPageQry
     * @return
     */
    public PageResponse<VehicleApplyCO> execute(VehicleApplyPageQry vehicleApplyPageQry) {
        Map<String, Object> parmas = PageQueryHelper.toHashMap(vehicleApplyPageQry);
        PageResponse<VehicleApplyDO> pageResponse = vehicleApplyRepository.listPage(parmas);
        List<VehicleApplyCO> examCenterCOS = vehicleApplyCoConvertor.converDOsToCOs(pageResponse.getData());
        return PageResponse.of(examCenterCOS, pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());
    }

    public VehicleApplyCO getInfoById(Long id) {
        VehicleApplyDO vehicleApplyDO = vehicleApplyRepository.getInfoById(id);
        QueryWrapper<VehicleAuditDO> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("vehicle_apply_id", id);
        objectQueryWrapper.orderByDesc("id");
        ArrayList<VehicleApplyCO.VehicleAuditLog> vehicleAuditLogs = new ArrayList<>();
        List<VehicleAuditDO> list = vehicleAuditRepository.list(objectQueryWrapper);
        list.forEach(item -> {
            VehicleApplyCO.VehicleAuditLog vehicleAuditLog = new VehicleApplyCO.VehicleAuditLog();
            BeanUtils.copyProperties(item, vehicleAuditLog);
            vehicleAuditLogs.add(vehicleAuditLog);
        });
        VehicleApplyCO vehicleApplyCO = vehicleApplyCoConvertor.converDOToCO(vehicleApplyDO);
        vehicleApplyCO.setReasonVisit(vehicleApplyCO.getRemarks());
        vehicleApplyCO.setVehicleAuditLogList(vehicleAuditLogs);
        if (!list.isEmpty()) {
            VehicleAuditDO vehicleAuditDO = list.get(0);
            vehicleApplyCO.setCurrentAuditCorpId(vehicleAuditDO.getAuditCorpId());
            vehicleApplyCO.setCurrentAuditCorpName(vehicleAuditDO.getAuditCorpName());
            vehicleApplyCO.setCurrentAuditDeptId(vehicleAuditDO.getAuditDeptId());
            vehicleApplyCO.setCurrentAuditDeptName(vehicleAuditDO.getAuditDeptName());
            vehicleApplyCO.setCurrentAuditUserId(vehicleAuditDO.getAuditUserId());
            vehicleApplyCO.setCurrentAuditUserName(vehicleAuditDO.getAuditUserName());
            vehicleApplyCO.setCurrentAuditStatus(vehicleAuditDO.getAuditStatus());
            vehicleApplyCO.setCurrentRemarks(vehicleAuditDO.getRemarks());
        }
        return vehicleApplyCO;
    }

    public PageResponse<FgsVehicleCountCo> fgsCount(HashMap<String, String> qry) {
        PageResponse<FgsVehicleCountDto> pageResponse = vehicleApplyRepository.fgsCount(qry);
        List<FgsVehicleCountCo> examCenterCOS = vehicleApplyCoConvertor.converCOsToDtos(pageResponse.getData());
        return PageResponse.of(examCenterCOS, pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());

    }

    public PageResponse<VehicleApplyCO> getCarByVehicleBelongType(VehicleApplyPageQry qry) {
        Map<String, Object> parmas = PageQueryHelper.toHashMap(qry);
        PageResponse<VehicleApplyDO> pageResponse = vehicleApplyRepository.getCarByVehicleBelongType(parmas);
        List<VehicleApplyCO> examCenterCOS = vehicleApplyCoConvertor.converDOsToCOs(pageResponse.getData());
        return PageResponse.of(examCenterCOS, pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());

    }

    public MultiResponse<AppCountCO> getAppCount() {
        Map<String, Object> parmas = new HashMap<>();
        parmas.put("userId", AuthContext.getUserId());
        List<AppCountDTO> appCountDTO = vehicleApplyRepository.getAppCount(parmas);
        return MultiResponse.of(vehicleApplyCoConvertor.converAppCountDTOsToAppCountCOs(appCountDTO));
    }
}

