package com.benyun.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.benyun.core.dao.AddrCategoryDao;
import com.benyun.core.dao.WdInfoDao;
import com.benyun.core.entity.AddrCategory;
import com.benyun.core.entity.WdCount;
import com.benyun.core.entity.WdCountBody;
import com.benyun.core.entity.WdInfo;
import com.benyun.core.service.ChannelMapService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Service
public class ChannelMapServiceImpl implements ChannelMapService {

    @Autowired
    private AddrCategoryDao addrCategoryDao;

    @Autowired
    private WdInfoDao originWdInfoDao;

    @Override
    public WdCount area(List<String> channel, String rankType, String searchText, String[] addrCode) {
        long start = System.currentTimeMillis();
        List<String> addrCodeList = new ArrayList<>();

        //1.根据不同级别得到地区码
        if ("province".equals(rankType)) {
            //省码
            Collections.addAll(addrCodeList, addrCode);
        } else if ("city".equals(rankType)) {
            //省的所有市
            for (String s : addrCode) {
                String substring = s.substring(0, 2);
                addrCodeList.add(substring);
            }
        } else if ("zone".equals(rankType)) {
            //市的所有区
            for (String s : addrCode) {
                String substring = s.substring(0, 4);
                addrCodeList.add(substring);
            }
        } else {
            return null;
        }

        //2.找到相关地区信息
        QueryWrapper<AddrCategory> queryWrapper = new QueryWrapper<>();
        if ("province".equals(rankType)) {
            queryWrapper.eq("city", "");
        } else if ("city".equals(rankType)) {
            queryWrapper.eq("district", "").and(addrCategoryQueryWrapper -> {
                addrCategoryQueryWrapper.ne("city", "");
            });
        } else if ("zone".equals(rankType)) {
            queryWrapper.ne("district", "");
        }
        queryWrapper.and(addrCategoryQueryWrapper -> {
            addrCategoryQueryWrapper.or(addrCategoryQueryWrapper1 -> {
                for (String s : addrCodeList) {
                    addrCategoryQueryWrapper1.likeRight("addr_code", s).or();
                }
            });
        });

        //TODO 前置分类删选还没做


        List<AddrCategory> addrCategories = addrCategoryDao.selectList(queryWrapper);


        //3.组装地区信息
        List<WdCountBody> result = new ArrayList<>();
        for (AddrCategory addrCategory : addrCategories) {
            WdCountBody wdCountBody = new WdCountBody();

            //组装
            wdCountBody.setLat(addrCategory.getLatGd());
            wdCountBody.setLng(addrCategory.getLngGd());
            wdCountBody.setAddrCode(addrCategory.getAddrCode());
            if ("province".equals(rankType)) {
                wdCountBody.setName(addrCategory.getProvince());
            } else if ("city".equals(rankType)) {
                wdCountBody.setName(addrCategory.getCity());
            } else if ("zone".equals(rankType)) {
                wdCountBody.setName(addrCategory.getDistrict());
            }
            result.add(wdCountBody);
        }

        //4.统计地区网点信息
        Long total = 0L;
        for (WdCountBody wdCountBody : result) {
            QueryWrapper<WdInfo> queryWrapper1 = new QueryWrapper<>();
            String code = wdCountBody.getAddrCode();

            //根据地区前缀，使用模糊查询
            if ("province".equals(rankType))
                code = code.substring(0, 2);
            else if ("city".equals(rankType))
                code = code.substring(0, 4);

            queryWrapper1.likeRight("addr_code", code).and(originWdInfoQueryWrapper -> {
                originWdInfoQueryWrapper.in("wd_type_code", channel);
            });
            if (searchText != null && !searchText.trim().equals("")) {
                queryWrapper1.and(originWdInfoQueryWrapper -> {
                    originWdInfoQueryWrapper.like("wd_name", searchText);
                });
            }
            Long aLong = originWdInfoDao.selectCount(queryWrapper1);
            wdCountBody.setCount(aLong);
            total += aLong;
        }

        //5.计算radio
        for (WdCountBody wdCountBody : result) {
            if (total != 0)
                wdCountBody.setRadio((float) wdCountBody.getCount() / total);
            else
                wdCountBody.setRadio(0.0f);
        }
        long end = System.currentTimeMillis();
        System.out.println("运行时间：" + (end - start));

        WdCount wdCount = new WdCount();
        wdCount.setWdCountBodyList(result);
        wdCount.setTotal(total);
        return wdCount;
    }

    @Override
    public PageInfo<WdInfo> list(String[] channel, String rankType, String searchText, String orderby, String[] addrCode, int pageNum, int pageSize) {
        //1.根据不同级别得到地区码
        List<String> addrCodeList = new ArrayList<>();
        if ("province".equals(rankType)) {
            //省码
            for (String s : addrCode) {
                String substring = s.substring(0, 2);
                addrCodeList.add(substring);
            }
        } else if ("city".equals(rankType)) {
            //省的所有市
            for (String s : addrCode) {
                String substring = s.substring(0, 4);
                addrCodeList.add(substring);
            }
        } else if ("zone".equals(rankType)) {
            //区
            for (String s : addrCode) {
                String substring = s.substring(0, 6);
                addrCodeList.add(substring);
            }
        } else {
            return null;
        }


        //2.根据地区前缀找到所有的网点
        QueryWrapper<WdInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("wd_type_code", channel).and(originWdInfoQueryWrapper -> {
            for (String s : addrCodeList) {
                originWdInfoQueryWrapper.likeRight("addr_code", s).or();
            }
        });

        if (searchText != null && !searchText.trim().equals("")) {
            queryWrapper.and(originWdInfoQueryWrapper -> {
                originWdInfoQueryWrapper.like("wd_name", searchText);
            });
        }
        if (orderby != null && !orderby.trim().equals("")) {
            queryWrapper.orderByDesc(orderby);
        }

        PageHelper.startPage(pageNum, pageSize);
        List<WdInfo> originWdInfos = originWdInfoDao.selectList(queryWrapper);
        PageInfo<WdInfo> PageInfo = new PageInfo<>(originWdInfos);
        return PageInfo;
    }

    @Override
    public PageInfo<WdInfo> point(String[] channel, String searchText, String[] addrCode, int pageNum, int pageSize) {
        QueryWrapper<WdInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("wd_type_code", channel);
        for (String s : addrCode) {
            queryWrapper.and(originWdInfoQueryWrapper -> {
                originWdInfoQueryWrapper.eq("addr_code",s);
            });
        }
        if (searchText != null && !searchText.trim().equals("")) {
            queryWrapper.and(originWdInfoQueryWrapper -> {
                originWdInfoQueryWrapper.like("wd_name", searchText);
            });
        }
        PageHelper.startPage(pageNum, pageSize);
        List<WdInfo> originWdInfos = originWdInfoDao.selectList(queryWrapper);
        PageInfo<WdInfo> PageInfo = new PageInfo<>(originWdInfos);
        return PageInfo;
    }
}
