package cn.com.ty.lift.system.homepage.service;

import cn.com.ty.lift.common.constants.ApiConstants;
import cn.com.ty.lift.common.constants.CommonEnum;
import cn.com.ty.lift.common.utils.ProjectUtils;
import cn.com.ty.lift.system.constants.CommonConstants;
import cn.com.ty.lift.system.homepage.dao.mapper.HomeLiftDataMapper;
import cn.com.ty.lift.system.homepage.dao.mapper.StatisticsCountMapper;
import cn.com.ty.lift.system.homepage.dao.model.LiftUnitsDataModel;
import cn.com.ty.lift.system.settings.dao.entity.MaintenanceCompany;
import cn.com.ty.lift.system.settings.service.IMaintenanceCompanyService;
import cn.com.ty.lift.system.user.service.IAreaCodeService;
import cn.com.xwy.boot.web.dto.RestResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author huangyuan
 * @date 2020/2/14
 * @description
 */
@Service
public class PlatformDataStatisticsService {

    @Resource
    private IMaintenanceCompanyService maintenanceCompanyService;

    @Resource
    private IAreaCodeService areaCodeService;

    @Resource
    private HomeLiftDataMapper homeLiftDataMapper;

    @Resource
    private StatisticsCountMapper statisticsCountMapper;

    /**
     * @param
     * @return
     * @description 客户数据统计
     * @date 2020/2/14 6:52 下午
     */
    public RestResponse customerStatistics() {
        //省份名称 -> 公司数量(公司认证状态->公司数据)
        Map<String, Map<Integer, Long>> provinceCodeToCompanyNum = new HashMap<>();
        //获取省份数据
        Map<String, String> provinceCodeToName = areaCodeService.getProvinceCodeToName();
        //获取公司数据通过省份编码分组的数据
        Map<String, List<MaintenanceCompany>> provinceCodeToMaintenanceCompany = maintenanceCompanyService
                .getProvinceCodeToMaintenanceCompany();
        //封装省份对应公司数据
        if (provinceCodeToMaintenanceCompany.size() > 0) {
            provinceCodeToMaintenanceCompany.forEach((provinceCode, maintenanceCompanyList) -> {
                //获取省份名称
                String provinceName = provinceCodeToName.get(provinceCode);
                if (StringUtils.isNotBlank(provinceName)) {
                    //vip标识->公司数量:初始化数据
                    Map<Integer, Long> vipFlagToCompanyNum = CommonConstants.VIP_STATUS_TO_COMPANY_NUM;
                    long allCompanyNum = maintenanceCompanyList.stream().count();
                    //设置省份下所有公司数量
                    vipFlagToCompanyNum.put(CommonConstants.PlatformDataStatisticsConstants.IS_VIP_ALL, allCompanyNum);
                    //设置省份下是vip的公司数量
                    long isNotVipCompanyNum = maintenanceCompanyList.stream()
                            .filter(maintenanceCompany ->
                                    CommonConstants.PlatformDataStatisticsConstants.IS_VIP_YES
                                            == maintenanceCompany.getVipFlag())
                            .count();
                    vipFlagToCompanyNum.put(CommonConstants.PlatformDataStatisticsConstants.IS_VIP_YES, isNotVipCompanyNum);
                    //设置省份下所有不是vip公司的数量
                    vipFlagToCompanyNum.put(CommonConstants.PlatformDataStatisticsConstants.IS_VIP_NO,
                            allCompanyNum - isNotVipCompanyNum);

                    provinceCodeToCompanyNum.put(provinceName, vipFlagToCompanyNum);
                }
            });
        }
        return RestResponse.success(ProjectUtils.transReturnMapToStatisticsKVModel(provinceCodeToCompanyNum),
                ApiConstants.RESULT_SUCCESS, "获取客户统计成功");
    }

    /**
     * @param
     * @return
     * @description 台量统计
     * @date 2020/2/14 6:54 下午
     */
    public RestResponse unitsStatistics() {
        //省份名称 -> 台量(台量状态 -> 台量)
        Map<String, Map<Integer, Long>> provinceCodeToUnits = new HashMap<>();
        //获取省份数据
        Map<String, String> provinceCodeToName = areaCodeService.getProvinceCodeToName();
        //获取电梯信息
        List<LiftUnitsDataModel> liftUnitsDataModelList = homeLiftDataMapper.getLiftUnitsData();
        if (liftUnitsDataModelList != null && liftUnitsDataModelList.size() > 0) {
            //公司省份编码 -> 电梯数据
            Map<String, List<LiftUnitsDataModel>> provinceCodeToLiftUnitsDataModel = liftUnitsDataModelList.stream()
                    .collect(Collectors.groupingBy(liftUnitsDataModel -> {
                        if (StringUtils.isNotBlank(liftUnitsDataModel.getProvinceCode())) {
                            return liftUnitsDataModel.getProvinceCode();
                        }
                        return "暂无";
                    }));
            //设置台量
            provinceCodeToLiftUnitsDataModel.forEach((provinceCode, LiftUnitsDataModelList) -> {
                Map<Integer, Long> unitsStatusToNum = new HashMap<>();
                //初始化台量为0 0:总台量 1:活跃台量
                unitsStatusToNum.put(0, 0L);
                unitsStatusToNum.put(1, 0L);
                String provinceName = provinceCodeToName.get(provinceCode);
                if (StringUtils.isNotBlank(provinceName)) {
                    if (LiftUnitsDataModelList != null && liftUnitsDataModelList.size() > 0) {
                        //计算总台量
                        unitsStatusToNum.put(0, liftUnitsDataModelList.stream().count());
                        //计算活跃台量
                        unitsStatusToNum.put(1, liftUnitsDataModelList.stream().filter(liftUnitsDataModel -> {
                            if (StringUtils.isNotBlank(liftUnitsDataModel.getLiftCompanyStatus())) {
                                //如果电梯没有停保就是活跃电梯
                                return !CommonEnum.LiftStatus.STOP_INSURANCE.getCode()
                                        .equals(liftUnitsDataModel.getLiftCompanyStatus());
                            }
                            return false;
                        }).count());
                    }
                    provinceCodeToUnits.put(provinceName, unitsStatusToNum);
                }
            });
        }
        return RestResponse.success(ProjectUtils.transReturnMapToStatisticsKVModel(provinceCodeToUnits),
                ApiConstants.RESULT_SUCCESS, "获取台量统计成功");
    }

    /**
     * 平台统计
     *
     * @return
     */
    @RequestMapping("/overView")
    public RestResponse overView() {
        Map<Integer, Long> recordCountMap = new HashMap<>();
        //设置认证台量
        recordCountMap.put(0, statisticsCountMapper.getLiftTotalCount());
        //项目总录入数量
        recordCountMap.put(1, statisticsCountMapper.getProjectTotalCount());
        //维保次数
        recordCountMap.put(2, statisticsCountMapper.getMaintenanceRecordTotalCount());
        //急修次数
        recordCountMap.put(3, statisticsCountMapper.getEmergencyRecordTotalCount());
        return RestResponse.success(ProjectUtils.transReturnMapToStatisticsKVModel(recordCountMap),
                ApiConstants.RESULT_SUCCESS, "获取平台统计成功");
    }

}
