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

import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.service.CrudService;
import com.jeeplus.common.utils.IdGen;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.sys.dao.MainDictDao;
import com.jeeplus.modules.sys.dao.MainDictDetailDao;
import com.jeeplus.modules.sys.entity.MainDict;
import com.jeeplus.modules.sys.entity.MainDictDetail;
import com.jeeplus.modules.sys.entity.Office;
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 java.util.*;

/**
 * 字典明细Service
 * @author 马鹏波
 * @version 2018-05-30
 */
@Service
@Transactional(readOnly = true)
public class MainDictDetailService extends CrudService<MainDictDetailDao, MainDictDetail> {

	@Autowired
	private MainDictDetailDao mainDictDetailDao;
	@Autowired
	private MainDictDao mainDictDao;

	public MainDictDetail get(String id) {
		return super.get(id);
	}

	public MainDictDetail getByDetailKey(String detailKey,String companyId) {
		MainDictDetail mainDictDetail = new MainDictDetail();
		mainDictDetail.setDetailKey(detailKey);
		mainDictDetail.setCompany(new Office(companyId));
		return dao.getByDetailKey(mainDictDetail);
	}
	
	public List<MainDictDetail> findList(MainDictDetail mainDictDetail) {
		return super.findList(mainDictDetail);
	}
	
	public Page<MainDictDetail> findPage(Page<MainDictDetail> page, MainDictDetail mainDictDetail) {
		return super.findPage(page, mainDictDetail);
	}
	
	@Transactional(readOnly = false)
	public void save(MainDictDetail mainDictDetail) {
		super.save(mainDictDetail);
	}
	
	@Transactional(readOnly = false)
	public void delete(MainDictDetail mainDictDetail) {
		super.delete(mainDictDetail);
	}

	/**
	 * 树
	 * @param typeId
	 * @return
	 */
	public List<MainDictDetail> findList(String typeId,String company){
		MainDict mainDict=mainDictDao.get(typeId);
		MainDictDetail mainDictDetail = new MainDictDetail();
		mainDictDetail.setTypeId(typeId);
		mainDictDetail.setBranchOffice(company);//登录人属于总公司
		List<MainDictDetail> list = super.findList(mainDictDetail);
		return list;
	}

	/**
	 * 选择树
	 *
	 * @param typeId
	 *
	 * @return
	 */
	public List<MainDictDetail> findListByType(String typeId){
		List<MainDictDetail> list=new ArrayList<>();
		MainDictDetail mainDictDetail = new MainDictDetail();
		mainDictDetail.setTypeId(typeId);
		MainDict mainDict=mainDictDao.get(typeId);
		if("1".equals(mainDict.getDictType())){
			mainDictDetail.setBranchOffice(UserUtils.getSelectCompany().getId());
		}else{
			mainDictDetail.setBranchOffice(UserUtils.getSelectBranchOffice());
		}
			list = mainDictDetailDao.findList(mainDictDetail);
		return list;
	}

	/**
	 * gl 过滤条件 是否查询系统级字典
	 * @param
	 * @param typeId
	 * @param
	 * @return
	 */
	public List<MainDictDetail> findListOther(String typeId){
		MainDictDetail mainDictDetail = new MainDictDetail();
		boolean admin = UserUtils.getUser().isAdmin();
		List<MainDictDetail> list=new ArrayList<>();
		if(!admin){
			mainDictDetail.setTypeId(typeId);
			mainDictDetail.setBranchOffice(UserUtils.getSelectCompany().getId());
			mainDictDetail.setLevel("2");
			list = mainDictDetailDao.findListOther(mainDictDetail);
			return list;
		}else{
			mainDictDetail.setTypeId(typeId);
			mainDictDetail.setLevel("1");
			return super.findList(mainDictDetail);
		}
	}

	/**
	 * gl 过滤条件 是否查询系统级字典
	 * @param
	 * @param typeId
	 * @param
	 * @return
	 */
	public List<MainDictDetail> getDictDetailDefalt(String typeId){
		MainDictDetail mainDictDetail = new MainDictDetail();
		mainDictDetail.setTypeId(typeId);
		mainDictDetail.setLevel("1");
		return super.findList(mainDictDetail);
	}

	/**
	 * 自定义
	 * @param typeId
	 * @return
	 */
	public List<MainDictDetail> findLists(String typeId,String company){
		MainDict mainDict=mainDictDao.get(typeId);
		MainDictDetail mainDictDetail = new MainDictDetail();
		mainDictDetail.setTypeId(typeId);
		mainDictDetail.setBranchOffice(company);//登录人属于总公司
		List<MainDictDetail> list = super.findList(mainDictDetail);
		return list;
	}

	/**
	 * 获取父节点名称
	 * @param parentIds
	 * @return
	 */
	public String getParentName(String parentIds){
		String str = "";
		if(!Strings.isNullOrEmpty(parentIds) && "0,".equals(parentIds)){
			String[] split = parentIds.split(",");
			for (int i=0; i<split.length;i++){
				if(!"0".equals(split[i])) {
					MainDictDetail mainDictDetail = mainDictDetailDao.get(split[i]);
					if(mainDictDetail != null){
						if(i + 1 == split.length){
							str += mainDictDetail.getDetailName();
						}else{
							str += mainDictDetail.getDetailName() + "/";
						}

					}
				}
			}
		}
		return str;
	}

	/**
	 * 修改末级标记
	 * @return
	 */
	public int upateEndFlag(MainDictDetail md){
		return mainDictDetailDao.upateEndFlag(md);
	}

	/**
	 * 更改删除标志
	 * @return
	 */
	@Transactional(readOnly = false)
	public int updateDelFlag(String DEL_FLAG_DELETE,String id){
		MainDictDetail mainDictDetail = new MainDictDetail();
		mainDictDetail.setDelFlag(DEL_FLAG_DELETE);
		mainDictDetail.setId(id);
		return mainDictDetailDao.deleteByLogic(mainDictDetail);
	}

	public MainDictDetail getNameByIdAndKey(MainDictDetail mainDictDetail){
		return mainDictDetailDao.getNameByIdAndKey(mainDictDetail);
	}

	public Boolean getDetailKey(String detailKey,String typeId,String id,String branchOffice){
		MainDict mainDict=mainDictDao.get(typeId);
		List<MainDictDetail> m= mainDictDetailDao.getDetailKey(detailKey,typeId,id,branchOffice);
		if (m!=null&&m.size()>0){
			return true;
		}else{
			return false;
		}
	}


	/**
	 * 同步默认字典
	 * @return
	 */
	@Transactional(readOnly = false)
	public boolean syncDict(String dictId,String company){
		MainDict mainDict=mainDictDao.get(dictId);
		boolean admin = UserUtils.getUser().isAdmin();
		boolean flag = false;
		if(admin){
			return flag;
		}
		MainDictDetail dictDetail = new MainDictDetail();
		dictDetail.setTypeId(dictId);
		List<MainDictDetail> list = this.getDictDetailDefalt(dictId);
		List<MainDictDetail> newList=new ArrayList<>();
		MainDictDetail rootNode = new MainDictDetail("0");
		rootNode.setParentIds("");
		rootNode.setNewId("0");
		this.createNewList(newList,list,rootNode);
		List<MainDictDetail> newList2=new ArrayList<>();
		for(MainDictDetail mainDictDetail1:newList){//设置父节点
			if(!"0".equals(mainDictDetail1.getParentId())){
				for(MainDictDetail newDetail1:newList){
					if(mainDictDetail1.getParentId().equals(newDetail1.getId())){
						mainDictDetail1.setParentId(newDetail1.getNewId());
						break;
					}
				}
			}
			mainDictDetail1.setLevel("2");
			mainDictDetail1.setBranchOffice(company);
			mainDictDetail1.setCompany(UserUtils.getSelectCompany());
			newList2.add(mainDictDetail1);
		}
		for(MainDictDetail newDetail12:newList2){
			MainDictDetail nameByIdAndKey = mainDictDetailDao.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());
				mainDictDetailDao.insert(newDetail12);
				flag=true;
			}
		}
		return flag;
	}

    private void createNewList(List<MainDictDetail> newList, List<MainDictDetail> list, MainDictDetail rootNode) {
        Iterator<MainDictDetail> iterator = list.iterator();
        while (iterator.hasNext()){
            MainDictDetail 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);
            }
        }
    }

    public boolean selectFlag(MainDictDetail mainDictDetail){
		Office office=UserUtils.getSelectOffice();
		boolean admin = UserUtils.getUser().isAdmin();
		if(!admin){
			if("check_subjects".equals(mainDictDetail.getType())||"language".equals(mainDictDetail.getType())
					||"staff_duty".equals(mainDictDetail.getType())
					||"training_type".equals(mainDictDetail.getType())
					||"record_type".equals(mainDictDetail.getType())
					||"certificate_type".equals(mainDictDetail.getType())
					||"resignation_reason".equals(mainDictDetail.getType())
					||"bank_type".equals(mainDictDetail.getType())
					){//考核科目
				mainDictDetail.setBranchOffice(UserUtils.getSelectBranchOffice());
			}else{
				mainDictDetail.setLevel("2");
				mainDictDetail.setBranchOffice(UserUtils.getSelectCompany().getId());
			}
		List<MainDictDetail> list = super.findList(mainDictDetail);
		if(list!=null&&list.size()>0){
			return true;
		}else{
			return false;
		}
		}
		return true;
	}
}