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.CollectionUtils;
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.core.exception.BusinessException;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.fire.domain.DemMicroStation;
import com.usky.fire.mapper.DemMicroStationMapper;
import com.usky.fire.service.BaseCompanyService;
import com.usky.fire.service.DemMicroStationService;
import com.usky.fire.service.DemWaterSourceService;
import com.usky.fire.service.vo.DemMicroStationExportVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 微型消防站 服务实现类
 * </p>
 *
 * @author JCB
 * @since 2022-10-24
 */
@Service
public class DemMicroStationServiceImpl extends AbstractCrudService<DemMicroStationMapper, DemMicroStation> implements DemMicroStationService {

    @Autowired
    private BaseCompanyService baseCompanyService;
    @Autowired
    private DemWaterSourceService demWaterSourceService;

    @Override
    public CommonPage<Map<String, Object>> microStationList(String squadron, String stationName, String stationType, Integer id, Integer pageNum, Integer pageSize) {
        List<Map<String, Object>> list = new ArrayList<>();
        IPage<DemMicroStation> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<DemMicroStation> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(DemMicroStation::getId, DemMicroStation::getBranchName, DemMicroStation::getSquadron,
                DemMicroStation::getStationName, DemMicroStation::getCompanyNature, DemMicroStation::getAddress,
                DemMicroStation::getLocation, DemMicroStation::getFixedPhone, DemMicroStation::getChargeName,
                DemMicroStation::getChargePhone, DemMicroStation::getStationType, DemMicroStation::getLongitude,
                DemMicroStation::getDimension, DemMicroStation::getCreateTime, DemMicroStation::getUpdateTime)
                .eq(DemMicroStation::getDeleteFlag, 0)
                .eq(StringUtils.isNotBlank(stationType), DemMicroStation::getStationType, stationType)
                .like(StringUtils.isNotBlank(squadron), DemMicroStation::getSquadron, squadron)
                .like(StringUtils.isNotBlank(stationName), DemMicroStation::getStationName, stationName)
                .orderByDesc(DemMicroStation::getId);
        page = this.page(page, queryWrapper);
        if (CollectionUtils.isNotEmpty(page.getRecords())) {
            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("branchName", page.getRecords().get(i).getBranchName());
                map.put("squadron", page.getRecords().get(i).getSquadron());
                map.put("stationName", page.getRecords().get(i).getStationName());
                map.put("companyNature", page.getRecords().get(i).getCompanyNature());
                map.put("address", page.getRecords().get(i).getAddress());
                map.put("location", page.getRecords().get(i).getLocation());
                map.put("fixedPhone", page.getRecords().get(i).getFixedPhone());
                map.put("chargeName", page.getRecords().get(i).getChargeName());
                map.put("chargePhone", page.getRecords().get(i).getChargePhone());
                map.put("stationType", page.getRecords().get(i).getStationType());
                map.put("longitude", page.getRecords().get(i).getLongitude());
                map.put("dimension", page.getRecords().get(i).getDimension());
                map.put("createTime", page.getRecords().get(i).getCreateTime());
                map.put("updateTime", page.getRecords().get(i).getUpdateTime());
                list.add(map);
            }
        }
        return new CommonPage<>(list, page.getTotal(), pageSize, pageNum);
    }

    @Override
    public List<DemMicroStationExportVO> microStationListExport(String squadron, String stationName, String stationType){
        List<DemMicroStationExportVO> list = new ArrayList<>();
        LambdaQueryWrapper<DemMicroStation> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(DemMicroStation::getId, DemMicroStation::getBranchName, DemMicroStation::getSquadron,
                DemMicroStation::getStationName, DemMicroStation::getCompanyNature, DemMicroStation::getAddress,
                DemMicroStation::getLocation, DemMicroStation::getFixedPhone, DemMicroStation::getChargeName,
                DemMicroStation::getChargePhone, DemMicroStation::getStationType, DemMicroStation::getLongitude,
                DemMicroStation::getDimension, DemMicroStation::getCreateTime, DemMicroStation::getUpdateTime)
                .eq(DemMicroStation::getDeleteFlag, 0)
                .eq(StringUtils.isNotBlank(stationType), DemMicroStation::getStationType, stationType)
                .like(StringUtils.isNotBlank(squadron), DemMicroStation::getSquadron, squadron)
                .like(StringUtils.isNotBlank(stationName), DemMicroStation::getStationName, stationName)
                .orderByDesc(DemMicroStation::getId);
        List<DemMicroStation> list1 = this.list(queryWrapper);
        if (CollectionUtils.isNotEmpty(list1)) {
            for (int i = 0; i < list1.size(); i++) {
                DemMicroStationExportVO demMicroStationExportVO = new DemMicroStationExportVO();
                demMicroStationExportVO.setBranchName(list1.get(i).getBranchName());
                demMicroStationExportVO.setSquadron(list1.get(i).getSquadron());
                demMicroStationExportVO.setStationName(list1.get(i).getStationName());
                demMicroStationExportVO.setCompanyNature(list1.get(i).getCompanyNature());
                demMicroStationExportVO.setAddress(list1.get(i).getAddress());
                demMicroStationExportVO.setFixedPhone(list1.get(i).getFixedPhone());
                demMicroStationExportVO.setChargeName(list1.get(i).getChargeName());
                demMicroStationExportVO.setChargePhone(list1.get(i).getChargePhone());
                demMicroStationExportVO.setStationType(list1.get(i).getStationType());
                demMicroStationExportVO.setLongitude(list1.get(i).getLongitude());
                demMicroStationExportVO.setDimension(list1.get(i).getDimension());
                demMicroStationExportVO.setCreateTime(list1.get(i).getCreateTime());
                demMicroStationExportVO.setUpdateTime(list1.get(i).getUpdateTime());
                demMicroStationExportVO.setLocation(list1.get(i).getLocation());
                list.add(demMicroStationExportVO);
            }
        }

        return list;
    }

    @Override
    public void updateMicroStation(DemMicroStation demMicroStation) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        demMicroStation.setUpdateTime(dateFormat.format(new Date()));
        this.updateById(demMicroStation);
    }

    @Override
    public void delMicroStation(Integer id) {
        DemMicroStation demMicroStation = new DemMicroStation();
        demMicroStation.setId(id);
        demMicroStation.setDeleteFlag("1");
        this.updateById(demMicroStation);
    }

    @Override
    public List<Map<String, Object>> microStationDroplistList(String branchName) {
        List<Map<String, Object>> list = new ArrayList<>();
        LambdaQueryWrapper<DemMicroStation> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(DemMicroStation::getSquadron)
                .eq(DemMicroStation::getDeleteFlag, "0")
                .eq(StringUtils.isNotBlank(branchName), DemMicroStation::getBranchName, branchName)
                .groupBy(DemMicroStation::getSquadron);
        List<DemMicroStation> list1 = this.list(queryWrapper);
        if (CollectionUtils.isNotEmpty(list1)) {
            for (int i = 0; i < list1.size(); i++) {
                Map<String, Object> map = new HashMap<>();
                map.put("squadron", list1.get(i).getSquadron());
                list.add(map);
            }
        }
        return list;
    }


    @Override
    public List<Object> microStationScatterer(String streetTown) {
        IPage<DemMicroStation> page = new Page<>(1, 500);
        LambdaQueryWrapper<DemMicroStation> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(DemMicroStation::getId, DemMicroStation::getStationName, DemMicroStation::getLongitude,
                DemMicroStation::getDimension)
                .eq(DemMicroStation::getDeleteFlag, "0")
                .eq(StringUtils.isNotBlank(streetTown), DemMicroStation::getRemark, streetTown)
                .orderByDesc(DemMicroStation::getId);
        page = this.page(page, queryWrapper);
        List<DemMicroStation> list1 = page.getRecords();
        List<Object> list = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(list1)) {
            for (int i = 0; i < list1.size(); i++) {
                Map<String, Object> map = new HashMap<>();
                map.put("id", list1.get(i).getId());
                map.put("stationName", list1.get(i).getStationName());
                map.put("longitude", list1.get(i).getLongitude());
                map.put("latitude", list1.get(i).getDimension());
                list.add(map);
            }
        }
        return list;
    }

    @Override
    public List<Object> synthesizePoliceSituationScatterer(String streetTown, String scattererType) {
        List<Object> list = new ArrayList<>();
        switch (scattererType) {
            case "消防站":
                list = this.microStationScatterer(streetTown);
                break;
            case "监控":
                break;
            case "重点单位":
                list = baseCompanyService.companyScatterer(streetTown, "1");
                break;
            case "消防车":
                break;
            case "消火栓/天然水源":
                list = demWaterSourceService.waterSourceList(streetTown);
                break;
            default:
                throw new BusinessException("该类型点位未定义");
        }
        return list;
    }

    @Override
    public CommonPage<DemMicroStation> page(Integer current, Integer size, String stationName) {
        IPage<DemMicroStation> page = new Page<>(current, size);
        LambdaQueryWrapper<DemMicroStation> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(StringUtils.isNotBlank(stationName), DemMicroStation::getStationName, stationName).eq(DemMicroStation::getDeleteFlag,0)
                .orderByDesc(DemMicroStation::getId);
        page = this.page(page, queryWrapper);
        return new CommonPage<>(page.getRecords(), page.getTotal(), size, current);
    }

}
