package com.zcloud.modules.oa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zcloud.common.utils.PageUtils;
import com.zcloud.common.utils.Query;
import com.zcloud.modules.oa.dao.OaDepartmentDao;
import com.zcloud.modules.oa.entity.OaDepartmentEntity;
import com.zcloud.modules.oa.service.OaDepartmentService;
import org.springframework.stereotype.Service;

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


@Service("oaDepartmentService")
public class OaDepartmentServiceImpl extends ServiceImpl<OaDepartmentDao, OaDepartmentEntity> implements OaDepartmentService {

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		params.put("sidx","order_by");
		params.put("order","asc");
		String parentId = (String)params.get("parentId");

		QueryWrapper<OaDepartmentEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("parent_id", parentId);
		queryWrapper.eq("is_delete",0);
		if(params.get("name") != null){
			queryWrapper.like("name", (String)params.get("name"));
		}
		if(params.get("level") != null){
			queryWrapper.eq("level", (String)params.get("level"));
		}

		IPage<OaDepartmentEntity> page = this.page(
				new Query<OaDepartmentEntity>().getPage(params),
				queryWrapper
		);

		return new PageUtils(page);
	}

	@Override
	public List<OaDepartmentEntity> queryListParentId(String parentId,String corpinfoId) {
		return baseMapper.queryListParentId(parentId,corpinfoId);
	}

	@Override
	public void delete(String departmentId){
		baseMapper.deleteById(departmentId);
	}

	@Override
	public List<OaDepartmentEntity> getDeptTree(String parentId,String corpinfoId){
		return this.getAlldepartmentList(parentId,corpinfoId);
	}

	@Override
	public boolean saveDepartment(OaDepartmentEntity department) {
		QueryWrapper<OaDepartmentEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("name",department.getName());
		queryWrapper.eq("corpinfo_id",department.getCorpinfoId());
//		queryWrapper.eq("parent_id",department.getParentId());

		OaDepartmentEntity oaDepartmentEntity = baseMapper.selectOne(queryWrapper);
		if (oaDepartmentEntity != null)
			return false;

		baseMapper.insert(department);
		return true;
	}

	@Override
	public OaDepartmentEntity getByName(String name) {
		QueryWrapper<OaDepartmentEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("name",name);
		return baseMapper.selectOne(queryWrapper);
	}

	@Override
	public void editOpenid(String phone, String openid) {
		OaDepartmentEntity departmentEntity = new OaDepartmentEntity();
		departmentEntity.setOpenid(openid);
		baseMapper.update(departmentEntity,
				new QueryWrapper<OaDepartmentEntity>().eq("phone", phone));
	}

	/**
	 * 获取所有部门列表
	 */
	private List<OaDepartmentEntity> getAlldepartmentList(String parentId,String corpinfoId){
		//查询根部门列表
		List<OaDepartmentEntity> departmentList = queryListParentId(parentId,corpinfoId);
		//递归获取子部门
		getdepartmentTreeList(departmentList);

		return departmentList;
	}

	/**
	 * 递归
	 */
	private List<OaDepartmentEntity> getdepartmentTreeList(List<OaDepartmentEntity> departmentList){
		List<OaDepartmentEntity> subdepartmentList = new ArrayList<OaDepartmentEntity>();

		for(OaDepartmentEntity entity : departmentList){
			//目录
			entity.setList(getdepartmentTreeList(queryListParentId(entity.getDepartmentId(),entity.getCorpinfoId())));
			subdepartmentList.add(entity);
		}

		return subdepartmentList;
	}
}
