package com.benyun.core.service.impl;

import com.benyun.core.dao.*;
import com.benyun.core.entity.*;
import com.benyun.core.entity.bo.*;
import com.benyun.core.entity.vo.BrandListQueryBody;
import com.benyun.core.service.BrandService;
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.List;

@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    BrandMapper brandMapper;
    @Autowired
    BrandStatisticsMapper brandStatisticsMapper;
    @Autowired
    BrandProvinceMapper brandProvinceMapper;
    @Autowired
    BrandCityMapper brandCityMapper;
    @Autowired
    BrandZoneMapper brandZoneMapper;
    @Autowired
    EnterpriseMapper enterpriseMapper;

    @Override
    public List<BrandSearch> searchByLikeName(String text) {
        List<Brand> list = brandMapper.searchList(text);
        List<BrandSearch> sList = new ArrayList<>();
        for (Brand brand : list){
            Enterprise enterprise = enterpriseMapper.searchByUsci(brand.getEnterpriseUsci());
            BrandSearch bs = new BrandSearch();
            bs.setBrandId(brand.getBrandId());
            bs.setBrandName(brand.getBrandName());
            if (enterprise != null)
                bs.setEnterpriseName(enterprise.getEnterpriseName());
            bs.setUpdateTime(brand.getUpdateTime());
            sList.add(bs);
        }
        return sList;
    }

    @Override
    public BrandInfo searchByBrandId(String brandId) {
        Brand brand = brandMapper.searchById(brandId);
        BrandInfo info = new BrandInfo();
        if (brand != null){
            Enterprise enterprise = enterpriseMapper.searchByUsci(brand.getEnterpriseUsci());
            info.setBrandId(brandId);
            info.setBrandName(brand.getBrandName());
            info.setBrandImg(brand.getBrandImg());
            info.setIndexScore(brand.getIndexScore());
            if (enterprise != null){
                info.setEnterpriseUsci(enterprise.getEnterpriseUsci());
                info.setEnterpriseName(enterprise.getEnterpriseName());
                info.setAddrCode(enterprise.getAddrCode());
                info.setAddrInfo(enterprise.getAddrInfo());
                info.setTelephone(enterprise.getTelephone());
            }
            info.setUpdateTime(brand.getUpdateTime());
        }else return null;
        return info;
    }

    @Override
    public BrandProperty searchPropertyByBrandId(String brandId) {
        BrandStatistics brandStatistics = brandStatisticsMapper.searchByBrandId(brandId);
        Brand brand = brandMapper.searchById(brandId);
        BrandProperty bp = new BrandProperty();
        if (brand != null){
            bp.setIndustryCode(brand.getIndustryCode());
            bp.setPerCapitaConsumption(brand.getPerCapitaConsumption());
            bp.setCommentCount(brand.getCommentCount());
            bp.setScore(brand.getScore());
        }else {
            bp.setIndustryCode("****");
            bp.setPerCapitaConsumption(null);
            bp.setCommentCount(null);
            bp.setScore(null);
        }
        if (brandStatistics != null){
            float activeRatio = (float) brandStatistics.getBusinessCount()/brandStatistics.getTotal();
            bp.setActiveRatio((int)(activeRatio*100)+"%");
        }else {
            bp.setActiveRatio("****");
        }
        return bp;
    }

    @Override
    public List<BrandSimilar> searchSimilarByBrandId(String brandId) {
//        先查询品牌id得到industryCode
        Brand brand = brandMapper.searchById(brandId);
        if (brand == null)
            return new ArrayList<>();
//        再根据industryCode得到其他品牌的信息
        if (brand.getIndustryCode() == null)
            return new ArrayList<>();
        List<Brand> brands = brandMapper.searchByIndustryCodeList(brandId, brand.getIndustryCode());
        if (brands.isEmpty())
            return new ArrayList<>();
        List<BrandSimilar> similarList = new ArrayList<>();
        for (Brand b : brands){
            BrandSimilar bs = new BrandSimilar();
            bs.setBrandId(b.getBrandId());
            bs.setBrandImg(b.getBrandImg());
            bs.setBrandName(b.getBrandName());
            bs.setUpdateTime(b.getUpdateTime());
            similarList.add(bs);
        }
        return similarList;
    }

    @Override
    public List<BrandStatus> searchStatusByBrandId(String brandId) {
//        查询状态后拆分
        BrandStatistics statistics = brandStatisticsMapper.searchByBrandId(brandId);
        if (statistics == null)
            return new ArrayList<>();
        int businessCount = statistics.getBusinessCount();
        int pauseBusinessCount = statistics.getPauseBusinessCount();
        int noBusinessCount = statistics.getNoBusinessCount();
        List<BrandStatus> statuses = new ArrayList<>();
        if (businessCount != 0){
            BrandStatus status = new BrandStatus();
            status.setBusinessStatus("正常");
            status.setCount(businessCount);
            statuses.add(status);
        }
        if (pauseBusinessCount != 0){
            BrandStatus status = new BrandStatus();
            status.setBusinessStatus("暂停");
            status.setCount(pauseBusinessCount);
            statuses.add(status);
        }
        if (noBusinessCount != 0){
            BrandStatus status = new BrandStatus();
            status.setBusinessStatus("尚未营业");
            status.setCount(noBusinessCount);
            statuses.add(status);
        }
        return statuses;
    }

    @Override
    public BrandDistribution searchDistributionByBrandId(String brandId) {
//        省份:31
//        城市:342
//        区县:2989
//        查询统计并附加信息
        int province = 31;
        int city = 342;
        int zone = 2989;
        BrandStatistics statistics = brandStatisticsMapper.searchByBrandId(brandId);
        if (statistics == null)
            return null;
        BrandDistribution bd = new BrandDistribution();
        int cpc = statistics.getCoverProvinceCount();
        int ccc = statistics.getCoverCityCount();
        int czc = statistics.getCoverZoneCount();
        bd.setTotal(statistics.getTotal());
        bd.setCoverProvinceCount(cpc);
        float provinceRatio = (float) cpc/province;
        bd.setCoverProvinceRatio((int)(provinceRatio*100) + "%");
        bd.setCoverCityCount(ccc);
        float cityRatio = (float) ccc/city;
        bd.setCoverCityRatio((int)(cityRatio*100) + "%");
        bd.setCoverZoneCount(czc);
        float zoneRatio = (float) czc/zone;
        bd.setCoverZoneRatio((int)(zoneRatio*100) + "%");
        return bd;
    }

    @Override
    public List<BrandProvinceBo> searchProvinceByBrandId(String brandId) {
        List<BrandProvince> provinces = brandProvinceMapper.searchByBrandId(brandId);
        BrandStatistics statistics = brandStatisticsMapper.searchByBrandId(brandId);
        if (provinces.isEmpty() || statistics == null)
            return new ArrayList<>();
        int total = statistics.getTotal();
        List<BrandProvinceBo> bpbs = new ArrayList<>();
        for (BrandProvince bp : provinces){
            int disCount = bp.getDisCount();
            BrandProvinceBo bpb = new BrandProvinceBo();
//            bpb.setBrandId(bp.getBrandId());
            bpb.setCode(bp.getAddrCode());
            bpb.setName(bp.getProvince());
            bpb.setLocation(bp.getLat().toString() + "," + bp.getLng().toString());
            bpb.setCount(disCount);
            float ratio = (float) disCount/total;
            bpb.setRatio((ratio*100) + "%");
            bpb.setUpdateTime(bp.getUpdateTime());
            bpbs.add(bpb);
        }
        return bpbs;
    }

    @Override
    public BrandDisCloud searchDisCloudByBrandId(String brandId) {
        List<BrandCity> brandCities = brandCityMapper.searchByBrandId(brandId);
        List<BrandZone> brandZones = brandZoneMapper.searchByBrandId(brandId);
        BrandDisCloud disCloud = new BrandDisCloud();
        List<BrandDisItem> zoneDis = new ArrayList<>();
        List<BrandDisItem> cityDis = new ArrayList<>();
        for (BrandZone bz : brandZones){
            BrandDisItem bdi = new BrandDisItem();
            bdi.setCode(bz.getAddrCode());
            bdi.setName(bz.getZone());
            bdi.setCount(bz.getDisCount());
            zoneDis.add(bdi);
        }
        for (BrandCity bc : brandCities){
            BrandDisItem bdi = new BrandDisItem();
            bdi.setCode(bc.getAddrCode());
            bdi.setName(bc.getCity());
            bdi.setCount(bc.getDisCount());
            cityDis.add(bdi);
        }
        disCloud.setZoneDistribution(zoneDis);
        disCloud.setCityDistribution(cityDis);
        return disCloud;
    }

    @Override
    public PageInfo<List<BrandSearch>> searchListByMulti(BrandListQueryBody body) {
        PageHelper.startPage(body.getPageNum(),body.getPageSize(),true);
        String text = body.getText();
        List<String> industryCode = body.getIndustryCode();
        Integer countDown = null;
        Integer countUp = null;
        Integer coverDown = null;
        Integer coverUp = null;
        try {
            countDown = Integer.parseInt(body.getWdCount().get(0));
            countUp = Integer.parseInt(body.getWdCount().get(1));
            coverDown = Integer.parseInt(body.getCoverCityCount().get(0));
            coverUp = Integer.parseInt(body.getCoverCityCount().get(1));
        }catch (Exception e){}
        List<BrandSearch> brandSearches = brandMapper.searchSearchByMulti(text, industryCode, countUp, countDown, coverUp, coverDown);
        PageInfo info = new PageInfo(brandSearches);
        return info;
    }

    @Override
    public List<BrandTotal> searchTotal() {
        List<BrandStatistics> list = brandStatisticsMapper.searchList();
        if (list.isEmpty())
            return new ArrayList<>();
        List<BrandTotal> tList = new ArrayList<>();
        for (BrandStatistics bs : list){
            BrandTotal bt = new BrandTotal();
            bt.setBrandId(bs.getBrandId());
            bt.setBrandName(bs.getBrandName());
            bt.setTotal(bs.getTotal());
            bt.setUpdateTime(bs.getUpdateTime());
            tList.add(bt);
        }
        return tList;
    }

    @Override
    public BStoreData searchAvgScoreByBrandId(String brandId) {
        Brand brand = brandMapper.searchById(brandId);
        if (brand == null)
            return null;
        BStoreData bStoreData = new BStoreData();
        bStoreData.setBrandName(brand.getBrandName());
        List<BrandZone> zones = brandZoneMapper.searchByBrandId(brandId);
        if (zones.isEmpty())
            return null;
        List<Info> infos = new ArrayList<>();
        for (BrandZone bz : zones){
            Info info = new Info();
            info.setAddrCode(bz.getAddrCode());
            info.setAvgScore(bz.getAvgScore());
            infos.add(info);
        }
        bStoreData.setInfo(infos);
        return bStoreData;
    }

    @Override
    public List<BrandAddrDistribution> searchAddrDistribution(List<String> addrCodes) {
        List<BrandAddrDistribution> list = brandZoneMapper.searchByAddrCodeGroup(addrCodes);
        return list;
    }
}
