package com.zcloud.service.system.impl;

import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.entity.system.Dictionaries;
import com.zcloud.mapper.datasource.system.DictionariesCorpMapper;
import com.zcloud.service.system.DictionariesCorpService;
import com.zcloud.util.Jurisdiction;
import com.zcloud.util.UuidUtil;
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
 * 官网：www.qdkjchina.com
 */
@Service
@Transactional //开启事物
public class DictionariesCorpServiceImpl implements DictionariesCorpService {

	@Autowired
	private DictionariesCorpMapper dictionariesCorpMapper;

	/**
	 * 获取所有数据并填充每条数据的子级列表(递归处理)
	 * @param MENU_ID
	 * @return
	 * @throws Exception
	 */
	public List<Dictionaries> listAllDict(PageData pd) throws Exception {
		List<Dictionaries> dictList = this.listSubDictByParentId(pd);
		for(Dictionaries dict : dictList){
			dict.setTreeurl("dictionaries_list.html?DICTIONARIES_ID="+dict.getDICTIONARIES_ID());
			PageData subPd = new PageData();
			subPd.put("PARENT_ID", dict.getDICTIONARIES_ID());	//企业ID
			subPd.put("CORPINFO_ID", pd.getString("CORPINFO_ID"));	//企业ID
			dict.setSubDict(this.listAllDict(subPd));
			dict.setTarget("treeFrame");
		}
		return dictList;
	}

	/**
	 * 通过ID获取其子级列表
	 * @param parentId
	 * @return
	 * @throws Exception
	 */
	public List<Dictionaries> listSubDictByParentId(PageData pd) throws Exception {
//		System.out.println(dictionariesCorpMapper.listSubDictByParentId(pd));
		return dictionariesCorpMapper.listSubDictByParentId(pd);
	}

	/**
	 * 获取所有数据并填充每条数据的子级列表(递归处理)用于代码生成器引用数据字典
	 * @param MENU_ID
	 * @return
	 * @throws Exception
	 */
	public List<Dictionaries> listAllDictToCreateCode(PageData pd) throws Exception {
		List<Dictionaries> dictList = this.listSubDictByParentId(pd);
		for(Dictionaries dict : dictList){
			dict.setTreeurl("setDID('"+dict.getDICTIONARIES_ID()+"');");
			PageData subPd = new PageData();
			subPd.put("PARENT_ID", dict.getDICTIONARIES_ID());	//企业ID
			subPd.put("CORPINFO_ID", pd.getString("CORPINFO_ID"));	//企业ID
			dict.setSubDict(this.listAllDictToCreateCode(subPd));
			dict.setTarget("treeFrame");
		}
		return dictList;
	}
	/**
	 * 获取所有数据并填充每条数据的子级列表(递归处理)下拉ztree用
	 * @param MENU_ID
	 * @return
	 * @throws Exception
	 */
	public List<PageData> listAllDictToSelect(String parentId, List<PageData> zdicPdList) throws Exception {
		List<PageData>[] arrayDep = this.listAllbyPd(parentId,zdicPdList);
		List<PageData> dicPdList = arrayDep[1];
		for(PageData pd : dicPdList){
			this.listAllDictToSelect(pd.getString("id"),arrayDep[0]);
		}
		return arrayDep[0];
	}
	/**下拉ztree用
	 * @param parentId
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<PageData>[] listAllbyPd(String parentId, List<PageData> zdicPdList) throws Exception {
		PageData subPd = new PageData();
		subPd.put("PARENT_ID", parentId);	//上级ID
		subPd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());	//企业ID
		List<Dictionaries> dicList = this.listSubDictByParentId(subPd);
		List<PageData> dicPdList = new ArrayList<PageData>();
		for(Dictionaries dic : dicList){
			PageData pd = new PageData();
			pd.put("id", dic.getDICTIONARIES_ID());
			pd.put("parentId", dic.getPARENT_ID());
			pd.put("name", dic.getNAME());
			pd.put("icon", "../../../assets/images/user.gif");
			dicPdList.add(pd);
			zdicPdList.add(pd);
		}
		List<PageData>[] arrayDep = new List[2];
		arrayDep[0] = zdicPdList;
		arrayDep[1] = dicPdList;
		return arrayDep;
	}
	/**列表
	 * @param page
	 * @throws Exception
	 */
	public List<PageData> list(Page page)throws Exception{
		return dictionariesCorpMapper.datalistPage(page);
	}

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

	public PageData findByName(String name, String CORPINFO_ID, String PARENT_ID) throws Exception {
		PageData pageData = new PageData();
		pageData.put("NAME", name);
		pageData.put("CORPINFO_ID", CORPINFO_ID);
		pageData.put("PARENT_ID", PARENT_ID);
		return dictionariesCorpMapper.getPdByParIdAndName(pageData);
	}
	/**通过编码获取数据
	 * @param pd
	 * @throws Exception
	 */
	public PageData findByBianma(PageData pd)throws Exception{
		return dictionariesCorpMapper.findByBianma(pd);
	}

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

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

	/**排查表检查是否被占用
	 * @param pd
	 * @throws Exception
	 */
	public PageData findFromTbs(PageData pd)throws Exception{
		return dictionariesCorpMapper.findFromTbs(pd);
	}

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

	/**
	 * 通过IDS获取列表
	 * @param dictionariesIds
	 * @return
	 * @throws Exception
	 */
	public List<Dictionaries> listSubDictByIds(String[] dictionariesIds) throws Exception {
		return dictionariesCorpMapper.listSubDictByIdS(dictionariesIds);
	}

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

	/**新增(增加在线考试使用人员类别)
	 * @param pd
	 * @throws Exception
	 */
	public PageData saveSelf(PageData pd)throws Exception{
		PageData subPd = new PageData();
		PageData lc = new PageData();
		String PARENT_ID = "";
		String BIANMA = "AUTOMATICDICT000";
		/**后续如有新分类自行添加if*/
		if ("APPLICABLE_PERSONNEL".equals(pd.getString("DICTTYPE"))) {
			PARENT_ID = "532282f9c48346b8b646531909c739df";
			lc.put("NAME", pd.getString("LEARNERCATEGORY"));
			BIANMA = "APPLICABLEPERSONNEL000";
		}
		subPd.put("PARENT_ID", PARENT_ID);	//上级ID
		subPd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());	//企业ID
		PageData pageDataSize = dictionariesCorpMapper.getCountByParId(subPd);
		Integer count = Integer.valueOf(pageDataSize.get("zs").toString()) +1 ;
//		List<Dictionaries> list = this.listSubDictByParentId(subPd);
		if (count <10) {
			BIANMA = BIANMA.substring(0,BIANMA.length()-1) + count;
		} else if (count <100) {
			BIANMA = BIANMA.substring(0,BIANMA.length()-2) + count;
		} else {
			BIANMA = BIANMA.substring(0,BIANMA.length()-3) + count;
		}
		lc.put("DICTIONARIES_ID", UuidUtil.get32UUID());
		lc.put("NAME_EN", BIANMA);
		lc.put("BIANMA", BIANMA);
		lc.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());	//企业ID
		lc.put("ORDER_BY", count);
		lc.put("PARENT_ID", PARENT_ID);
		lc.put("YNDEL", "no");
		this.save(lc);
		return lc;
	}
	/**新增(动态字典信息)
	 * @param pd
	 * @throws Exception
	 */
	public PageData saveByZidongxinzeng(PageData pd)throws Exception{
		PageData lc = dictionariesCorpMapper.getPdByParIdAndName(pd);
		if(null == lc){
			PageData pageDataSize = dictionariesCorpMapper.getCountByParId(pd);
			Integer count = Integer.valueOf(pageDataSize.get("zs").toString()) +1 ;
			String BIANMA = pd.getString("BIANMA") + String.format("%04d", count);
			lc = new PageData();
			lc.put("DICTIONARIES_ID", UuidUtil.get32UUID());
			lc.put("NAME", pd.getString("NAME"));
			lc.put("NAME_EN", BIANMA);
			lc.put("BIANMA", BIANMA);
			lc.put("ORDER_BY", count);
			lc.put("PARENT_ID", pd.getString("PARENT_ID"));
			lc.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());	//企业ID
			lc.put("YNDEL", "no");
			this.save(lc);
		}
		return lc;
	}

}
