package com.fjhx.service.classify.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.base.BaseEntity;
import com.fjhx.entity.classify.Classify;
import com.fjhx.enums.classify.ClassifyTypeEnum;
import com.fjhx.mapper.classify.ClassifyMapper;
import com.fjhx.params.classify.ClassifyVo;
import com.fjhx.service.classify.ClassifyService;
import com.fjhx.utils.Assert;
import com.fjhx.utils.TreeUtil;
import com.fjhx.utils.wrapperUtil.IWrapper;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 分类 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-11-24
 */
@Service
public class ClassifyServiceImpl extends ServiceImpl<ClassifyMapper, Classify> implements ClassifyService {

    @Override
    public Page<Classify> getPage(Map<String, Object> condition) {
        IWrapper<Classify> wrapper = IWrapper.getWrapper(condition);
        return page(condition, wrapper);
    }

    @Override
    public void add(ClassifyVo classifyVo) {
        if (Func.isEmpty(classifyVo.getParentId())) {
            classifyVo.setParentId(0L);
            classifyVo.setParentIdSet("0");
        }

        long count = count(q -> q.eq(Classify::getName, classifyVo.getName()).eq(Classify::getType, classifyVo.getType()));
        Assert.eqZero(count, "分类名称已存在");

        save(classifyVo);
    }

    @Override
    public void edit(ClassifyVo classifyVo) {
        if (Func.isEmpty(classifyVo.getParentId())) {
            classifyVo.setParentId(0L);
            classifyVo.setParentIdSet("0");
        }

        long count = count(q -> q.ne(BaseEntity::getId, classifyVo.getId())
                .eq(Classify::getName, classifyVo.getName())
                .eq(Classify::getType, classifyVo.getType()));
        Assert.eqZero(count, "分类名称已存在");

        updateById(classifyVo);
    }

    @Override
    public void delete(ClassifyVo classifyVo) {
        //查询是否存在子级
        List<Classify> list = lambdaQuery().apply("FIND_IN_SET(" + classifyVo.getId() + ", parent_id_set)").list();
        if (Func.isNotEmpty(list)) {
            throw new ServiceException("存在子级，无法删除！");
        } else {
            if (classifyVo.getType() == ClassifyTypeEnum.CLASSIFY_TYPE_2.getKey()) {
                // //查询是否被物料挂钩
                // List<ProductInfo> materials = productInfoService.lambdaQuery().eq(ProductInfo::getClassifyId, classifyVo.getId()).list();
                // if (Func.isNotEmpty(materials)) {
                //     throw new ServiceException("有物料绑定，无法删除！");
                // }
            } else {
                //查询是否被产品挂钩
            }
        }
        removeById(classifyVo.getId());
    }

    @Override
    public List<JSONObject> tree(ClassifyVo entity) {
        if (Func.isEmpty(entity.getType())) {
            return new ArrayList<>();
        }

        //查询数据
        List<Classify> classifies = lambdaQuery().eq(Classify::getType, entity.getType()).apply(Func.isNotEmpty(entity.getName()), "instr(`name`, '" + entity.getName() + "') > 0").orderByAsc(Classify::getId).list();
        if (Func.isEmpty(classifies)) {
            return new ArrayList<>();
        }

        return TreeUtil.buildTreeObj(classifies);
    }
}
