package cn.com.tenlion.systembase.service.statistics.impl;


import cn.com.tenlion.systembase.dao.statistics.IStatisticsDao;
import cn.com.tenlion.systembase.pojo.dtos.statistics.StatisticsDTO;
import cn.com.tenlion.systembase.pojo.dtos.statistics.StatisticsLineDTO;
import cn.com.tenlion.systembase.service.statistics.IStatisticsService;
import ink.wgink.common.base.DefaultBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName: ResidentialIntroductionServiceImpl
 * @Description: 小区简介
 * @Author: CodeFactory
 * @Date: 2021-11-22 15:31:23
 * @Version: 3.0
 **/
@Service
public class StatisticsServiceImpl extends DefaultBaseService implements IStatisticsService {

    @Autowired
    private IStatisticsDao statisticsDao;
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Calendar cal = Calendar.getInstance();

    @Override
    public List<StatisticsDTO> baseSysInfo(Map<String, Object> params) {
        // 综治中心数量
        List<StatisticsDTO> statisticsDTOList = new ArrayList<>();
        Integer comprehensiveCenterCount = comprehensiveCenterCount(params);
        StatisticsDTO statisticsDTO = new StatisticsDTO();
        statisticsDTO.setDataType("综治中心");
        statisticsDTO.setCount(comprehensiveCenterCount.toString());
        statisticsDTOList.add(statisticsDTO);

        // 学校数量
        Integer schoolCount = schoolCount(params);
        statisticsDTO = new StatisticsDTO();
        statisticsDTO.setDataType("学校");
        statisticsDTO.setCount(schoolCount.toString());
        statisticsDTOList.add(statisticsDTO);

        // 重点区域数量
        Integer keyAreaCount = keyAreaCount(params);
        statisticsDTO = new StatisticsDTO();
        statisticsDTO.setDataType("重点区域");
        statisticsDTO.setCount(keyAreaCount.toString());
        statisticsDTOList.add(statisticsDTO);

        // 重点场所数量
        Integer keyPlaceCount = keyPlaceCount(params);
        statisticsDTO = new StatisticsDTO();
        statisticsDTO.setDataType("重点场所");
        statisticsDTO.setCount(keyPlaceCount.toString());
        statisticsDTOList.add(statisticsDTO);

        // 两新组织数量
        Integer organizationCount = organizationCount(params);
        statisticsDTO = new StatisticsDTO();
        statisticsDTO.setDataType("两新组织");
        statisticsDTO.setCount(organizationCount.toString());
        statisticsDTOList.add(statisticsDTO);
        return statisticsDTOList;
    }

    @Override
    public Integer organizationCount(Map<String, Object> params) {
        Integer organizeCount = statisticsDao.organizeCount(params);
        Integer enterpriseCount = statisticsDao.enterpriseCount(params);
        return organizeCount + enterpriseCount;
    }

    @Override
    public Integer comprehensiveCenterCount(Map<String, Object> params) {
        return statisticsDao.comprehensiveCenterCount(params);
    }

    @Override
    public Integer schoolCount(Map<String, Object> params) {
        return statisticsDao.schoolCount(params);
    }

    @Override
    public Integer keyAreaCount(Map<String, Object> params) {
        return statisticsDao.keyAreaCount(params);
    }

    @Override
    public Integer keyPlaceCount(Map<String, Object> params) {
        return statisticsDao.keyPlaceCount(params);
    }

    @Override
    public List<StatisticsDTO> schoolInfo(Map<String, Object> params) {
        return statisticsDao.schoolInfo(params);
    }

    @Override
    public List<StatisticsDTO> personHarmInfo(Map<String, Object> params) {
        params.put("degreeOfHarmName", "yes");
        return statisticsDao.personKeyInfo(params);
    }

    @Override
    public List<StatisticsDTO> personFollowInfo(Map<String, Object> params) {
        params.put("isFollow", "yes");
        return statisticsDao.personKeyInfo(params);
    }

    @Override
    public List<StatisticsDTO> caseDetectInfo(Map<String, Object> params) {
        return statisticsDao.caseDetectInfo(params);
    }

    @Override
    public List<StatisticsDTO> areaSecurityInfo(Map<String, Object> params) {
        params.put("securityQuestionName", "yes");
        return statisticsDao.areaSecurityInfo(params);
    }

    @Override
    public List<StatisticsDTO> areaTypeInfo(Map<String, Object> params) {
        params.put("involveAreaTypeName", "yes");
        return statisticsDao.areaSecurityInfo(params);
    }

    @Override
    public List<StatisticsDTO> majorInfo(Map<String, Object> params) {
        List<StatisticsDTO> statisticsDTOList = statisticsDao.majorInfo(params);
        if(null != statisticsDTOList && statisticsDTOList.size() > 0) {
            for(StatisticsDTO statisticsDTO: statisticsDTOList) {
                String[] dataType = statisticsDTO.getDataType().split(",");
                if(dataType.length >= 3) {
                    statisticsDTO.setDataType(dataType[2]);
                }
            }
        }
        return statisticsDTOList;
    }

    @Override
    public List<StatisticsLineDTO> caseInfo(Map<String, Object> params) {
        List<StatisticsLineDTO> statisticsLineDTOList = new ArrayList<>();
        int month = 12;
        int year = 0;
        if(null == params.get("year")) {
            params.put("year", cal.get(Calendar.YEAR));
            month = cal.get(Calendar.MONTH) + 1;
            year = cal.get(Calendar.YEAR);
        }else {
            year = Integer.valueOf(params.get("year").toString());
        }
        for(int i=1;i<=month;i++) {
            if (i < 10) {
                params.put("eventDate", year + "0" + i);
            } else {
                params.put("eventDate", year + "" + i);
            }
            StatisticsLineDTO statisticsLineDTO = new StatisticsLineDTO();
            statisticsLineDTO.setYear(String.valueOf(year));
            statisticsLineDTO.setMonth(String.valueOf(i) + "月");
            List<StatisticsDTO> statisticsDTOList = new ArrayList<>();
            // 安全事故
            params.put("eventTypeText", "安全事故");
            StatisticsDTO safeDTO = caseList(params);
            statisticsDTOList.add(safeDTO);

            params.put("eventTypeText", "群体性事件");
            StatisticsDTO groupDTO = caseList(params);
            statisticsDTOList.add(groupDTO);

            params.put("eventTypeText", "食品安全事故");
            StatisticsDTO foodSafeDTO = caseList(params);
            statisticsDTOList.add(foodSafeDTO);

            params.put("eventTypeText", "有关刑事案件");
            StatisticsDTO criminalDTO = caseList(params);
            statisticsDTOList.add(criminalDTO);

            params.put("eventTypeText", "其他");
            StatisticsDTO otherDTO = caseList(params);
            statisticsDTOList.add(otherDTO);

            statisticsLineDTO.setStatisticsDTOList(statisticsDTOList);

            statisticsLineDTOList.add(statisticsLineDTO);
        }

        return statisticsLineDTOList;
    }

    private StatisticsDTO caseList(Map<String, Object> params) {
        StatisticsDTO statisticsDTO = statisticsDao.caseInfo(params);
        if(null == statisticsDTO) {
            statisticsDTO = new StatisticsDTO();
            statisticsDTO.setCount(String.valueOf(0));
            statisticsDTO.setDataType(params.get("eventTypeText").toString());
        }else {
            statisticsDTO.setDataType(params.get("eventTypeText").toString());
        }
        return statisticsDTO;
    }


    @Override
    public List<StatisticsLineDTO> keyPlaceInfo(Map<String, Object> params) {
        List<StatisticsLineDTO> statisticsLineDTOList = new ArrayList<>();
        int month = 12;
        int year = 0;
        if(null == params.get("year")) {
            params.put("year", cal.get(Calendar.YEAR));
            month = cal.get(Calendar.MONTH) + 1;
            year = cal.get(Calendar.YEAR);
        }else {
            year = Integer.valueOf(params.get("year").toString());
        }
        for(int i=1;i<=month;i++) {
            if (i < 10) {
                params.put("patrolTime", year + "-0" + i);
            } else {
                params.put("patrolTime", year + "-" + i);
            }

            StatisticsLineDTO statisticsLineDTO = new StatisticsLineDTO();
            statisticsLineDTO.setYear(String.valueOf(year));
            statisticsLineDTO.setMonth(String.valueOf(i) + "月");
            List<StatisticsDTO> statisticsDTOList = new ArrayList<>();

            Integer keyAreaPatrolCount = keyAreaPatrolCount(params);
            StatisticsDTO statisticsDTO = new StatisticsDTO();
            statisticsDTO.setDataType("重点区域");
            statisticsDTO.setCount(keyAreaPatrolCount.toString());
            statisticsDTOList.add(statisticsDTO);

            Integer keyPlacePatrolCount = keyPlacePatrolCount(params);
            statisticsDTO = new StatisticsDTO();
            statisticsDTO.setDataType("重点场所");
            statisticsDTO.setCount(keyPlacePatrolCount.toString());
            statisticsDTOList.add(statisticsDTO);

            statisticsLineDTO.setStatisticsDTOList(statisticsDTOList);

            statisticsLineDTOList.add(statisticsLineDTO);

        }
        return statisticsLineDTOList;
    }

    @Override
    public Integer keyAreaPatrolCount(Map<String, Object> params) {
        return statisticsDao.keyAreaPatrolCount(params);
    }

    @Override
    public Integer keyPlacePatrolCount(Map<String, Object> params) {
        return statisticsDao.keyPlacePatrolCount(params);
    }
}