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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import org.security.common.Constant;
import org.security.common.eum.EducationEnum;
import org.security.ht.vo.basic.EmployeeVO;
import org.security.trans.dataStatistics.service.OutEmployeeDataService;
import org.security.trans.dataStatistics.vo.HistogramDataVO;
import org.security.trans.dataStatistics.vo.PieChartDataVO;
import org.security.trans.outsourcing.dto.OutEmployeeDTO;
import org.security.trans.outsourcing.entity.OutEmployee;
import org.security.trans.outsourcing.service.IOutEmployeeService;
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 ：wzf
 * @apiNote ：外包公司员工数据统计实现类
 * @since ：Created in 2022/12/2 11:34
 */
@Service
@AllArgsConstructor
public class OutEmployeeDataServiceImpl implements OutEmployeeDataService {

	private final IOutEmployeeService outEmployeeService;


	@Override
	public List<PieChartDataVO> getSexData(OutEmployeeDTO outEmployeeDTO) {
		List<OutEmployee> list = outEmployeeService.list(new QueryWrapper<OutEmployee>().lambda()
			.eq(Func.isNotEmpty(outEmployeeDTO.getCompanyId()), OutEmployee::getCompanyId, outEmployeeDTO.getCompanyId()));
		ArrayList<PieChartDataVO> vos = new ArrayList<>();
		if (Func.isNotEmpty(list)) {
			Map<String, List<OutEmployee>> collect = list.stream().filter(item -> Func.isNotEmpty(item.getSex())).collect(Collectors.groupingBy(OutEmployee::getSex));
			if (Func.isNotEmpty(collect)) {
				collect.forEach((key, value) -> {
					PieChartDataVO vo = new PieChartDataVO();
					vo.setName(Constant.SEX_BOY.equals(key) ? "男" : "女");
					vo.setValue(Func.isNotEmpty(value) ? value.size() : 0);
					vos.add(vo);
				});
			}
		}
		return vos;
	}

	@Override
	public List<HistogramDataVO> getAgeData(OutEmployeeDTO outEmployeeDTO) {
		List<OutEmployee> list = outEmployeeService.list(new QueryWrapper<OutEmployee>().lambda()
			.eq(Func.isNotEmpty(outEmployeeDTO.getCompanyId()), OutEmployee::getCompanyId, outEmployeeDTO.getCompanyId()));
		List<HistogramDataVO> vos = new ArrayList<>();
		if (Func.isNotEmpty(list)) {
			//没有年龄信息的
			List<OutEmployee> collectU = list.stream().filter(item -> Func.isEmpty(item.getAge())).collect(Collectors.toList());
			setData(vos, collectU, "年龄未知");
			//统计20岁以下
			List<OutEmployee> collect = list.stream().filter(item -> Func.isNotEmpty(item.getAge()) && Integer.parseInt(item.getAge()) < 20).collect(Collectors.toList());
			setData(vos, collect, "20岁以下");
			//统计[20-30)岁
			List<OutEmployee> collect1 = list.stream().filter(item -> Func.isNotEmpty(item.getAge()) && Integer.parseInt(item.getAge()) >= 20 && Integer.parseInt(item.getAge()) < 30).collect(Collectors.toList());
			setData(vos, collect1, "[20-30)");
			//统计[30-40}岁
			List<OutEmployee> collect2 = list.stream().filter(item -> Func.isNotEmpty(item.getAge()) && Integer.parseInt(item.getAge()) >= 30 && Integer.parseInt(item.getAge()) < 40).collect(Collectors.toList());
			setData(vos, collect2, "[30-40)");
			//统计[40-50)岁
			List<OutEmployee> collect3 = list.stream().filter(item -> Func.isNotEmpty(item.getAge()) && Integer.parseInt(item.getAge()) >= 40 && Integer.parseInt(item.getAge()) < 50).collect(Collectors.toList());
			setData(vos, collect3, "[40-50)");
			//50岁及以上
			List<OutEmployee> collect4 = list.stream().filter(item -> Func.isNotEmpty(item.getAge()) && Integer.parseInt(item.getAge()) >= 50).collect(Collectors.toList());
			setData(vos, collect4, "50岁及以上");
		}
		return vos;
	}

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

	@Override
	public List<PieChartDataVO> getNationTypeData(OutEmployeeDTO outEmployeeDTO) {
//		outEmployeeService.getNationTypeData(outEmployeeDTO);
		List<PieChartDataVO> vos = new ArrayList<>();
		List<OutEmployee> list = outEmployeeService.list(new QueryWrapper<OutEmployee>().lambda().eq(Func.isNotEmpty(outEmployeeDTO.getCompanyId()), OutEmployee::getCompanyId, outEmployeeDTO.getCompanyId()));
		if (Func.isNotEmpty(list)){
			//民族不为空
			Map<String, List<OutEmployee>> collect = list.stream().filter(item -> Func.isNotEmpty(item.getNation())).collect(Collectors.groupingBy(OutEmployee::getNation));
			if (Func.isNotEmpty(collect)){
				collect.forEach((key,value) ->{
					PieChartDataVO vo = new PieChartDataVO();
					vo.setName(outEmployeeService.getDictName(key));
					vo.setValue(Func.isNotEmpty(value) ? value.size() : 0);
					vos.add(vo);
				});
			}
			//民族为空
			List<OutEmployee> collect1 = list.stream().filter(item -> Func.isEmpty(item.getNation())).collect(Collectors.toList());
			PieChartDataVO dataVO = new PieChartDataVO();
			dataVO.setName("未知");
			dataVO.setValue(Func.isNotEmpty(collect1) ? collect1.size() : 0);
			vos.add(dataVO);
		}
		return vos;
	}

	@Override
	public List<PieChartDataVO> getPoliticalOutlookTypeData(OutEmployeeDTO outEmployeeDTO) {
		List<OutEmployee> list = outEmployeeService.list(new QueryWrapper<OutEmployee>().lambda()
			.eq(Func.isNotEmpty(outEmployeeDTO.getCompanyId()), OutEmployee::getCompanyId, outEmployeeDTO.getCompanyId()));
		List<PieChartDataVO> vos = new ArrayList<>();
		if (Func.isNotEmpty(list)){
			//政治面貌不为空
			Map<String, List<OutEmployee>> collect = list.stream().filter(item -> Func.isNotEmpty(item.getPoliticalOutlook())).collect(Collectors.groupingBy(OutEmployee::getPoliticalOutlook));
			if (Func.isNotEmpty(collect)){
				collect.forEach((key,value) ->{
					setPieChartResultData(vos,EducationEnum.parseByCode(key).getLabel(),value);
				});
			}
			//政治面貌为空
			List<OutEmployee> collect1 = list.stream().filter(item -> Func.isEmpty(item.getPoliticalOutlook())).collect(Collectors.toList());
			PieChartDataVO vo = new PieChartDataVO();
			vo.setName("未知");
			vo.setValue(Func.isNotEmpty(collect1) ? collect1.size() : 0);
			vos.add(vo);
		}
		return vos;
	}

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