package com.ym.mec.biz.service.impl;

import java.util.*;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.enums.FivePlusGradeEnum;
import com.ym.mec.biz.dal.enums.GradeTypeEnum;
import com.ym.mec.biz.dal.enums.SixPlusGradeEnum;
import com.ym.mec.common.exception.BizException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ym.mec.biz.dal.dao.OrganizationDao;
import com.ym.mec.biz.dal.entity.Organization;
import com.ym.mec.biz.dal.page.OrganizationQueryInfo;
import com.ym.mec.biz.service.OrganizationService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.common.service.impl.BaseServiceImpl;

@Service
public class OrganizationServiceImpl extends BaseServiceImpl<Integer, Organization> implements OrganizationService {

	@Autowired
	private OrganizationDao organizationDao;
	@Autowired
	private SysUserFeignService sysUserFeignService;

	@Override
	public BaseDAO<Integer, Organization> getDAO() {
		return organizationDao;
	}

	@Override
	public PageInfo<Organization> queryTreePage(OrganizationQueryInfo queryInfo){
		PageInfo<Organization> pageInfo = queryPage(queryInfo);
		for (Organization organization:pageInfo.getRows()) {
			organization = getTree(organization);
		}
		return pageInfo;
	}

	@Override
	public List<Organization> queryEmployeeOrgan(){
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if(sysUser == null){
			throw new BizException("获取用户信息失败");
		}
		Integer userId = sysUser.getId();
		return organizationDao.queryEmployeeOrgan(userId);
	}

	@Override
	public List<Organization> queryEmployeeOrganByUser(){
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if(sysUser == null){
			throw new BizException("获取用户信息失败");
		}
		Integer userId = sysUser.getId();
		if(sysUser.getIsSuperAdmin()){
			userId = null;
		}
		return organizationDao.queryEmployeeOrgan(userId);
	}

	@Override
	public String getOrganName(String organId) {
		return organizationDao.getOrganName(organId);
	}


    private Organization getTree(Organization org){
		//得到根节点对象
		//获取子节点list
		List<Organization> byParentId = organizationDao.findByParentId(org.getId(), null);
		//如果存在子节点
		if(byParentId != null && byParentId.size() > 0) {
			//将子节点list放入父节点对象
			org.setOrganizations(byParentId);
			//遍历子节点....
			for (Organization organization : byParentId) {
				getTree(organization);
			}
		}
			return org;
	}

	@Override
	public Map<Integer, String> getGradeList(Integer id) {
		Organization organization = organizationDao.get(id);
		Map<Integer, String> gradeMap = new HashMap<>();

		if(Objects.isNull(organization)){
			gradeMap.put(GradeTypeEnum.SIX_PLUS.getCode(), GradeTypeEnum.SIX_PLUS.getDesc());
			return gradeMap;
		}

		if(organization.getGradeType().equals(GradeTypeEnum.FIVE_PLUS)){
			FivePlusGradeEnum[] fivePlusGradeEnums = FivePlusGradeEnum.values();
			for (FivePlusGradeEnum fivePlusGradeEnum : fivePlusGradeEnums) {
				gradeMap.put(fivePlusGradeEnum.getCode(),fivePlusGradeEnum.getDesc());
			}
		}
		if(organization.getGradeType().equals(GradeTypeEnum.SIX_PLUS)){
			SixPlusGradeEnum[] sixPlusGradeEnums = SixPlusGradeEnum.values();
			for (SixPlusGradeEnum sixPlusGradeEnum : sixPlusGradeEnums) {
				gradeMap.put(sixPlusGradeEnum.getCode(),sixPlusGradeEnum.getDesc());
			}
		}
		return gradeMap;
	}
}
