package cn.pluss.platform.dict.impl;

import cn.jiguang.common.utils.StringUtils;
import cn.pluss.platform.dict.DictService;
import cn.pluss.platform.entity.Dict;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.mapper.DictMapper;
import cn.pluss.platform.util.tree.TreeUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {

    @Override
    public boolean saveOrUpdate(Dict entity) {
        checkInsert(entity);
        checkUpdate(entity);

        return super.saveOrUpdate(entity);
    }

    /** 插入前检查 */
    private void checkInsert(Dict entity) {
        if (StringUtils.isEmpty(entity.getDesc())) {
            throw new MsgException("请输入字典描述信息");
        }

        if (!StringUtils.isEmpty(entity.getId())) {
            return;
        }

        if (StringUtils.isEmpty(entity.getPId())) {
            if (StringUtils.isEmpty(entity.getCode())) {
                throw new MsgException("字典编码不能为空!");
            }

            // 新添加字典组
            Dict queryEty = new Dict().setCode(entity.getCode());
            QueryWrapper<Dict> queryWrapper = new QueryWrapper<>(queryEty);
            queryEty = getOne(queryWrapper);

            if (queryEty != null) {
                throw new MsgException("已存在相同编码的字典组!");
            }
        } else {
            // 新添加字典项
            if (StringUtils.isEmpty(entity.getValue())) {
                throw new MsgException("字典值不能为空!");
            }

            Dict queryEty = new Dict().setCode(entity.getValue());
            queryEty.setPId(entity.getPId());
            QueryWrapper<Dict> queryWrapper = new QueryWrapper<>(queryEty);
            queryEty = getOne(queryWrapper);

            if (queryEty != null) {
                throw new MsgException("同字典组中已存在相同子项值!");
            }
        }
    }

    /** 更新前检查 */
    private void checkUpdate(Dict entity) {
        if (StringUtils.isEmpty(entity.getDesc())) {
            throw new MsgException("请输入字典描述信息");
        }

        if (StringUtils.isEmpty(entity.getId())) {
            return;
        }

        if (StringUtils.isEmpty(entity.getPId())) {
            if (StringUtils.isEmpty(entity.getCode())) {
                throw new MsgException("字典编码不能为空!");
            }

            // 修改时若修改code发现相同的code已经存在
            Dict queryEty = new Dict().setCode(entity.getCode());
            QueryWrapper<Dict> queryWrapper = new QueryWrapper<>(queryEty);
            queryEty = getOne(queryWrapper);

            if (queryEty != null && !queryEty.getId().equals(entity.getId())) {
                throw new MsgException("已存在相同编码的字典组!");
            }
        } else {
            Dict queryEty = new Dict().setCode(entity.getValue());
            queryEty.setPId(entity.getPId());
            QueryWrapper<Dict> queryWrapper = new QueryWrapper<>(queryEty);
            queryEty = getOne(queryWrapper);

            if (queryEty != null && !queryEty.getId().equals(entity.getId())) {
                throw new MsgException("该字典组中已存在相同子项值!");
            }
        }
    }
}
