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

import java.util.Collections;
import java.util.List;

import com.bonus.canteen.core.cook.vo.CookDishesTypeVO;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bonus.canteen.core.cook.mapper.CookDishesTypeMapper;
import com.bonus.canteen.core.cook.domain.CookDishesType;
import com.bonus.canteen.core.cook.service.ICookDishesTypeService;

/**
 * 菜品类型信息Service业务层处理
 * 
 * @author xsheng
 * @date 2025-05-25
 */
@Service
public class CookDishesTypeServiceImpl implements ICookDishesTypeService {
    @Autowired
    private CookDishesTypeMapper cookDishesTypeMapper;

    /**
     * 查询菜品类型信息
     * 
     * @param dishesTypeId 菜品类型信息主键
     * @return 菜品类型信息
     */
    @Override
    public CookDishesType selectCookDishesTypeByDishesTypeId(Long dishesTypeId) {
        return cookDishesTypeMapper.selectCookDishesTypeByDishesTypeId(dishesTypeId);
    }

    /**
     * 查询菜品类型信息列表
     * 
     * @param cookDishesType 菜品类型信息
     * @return 菜品类型信息
     */
    @Override
    public List<CookDishesTypeVO> selectCookDishesTypeList(CookDishesType cookDishesType) {
        return cookDishesTypeMapper.selectCookDishesTypeList(cookDishesType);
    }

    /**
     * 新增菜品类型信息
     * 
     * @param cookDishesType 菜品类型信息
     * @return 结果
     */
    @Override
    public int insertCookDishesType(CookDishesType cookDishesType) {
        cookDishesType.setCreateTime(DateUtils.getNowDate());
        try {
            checkParam(cookDishesType, false);
            return cookDishesTypeMapper.insertCookDishesType(cookDishesType);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 修改菜品类型信息
     * 
     * @param cookDishesType 菜品类型信息
     * @return 结果
     */
    @Override
    public int updateCookDishesType(CookDishesType cookDishesType) {
        cookDishesType.setUpdateTime(DateUtils.getNowDate());
        try {
            checkParam(cookDishesType, true);
            return cookDishesTypeMapper.updateCookDishesType(cookDishesType);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    public void checkParam(CookDishesType cookDishesType, boolean isUpdate) {
        if (isUpdate) {
            if (cookDishesType.getDishesTypeId() == null) {
                throw new ServiceException("菜品分类ID不能为空");
            }
        }
        if (cookDishesType.getDishesTypeName() == null || cookDishesType.getDishesTypeName().isEmpty()) {
            throw new ServiceException("菜品分类名称不能为空");
        }
        if (cookDishesTypeMapper.checkIsExistByName(cookDishesType) > 0){
            throw new ServiceException("该菜品分类名称已存在");
        }
        if(cookDishesType.getParentId() == null || cookDishesType.getParentId() == 0) {
            cookDishesType.setLevel(0L);
        }else {
            if(cookDishesType.getLevel() == null){
                cookDishesType.setLevel(1L);
            }
        }
    }

    /**
     * 批量删除菜品类型信息
     * 
     * @param dishesTypeIds 需要删除的菜品类型信息主键
     * @return 结果
     */
    @Override
    public int deleteCookDishesTypeByDishesTypeIds(Long[] dishesTypeIds) {
        //  判断该菜品类型信息是否被使用
        if (cookDishesTypeMapper.checkIsUse(dishesTypeIds) > 0){
            throw new ServiceException("该菜品类型信息被使用，不允许删除！");
        }
        return cookDishesTypeMapper.deleteCookDishesTypeByDishesTypeIds(dishesTypeIds);
    }

    /**
     * 删除菜品类型信息信息
     * 
     * @param dishesTypeId 菜品类型信息主键
     * @return 结果
     */
    @Override
    public int deleteCookDishesTypeByDishesTypeId(Long dishesTypeId) {
        return cookDishesTypeMapper.deleteCookDishesTypeByDishesTypeId(dishesTypeId);
    }

    @Override
    public List<CookDishesTypeVO> selectList(CookDishesType cookDishesType) {
        return cookDishesTypeMapper.selectList(cookDishesType);
    }
}
