/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.leavetype.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import com.jeeplus.common.utils.IdGen;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.sys.entity.MainDictDetail;
import com.jeeplus.modules.sys.entity.Office;
import com.jeeplus.modules.sys.service.MainDictDetailService;
import com.jeeplus.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.service.CrudService;
import com.jeeplus.modules.leavetype.entity.LeaveType;
import com.jeeplus.modules.leavetype.dao.LeaveTypeDao;

/**
 * 假期类型字典Service
 * @author ssrh
 * @version 2018-09-07
 */
@Service
@Transactional(readOnly = true)
public class LeaveTypeService extends CrudService<LeaveTypeDao, LeaveType> {

	@Autowired
	private LeaveTypeDao leaveTypeDao;
	public LeaveType get(String id) {
		return super.get(id);
	}
	
	public List<LeaveType> findList(LeaveType leaveType) {
		return super.findList(leaveType);
	}

	public Page<LeaveType> findPage(Page<LeaveType> page, LeaveType leaveType) {
		return super.findPage(page, leaveType);
	}
	
	@Transactional(readOnly = false)
	public void save(LeaveType leaveType) {
		super.save(leaveType);
	}
	
	@Transactional(readOnly = false)
	public void delete(LeaveType leaveType) {
		super.delete(leaveType);
	}

	/**
	 * 修改末级标记
	 * @return
	 */
	@Transactional(readOnly = false)
	public int upateEndFlag(LeaveType md){
		return dao.upateEndFlag(md);
	}
	@Transactional(readOnly = false)
	public int updateDelFlag(String id,String flat){
		return dao.updateDelFlag(id,flat);
	}

	/**
	 * 同步默认字典
	 * @return
	 */
	@Transactional(readOnly = false)
	public boolean syncDict(LeaveType leaveType){
		boolean admin = UserUtils.getUser().isAdmin();
		boolean flag = false;
		if(admin){
			return flag;
		}
		List<LeaveType> list = this.findList(leaveType);
		List<LeaveType> newList=new ArrayList<>();
		LeaveType rootNode=new LeaveType("0");
		rootNode.setParentIds("");
		rootNode.setNewId("0");
		this.createNewList(newList,list,rootNode);
		List<LeaveType> newList2=new ArrayList<>();
		for(LeaveType leaveType1:newList){//设置父节点
			if(!"0".equals(leaveType1.getParentId())){
				for(LeaveType newDetail1:newList){
					if(leaveType1.getParentId().equals(newDetail1.getId())){
						leaveType1.setParentId(newDetail1.getNewId());
						break;
					}
				}
			}
			leaveType1.setIsSys("1");
			leaveType1.setOfficeId(UserUtils.getSelectOffice().getId());
			leaveType1.setBranchOffice(UserUtils.getSelectBranchOffice());
			leaveType1.setCompanyId(UserUtils.getSelectCompany().getId());
			newList2.add(leaveType1);
		}

		for(LeaveType newDetail12:newList2){
			LeaveType nameByIdAndKey = leaveTypeDao.getNameByIdAndKey(newDetail12);
			if(nameByIdAndKey==null){
				newDetail12.setId(newDetail12.getNewId());
				newDetail12.setCreateBy(UserUtils.getUser());
				newDetail12.setCreateDate(new Date());
				newDetail12.setUpdateBy(UserUtils.getUser());
				newDetail12.setUpdateDate(new Date());
				leaveTypeDao.insert(newDetail12);
				flag=true;
			}
		}
		return flag;
	}

	private void createNewList(List<LeaveType> newList, List<LeaveType> list, LeaveType rootNode) {
		Iterator<LeaveType> iterator = list.iterator();
		while (iterator.hasNext()){
			LeaveType next = iterator.next();
			if(next.getParentId().equals(rootNode.getId())){
				next.setNewId(IdGen.uuid());
				next.setParentIds(rootNode.getParentIds()+rootNode.getNewId()+",");
				newList.add(next);
				this.createNewList(newList,list,next);
			}
		}
	}


}