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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonus.canteen.core.menu.dto.MenuDishesTypeAddDTO;
import com.bonus.canteen.core.menu.mapper.MenuDishesTypeMapper;
import com.bonus.canteen.core.menu.service.MenuDishesTypeService;
import com.bonus.canteen.core.menu.domain.MenuDishesType;
import com.bonus.canteen.core.menu.vo.MenuDishesTypeAllVO;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.houqin.constant.DelFlagEnum;
import com.bonus.common.houqin.i18n.I18n;
import com.bonus.common.houqin.utils.id.Id;
import com.bonus.common.security.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author xliu
 * @date 2025/4/8 19:16
 */
@Service
public class MenuDishesTypeServiceImpl  extends ServiceImpl<MenuDishesTypeMapper, MenuDishesType> implements MenuDishesTypeService {

    @Resource
    private MenuDishesTypeMapper menuDishesTypeMapper;

    @Override
    public List<MenuDishesTypeAllVO> getAllMenuDishesTypeList() {
        return menuDishesTypeMapper.getAllMenuDishesTypeList();
    }


    @Override
    public int addMenuDishesType(MenuDishesTypeAddDTO content) {
        List<MenuDishesType> cookList = this.baseMapper.selectList(Wrappers.lambdaQuery(MenuDishesType.class)
                .eq(MenuDishesType::getTypeName, content.getTypeName())
                .eq(MenuDishesType::getDelFlag, DelFlagEnum.DEL_FALSE.key()));
        if (CollUtil.isNotEmpty(cookList)) {
            throw new ServiceException(I18n.getMessage("menu_type_name_repeat", new Object[0]));
        } else {
            String username = SecurityUtils.getUsername();
            MenuDishesType dishesType = new MenuDishesType();
            BeanUtil.copyProperties(content, dishesType, new String[0]);
            dishesType.setCrby(username);
            dishesType.setTypeId(Id.next());
            return this.baseMapper.insert(dishesType);
        }
    }
}
