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

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lz.common.utils.NumberUtil;
import com.lz.common.utils.PageUtils;
import com.lz.modules.app.dao.DepartmentsDao;
import com.lz.modules.app.dao.StaffDao;
import com.lz.modules.app.entity.DepartmentsEntity;
import com.lz.modules.app.entity.DepartmentsStaffRelateEntity;
import com.lz.modules.app.entity.StaffEntity;
import com.lz.modules.app.service.DepartmentsStaffRelateService;
import com.lz.modules.flow.dao.FlowManagerMapper;
import com.lz.modules.flow.entity.FlowManager;
import com.lz.modules.flow.model.FlowDto;
import com.lz.modules.flow.model.FlowInfo;
import com.lz.modules.flow.service.FlowManagerService;
import com.lz.modules.flow.service.RecordRoleService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
* <p>
* 流程管理表 服务类
* </p>
*
* @author quyixiao
* @since 2020-09-22
*/

@Service
public class FlowManagerServiceImpl extends ServiceImpl<FlowManagerMapper, FlowManager> implements FlowManagerService {


    @Autowired
	private FlowManagerMapper flowManagerMapper;

    @Autowired
	private DepartmentsDao departmentsDao;

    @Autowired
	private DepartmentsStaffRelateService departmentsStaffRelateService;

    @Autowired
	private StaffDao staffDao;

    @Autowired
	private RecordRoleService recordRoleService;



	@Override
	public FlowManager selectFlowManagerById(Long id){
		return flowManagerMapper.selectFlowManagerById(id);
	}



	@Override
	public Long insertFlowManager(FlowManager flowManager){
		return flowManagerMapper.insertFlowManager(flowManager);
	}



	@Override
	public int updateFlowManagerById(FlowManager flowManager){
		return flowManagerMapper.updateFlowManagerById(flowManager);
	}



	@Override
	public int updateCoverFlowManagerById(FlowManager flowManager){
		return flowManagerMapper.updateCoverFlowManagerById(flowManager);
	}



	@Override
	public int deleteFlowManagerById(Long id){
		return flowManagerMapper.deleteFlowManagerById(id);
	}

	@Override
	public PageUtils queryPage(Map<String, Object> params) {

		PageUtils pageUtils = PageUtils.startPage(
				NumberUtil.objToIntDefault(params.get("page"), 1),
				NumberUtil.objToIntDefault(params.get("limit"), 10)).doSelect(
				page -> flowManagerMapper.selectByCondition(page, params)
		);

		return pageUtils;
	}

	@Override
	public void deleteBatchIds(List<Long> asList) {

	}

    @Override
    public List<FlowDto> getFowList() {
		List<DepartmentsEntity> tDepartments = departmentsDao.selectAll();
		List<DepartmentsEntity> parentDepartments = tDepartments.stream()
				//根据两个属性进行过滤
				.filter(s -> s.getDepartmentParentId().equals("1"))
				.collect(Collectors.toList());
		List<DepartmentsStaffRelateEntity> departmentsStaffRelateEntities = departmentsStaffRelateService.selectAll();
		Map<String, FlowInfo> departmentsStaffRelateEntityMap = new HashMap<>();
		Map<String, DepartmentsStaffRelateEntity> singleRelateMap = new HashMap<>();

		for(DepartmentsStaffRelateEntity d : departmentsStaffRelateEntities){
			singleRelateMap.put(d.getDepartmentId(),d);
			if(new Integer(1).equals(d.getIsLeader())){
				FlowInfo flowInfo = new FlowInfo();
				flowInfo.setParentDepartmentRelate(d);
				List<DepartmentsStaffRelateEntity> list = new ArrayList<>();
				for(DepartmentsStaffRelateEntity d2 : departmentsStaffRelateEntities){
					if(d2.getDepartmentId().equals(d.getDepartmentId()) && new Integer(0).equals(d2.getIsLeader())){
						list.add(d2);
					}
				}
				flowInfo.setList(list);
				departmentsStaffRelateEntityMap.put(d.getDepartmentId(),flowInfo);
			}
		}

		List<StaffEntity> staffEntities = staffDao.selectAll();
		Map<Long ,StaffEntity> staffMap = new HashMap<>();
		for(StaffEntity staffEntity : staffEntities){
			staffMap.put(staffEntity.getId(),staffEntity);
		}

		List<FlowDto> list = new ArrayList<>();
		for (DepartmentsEntity d : parentDepartments) {
			StaffEntity staff = getStaff(departmentsStaffRelateEntityMap,staffMap,d.getDepartmentId(),singleRelateMap);
			FlowDto entity = buildMenuEntity(d.getDepartmentId(),d.getDepartmentName() , "1", staff.getName(),staff.getId());
			List<FlowDto> childList = getMenuList(tDepartments, entity,departmentsStaffRelateEntityMap,staffMap,singleRelateMap);
			entity.setList(childList);
			list.add(entity);
		}
		return list;
	}


	public FlowDto buildMenuEntity(String departmentId, String departmentName, String parentId, String realName,Long staffId) {
		FlowDto flowDto = new FlowDto();
		flowDto.setDepartmentId(departmentId);
		flowDto.setDepartmentName(departmentName);
		flowDto.setDepartmentParentId(parentId);
		flowDto.setRealName(realName);
		flowDto.setStaffId(staffId);
		return flowDto;
	}


	public StaffEntity getStaff(Map<String, FlowInfo> departmentsStaffRelateEntityMap, Map<Long, StaffEntity> staffMap, String departmentId
			, Map<String, DepartmentsStaffRelateEntity> singleRelateMap) {
		FlowInfo flowInfo = departmentsStaffRelateEntityMap.get(departmentId);
		if (flowInfo != null) {
			return doGetStaff(flowInfo.getParentDepartmentRelate(), staffMap);
		} else {
			return doGetStaff(singleRelateMap.get(departmentId), staffMap);
		}
	}

	public StaffEntity doGetStaff(DepartmentsStaffRelateEntity departmentsStaffRelateEntity, Map<Long, StaffEntity> staffMap) {
		if (departmentsStaffRelateEntity != null) {
			StaffEntity staffEntity = staffMap.get(departmentsStaffRelateEntity.getStaffId());
			if (staffEntity != null) {
				return staffEntity;
			}
		}
		return new StaffEntity(0l,"");
	}

	public List<FlowDto> getMenuList(List<DepartmentsEntity> tDepartments,FlowDto flowDto,
									 Map<String, FlowInfo> departmentsStaffRelateEntityMap, Map<Long, StaffEntity> staffMap
			, Map<String, DepartmentsStaffRelateEntity> singleRelateMap) {
		List<FlowDto> flowDtos = new ArrayList<>();
		for (DepartmentsEntity child : tDepartments) {
			if (child.getDepartmentParentId().equals(flowDto.getDepartmentId())) {
				StaffEntity staff = getStaff(departmentsStaffRelateEntityMap, staffMap, child.getDepartmentId(), singleRelateMap);
				FlowDto entity = buildMenuEntity(child.getDepartmentId(),
						child.getDepartmentName(), flowDto.getDepartmentId(), staff.getName(),staff.getId());

				List<FlowDto> list = getMenuList(tDepartments, entity, departmentsStaffRelateEntityMap, staffMap, singleRelateMap);
				if (CollectionUtils.isEmpty(list) || list.size() == 0) {
					FlowInfo flowInfo = departmentsStaffRelateEntityMap.get(child.getDepartmentId());
					if (flowInfo != null && CollectionUtils.isNotEmpty(flowInfo.getList()) && flowInfo.getList().size() > 0) {
						list = new ArrayList<>();
						for (DepartmentsStaffRelateEntity dr : flowInfo.getList()) {
							StaffEntity staffEntity = staffMap.get(dr.getStaffId());
							FlowDto childEntiry = buildMenuEntity(dr.getDepartmentId() + "_" + dr.getStaffId(),
									"		",
									entity.getDepartmentId(),
									staffEntity.getName(),staff.getId());
							list.add(childEntiry);
						}
					}
				}
				entity.setList(list);
				flowDtos.add(entity);
			}
		}
		return flowDtos;
	}


}
