package com.usky.fire.service.impl;

import com.alibaba.fastjson.JSONObject;
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.BaseGgpFacility;
import com.usky.fire.mapper.BaseGgpFacilityMapper;
import com.usky.fire.service.BaseGgpFacilityService;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.fire.service.vo.BaseGgpFacilityListVO;
import com.usky.fire.service.vo.BaseGgpFacilityStatisticVO;
import com.usky.fire.service.vo.BaseGgpFacilityVO;
import com.usky.fire.service.vo.CompanyDataVo;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 地理信息 服务实现类
 * </p>
 *
 * @author han
 * @since 2023-04-10
 */
@Service
public class BaseGgpFacilityServiceImpl extends AbstractCrudService<BaseGgpFacilityMapper, BaseGgpFacility> implements BaseGgpFacilityService {
    @Override
    public List<BaseGgpFacility> baseGgpFacilityList(BaseGgpFacilityVO baseGgpFacilityVO) {
        List<BaseGgpFacility> list = new ArrayList<>();
        if (baseGgpFacilityVO.getId()==null){
            if (baseGgpFacilityVO.getFacilityType().size()==2 && baseGgpFacilityVO.getFacilityType().get(0)==39 && baseGgpFacilityVO.getFacilityType().get(1)==42){
                LambdaQueryWrapper<BaseGgpFacility> query1 = Wrappers.lambdaQuery();
                query1.select(BaseGgpFacility::getId,BaseGgpFacility::getFacilityName,BaseGgpFacility::getAddress,
                        BaseGgpFacility::getFacilityType,BaseGgpFacility::getLongitude,BaseGgpFacility::getLatitude,
                        BaseGgpFacility::getStreetTown,BaseGgpFacility::getContact,BaseGgpFacility::getContactPhone,
                        BaseGgpFacility::getCheckComment);
                query1.eq(StringUtils.isNotBlank(baseGgpFacilityVO.getStreetTown()),BaseGgpFacility::getStreetTown,
                        baseGgpFacilityVO.getStreetTown());
                query1.eq(BaseGgpFacility::getFacilityType,39);
                List<BaseGgpFacility> list1 = this.list(query1);
                LambdaQueryWrapper<BaseGgpFacility> query2 = Wrappers.lambdaQuery();
                query2.eq(StringUtils.isNotBlank(baseGgpFacilityVO.getStreetTown()),BaseGgpFacility::getStreetTown,
                        baseGgpFacilityVO.getStreetTown());
                query2.eq(BaseGgpFacility::getFacilityType,42);
                List<BaseGgpFacility> list2 = this.list(query2);
                list.addAll(list1);
                list.addAll(list2);
            }else {
                LambdaQueryWrapper<BaseGgpFacility> query = Wrappers.lambdaQuery();
                if (baseGgpFacilityVO.getFacilityType().size()==1){
                    if(baseGgpFacilityVO.getFacilityType().get(0)==39){
                        query.select(BaseGgpFacility::getId,BaseGgpFacility::getFacilityName,BaseGgpFacility::getAddress,
                                BaseGgpFacility::getFacilityType,BaseGgpFacility::getLongitude,BaseGgpFacility::getLatitude,
                                BaseGgpFacility::getStreetTown,BaseGgpFacility::getContact,BaseGgpFacility::getContactPhone,
                                BaseGgpFacility::getCheckComment);
                    }
                }
                query.eq(StringUtils.isNotBlank(baseGgpFacilityVO.getStreetTown()),BaseGgpFacility::getStreetTown, baseGgpFacilityVO.getStreetTown());
                query.in(CollectionUtils.isNotEmpty(baseGgpFacilityVO.getFacilityType()),BaseGgpFacility::getFacilityType,
                        baseGgpFacilityVO.getFacilityType());
                list = this.list(query);
            }
        }else {
            LambdaQueryWrapper<BaseGgpFacility> query = Wrappers.lambdaQuery();
            query.eq(BaseGgpFacility::getId, baseGgpFacilityVO.getId());
            list = this.list(query);
        }

//        if (CollectionUtils.isNotEmpty(list)) {
//            for (int j = 0; j < list.size(); j++) {
//                BaseGgpFacilityListVO baseGgpFacilityListVO = new BaseGgpFacilityListVO();
//                Object data = null;
//                data = list.get(j).getExtendData();
//                JSONObject jsonObject = JSONObject.parseObject(data.toString());
//                baseGgpFacilityListVO.setBaseGgpFacility(list.get(j));
//                baseGgpFacilityListVO.setExtendData(jsonObject);
//                list1.add(baseGgpFacilityListVO);
//            }
//        }
        return list;
    }

    @Override
    public List<BaseGgpFacilityStatisticVO> statistic(BaseGgpFacilityVO baseGgpFacilityVO){
        List<BaseGgpFacilityStatisticVO> list = new ArrayList<>();
        QueryWrapper<BaseGgpFacility> queryWrapper = Wrappers.query();
        if(baseGgpFacilityVO.getStatisticType().equals("street")){
            queryWrapper.select("street_town as streetTown","count(*) as count")
                    .in("facility_type",baseGgpFacilityVO.getFacilityType())
                    .groupBy("street_town");
            List<Map<String,Object>> maps = this.listMaps(queryWrapper);
            if(maps.size() > 0){
                for (int i = 0; i < maps.size(); i++) {
                    BaseGgpFacilityStatisticVO statisticVO = new BaseGgpFacilityStatisticVO();
                    statisticVO.setStreetTown(maps.get(i).get("streetTown").toString());
                    statisticVO.setCount(Integer.parseInt(maps.get(i).get("count").toString()));
                    list.add(statisticVO);
                }
            }
        }else if(baseGgpFacilityVO.getStatisticType().equals("facilityType")){
            queryWrapper.select("facility_type as facilityType","count(*) as count")
                    .in("facility_type",baseGgpFacilityVO.getFacilityType())
                    .groupBy("facility_type");
            List<Map<String,Object>> maps = this.listMaps(queryWrapper);
            if(maps.size() > 0){
                for (int i = 0; i < maps.size(); i++) {
                    BaseGgpFacilityStatisticVO statisticVO = new BaseGgpFacilityStatisticVO();
                    statisticVO.setFacilityType(Integer.parseInt(maps.get(i).get("facilityType").toString()));
                    statisticVO.setCount(Integer.parseInt(maps.get(i).get("count").toString()));
                    list.add(statisticVO);
                }
            }
        }


        return list;
    }
}
