package com.bonus.canteen.core.cook.service.impl;

import java.util.List;

import com.bonus.canteen.core.common.utils.TreeNodeUtil;
import com.bonus.canteen.core.cook.vo.CookMaterialTypeTreeVO;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.DateUtils;
import com.bonus.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bonus.canteen.core.cook.mapper.CookMaterialTypeMapper;
import com.bonus.canteen.core.cook.domain.CookMaterialType;
import com.bonus.canteen.core.cook.service.ICookMaterialTypeService;

/**
 * 原料类别Service业务层处理
 *
 * @author xsheng
 * @date 2025-05-25
 */
@Service
public class CookMaterialTypeServiceImpl implements ICookMaterialTypeService {
    @Autowired
    private CookMaterialTypeMapper cookMaterialTypeMapper;

    /**
     * 查询原料类别
     *
     * @param materialTypeId 原料类别主键
     * @return 原料类别
     */
    @Override
    public CookMaterialType selectCookMaterialTypeByMaterialTypeId(Long materialTypeId) {
        return cookMaterialTypeMapper.selectCookMaterialTypeByMaterialTypeId(materialTypeId);
    }

    /**
     * 查询原料类别列表
     *
     * @param cookMaterialType 原料类别
     * @return 原料类别
     */
    @Override
    public List<CookMaterialType> selectCookMaterialTypeList(CookMaterialType cookMaterialType) {
        return cookMaterialTypeMapper.selectCookMaterialTypeList(cookMaterialType);
    }

    /**
     * 新增原料类别
     *
     * @param cookMaterialType 原料类别
     * @return 结果
     */
    @Override
    public int insertCookMaterialType(CookMaterialType cookMaterialType) {
        cookMaterialType.setCreateTime(DateUtils.getNowDate());
        try {
            if (StringUtils.isBlank(cookMaterialType.getMaterialTypeName())){
                throw new ServiceException("请输入原料类别名称");
            }
            //判断是否有同名的原料类别
            CookMaterialType checkMaterialType = cookMaterialTypeMapper.selectCookMaterialTypeByMaterialTypeName(cookMaterialType.getMaterialTypeName(), null);
            if (checkMaterialType != null) {
                throw new ServiceException("已存在同名的原料类别");
            }
            if (cookMaterialType.getParentId() == null) {
                cookMaterialType.setParentId(0L);
                cookMaterialType.setLevel(0L);
            }else if(cookMaterialType.getParentId() == 0L){
                cookMaterialType.setLevel(1L);
            } else{
                cookMaterialType.setLevel(2L);
            }
            return cookMaterialTypeMapper.insertCookMaterialType(cookMaterialType);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 修改原料类别
     *
     * @param cookMaterialType 原料类别
     * @return 结果
     */
    @Override
    public int updateCookMaterialType(CookMaterialType cookMaterialType) {
        cookMaterialType.setUpdateTime(DateUtils.getNowDate());
        try {
            if (StringUtils.isNull(cookMaterialType.getMaterialTypeId())){
                throw new ServiceException("未携带原料类别ID");
            }
            if (StringUtils.isBlank(cookMaterialType.getMaterialTypeName())){
                throw new ServiceException("请输入原料类别名称");
            }
            //判断是否有同名的原料类别
            CookMaterialType checkMaterialType = cookMaterialTypeMapper.selectCookMaterialTypeByMaterialTypeName(cookMaterialType.getMaterialTypeName(), cookMaterialType.getMaterialTypeId());
            if (checkMaterialType != null) {
                throw new ServiceException("已存在同名的原料类别");
            }
            if (cookMaterialType.getParentId() == null) {
                cookMaterialType.setParentId(0L);
                cookMaterialType.setLevel(0L);
            }else if(cookMaterialType.getParentId() == 0L){
                cookMaterialType.setLevel(1L);
            } else{
                cookMaterialType.setLevel(2L);
            }
            return cookMaterialTypeMapper.updateCookMaterialType(cookMaterialType);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 批量删除原料类别
     *
     * @param materialTypeIds 需要删除的原料类别主键
     * @return 结果
     */
    @Override
    public int deleteCookMaterialTypeByMaterialTypeIds(Long[] materialTypeIds) {
        //判断是否有子级
        int code = cookMaterialTypeMapper.selectCookMaterialTypeByParentId(materialTypeIds);
        if (code > 0) {
            throw new ServiceException("存在子级营养，不允许删除！");
        }
        return cookMaterialTypeMapper.deleteCookMaterialTypeByMaterialTypeIds(materialTypeIds);
    }

    /**
     * 获取所有原料类别树形结构
     *
     * @return 原料类别树形结构
     */
    @Override
    public List<CookMaterialTypeTreeVO> getTree() {
        List<CookMaterialTypeTreeVO> treeList = cookMaterialTypeMapper.getTree();
        return TreeNodeUtil.assembleTree(treeList);
    }
}
