package com.usky.fire.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.fire.domain.DemVehicle;
import com.usky.fire.mapper.DemVehicleMapper;
import com.usky.fire.service.DemVehicleService;
import com.usky.fire.service.vo.DemVehicleExportVO;
import org.springframework.data.redis.support.collections.DefaultRedisList;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 车辆信息 服务实现类
 * </p>
 *
 * @author JCB
 * @since 2022-10-19
 */
@Service
public class DemVehicleServiceImpl extends AbstractCrudService<DemVehicleMapper, DemVehicle> implements DemVehicleService {

    @Override
    public CommonPage<Object> vehicleList(String vehicleCode, String vehicleAbbreviat, Integer id, Integer pageNum, Integer pageSize) {
        List<Object> list = new ArrayList<>();
        IPage<DemVehicle> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<DemVehicle> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(DemVehicle::getId, DemVehicle::getVehicleCode, DemVehicle::getVehicleAbbreviat,
                DemVehicle::getRadioChannel, DemVehicle::getRadioCallSign, DemVehicle::getFireOrga,
                DemVehicle::getVehicleClassCode, DemVehicle::getLicensePlate, DemVehicle::getAssetCode,
                DemVehicle::getEquipTime, DemVehicle::getFireFightStatus, DemVehicle::getRecordStatus,
                DemVehicle::getPoliceListCode, DemVehicle::getCommanderName, DemVehicle::getReporter,
                DemVehicle::getDriver, DemVehicle::getSoldier, DemVehicle::getTypeCode,
                DemVehicle::getVehicleStatus)
                .eq(DemVehicle::getDeleteFlag, 0)
                .like(StringUtils.isNotBlank(vehicleAbbreviat), DemVehicle::getVehicleAbbreviat, vehicleAbbreviat)
                .like(StringUtils.isNotBlank(vehicleCode), DemVehicle::getVehicleCode, vehicleCode)
                .eq(id != null && id != 0, DemVehicle::getId, id)
                .orderByDesc(DemVehicle::getId);
        page = this.page(page, queryWrapper);
        for (int i = 0; i < page.getRecords().size(); i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", page.getRecords().get(i).getId());
            map.put("vehicleCode", page.getRecords().get(i).getVehicleCode());
            map.put("vehicleAbbreviat", page.getRecords().get(i).getVehicleAbbreviat());
            map.put("radioChannel", page.getRecords().get(i).getRadioChannel());
            map.put("radioCallSign", page.getRecords().get(i).getRadioCallSign());
            map.put("fireOrga", page.getRecords().get(i).getFireOrga());
            map.put("vehicleClassCode", page.getRecords().get(i).getVehicleClassCode());
            map.put("licensePlate", page.getRecords().get(i).getLicensePlate());
            map.put("assetCode", page.getRecords().get(i).getAssetCode());
            map.put("equipTime", page.getRecords().get(i).getEquipTime());
            map.put("fireFightStatus", page.getRecords().get(i).getFireFightStatus());
            map.put("recordStatus", page.getRecords().get(i).getRecordStatus());
            map.put("policeListCode", page.getRecords().get(i).getPoliceListCode());
            map.put("commanderName", page.getRecords().get(i).getCommanderName());
            map.put("reporter", page.getRecords().get(i).getReporter());
            map.put("driver", page.getRecords().get(i).getDriver());
            map.put("soldier", page.getRecords().get(i).getSoldier());
            map.put("typeCode", page.getRecords().get(i).getTypeCode());
            map.put("vehicleStatus", page.getRecords().get(i).getVehicleStatus());
            list.add(map);
        }
        return new CommonPage<>(list, page.getTotal(), pageSize, pageNum);
    }

    @Override
    public List<DemVehicleExportVO> vehicleListExport(String vehicleCode, String vehicleAbbreviat){
        List<DemVehicleExportVO> list = new ArrayList<>();
        LambdaQueryWrapper<DemVehicle> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(DemVehicle::getId, DemVehicle::getVehicleCode, DemVehicle::getVehicleAbbreviat,
                DemVehicle::getRadioChannel, DemVehicle::getRadioCallSign, DemVehicle::getFireOrga,
                DemVehicle::getVehicleClassCode, DemVehicle::getLicensePlate, DemVehicle::getAssetCode,
                DemVehicle::getEquipTime, DemVehicle::getFireFightStatus, DemVehicle::getRecordStatus,
                DemVehicle::getPoliceListCode, DemVehicle::getCommanderName, DemVehicle::getReporter,
                DemVehicle::getDriver, DemVehicle::getSoldier, DemVehicle::getTypeCode,
                DemVehicle::getVehicleStatus)
                .eq(DemVehicle::getDeleteFlag, 0)
                .like(StringUtils.isNotBlank(vehicleAbbreviat), DemVehicle::getVehicleAbbreviat, vehicleAbbreviat)
                .like(StringUtils.isNotBlank(vehicleCode), DemVehicle::getVehicleCode, vehicleCode)
                .orderByDesc(DemVehicle::getId);
        List<DemVehicle> list1= this.list(queryWrapper);
        for (int i = 0; i < list1.size(); i++) {
            DemVehicleExportVO demVehicleExportVO = new DemVehicleExportVO();
            demVehicleExportVO.setVehicleAbbreviat((list1.get(i).getVehicleAbbreviat()).trim());
            demVehicleExportVO.setRadioChannel(list1.get(i).getRadioChannel());
            demVehicleExportVO.setRadioCallSign(list1.get(i).getRadioCallSign());
            demVehicleExportVO.setFireOrga(list1.get(i).getFireOrga());
            demVehicleExportVO.setVehicleClassCode(list1.get(i).getVehicleClassCode());
            demVehicleExportVO.setLicensePlate(list1.get(i).getLicensePlate());
            demVehicleExportVO.setEquipTime(list1.get(i).getEquipTime());
            demVehicleExportVO.setFireFightStatus(list1.get(i).getFireFightStatus());
            demVehicleExportVO.setRecordStatus(list1.get(i).getRecordStatus());
            demVehicleExportVO.setCommanderName(list1.get(i).getCommanderName());
            demVehicleExportVO.setDriver(list1.get(i).getDriver());
            demVehicleExportVO.setSoldier(list1.get(i).getSoldier());
            demVehicleExportVO.setTypeCode(list1.get(i).getTypeCode());
            demVehicleExportVO.setVehicleStatus(list1.get(i).getVehicleStatus());
            demVehicleExportVO.setAssetCode(list1.get(i).getAssetCode());
            demVehicleExportVO.setVehicleCode(list1.get(i).getVehicleCode());
            demVehicleExportVO.setPoliceListCode(list1.get(i).getPoliceListCode());
            demVehicleExportVO.setReporter(list1.get(i).getReporter());
            list.add(demVehicleExportVO);
        }

        return list;
    }

    @Override
    public void updateVehicle(DemVehicle demVehicle) {
        this.updateById(demVehicle);
    }

    @Override
    public void delVehicle(Integer id) {
        DemVehicle demVehicle = new DemVehicle();
        demVehicle.setId(id);
        demVehicle.setDeleteFlag("1");
        this.updateById(demVehicle);
    }

    @Override
    public List<DemVehicle> vehicleList(List<String> vehicleIdList) {
        LambdaQueryWrapper<DemVehicle> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(DemVehicle::getRadioCallSign, DemVehicle::getVehicleId, DemVehicle::getLicensePlate)
                .in(DemVehicle::getVehicleId, vehicleIdList);
        List<DemVehicle> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public List<DemVehicle> list(String zdCode, String licensePlate) {
        LambdaQueryWrapper<DemVehicle> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(StringUtils.isNotBlank(zdCode),DemVehicle::getFireOrga, zdCode)
                    .eq(StringUtils.isNotBlank(licensePlate),DemVehicle::getLicensePlate, licensePlate);
        return this.list(queryWrapper);
    }

}
