package com.zcloud.primeport.service;

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.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zcloud.primeport.api.VehicleApplyServiceI;
import com.zcloud.primeport.command.VehicleApplyAddExe;
import com.zcloud.primeport.command.VehicleApplyRemoveExe;
import com.zcloud.primeport.command.VehicleApplyUpdateExe;
import com.zcloud.primeport.command.query.VehicleApplyQueryExe;
import com.zcloud.primeport.domain.enums.AuditEnum;
import com.zcloud.primeport.domain.model.VehicleApplyE;
import com.zcloud.primeport.dto.*;
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.dto.clientobject.VehicleAuditCO;
import com.zcloud.primeport.persistence.dataobject.VehicleApplyDO;
import com.zcloud.primeport.persistence.dataobject.VehicleBlackDO;
import com.zcloud.primeport.persistence.repository.VehicleApplyRepository;
import com.zcloud.primeport.persistence.repository.VehicleBlackRepository;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

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
 */
@Service
@AllArgsConstructor
public class VehicleApplyServiceImpl implements VehicleApplyServiceI {
    private final VehicleApplyAddExe vehicleApplyAddExe;
    private final VehicleApplyUpdateExe vehicleApplyUpdateExe;
    private final VehicleApplyRemoveExe vehicleApplyRemoveExe;
    private final VehicleApplyQueryExe vehicleApplyQueryExe;
    private final VehicleApplyRepository vehicleApplyRepository;
    private final VehicleBlackRepository blackRepository;

    @Override
    public PageResponse<VehicleApplyCO> listPage(VehicleApplyPageQry qry) {

        return vehicleApplyQueryExe.execute(qry);
    }

    @Override
    public SingleResponse add(VehicleApplyAddCmd cmd) {
        VehicleApplyE execute = vehicleApplyAddExe.execute(cmd);
        return SingleResponse.of(execute);
    }

    @Override
    public void edit(VehicleApplyUpdateCmd vehicleApplyUpdateCmd) {
        vehicleApplyUpdateExe.execute(vehicleApplyUpdateCmd);
    }

    @Override
    public void remove(Long id) {
        vehicleApplyRemoveExe.execute(id);
    }

    @Override
    public void removeBatch(Long[] ids) {
        vehicleApplyRemoveExe.execute(ids);
    }

    @Override
    public VehicleApplyCO getInfoById(Long id) {
        return vehicleApplyQueryExe.getInfoById(id);
    }

    @Override
    public Map<String, Object> checkLicenceNo(String licenceNo, Long id) {
        QueryWrapper<VehicleApplyDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("licence_no", licenceNo);
        queryWrapper.in("vehicle_belong_type", 1,2,3,4,8,9);
        queryWrapper.eq("delete_enum", "false");
        queryWrapper.ne("audit_flag", 3);
        if (id != null) {
            queryWrapper.ne("id", id);
        }
        Map<String, Object> result = new HashMap<>();
        result.put("available", true);
        if (vehicleApplyRepository.count(queryWrapper) != 0) {
            result.put("available", false);
            result.put("availableMessage", "该车牌号已存在");
        }
        QueryWrapper<VehicleBlackDO> queryBlackWrapper = new QueryWrapper<>();
        queryBlackWrapper.eq("licence_no", licenceNo);
        queryBlackWrapper.eq("delete_enum", "false");
        if (blackRepository.count(queryBlackWrapper) != 0) {
            result.put("available", false);
            result.put("availableMessage", "车牌号已拉黑");
        }
        return result;
    }

    @Override
    public void inspectCarSave(VehicleApplyInspectAddCmd cmd) {
        List<VehicleApplyInspectAddCmd.InspectCar> inspectCarList = cmd.getInspectCarList();
        ArrayList<VehicleApplyDO> objects = new ArrayList<>();
        inspectCarList.forEach(item -> {
            VehicleApplyDO vehicleApplyDO = new VehicleApplyDO();
            vehicleApplyDO.setAuditFlag(AuditEnum.NO_APPROVAL_REQUIRED.getCode());
            vehicleApplyDO.setStatusFlag(2);
            vehicleApplyDO.setMkmjPermission(1);
            vehicleApplyDO.setExpiredExpiration("2");
            vehicleApplyDO.setRemarks(cmd.getCheckDepartmentName());
            vehicleApplyDO.setVehicleBelongType(cmd.getVehicleBelongType());
//            vehicleApplyDO.setApplyTime(DateUtil.date2Str(new Date()));
            BeanUtils.copyProperties(item, vehicleApplyDO);
            objects.add(vehicleApplyDO);
        });
        vehicleApplyRepository.saveBatch(objects);
    }

    @Override
    public void inspectCarEdit(VehicleApplyInspectUpdateCmd cmd) {
        vehicleApplyUpdateExe.inspectCarEdit(cmd);
    }

    @Override
    public PageResponse<FgsVehicleCountCo> fgsCount(HashMap<String, String> qry) {

        return vehicleApplyQueryExe.fgsCount(qry);
    }

    @Override
    public PageResponse<VehicleAuditCO> xgfCarApplyList(XgfApplyCarPageQry qry) {
        return null;
    }

    @Override
    public PageResponse<VehicleApplyCO> getCarByVehicleBelongType(VehicleApplyPageQry qry) {
        return vehicleApplyQueryExe.getCarByVehicleBelongType(qry);
    }

    @Override
    public void xgfCarAuthorization(CarAuthCmd cmd) {
        UpdateWrapper<VehicleApplyDO> objectUpdateWrapper = new UpdateWrapper<>();
        objectUpdateWrapper.set("mkmj_permission", cmd.getMkmjPermission());
        objectUpdateWrapper.eq("id", cmd.getVehicleApplyId());
        vehicleApplyRepository.update(objectUpdateWrapper);
    }

    @Override
    public Map<String, Object> checkLicenceNoBlack(String licenceNo) {
        QueryWrapper<VehicleBlackDO> queryBlackWrapper = new QueryWrapper<>();
        queryBlackWrapper.eq("licence_no", licenceNo);
        queryBlackWrapper.eq("delete_enum", "false");
        Map<String, Object> result = new HashMap<>();
        result.put("available", true);
        if (blackRepository.count(queryBlackWrapper) != 0) {
            result.put("available", false);
            result.put("availableMessage", "车牌号已拉黑");
        }
        return result;
    }

    @Override
    public MultiResponse<AppCountCO> getAppCount() {
        return vehicleApplyQueryExe.getAppCount();
    }
}

