package com.usky.fire.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.fire.domain.DemVehicle;
import com.usky.fire.domain.DemVehicleFight;
import com.usky.fire.mapper.DemVehicleFightMapper;
import com.usky.fire.service.DemVehicleFightService;
import com.usky.fire.service.DemVehicleService;
import org.springframework.beans.factory.annotation.Autowired;
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 DemVehicleFightServiceImpl extends AbstractCrudService<DemVehicleFightMapper, DemVehicleFight> implements DemVehicleFightService {
    @Autowired
    private DemVehicleService demVehicleService;

    @Override
    public List<Map<String, Object>> vehicleFightList(String policeId) {
        List<Map<String, Object>> list = new ArrayList<>();
        LambdaQueryWrapper<DemVehicleFight> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(DemVehicleFight::getId, DemVehicleFight::getVehicleId, DemVehicleFight::getCommander,
                DemVehicleFight::getReporter, DemVehicleFight::getSoldier, DemVehicleFight::getDriver,
                DemVehicleFight::getOrgaAbbreviat,DemVehicleFight::getDthh)
                .eq(DemVehicleFight::getCaseId, policeId);
        List<DemVehicleFight> list1 = this.list(queryWrapper);
        if (CollectionUtils.isNotEmpty(list1)) {
            List<String> vehicleIdList = new ArrayList<>();
            for (int i = 0; i < list1.size(); i++) {
                vehicleIdList.add(list1.get(i).getVehicleId());
            }
            List<DemVehicle> demVehicles = demVehicleService.vehicleList(vehicleIdList);
            for (int i = 0; i < list1.size(); i++) {
                Map<String, Object> map = new HashMap<>();
                map.put("commander", list1.get(i).getCommander());
                map.put("reporter", list1.get(i).getReporter());
                map.put("soldier", list1.get(i).getSoldier());
                map.put("driver", list1.get(i).getDriver());
                map.put("orgaAbbreviat", list1.get(i).getOrgaAbbreviat());
                map.put("dthh", list1.get(i).getDthh());
                map.put("licensePlate", null);
                if (CollectionUtils.isNotEmpty(demVehicles)) {
                    for (int j = 0; j < demVehicles.size(); j++) {
                        if (list1.get(i).getVehicleId().equals(demVehicles.get(j).getVehicleId())) {
                            map.put("licensePlate", demVehicles.get(j).getLicensePlate());
                        }
                    }
                }
                list.add(map);
            }
        }
        return list;
    }

}
