package com.goafanti.admin.service.impl;

import com.alibaba.fastjson.JSON;
import com.goafanti.admin.service.DepartmentService;
import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.dao.DepartmentMapper;
import com.goafanti.common.dao.WorkingHoursMapper;
import com.goafanti.common.model.Department;
import com.goafanti.common.model.WorkingHours;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.shiro.token.TokenManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DepartmentServiceImpl extends BaseMybatisDao<WorkingHoursMapper> implements DepartmentService {

	@Autowired
	private WorkingHoursMapper	workingHoursMapper;
	@Autowired
	private DepartmentMapper departmentMapper;


	@Override
	public int addWorkingHours(WorkingHours in) {
		return workingHoursMapper.insertSelective(in);
	}

	@Override
	public int updateWorkingHours(WorkingHours in) {
		return workingHoursMapper.updateByPrimaryKeySelective(in);
	}

	@Override
	public int deleteWorkingHours(Integer id) {
		return workingHoursMapper.deleteByPrimaryKey(id);
	}

	@Override
	public List<WorkingHours> selectWorkingHours() {
		return workingHoursMapper.selectList();
	}

	@Override
	public WorkingHours getWorkingHours(String depId) {
		return workingHoursMapper.selectBydepId(depId);
	}

	@Override
	public boolean checkWorkingHoursType(WorkingHours in) {
		//如果没有分类，则无法判断数据库，也用于修改
		if (in.getType()!=null){
			List<WorkingHours> typeCount = workingHoursMapper.getTypeCount(in.getType());
			//新增，有则不允许新增
			if (in.getId()==null) {
				if(typeCount.size()>0) return true;
				//修改，判断
			}else {
				for (WorkingHours e : typeCount) {
					if (!e.getId().equals(in.getId())){
						return true;
					}
				}
			}
		}
		return false;
	}

	@Override
	public boolean checkDepWorkingHouresType(Integer id) {
		int i=workingHoursMapper.getDepTypeCount(id);
		if(i>0) {
			return true;
		}
		return false;
	}

	@Override
	public List<String> selectMyDeps() {
		return selectCurrentDep(TokenManager.getAdminId());
	}

	@Override
	public List<String> selectMyDeps(String depId) {
		List<String>ls= new ArrayList<>();
		ls.add(depId);
		List<String> myAll=new ArrayList<>();
		List<String> my=selectMyDeps();
		if (ls!=null&&!my.isEmpty()){
			for (String dep : ls) {
				for (String dep2 : my) {
					if (dep.equals(dep2))myAll.add(dep);
				}
			}
		}
		return myAll;
	}

	private List<String> selectCurrentDep(String managerId){
		List<String> alldep = new ArrayList<>();
		List<String> superData = departmentMapper.selectCurrentDeps(managerId);
		if (!superData.isEmpty()){
			List<Department> departments = departmentMapper.selectAllDeps();
			iterationAllDep(alldep,departments,superData);
//			iterationDep(alldep,superData);
		}
		return alldep;
	}

	private void iterationAllDep(List<String> alldep, List<Department> departments,List<String> superData) {
		List<String> deps2 =new ArrayList<>();
		for (String dep : superData) {
			if (!alldep.contains(dep)){
				alldep.add(dep);
				departments.forEach(e ->{
					if (e.getSuperId()!=null&&e.getSuperId().equals(dep)){
						deps2.add(e.getId());
					}
				});
			}
		}
		if (!deps2.isEmpty())iterationAllDep(alldep,departments,deps2);
	}

	private void iterationDep(List<String> alldep, List<String> superData) {
		for (String dep : superData) {
			if (!alldep.contains(dep)){
				alldep.add(dep);
				List<String> deps2 = departmentMapper.selectSubDeps(dep);
				if (!deps2.isEmpty())iterationDep(alldep,deps2);
			}

		}
	}


	@Override
	public List<String> selectSubDeps(String  deps) {
		List<String>ls= JSON.parseArray(deps,String.class);
			List<String> myAll=new ArrayList<>();
			List<String> my=selectMyDeps();
			if (ls!=null&&!my.isEmpty()){
				for (String dep : ls) {
					for (String dep2 : my) {
						if (dep.equals(dep2))myAll.add(dep);
					}
				}
			}
		return myAll;
	}
	public List<String> selectSubDeps(List<String>  deps) {
		return comparePermissions(selectMyDeps(),deps);
	}
	@Override
	public List<String> comparePermissions(List<String> aDep, String deps) {
		List<String> myAll=new ArrayList<>();
		List<String> input=parseArray(deps);
		if (TokenManager.hasRole(AFTConstants.SUPERADMIN)){
			return input;
		}
		for (String s : aDep) {
			for (String in : input) {
				if (s.equals(in))myAll.add(s);
			}
		}
		return myAll;
	}




	public List<String> comparePermissions(List<String> aDep, List<String> input) {
		List<String> myAll=new ArrayList<>();
		for (String s : aDep) {
			for (String in : input) {
				if (s.equals(in))myAll.add(s);
			}
		}
		return myAll;
	}

	@Override
	public 	List<Department> selectSubordinateDeps(String depId){
		return departmentMapper.selectSubDepsList(depId);
	}

	@Override
	public List<String> parseArray(String deps) {
		return JSON.parseArray(deps,String.class);
	}

}
