package com.zcloud.primeport.persistence.repository.impl;

import com.alibaba.cola.dto.PageResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jjb.saas.framework.auth.utils.AuthContext;
import com.jjb.saas.framework.repository.common.PageHelper;
import com.jjb.saas.framework.repository.repo.impl.BaseRepositoryImpl;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import com.zcloud.gbscommon.utils.Query;
import com.zcloud.gbscommon.utils.Tools;
import com.zcloud.primeport.domain.enums.AuditEnum;
import com.zcloud.primeport.domain.enums.MenuEnum;
import com.zcloud.primeport.domain.enums.VehicleBelongTypeEnum;
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.VehicleMessageDO;
import com.zcloud.primeport.persistence.mapper.VehicleApplyMapper;
import com.zcloud.primeport.persistence.repository.VehicleApplyRepository;
import com.zcloud.primeport.plan.mjDevice.dto.OneLevelCarSaveDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * web-infrastructure
 *
 * @Author makejava
 * @Date 2026-03-05 11:33:24
 */
@Service
@RequiredArgsConstructor
public class VehicleApplyRepositoryImpl extends BaseRepositoryImpl<VehicleApplyMapper, VehicleApplyDO> implements VehicleApplyRepository {
    private final VehicleApplyMapper vehicleApplyMapper;

    @Override
    public PageResponse<VehicleApplyDO> listPage(Map<String, Object> params) {
        Page<Map<String, Object>> page = new Page<>(Integer.parseInt(params.get("pageIndex").toString()),
                Integer.parseInt(params.get("pageSize").toString()));
        String menuPerms = "";
        page.setOptimizeJoinOfCountSql(false);
        if (!org.springframework.util.ObjectUtils.isEmpty(params.get("menuPath"))) {
            menuPerms = MenuEnum.getMenuKeyByPath(params.get("menuPath").toString());
        }
        IPage<VehicleApplyDO> result = vehicleApplyMapper.listPage(page, params, menuPerms);
        return PageHelper.pageToResponse(result, result.getRecords());
    }

    @Override
    public PageResponse<FgsVehicleCountDto> fgsCount(HashMap<String, String> qry) {
        Page<Map<String, Object>> page = new Page<>(Integer.parseInt(qry.get("pageIndex").toString()),
                Integer.parseInt(qry.get("pageSize").toString()));
        IPage<FgsVehicleCountDto> result = vehicleApplyMapper.fgsCount(page, qry);

        return PageHelper.pageToResponse(result, result.getRecords());
    }

    @Override
    public PageResponse<VehicleApplyDO> getCarByVehicleBelongType(Map<String, Object> params) {
        IPage<VehicleApplyDO> iPage = new Query<VehicleApplyDO>().getPage(params);
        QueryWrapper<VehicleApplyDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("vehicle_belong_type", params.get("vehicleBelongType"));
        queryWrapper.eq("audit_flag", AuditEnum.APPROVED.getCode());
        queryWrapper.eq("mkmj_permission", 1);
        queryWrapper.like(!Tools.isEmpty(params.get("licenceNo")), "licence_no", params.get("licenceNo"));
        String vehicleBelongType = params.get("vehicleBelongType").toString();
        if (VehicleBelongTypeEnum.JGD_PRI_VEHICLES.getCode().equals(vehicleBelongType)
                || VehicleBelongTypeEnum.FGS_PRI_VEHICLES.getCode().equals(vehicleBelongType)) {
            // 分公司、股份个人车，查询当前登录用户车辆
            queryWrapper.eq("employee_vehicle_user_id", AuthContext.getUserId());
        }
        if (VehicleBelongTypeEnum.JGD_PUB_VEHICLES.getCode().equals(vehicleBelongType) || VehicleBelongTypeEnum.FGS_PUB_VEHICLES.getCode().equals(vehicleBelongType)) {
            // 分公司、股份公车，查询当前登录
            queryWrapper.eq("vehicle_corp_id", AuthContext.getTenantId());
        }
        IPage<VehicleApplyDO> result = vehicleApplyMapper.selectPage(iPage, queryWrapper);
        return PageHelper.pageToResponse(result, result.getRecords());
    }

    @Override
    public VehicleApplyDO getInfoById(Long id) {
        return vehicleApplyMapper.getInfoById(id);
    }

    @Override
    public List<AppCountDTO> getAppCount(Map<String, Object> parmas) {
        List<VehicleApplyDO> appCount = vehicleApplyMapper.getAppCount(parmas);
        if (appCount.isEmpty()) {
            return new ArrayList<>();
        }
        List<AppCountDTO> appCountDTOList = new ArrayList<>();
        appCount.forEach(item ->{
            AppCountDTO appCountDTO = new AppCountDTO();
            appCountDTO.setType(item.getType());
            appCountDTO.setBelongType(item.getBelongType());
            appCountDTO.setAuditUserId(item.getAuditUserId());
            appCountDTO.setWaitAuditCount(item.getWaitAuditCount());
            appCountDTOList.add(appCountDTO);
        });
        return appCountDTOList;
    }

    @Override
    public List<OneLevelCarSaveDto> listTodayHasPort(String licenceNo) {
        return vehicleApplyMapper.listTodayHasPort(licenceNo);
    }
}

