package com.fjhx.item.service.product.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.common.constant.SourceConstant;
import com.fjhx.item.entity.product.dto.ProductClassifyDto;
import com.fjhx.item.entity.product.po.ProductClassify;
import com.fjhx.item.mapper.product.ProductClassifyMapper;
import com.fjhx.item.service.product.ProductClassifyService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.TreeUtil;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


/**
 * <p>
 * 产品分类 服务实现类
 * </p>
 *
 * @author zlj
 * @since 2023-03-15
 */
@DS(SourceConstant.ITEM)
@Service
public class ProductClassifyServiceImpl extends ServiceImpl<ProductClassifyMapper, ProductClassify> implements ProductClassifyService {

    @Autowired
    private ISysUserService iSysUserService;

    @Override
    public List<JSONObject> tree(ProductClassify productClassify) {
        // 分类名称
        String name = productClassify.getName();

        List<ProductClassify> list = lambdaQuery()
                .select(ProductClassify::getId, ProductClassify::getName, ProductClassify::getParentId)
                .eq(ProductClassify::getDefinition, productClassify.getDefinition())
                .like(ObjectUtil.isNotEmpty(name), ProductClassify::getName, name)
                .list();

        // 构建树形
        return TreeUtil.buildTree("name", list);
    }

    @Override
    public void add(ProductClassifyDto productClassifyDto) {

        Long parentId = productClassifyDto.getParentId();
        if (parentId == null) {
            productClassifyDto.setParentId(0L);
            productClassifyDto.setParentIdSet(null);
        } else if (parentId == 0L) {
            productClassifyDto.setParentIdSet(null);
        } else {
            // 查询父级分类
            ProductClassify parentClassify = getById(parentId);
            if (parentClassify == null) {
                throw new ServiceException("没有找到父级分类");
            }

            // 赋值父级id集合
            String parentIdSet = parentClassify.getParentIdSet();
            productClassifyDto.setParentIdSet((ObjectUtil.isEmpty(parentIdSet) ? "" : parentIdSet + ",") + parentId);
        }

        peerNameOnly(productClassifyDto.getDefinition(), productClassifyDto.getParentId(), productClassifyDto.getName(), null);

        this.save(productClassifyDto);
    }

    @Override
    public void edit(ProductClassifyDto productClassifyDto) {
        peerNameOnly(productClassifyDto.getDefinition(), productClassifyDto.getParentId(), productClassifyDto.getName(), productClassifyDto.getId());
        this.updateById(productClassifyDto);
    }

    @Override
    public void delete(Long id) {
        this.removeById(id);
    }

    @Override
    public List<Long> getChildIdList(long classifyId) {
        List<Long> idList = new ArrayList<>();
        idList.add(classifyId);

        recursionSetChildId(idList, list(), classifyId);
        return idList;
    }

    /**
     * 递归赋值子集id
     */
    private void recursionSetChildId(List<Long> idList, List<ProductClassify> list, Long id) {
        for (ProductClassify productClassify : list) {
            if (Objects.equals(id, productClassify.getParentId())) {
                idList.add(productClassify.getId());
                recursionSetChildId(idList, list, productClassify.getId());
            }
        }
    }

    /**
     * 保证同级名称不重复
     */
    private void peerNameOnly(Integer type, Long parentId, String classifyName, Long excludeId) {

        Long count = lambdaQuery()
                .eq(ProductClassify::getParentId, parentId)
                .eq(ProductClassify::getName, classifyName)
                .eq(ProductClassify::getDefinition, type)
                .ne(ObjectUtil.isNotEmpty(excludeId), ProductClassify::getId, excludeId)
                .count();

        if (count != 0) {
            throw new ServiceException("存在相同分类名称");
        }

    }

}
