package org.jeecg.modules.system.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.system.entity.SysCategory;
import org.jeecg.modules.system.mapper.SysCategoryMapper;
import org.jeecg.modules.system.model.TreeSelectModel;
import org.jeecg.modules.system.service.ISysCategoryService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: 分类字典
 * @Author: jeecg-boot
 * @Date:   2019-05-29
 * @Version: V1.0
 */
@Service
public class SysCategoryServiceImpl extends ServiceImpl<SysCategoryMapper, SysCategory> implements ISysCategoryService {

	@Override
	public void addSysCategory(SysCategory sysCategory) {
		if(oConvertUtils.isEmpty(sysCategory.getPid())){
			sysCategory.setPid(ISysCategoryService.ROOT_PID_VALUE);
		}else{
			//如果当前节点父ID不为空 则设置父节点的hasChild 为1
			SysCategory parent = baseMapper.selectById(sysCategory.getPid());
			if(parent!=null && !"1".equals(parent.getHasChild())){
				parent.setHasChild("1");
				baseMapper.updateById(parent);
			}
		}
		baseMapper.insert(sysCategory);
	}

	@Override
	public void updateSysCategory(SysCategory sysCategory) {
		if(oConvertUtils.isEmpty(sysCategory.getPid())){
			sysCategory.setPid(ISysCategoryService.ROOT_PID_VALUE);
		}else{
			//如果当前节点父ID不为空 则设置父节点的hasChild 为1
			SysCategory parent = baseMapper.selectById(sysCategory.getPid());
			if(parent!=null && !"1".equals(parent.getHasChild())){
				parent.setHasChild("1");
				baseMapper.updateById(parent);
			}
		}
		baseMapper.updateById(sysCategory);
	}

    @Override
    public List<SysCategory> getListByPid(String pid) {
        QueryWrapper<SysCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("pid", pid).orderByDesc("create_time");
        Page<SysCategory> page = new Page<>(1, 1000);
        IPage<SysCategory> pageList = this.page(page, wrapper);
        return pageList.getRecords();
    }

    @Override
    public JSONArray getByteDanceIndustryList() {
        JSONArray result = new JSONArray();
        String pid = "6c6b0ad4f49b65f3943528e8a35d74fc";
        List<SysCategory> categories = this.getListByPid(pid);
        for (SysCategory category : categories) {
            JSONObject object = new JSONObject();
            object.put("value", category.getCode());
            object.put("label", category.getName());
            JSONArray childListArray = new JSONArray();
            List<SysCategory> childList = this.getListByPid(category.getId());
            for (SysCategory child : childList) {
                JSONObject childObject = new JSONObject();
                childObject.put("value", child.getCode());
                childObject.put("label", child.getName());
                JSONArray secendChildListArray = new JSONArray();
                List<SysCategory> secendChildList = this.getListByPid(child.getId());
                for (SysCategory secendChild : secendChildList) {
                    JSONObject secendChildObject = new JSONObject();
                    secendChildObject.put("value", secendChild.getCode());
                    secendChildObject.put("label", secendChild.getName());
                    secendChildListArray.add(secendChildObject);
                }
                childObject.put("children", secendChildListArray);
                childListArray.add(childObject);
            }
            object.put("children", childListArray);
            result.add(object);
        }
        return result;
    }

	@Override
	public List<TreeSelectModel> queryListByCode(String pcode) throws JeecgBootException{
		String pid = ROOT_PID_VALUE;
		if(oConvertUtils.isNotEmpty(pcode)) {
			List<SysCategory> list = baseMapper.selectList(new LambdaQueryWrapper<SysCategory>().eq(SysCategory::getCode, pcode));
			if(list==null || list.size() ==0) {
				throw new JeecgBootException("该编码【"+pcode+"】不存在，请核实!");
			}
			if(list.size()>1) {
				throw new JeecgBootException("该编码【"+pcode+"】存在多个，请核实!");
			}
			pid = list.get(0).getId();
		}
		return baseMapper.queryListByPid(pid);
	}

	@Override
	public List<TreeSelectModel> queryListByPid(String pid) {
		if(oConvertUtils.isEmpty(pid)) {
			pid = ROOT_PID_VALUE;
		}
		return baseMapper.queryListByPid(pid);
	}

}
