package com.bootdo.datas.service.impl;

import com.bootdo.common.domain.DictDO;
import com.bootdo.common.service.DictService;
import com.bootdo.common.utils.ShiroUtils;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.datas.dao.DataDao;
import com.bootdo.datas.domain.UnitDO;
import com.bootdo.datas.dto.DateAndCountDTO;
import com.bootdo.datas.dto.DateAndCountListDTO;
import com.bootdo.datas.dto.MapDataDTO;
import com.bootdo.datas.dto.NameAndValueDTO;
import com.bootdo.datas.service.DataService;
import com.bootdo.datas.service.GyDataService;
import com.bootdo.system.domain.UserDO;
import com.google.common.collect.Lists;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.*;

@Service
public class DataServiceImpl implements DataService {

    private static Logger log = LoggerFactory.getLogger(DataServiceImpl.class);

    @Autowired
    private DataDao dataDao;

    @Autowired
    private DictService dictService;

    @Autowired
    private GyDataService gyDataService;

    @Value("${deployType:#{null}}")
    public String deployType;

    @Value("${sysCity:#{null}}")
    public String sysCity;

    @Override
    public List<MapDataDTO> mapDataList(String dataLevel,String auditStage) {
        return dataDao.mapDataList(dataLevel,auditStage);
    }

    @Override
    public List<DateAndCountDTO> trendData() {
        UserDO currUser = ShiroUtils.getUser();
        String city = currUser.getCity();
        if(StringUtils.isEmpty(city)){
            city = sysCity;
        }
        String auditStage = null;
        if("1".equals(deployType)){ //市级单独部署
            auditStage = "1";
        } else {
            auditStage = "2";
        }
        log.info("method: trendData, user: " + currUser.getUsername() + " city: " + city);
        return dataDao.trendData(city, auditStage);
    }

    @Override
    public List<NameAndValueDTO> industryData() {
        UserDO currUser = ShiroUtils.getUser();
        String city = currUser.getCity();
        if(StringUtils.isEmpty(city)){
            city = sysCity;
        }
        String auditStage = null;
        if("1".equals(deployType)){ //市级单独部署
            auditStage = "1";
        } else {
            auditStage = "2";
        }
        List<NameAndValueDTO> dtoList = dataDao.industryData(city,auditStage);

        log.info("method: industryData, user: " + currUser.getUsername() + " city: " +
                city + " 数据: " + dtoList);

        // 处理 超过6条时, 将6条以上转为其他中 合并集合
        if (!CollectionUtils.isEmpty(dtoList) && dtoList.size() > 6) {
            return mergeDataList(dtoList);
        }

        // 不处理直接返回
        return dtoList;
    }

    @Override
    public List<NameAndValueDTO> industryTypeData() {
        UserDO currUser = ShiroUtils.getUser();
        String city = currUser.getCity();
        if(StringUtils.isEmpty(city)){
            city = sysCity;
        }
        List<NameAndValueDTO> dtoList = new ArrayList<>();
        // 查询列表数据
        Map<String, Object> query = new HashMap<>(16);
        query.put("type", "industry_type");
        List<DictDO> dictList = dictService.list(query);
        query.clear();
        for (DictDO dictDO:dictList){
            String value = dictDO.getValue();
            query.put("industryTypeOne",value);
            int total = gyDataService.countTotal(query);
            if (total > 0) {
                NameAndValueDTO nameAndValueDTO = new NameAndValueDTO();
                nameAndValueDTO.setName(value);
                nameAndValueDTO.setValue(total);
                dtoList.add(nameAndValueDTO);
            }
        }

        log.debug("method: industryTypeData, user: " + currUser.getUsername() + " city: " +
                city + " datas: " + dtoList);

        // 处理 超过6条时, 将6条以上转为其他中 合并集合
//        if (!CollectionUtils.isEmpty(dtoList) && dtoList.size() > 6) {
//            return mergeDataList(dtoList);
//        }

        // 不处理直接返回
        return dtoList;
    }

    @Override
    public DateAndCountListDTO dataLevelData() {
        UserDO currUser = ShiroUtils.getUser();
        String city = currUser.getCity();
        if(StringUtils.isEmpty(city)){
            city = sysCity;
        }
        String auditStage = null;
        if("1".equals(deployType)){ //市级单独部署
            auditStage = "1";
        } else {
            auditStage = "2";
        }

        List<DateAndCountDTO> importantData = dataDao.dataLevelData(city, "重要数据",auditStage);
        List<DateAndCountDTO> coreData = dataDao.dataLevelData(city, "核心数据",auditStage);

        log.info("method: dataLevelData, user: " + currUser.getUsername() + " city: " +
                city + " 核心: " + coreData + " 重要: " + importantData);

        return new DateAndCountListDTO(importantData, coreData);
    }

    @Override
    public Double dataAuditData() {
        UserDO currUser = ShiroUtils.getUser();
        String city = currUser.getCity();
        if(StringUtils.isEmpty(city)){
            city = sysCity;
        }
        String auditStage = null;
        if("1".equals(deployType)){ //市级单独部署
            auditStage = "1";
        } else {
            auditStage = "2";
        }
        Double passingRate = dataDao.dataAuditData(city,auditStage);
        log.info("method: dataAuditData, user: " + currUser.getUsername() + " city: " +
                city + " 通过率: " + passingRate);
        return passingRate;
    }

    @Override
    public List<NameAndValueDTO> dataCategoryData() {
        UserDO currUser = ShiroUtils.getUser();
        String city = currUser.getCity();
        if(StringUtils.isEmpty(city)){
            city = sysCity;
        }

        List<NameAndValueDTO> result = dataDao.dataCategoryData(city);
        log.info("method: dataCategoryData, user: " + currUser.getUsername() + " city: " +
                city + " 数据结果: " + result.toString());
        return result;
    }



    /**
     * 超过六条 需要合并数据
     *
     * @param dtoList
     * @return
     */
    private List<NameAndValueDTO> mergeDataList(List<NameAndValueDTO> dtoList) {
        List<NameAndValueDTO> newDataList = Lists.newArrayList();
        NameAndValueDTO nameAndValueDTO = new NameAndValueDTO();
        nameAndValueDTO.setName("其他");
        nameAndValueDTO.setValue(0);
        for (int i = 0; i < dtoList.size(); i++) {
            if (i > 4) {
                nameAndValueDTO.setValue(nameAndValueDTO.getValue() + dtoList.get(i).getValue());
            } else {
                newDataList.add(dtoList.get(i));
            }
        }
        newDataList.add(nameAndValueDTO);
        return newDataList;
    }

    /**
     * 去除重复数据 由于Set的无序性，不会保持原来顺序
     *
     * @param list
     */
    public List<UnitDO> distinct(List<UnitDO> list) {

        List<UnitDO> newlist = new ArrayList<>();
        list.forEach(t -> {
            // anyMatch表示有一个匹配就返回true;
            boolean match = newlist.stream().anyMatch(u -> t.getReporterUnit().equals(u.getReporterUnit()));
            if (!match) {
                newlist.add(t);
            }
        });
        return newlist;
    }


}
