package com.usky.fire.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usky.fire.domain.DemGridMember;
import com.usky.fire.domain.DemGridMemderStatistic;
import com.usky.fire.mapper.DemGridMemderStatisticMapper;
import com.usky.fire.service.DemGridMemberService;
import com.usky.fire.service.DemGridMemderStatisticService;
import com.usky.common.mybatis.core.AbstractCrudService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 网格基础信息统计表 服务实现类
 * </p>
 *
 * @author han
 * @since 2023-10-30
 */
@Service
public class DemGridMemderStatisticServiceImpl extends AbstractCrudService<DemGridMemderStatisticMapper, DemGridMemderStatistic> implements DemGridMemderStatisticService {

    @Autowired
    private DemGridMemberService demGridMemberService;

    @Override
    public DemGridMemderStatistic gridMemderStatistic(String gridArea, String mapGridName, Integer gridGrade){
        DemGridMemderStatistic gridMemderStatistic = new DemGridMemderStatistic();

        QueryWrapper<DemGridMemderStatistic> queryWrapper = Wrappers.query();
        if(gridGrade == null){
            queryWrapper.select("SUM(personnel_num) as personnelNum","SUM(village_num) as villageNum","SUM(high_rise_num) as highRiseNum","SUM(office_buildings_num) as officeBuildingsNum","SUM(street_front_num) as streetFrontNum","SUM(key_company_num) as keyCompanyNum","SUM(factory_store_num) as factoryStoreNum","SUM(medical_institution_num) as medicalInstitutionNum","SUM(school_num) as schoolNum","SUM(elder_care_num) as elderCareNum","SUM(large_build_num) as largeBuildNum","SUM(dealer_market_num) as dealerMarketNum","SUM(pulic_entertainment_num) as pulicEntertainmentNum","SUM(religious_places_num) as religiousPlacesNum","SUM(hotel_num) as hotelNum","SUM(scale_rent_num) as scaleRentNum","SUM(gas_station_num) as gasStationNum","SUM(subway_station_num) as subwayStationNum","SUM(construction_site_num) as constructionSiteNum","SUM(community_num) as communityNum","SUM(other_num) as otherNum");

        }else{
            List<DemGridMember> list = new ArrayList<>();
            LambdaQueryWrapper<DemGridMember> queryWrapperList = Wrappers.lambdaQuery();
            queryWrapperList.eq(DemGridMember::getDeleteFlag, 0)
                    .eq(StringUtils.isNotBlank(gridArea),DemGridMember::getGridArea, gridArea)
                    .eq(StringUtils.isNotBlank(mapGridName),DemGridMember::getMapGridName, mapGridName)
                    .eq(gridGrade != null,DemGridMember::getGridGrade, gridGrade);
            list = demGridMemberService.list(queryWrapperList);
            if (CollectionUtils.isNotEmpty(list)) {
                List<Integer> id = new ArrayList<>();
                for (int i = 0; i < list.size(); i++) {
                    id.add(list.get(i).getId());
                }
                if(gridGrade == 2){
                    LambdaQueryWrapper<DemGridMember> queryWrapper1 = Wrappers.lambdaQuery();
                    queryWrapper1.in(DemGridMember::getSeniorGrid,id);
                    List<Integer> idList = new ArrayList<>();
                    List<DemGridMember> queryList = demGridMemberService.list(queryWrapper1);
                    if(queryList.size() > 0){
                        for (int i = 0; i < queryList.size(); i++) {
                            idList.add(queryList.get(i).getId());
                        }
                    }

                    if(idList.size() > 0){
                        queryWrapper.select("SUM(personnel_num) as personnelNum","SUM(village_num) as villageNum","SUM(high_rise_num) as highRiseNum","SUM(office_buildings_num) as officeBuildingsNum","SUM(street_front_num) as streetFrontNum","SUM(key_company_num) as keyCompanyNum","SUM(factory_store_num) as factoryStoreNum","SUM(medical_institution_num) as medicalInstitutionNum","SUM(school_num) as schoolNum","SUM(elder_care_num) as elderCareNum","SUM(large_build_num) as largeBuildNum","SUM(dealer_market_num) as dealerMarketNum","SUM(pulic_entertainment_num) as pulicEntertainmentNum","SUM(religious_places_num) as religiousPlacesNum","SUM(hotel_num) as hotelNum","SUM(scale_rent_num) as scaleRentNum","SUM(gas_station_num) as gasStationNum","SUM(subway_station_num) as subwayStationNum","SUM(construction_site_num) as constructionSiteNum","SUM(community_num) as communityNum","SUM(other_num) as otherNum")
                                .in("grid_member_id",idList);
                    }


                }else if(gridGrade == 3){
                    queryWrapper.select("SUM(personnel_num) as personnelNum","SUM(village_num) as villageNum","SUM(high_rise_num) as highRiseNum","SUM(office_buildings_num) as officeBuildingsNum"
                            ,"SUM(street_front_num) as streetFrontNum","SUM(key_company_num) as keyCompanyNum","SUM(factory_store_num) as factoryStoreNum"
                            ,"SUM(medical_institution_num) as medicalInstitutionNum","SUM(school_num) as schoolNum","SUM(elder_care_num) as elderCareNum"
                            ,"SUM(large_build_num) as largeBuildNum","SUM(dealer_market_num) as dealerMarketNum","SUM(pulic_entertainment_num) as pulicEntertainmentNum"
                            ,"SUM(religious_places_num) as religiousPlacesNum","SUM(hotel_num) as hotelNum","SUM(scale_rent_num) as scaleRentNum"
                            ,"SUM(gas_station_num) as gasStationNum","SUM(subway_station_num) as subwayStationNum","SUM(construction_site_num) as constructionSiteNum"
                            ,"SUM(community_num) as communityNum","SUM(other_num) as otherNum")
                            .in("grid_member_id",id);
                }
            }

        }
        List<Map<String,Object>> maps = this.listMaps(queryWrapper);
        if(maps.size() > 0){
            if(Objects.isNull(maps.get(0).get("personnelNum"))){
                gridMemderStatistic.setPersonnelNum(0);
            }else{
                gridMemderStatistic.setPersonnelNum(Integer.parseInt(maps.get(0).get("personnelNum").toString()));
            }

            if(Objects.isNull(maps.get(0).get("villageNum"))){
                gridMemderStatistic.setVillageNum(0);
            }else{
                gridMemderStatistic.setVillageNum(Integer.parseInt(maps.get(0).get("villageNum").toString()));
            }

            if(Objects.isNull(maps.get(0).get("highRiseNum"))){
                gridMemderStatistic.setHighRiseNum(0);
            }else{
                gridMemderStatistic.setHighRiseNum(Integer.parseInt(maps.get(0).get("highRiseNum").toString()));
            }

            if(Objects.isNull(maps.get(0).get("officeBuildingsNum"))){
                gridMemderStatistic.setOfficeBuildingsNum(0);
            }else{
                gridMemderStatistic.setOfficeBuildingsNum(Integer.parseInt(maps.get(0).get("officeBuildingsNum").toString()));
            }

            if(Objects.isNull(maps.get(0).get("streetFrontNum"))){
                gridMemderStatistic.setStreetFrontNum(0);
            }else{
                gridMemderStatistic.setStreetFrontNum(Integer.parseInt(maps.get(0).get("streetFrontNum").toString()));
            }

            if(Objects.isNull(maps.get(0).get("keyCompanyNum"))){
                gridMemderStatistic.setKeyCompanyNum(0);
            }else{
                gridMemderStatistic.setKeyCompanyNum(Integer.parseInt(maps.get(0).get("keyCompanyNum").toString()));
            }

            if(Objects.isNull(maps.get(0).get("factoryStoreNum"))){
                gridMemderStatistic.setFactoryStoreNum(0);
            }else{
                gridMemderStatistic.setFactoryStoreNum(Integer.parseInt(maps.get(0).get("factoryStoreNum").toString()));
            }

            if(Objects.isNull(maps.get(0).get("medicalInstitutionNum"))){
                gridMemderStatistic.setMedicalInstitutionNum(0);
            }else{
                gridMemderStatistic.setMedicalInstitutionNum(Integer.parseInt(maps.get(0).get("medicalInstitutionNum").toString()));
            }

            if(Objects.isNull(maps.get(0).get("schoolNum"))){
                gridMemderStatistic.setSchoolNum(0);
            }else{
                gridMemderStatistic.setSchoolNum(Integer.parseInt(maps.get(0).get("schoolNum").toString()));
            }

            if(Objects.isNull(maps.get(0).get("elderCareNum"))){
                gridMemderStatistic.setElderCareNum(0);
            }else{
                gridMemderStatistic.setElderCareNum(Integer.parseInt(maps.get(0).get("elderCareNum").toString()));
            }

            if(Objects.isNull(maps.get(0).get("largeBuildNum"))){
                gridMemderStatistic.setLargeBuildNum(0);
            }else{
                gridMemderStatistic.setLargeBuildNum(Integer.parseInt(maps.get(0).get("largeBuildNum").toString()));
            }

            if(Objects.isNull(maps.get(0).get("dealerMarketNum"))){
                gridMemderStatistic.setDealerMarketNum(0);
            }else{
                gridMemderStatistic.setDealerMarketNum(Integer.parseInt(maps.get(0).get("dealerMarketNum").toString()));
            }

            if(Objects.isNull(maps.get(0).get("pulicEntertainmentNum"))){
                gridMemderStatistic.setPulicEntertainmentNum(0);
            }else{
                gridMemderStatistic.setPulicEntertainmentNum(Integer.parseInt(maps.get(0).get("pulicEntertainmentNum").toString()));
            }

            if(Objects.isNull(maps.get(0).get("religiousPlacesNum"))){
                gridMemderStatistic.setReligiousPlacesNum(0);
            }else{
                gridMemderStatistic.setReligiousPlacesNum(Integer.parseInt(maps.get(0).get("religiousPlacesNum").toString()));
            }

            if(Objects.isNull(maps.get(0).get("hotelNum"))){
                gridMemderStatistic.setHotelNum(0);
            }else{
                gridMemderStatistic.setHotelNum(Integer.parseInt(maps.get(0).get("hotelNum").toString()));
            }

            if(Objects.isNull(maps.get(0).get("scaleRentNum"))){
                gridMemderStatistic.setScaleRentNum(0);
            }else{
                gridMemderStatistic.setScaleRentNum(Integer.parseInt(maps.get(0).get("scaleRentNum").toString()));
            }

            if(Objects.isNull(maps.get(0).get("gasStationNum"))){
                gridMemderStatistic.setGasStationNum(0);
            }else{
                gridMemderStatistic.setGasStationNum(Integer.parseInt(maps.get(0).get("gasStationNum").toString()));
            }

            if(Objects.isNull(maps.get(0).get("subwayStationNum"))){
                gridMemderStatistic.setSubwayStationNum(0);
            }else{
                gridMemderStatistic.setSubwayStationNum(Integer.parseInt(maps.get(0).get("subwayStationNum").toString()));
            }

            if(Objects.isNull(maps.get(0).get("constructionSiteNum"))){
                gridMemderStatistic.setConstructionSiteNum(0);
            }else{
                gridMemderStatistic.setConstructionSiteNum(Integer.parseInt(maps.get(0).get("constructionSiteNum").toString()));
            }

            if(Objects.isNull(maps.get(0).get("communityNum"))){
                gridMemderStatistic.setCommunityNum(0);
            }else{
                gridMemderStatistic.setCommunityNum(Integer.parseInt(maps.get(0).get("communityNum").toString()));
            }

            if(Objects.isNull(maps.get(0).get("otherNum"))){
                gridMemderStatistic.setOtherNum(0);
            }else{
                gridMemderStatistic.setOtherNum(Integer.parseInt(maps.get(0).get("otherNum").toString()));
            }

        }

        return gridMemderStatistic;
    }
}
