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

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

import cn.hutool.core.util.ObjectUtil;
import com.bonus.canteen.core.cook.vo.NutritionTypeVO;
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.CookNutritionTypeMapper;
import com.bonus.canteen.core.cook.domain.CookNutritionType;
import com.bonus.canteen.core.cook.service.ICookNutritionTypeService;

/**
 * 食材营养基础类型Service业务层处理
 * 
 * @author xsheng
 * @date 2025-05-25
 */
@Service
public class CookNutritionTypeServiceImpl implements ICookNutritionTypeService {
    @Autowired
    private CookNutritionTypeMapper cookNutritionTypeMapper;

    /**
     * 查询食材营养基础类型
     * 
     * @param nutritionTypeId 食材营养基础类型主键
     * @return 食材营养基础类型
     */
    @Override
    public CookNutritionType selectCookNutritionTypeByNutritionTypeId(Long nutritionTypeId) {
        return cookNutritionTypeMapper.selectCookNutritionTypeByNutritionTypeId(nutritionTypeId);
    }

    /**
     * 查询食材营养基础类型列表
     * 
     * @param cookNutritionType 食材营养基础类型
     * @return 食材营养基础类型
     */
    @Override
    public List<CookNutritionType> selectCookNutritionTypeList(CookNutritionType cookNutritionType) {
        return cookNutritionTypeMapper.selectCookNutritionTypeList(cookNutritionType);
    }

    /**
     * 新增食材营养基础类型
     * 
     * @param cookNutritionType 食材营养基础类型
     * @return 结果
     */
    @Override
    public int insertCookNutritionType(CookNutritionType cookNutritionType) {
        cookNutritionType.setCreateTime(DateUtils.getNowDate());
        if (StringUtils.isBlank(cookNutritionType.getNutritionTypeName())){
            throw new ServiceException("营养类别名称不能为空");
        }
        try {
            if (cookNutritionType.getParentId() == null || cookNutritionType.getParentId() == 0L){
                cookNutritionType.setParentId(0L);
                cookNutritionType.setLevel("0");
            }else{
                cookNutritionType.setLevel("1");
            }
            //判断名称是否重复
            CookNutritionType nutritionType = cookNutritionTypeMapper.selectCookNutritionTypeByNutritionTypeName(cookNutritionType.getNutritionTypeName(),null);
            if (nutritionType != null) {
                throw new ServiceException("营养类别名称重复");
            }
            return cookNutritionTypeMapper.insertCookNutritionType(cookNutritionType);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 修改食材营养基础类型
     * 
     * @param cookNutritionType 食材营养基础类型
     * @return 结果
     */
    @Override
    public int updateCookNutritionType(CookNutritionType cookNutritionType) {
        cookNutritionType.setUpdateTime(DateUtils.getNowDate());
        try {
            if (StringUtils.isNull(cookNutritionType.getNutritionTypeId())){
                throw new ServiceException("未携带食材营养类别ID");
            }
            if (StringUtils.isBlank(cookNutritionType.getNutritionTypeName())){
                throw new ServiceException("营养类别名称不能为空");
            }
            if (cookNutritionType.getParentId() == null || cookNutritionType.getParentId() == 0L){
                cookNutritionType.setParentId(0L);
                cookNutritionType.setLevel("0");
            }else{
                cookNutritionType.setLevel("1");
            }
            //判断名称是否重复
            CookNutritionType nutritionType = cookNutritionTypeMapper.selectCookNutritionTypeByNutritionTypeName(cookNutritionType.getNutritionTypeName(),cookNutritionType.getNutritionTypeId());
            if (nutritionType != null) {
                throw new ServiceException("营养类别名称重复");
            }
            return cookNutritionTypeMapper.updateCookNutritionType(cookNutritionType);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 批量删除食材营养基础类型
     * 
     * @param nutritionTypeIds 需要删除的食材营养基础类型主键
     * @return 结果
     */
    @Override
    public int deleteCookNutritionTypeByNutritionTypeIds(Long[] nutritionTypeIds) {
        // 检查是否有食材使用该营养类型子集
        int code = cookNutritionTypeMapper.checkIsExistChildren(nutritionTypeIds);
        if (code > 0) {
            throw new ServiceException("删除失败，营养类型下存在子集");
        }
        return cookNutritionTypeMapper.deleteCookNutritionTypeByNutritionTypeIds(nutritionTypeIds);
    }

    /**
     * 获取所有食材类别
     *
     * @return 食材类别列表
     */
    @Override
    public List<NutritionTypeVO> getNutritionTypeList() {
        List<NutritionTypeVO> bigTypeList =cookNutritionTypeMapper.selectBigTypeList();
        List<NutritionTypeVO> nutritionTypeVOList = new ArrayList<>();
        for(NutritionTypeVO bean : bigTypeList){
            NutritionTypeVO nutritionTypeVO = new NutritionTypeVO();
            List<NutritionTypeVO> littleTypeList = cookNutritionTypeMapper.selectLittleTypeList(bean.getId()+"");
            nutritionTypeVO.setId(Integer.parseInt(bean.getId()+""));
            nutritionTypeVO.setName(bean.getName());
            nutritionTypeVO.setLittleTypeList(littleTypeList);
            nutritionTypeVOList.add(nutritionTypeVO);
        }

        return nutritionTypeVOList;
    }
}
