package com.lz.modules.flow.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lz.common.utils.ISelect;
import com.lz.common.utils.PageUtils;
import com.lz.common.utils.R;
import com.lz.common.utils.StringUtil;
import com.lz.modules.app.entity.DepartmentsEntity;
import com.lz.modules.app.entity.StaffSimpleInfo;
import com.lz.modules.app.service.DepartmentsService;
import com.lz.modules.app.service.StaffOccupationService;
import com.lz.modules.app.service.StaffService;
import com.lz.modules.flow.dao.EvaluationGroupMapper;
import com.lz.modules.flow.entity.EvaluationGroup;
import com.lz.modules.flow.req.EvaluationGroupReq;
import com.lz.modules.flow.service.EvaluationGroupService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;

/**
* <p>
* 考评组表 服务类
* </p>
*
* @author quyixiao
* @since 2020-10-13
*/

@Service
public class EvaluationGroupServiceImpl extends ServiceImpl<EvaluationGroupMapper, EvaluationGroup> implements EvaluationGroupService {


    @Autowired
	private EvaluationGroupMapper evaluationGroupMapper;
    @Autowired
	private StaffService staffService;
    @Autowired
	private StaffOccupationService staffOccupationService;

    @Autowired
	private EvaluationGroupService evaluationGroupService;

    @Autowired
	private DepartmentsService departmentsService;



	@Override
	public EvaluationGroup selectEvaluationGroupById(Long id){
		return evaluationGroupMapper.selectEvaluationGroupById(id);
	}



	@Override
	public Long insertEvaluationGroup(EvaluationGroup evaluationGroup){
		return evaluationGroupMapper.insertEvaluationGroup(evaluationGroup);
	}



	@Override
	public int updateEvaluationGroupById(EvaluationGroup evaluationGroup){
		return evaluationGroupMapper.updateEvaluationGroupById(evaluationGroup);
	}



	@Override
	public int updateCoverEvaluationGroupById(EvaluationGroup evaluationGroup){
		return evaluationGroupMapper.updateCoverEvaluationGroupById(evaluationGroup);
	}



	@Override
	public R deleteEvaluationGroupById(Long id){
		evaluationGroupMapper.deleteEvaluationGroupById(id);
		return R.ok();
	}

	@Override
	public PageUtils selectEvaluationGroupByReq(EvaluationGroupReq req){
		PageUtils pageUtils = PageUtils.startPage(req.getCurrPage(), req.getPageSize())
				.doSelect(page -> evaluationGroupMapper.seleteEvaluationGroupByReq(page, req));
		//下面实时统计考核人数
		List<EvaluationGroup> groups = pageUtils.getList();
		for (EvaluationGroup group:groups
			 ) {
			List<String> ids = evaluationGroupService.selectAllStaffIdsByGroupId(group.getId());
			group.setCounts(ids.size());
		}
		return pageUtils;
	}


	@Override
	public List<String> selectAllStaffIdsByGroupId(Long id) {
		EvaluationGroup evaluationGroup = this.selectEvaluationGroupById(id);
		if(evaluationGroup ==null ){
			return Collections.EMPTY_LIST;
		}
		String depIds = evaluationGroup.getDepIds();
		Set<String> allDeparmentIds = new HashSet<>();
		if(StringUtils.isNotBlank(depIds)){
			String[] split = depIds.split(",");
			for(String s:split){
				List<String> strings = staffService.selectAllDeparmentIdsByDepartmentParentId(s);
				allDeparmentIds.addAll(strings);
			}
		}
		List<String> depStaffIds = staffService.staffsByAllDeparmentIds(new ArrayList<>(allDeparmentIds));

		String staffIds = evaluationGroup.getStaffIds();
		if(StringUtils.isNotBlank(staffIds)){
			String[] split = staffIds.split(",");
			depStaffIds.addAll(Arrays.asList(split));
		}
		String outIds = evaluationGroup.getOutIds();
		if(StringUtils.isNotBlank(outIds)){
			String[] split = outIds.split(",");
			depStaffIds.removeAll(Arrays.asList(split));
		}
		List<String> distDepStaffIds = new ArrayList<>(new HashSet(depStaffIds));
		//去除离职
		List<String> data = staffOccupationService.removeDimissionStaffByStaffIds(distDepStaffIds);
		return data;
	}

	@Override
	public List<EvaluationGroup> selectEvaluationGroupByIds(List<Long> ids){
		return evaluationGroupMapper.selectEvaluationGroupByIds(ids);
	}

	@Override
	public EvaluationGroup selectEvaluationGroupByName(String name){
		return evaluationGroupMapper.selectEvaluationGroupByName(name);
	}

	@Override
	public List<StaffSimpleInfo> selectAllStaffSimpleInfoByGroupId(EvaluationGroup evaluationGroup){
		List<StaffSimpleInfo> staffSimpleInfos = new ArrayList<>();
		//下面获取部门人员
		if(!StringUtil.isEmpty(evaluationGroup.getDepIds())){


			List<String> dIds = Arrays.stream(evaluationGroup.
					getDepIds().split(",")).map(new Function<String, String>() {
				@Override
				public String apply(String s) {
					return s;
				}
			}).collect(Collectors.toList());
			//获取所有部门信息，包括子部门
			List<DepartmentsEntity> departmentsEntities = departmentsService.selectAllDepartmentIds(dIds);
			//下面获取部门下面的所有在职人员
			for (DepartmentsEntity depart:departmentsEntities
				 ) {

				List<StaffSimpleInfo> staffSimpleInfos1 = staffService.selectAllStaffSimpleInfos(depart);
				if(staffSimpleInfos1 != null){
					staffSimpleInfos.addAll(staffSimpleInfos1);
				}
			}
		}

		//下面获取指定人员
		if(!StringUtil.isEmpty(evaluationGroup.getStaffIds())){

			List<Long> sIds = Arrays.stream(evaluationGroup.getStaffIds()
			.split(",")).map(new Function<String, Long>() {
				@Override
				public Long apply(String s) {
					return Long.parseLong(s);
				}
			}).collect(Collectors.toList());
			List<StaffSimpleInfo> staffSimpleInfos1 = staffService.selectStaffSimpleInfos(sIds);
			if(staffSimpleInfos1 != null){
				staffSimpleInfos.addAll(staffSimpleInfos1);
			}
		}
		//下面去重
		if(staffSimpleInfos.size() > 1){
			//下面去除跨部门的人员，只保留第一次添加进来的人员
			Map<Long, StaffSimpleInfo> staffSimpleInfoMap =
					staffSimpleInfos.stream().collect(Collectors.toMap(StaffSimpleInfo::getId, Function.identity(), (e, replace) -> e));
			//下面删除排除人员信息
			if(!StringUtil.isEmpty(evaluationGroup.getOutIds())){
				List<Long> oIds = Arrays.stream(evaluationGroup.getOutIds().split(",")).map(new Function<String, Long>() {
					@Override
					public Long apply(String s) {
						return Long.parseLong(s);
					}
				}).collect(Collectors.toList());

				for (Long id:oIds
					 ) {
					staffSimpleInfoMap.remove(id);
				}
			}


			staffSimpleInfos = staffSimpleInfoMap.values().stream().collect(Collectors.toList());
		}


		return staffSimpleInfos;
	}
}
