package org.security.trans.dataStatistics.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import org.security.common.Constant;
import org.security.trans.dataStatistics.vo.HistogramDataVO;
import org.security.trans.dataStatistics.vo.PieChartDataVO;
import org.security.common.eum.EducationEnum;
import org.security.trans.basic.dto.EmployeeDTO;
import org.security.trans.dataStatistics.service.IEmployeeDataService;
import org.security.trans.basic.service.IEmployeeService;
import org.security.ht.vo.basic.EmployeeVO;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Create by wzf
 * @date 2022/5/23 14:45
 */
@Service
@AllArgsConstructor
public class EmployeeDataServiceImpl implements IEmployeeDataService {

	private final IEmployeeService employeeService;

	@Override
	public List<PieChartDataVO> getSexData(EmployeeDTO employeeDTO, Query query) {
		List<EmployeeVO> list = getEmployeeVOList(employeeDTO);
		ArrayList<PieChartDataVO> dataVos = new ArrayList<>();
		if (Func.isNotEmpty(list)) {
			Map<String, List<EmployeeVO>> collect = list.stream().filter(item -> Func.isNotEmpty(item.getGender())).collect(Collectors.groupingBy(EmployeeVO::getGender));
			collect.forEach((key, value) -> {
				PieChartDataVO pieChartDataVO = new PieChartDataVO();
				pieChartDataVO.setName(Constant.SEX_BOY.equals(key) ? "男" : "女");
				pieChartDataVO.setValue(value.size());
				dataVos.add(pieChartDataVO);
			});
		}
		return dataVos;
	}

	/**
	 * 获取统计数据
	 */
	private List<EmployeeVO> getEmployeeVOList(EmployeeDTO employeeDTO) {
		return employeeService.getEmployeeStatisticsData(employeeDTO);
	}

	@Override
	public List<HistogramDataVO> getAgeData(EmployeeDTO employeeDTO, Query query) {
		List<EmployeeVO> list = getEmployeeVOList(employeeDTO);
		ArrayList<HistogramDataVO> vos = new ArrayList<>();
		if (Func.isNotEmpty(list)) {
			//没有年龄信息的
			List<EmployeeVO> collectU = list.stream().filter(item -> Func.isEmpty(item.getAge())).collect(Collectors.toList());
			setData(vos, collectU, "年龄未知");
			//有年龄信息的
			List<EmployeeVO> collectHasAge = list.stream().filter(item -> Func.isNotEmpty(item.getAge())).collect(Collectors.toList());
			//统计20岁以下
			List<EmployeeVO> collect = collectHasAge.stream().filter(item -> Integer.parseInt(item.getAge()) < 20).collect(Collectors.toList());
			setData(vos, collect, "20岁以下");
			//统计[20-30)岁
			List<EmployeeVO> collect1 = collectHasAge.stream().filter(item -> Integer.parseInt(item.getAge()) >= 20 && Integer.parseInt(item.getAge()) < 30).collect(Collectors.toList());
			setData(vos, collect1, "[20-30)");
			//统计[30-40}岁
			List<EmployeeVO> collect2 = collectHasAge.stream().filter(item -> Integer.parseInt(item.getAge()) >= 30 && Integer.parseInt(item.getAge()) < 40).collect(Collectors.toList());
			setData(vos, collect2, "[30-40)");
			//统计[40-50)岁
			List<EmployeeVO> collect3 = collectHasAge.stream().filter(item -> Integer.parseInt(item.getAge()) >= 40 && Integer.parseInt(item.getAge()) < 50).collect(Collectors.toList());
			setData(vos, collect3, "[40-50)");
			//50岁及以上
			List<EmployeeVO> collect4 = collectHasAge.stream().filter(item -> Integer.parseInt(item.getAge()) >= 50).collect(Collectors.toList());
			setData(vos, collect4, "50岁及以上");

		}
		return vos;
	}

	@Override
	public List<PieChartDataVO> getEducateData(EmployeeDTO employeeDTO, Query query) {
		List<EmployeeVO> list = getEmployeeVOList(employeeDTO);
		ArrayList<PieChartDataVO> dataVos = new ArrayList<>();
		if (Func.isNotEmpty(list)) {
			//民族不为空的
			Map<String, List<EmployeeVO>> collect = list.stream().filter(item -> Func.isNotEmpty(item.getEducation())).collect(Collectors.groupingBy(EmployeeVO::getEducation));
			setPieData(dataVos, collect);
			//名族为空的
			List<EmployeeVO> collect1 = list.stream().filter(item -> Func.isEmpty(item.getEducation())).collect(Collectors.toList());
			setOtherData(dataVos, collect1);
		}
		return dataVos;
	}

	/**
	 *分组字段为空的数据归属赋值
	 */
	private void setOtherData(List<PieChartDataVO> dataVos, List<EmployeeVO> collect1) {
		PieChartDataVO vo = new PieChartDataVO();
		vo.setName("未知");
		vo.setValue(Func.isNotEmpty(collect1) ? collect1.size() : 0);
		dataVos.add(vo);
	}

	@Override
	public List<HistogramDataVO> getSeniorityInEnterpriseData(EmployeeDTO employeeDTO, Query query) {
		List<EmployeeVO> list = getEmployeeVOList(employeeDTO);
		ArrayList<HistogramDataVO> vos = new ArrayList<>();
		if (Func.isNotEmpty(list)) {
//			没有相关信息的
			List<EmployeeVO> collectUnknown = list.stream().filter(item -> Func.isEmpty(item.getSeniorityInEnterprise())).collect(Collectors.toList());
			setData(vos, collectUnknown, "未知");
//			有相关信息的
			List<EmployeeVO> collectHas = list.stream().filter(item -> Func.isNotEmpty(item.getSeniorityInEnterprise())).collect(Collectors.toList());

			//统计5年以下
			List<EmployeeVO> collect = collectHas.stream().filter(item -> item.getSeniorityInEnterprise() < 5).collect(Collectors.toList());
			setData(vos, collect, "5年以下");
			//统计[5-10)年
			List<EmployeeVO> collect1 = collectHas.stream().filter(item -> item.getSeniorityInEnterprise() >= 5 && item.getSeniorityInEnterprise() < 10).collect(Collectors.toList());
			setData(vos, collect1, "[5-10)");
			//统计[10-20}年
			List<EmployeeVO> collect2 = collectHas.stream().filter(item -> item.getSeniorityInEnterprise() >= 10 && item.getSeniorityInEnterprise() < 20).collect(Collectors.toList());
			setData(vos, collect2, "[10-20)");
			//统计[20-30)年
			List<EmployeeVO> collect3 = collectHas.stream().filter(item -> item.getSeniorityInEnterprise() >= 20 && item.getSeniorityInEnterprise() < 30).collect(Collectors.toList());
			setData(vos, collect3, "[20-30)");
			//统计30年及以上
			List<EmployeeVO> collect4 = collectHas.stream().filter(item -> item.getSeniorityInEnterprise() >= 30).collect(Collectors.toList());
			setData(vos, collect4, "30年及以上");
		}
		return vos;
	}

	@Override
	public List<HistogramDataVO> getProfessionalLengthOfServiceData(EmployeeDTO employeeDTO, Query query) {
		List<EmployeeVO> list = getEmployeeVOList(employeeDTO);
		ArrayList<HistogramDataVO> vos = new ArrayList<>();
		if (Func.isNotEmpty(list)) {
			//			没有相关信息的
			List<EmployeeVO> collectUnknown = list.stream().filter(item -> Func.isEmpty(item.getProfessionalLengthOfService())).collect(Collectors.toList());
			setData(vos, collectUnknown, "未知");
//			有相关信息的
			List<EmployeeVO> collectHas = list.stream().filter(item -> Func.isNotEmpty(item.getProfessionalLengthOfService())).collect(Collectors.toList());

			//统计5年以下
			List<EmployeeVO> collect = collectHas.stream().filter(item -> item.getProfessionalLengthOfService() < 5).collect(Collectors.toList());
			setData(vos, collect, "5年以下");
			//统计[5-10)年
			List<EmployeeVO> collect1 = collectHas.stream().filter(item -> item.getProfessionalLengthOfService() >= 5 && item.getProfessionalLengthOfService() < 10).collect(Collectors.toList());
			setData(vos, collect1, "[5-10)");
			//统计[10-20}年
			List<EmployeeVO> collect2 = collectHas.stream().filter(item -> item.getProfessionalLengthOfService() >= 10 && item.getProfessionalLengthOfService() < 20).collect(Collectors.toList());
			setData(vos, collect2, "[10-20)");
			//统计[20-30)年
			List<EmployeeVO> collect3 = collectHas.stream().filter(item -> item.getProfessionalLengthOfService() >= 20 && item.getProfessionalLengthOfService() < 30).collect(Collectors.toList());
			setData(vos, collect3, "[20-30)");
			//统计30年及以上
			List<EmployeeVO> collect4 = collectHas.stream().filter(item -> item.getProfessionalLengthOfService() >= 30).collect(Collectors.toList());
			setData(vos, collect4, "30年及以上");
		}
		return vos;
	}

	@Override
	public List<PieChartDataVO> getCivilAirDefenseTypeData(EmployeeDTO employeeDTO, Query query) {
		List<EmployeeVO> list = getEmployeeVOList(employeeDTO);
		ArrayList<PieChartDataVO> dataVos = new ArrayList<>();
		if (Func.isNotEmpty(list)) {
			Map<String, List<EmployeeVO>> collect = list.stream().filter(item -> Func.isNotEmpty(item.getCivilAirDefenseType())).collect(Collectors.groupingBy(EmployeeVO::getCivilAirDefenseType));
			setPieData(dataVos, collect);
		}
		return dataVos;
	}

	@Override
	public List<PieChartDataVO> getNationTypeData(EmployeeDTO employeeDTO) {
		List<EmployeeVO> list = getEmployeeVOList(employeeDTO);
		List<PieChartDataVO> vos = new ArrayList<>();
		if (Func.isNotEmpty(list)) {
			//民族不为空的
			Map<String, List<EmployeeVO>> collect = list.stream().filter(item -> Func.isNotEmpty(item.getNation())).collect(Collectors.groupingBy(EmployeeVO::getNation));
			if (Func.isNotEmpty(collect)) {
				collect.forEach((key, value) -> {
					setPieChartResultData(vos, key, value);
				});
			}
			//民族为空的
			List<EmployeeVO> collect1 = list.stream().filter(item -> Func.isEmpty(item.getNation())).collect(Collectors.toList());
			setOtherData(vos,collect1);
		}
		return vos;
	}

	/**
	 * 封装饼图返回结果
	 */
	private void setPieChartResultData(List<PieChartDataVO> vos, String key, List<EmployeeVO> value) {
		PieChartDataVO vo = new PieChartDataVO();
		vo.setName(key);
		vo.setValue(Func.isNotEmpty(value) ? value.size() : 0);
		vos.add(vo);
	}

	@Override
	public List<PieChartDataVO> getPoliticalOutlookTypeData(EmployeeDTO employeeDTO) {
		List<EmployeeVO> list = getEmployeeVOList(employeeDTO);
		List<PieChartDataVO> vos = new ArrayList<>();
		if (Func.isNotEmpty(list)) {
			//政治面貌不为空
			Map<String, List<EmployeeVO>> collect = list.stream().filter(item -> Func.isNotEmpty(item.getPolitics())).collect(Collectors.groupingBy(EmployeeVO::getPolitics));
			if (Func.isNotEmpty(collect)) {
				collect.forEach((key, value) -> {
					setPieChartResultData(vos, key, value);
				});
			}
			//政治面貌为空
			List<EmployeeVO> collect1 = list.stream().filter(item -> Func.isEmpty(item.getPolitics())).collect(Collectors.toList());
			setOtherData(vos,collect1);
		}
		return vos;
	}

	/**
	 * 封装饼图返回数据
	 */
	private void setPieData(ArrayList<PieChartDataVO> dataVos, Map<String, List<EmployeeVO>> collect) {
		collect.forEach((key, value) -> {
			PieChartDataVO dataVO = new PieChartDataVO();
			dataVO.setName(key);
			dataVO.setValue(Func.isNotEmpty(value) ? value.size() : 0);
			dataVos.add(dataVO);
		});
	}

	/**
	 * 封装返回对象
	 */
	private void setData(ArrayList<HistogramDataVO> vos, List<EmployeeVO> collect, String name) {
		HistogramDataVO histogramDataVO = new HistogramDataVO();
		histogramDataVO.setName(name);
		histogramDataVO.setValue(Func.isNotEmpty(collect) ? collect.size() : 0);
		vos.add(histogramDataVO);
	}
}
