package com.zcloud.service.corp.impl;

import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.mapper.datasource.corp.CorpTypeMapper;
import com.zcloud.service.corp.CorpTypeService;
import com.zcloud.util.Tools;
import net.sf.json.JSONArray;
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;

/**
 * 说明：企业类型
 * 作者：shaojie
 * 时间：2021-03-18
 * 官网：www.qdkjchina.com
 */
@Service
@Transactional //开启事物
public class CorpTypeServiceImpl implements CorpTypeService{

	@Autowired
	private CorpTypeMapper corptypeMapper;

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

	/**列表
	 * @param page
	 * @throws Exception
	 */
//	public List<PageData> corptypelist(Page page)throws Exception{
//		return corptypeareaMapper.datalistPage(page);
//	}
	public PageData corptypelist(PageData pd) throws Exception {
//		Map<String, String> map = new HashMap<String, String>();
		PageData cpd = new PageData();
		String errInfo = "success";
		List<PageData> varOList = corptypeMapper.corptypelist(pd);
		List<PageData> vList = corptypeMapper.nextcorptypelist(pd);
		for (PageData type : varOList) {
			type.put("NAME",type.getString("NAME"));
			List<PageData> types = corpType(vList, type.getString("CORPTYPE_ID"));
			if(type.size() !=0){
				type.put("children",types);
			}
		}
		JSONArray arr = JSONArray.fromObject(varOList);
		String json = arr.toString().replaceAll("CORPTYPE_ID","id");
		cpd.put("zTreeNodes", json);
		cpd.put("result", errInfo);
		return cpd;
	}
	public List<PageData> corpType(List<PageData> typeList, String pid) {
		List<PageData> result = new ArrayList<PageData>();
		for (PageData type : typeList) {
			if("0".equals(type.getString("CORPTYPE_ID"))) {
				continue;
			}
			type.put("NAME",type.getString("NAME"));
			// 获取菜单的id
			String corpTypeid = type.getString("CORPTYPE_ID");
			// 获取菜单的父id
			String parentid = type.getString("PARENT_ID");
			if(Tools.isEmpty(pid)) {
				continue;
			}
			if (parentid.equals(pid)) {
				// 递归查询当前子菜单的子菜单
				List<PageData> corpTypeList = corpType(typeList,corpTypeid);
				if(corpTypeList.size() !=0){
					type.put("children",corpTypeList);
				}
				result.add(type);
			}
		}
		return result;
	}

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

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

	/**列表
	 * @param page
	 * @throws Exception
	 */
	public List<PageData> list(Page page)throws Exception{
		return corptypeMapper.datalistPage(page);
	}

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

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

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

	/**通过父id获取数据
	 * @param pd
	 * @throws Exception
	 */
	public List<PageData> listAllByParentId(PageData pd)throws Exception{
		return corptypeMapper.listAllByParentId(pd);
	}

    public List<PageData> listMentDictionByPId(String PARENT_ID) throws  Exception{
        return corptypeMapper.listMentDictionByPId(PARENT_ID);
    }
    /**
     * 根据企业id获取对应的企业类型
     * @param pd
     * @return
     * @throws Exception
     */
    public List<PageData> listByCorpInfoTypeIds(PageData pd)throws  Exception{
        return corptypeMapper.listByCorpInfoTypeIds(pd);
    }
	/**列表(全部子集)
	 * @param pd
	 * @throws Exception
	 */
	@Override
	public List<PageData> listAllChild(PageData pd)throws Exception{
		return corptypeMapper.listAllChild(pd);
	}

}

