package cn.component.rule;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson2.JSON;
import cn.facade.abs.IFillRuleHandler;
import cn.orm.abs.ISqlToyDao;
import cn.orm.sqltoy.plus.Wrappers;
import cn.orm.sqltoy.plus.query.LambdaQueryWrapper;
import cn.pojo.atv.SysCategory;
import cn.pojo.atv.SysDepart;
import cn.service.orm.sqltoy.SqlToyDaoImpl;
import cn.facade.util.YouBianCodeUtil;
import cn.util.core.IsKit;

/**@Description: 分类字典编码生成规则*/
public class CategoryCodeRule implements IFillRuleHandler {

	private ISqlToyDao daoService = new SqlToyDaoImpl();

		
	public static final String ROOT_PID_VALUE = "0";

	@Override
	public String execute(Map<String,Object> params, String formData) {

		String categoryPid = ROOT_PID_VALUE;
		String categoryCode = null;
		if (IsKit.isNotNullOrEmpty(formData)) { 
			categoryPid = formData;
		} else {
			if (params != null) {
				Object obj = params.get("pid");
				if (IsKit.isNotEmpty(obj)) categoryPid = obj.toString();
			}
		}

		/*
		 * 分成三种情况
		 * 1.数据库无数据 调用YouBianCodeUtil.getNextYouBianCode(null);
		 * 2.添加子节点，无兄弟元素 YouBianCodeUtil.getSubYouBianCode(parentCode,null);
		 * 3.添加子节点有兄弟元素 YouBianCodeUtil.getNextYouBianCode(lastCode);
		 * */
		//找同类 确定上一个最大的code值
		/**LambdaQueryWrapper<SysCategory> query = new LambdaQueryWrapper<SysCategory>()
        .eq(SysCategory::getPid, categoryPid)
        .isNotNull(SysCategory::getCode)
        .orderByDesc(SysCategory::getCode);
        SysCategoryMapper baseMapper = (SysCategoryMapper) SpringContextUtils.getBean("sysCategoryMapper");
        List<SysCategory> list = baseMapper.selectList(query);*/
		LambdaQueryWrapper<SysCategory> lambdaQueryWrapper = Wrappers.lambdaWrapper(SysCategory.class)
				.eq(SysCategory::getPid, categoryPid)
				.isNotNull(SysCategory::getCode)
				.orderByDesc(SysCategory::getCode);
		lambdaQueryWrapper.assemble();

		System.out.println("WW_1:"+lambdaQueryWrapper.getSqlSegment());
		System.out.println("WW_2:"+JSON.toJSONString(lambdaQueryWrapper.getSqlSegmentParamMap()));
		List<SysCategory> list = daoService.findBySql(lambdaQueryWrapper.getSqlSegment(),null,SysCategory.class);

		if (list == null || list.size() == 0) {
			if (ROOT_PID_VALUE.equals(categoryPid)) {
				//情况1
				categoryCode = YouBianCodeUtil.getNextYouBianCode(null);
			} else {
				//情况2
				//SysCategory parent = (SysCategory) baseMapper.selectById(categoryPid);
				List<?> categoryList = new ArrayList<>();
				Map<String, Object> whereMap = new HashMap<>();
				whereMap.put("id",categoryPid);
				categoryList = daoService.getEntityList("SysCategory",whereMap,SysDepart.class);
				SysCategory parent = new SysCategory(); 
				if(IsKit.isNotNullOrEmpty(categoryList)) {
					parent = (SysCategory) categoryList.get(0);
				}

				categoryCode = YouBianCodeUtil.getSubYouBianCode(parent.getCode(), null);
			}
		} else {
			//情况3
			categoryCode = YouBianCodeUtil.getNextYouBianCode(list.get(0).getCode());
		}
		return categoryCode;
	}
}
