package com.zcloud.service.offduty.impl;

import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.entity.system.Department;
import com.zcloud.mapper.datasource.offduty.OffDutyMapper;
import com.zcloud.service.offduty.OffDutyService;
import com.zcloud.service.system.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 说明：休假
 * 作者：luoxiaobao
 * 时间：2021-06-11
 * 官网：www.zcloudchina.com
 */
@Service
@Transactional //开启事物
public class OffDutyServiceImpl implements OffDutyService {

	@Autowired
	private OffDutyMapper offdutyMapper;
	@Autowired
	private DepartmentService departmentService;

	/**新增
	 * @param pd
	 * @throws Exception
	 */
	public void save(PageData pd)throws Exception{
		offdutyMapper.save(pd);
	}

	/**删除
	 * @param pd
	 * @throws Exception
	 */
	public void delete(PageData pd)throws Exception{
		offdutyMapper.delete(pd);
	}

	/**修改
	 * @param pd
	 * @throws Exception
	 */
	public void edit(PageData pd)throws Exception{
		offdutyMapper.edit(pd);
	}

	/**列表
	 * @param page
	 * @throws Exception
	 */
	public List<PageData> list(Page page)throws Exception{
		if ("edit".equals(page.getPd().getString("TYPE"))) {
			List<String> deptArr = new ArrayList<String>();
			deptArr.add(page.getPd().getString("DEPARTMENT_ID"));
			findDeptChild(deptArr, page.getPd().getString("DEPARTMENT_ID"));
			if (deptArr.size() > 0) {
				page.getPd().put("DEPT_DOWN_ARR", deptArr);
			}
			page.getPd().remove("DEPARTMENT_ID");
		}
		return offdutyMapper.datalistPage(page);
	}

	private void findDeptChild(List<String> deptArr, String parentId) throws Exception {
		List<Department> deptList = departmentService.listSubDepartmentByParentId(parentId);
		for (Department d : deptList) {
			deptArr.add(d.getDEPARTMENT_ID());
			findDeptChild(deptArr, d.getDEPARTMENT_ID());
		}
	}

	/**列表(全部)
	 * @param pd
	 * @throws Exception
	 */
	public List<PageData> listAll(PageData pd)throws Exception{
		return offdutyMapper.listAll(pd);
	}

	/**通过id获取数据
	 * @param pd
	 * @throws Exception
	 */
	public PageData findById(PageData pd)throws Exception{
		return offdutyMapper.findById(pd);
	}

	/**批量删除
	 * @param ArrayDATA_IDS
	 * @throws Exception
	 */
	public void deleteAll(String[] ArrayDATA_IDS)throws Exception{
		offdutyMapper.deleteAll(ArrayDATA_IDS);
	}

}

