package cn.iocoder.yudao.module.system.service.restaurant;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.restaurant.vo.DishesPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.restaurant.DishesDO;
import cn.iocoder.yudao.module.system.dal.mysql.restaurant.DishesMapper;
import cn.iocoder.yudao.module.system.enums.ErrorCodeConstants;
import com.mzt.logapi.context.LogRecordContext;
import com.mzt.logapi.starter.annotation.LogRecord;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.Date;
import java.util.List;

import static cn.iocoder.yudao.module.system.enums.LogRecordConstants.*;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;


/**
 * 餐厅菜品 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class DishesServiceImpl implements DishesService {

    @Resource
    private DishesMapper dishesMapper;

    @Override
    @LogRecord(type = DISHES_TYPE, subType = DISHES_CREATE_SUB_TYPE, bizNo = "{{#id}}",
            success = DISHES_CREATE_SUCCESS)
    public void createDishes(DishesDO createReqVO) {
        createReqVO.setCreatedBy(SecurityFrameworkUtils.getLoginUserNickname());
        createReqVO.setCreatedTime(new Date());
        List<DishesDO> dishesDOS = dishesMapper.selectList(new LambdaQueryWrapperX<DishesDO>()
                .eq(DishesDO::getTenantId, TenantContextHolder.getTenantId())
                .eq(DishesDO::getRestaurantId, createReqVO.getRestaurantId())
                .eq(DishesDO::getCategoryId,createReqVO.getCategoryId())
                .eq(DishesDO::getFoodName, createReqVO.getFoodName()));
        if(CollectionUtils.isNotEmpty(dishesDOS)){
            throw exception(ErrorCodeConstants.DISHES_EXISTS);
        }
        dishesMapper.insert(createReqVO);
        LogRecordContext.putVariable("name", createReqVO.getFoodName());
        LogRecordContext.putVariable("id", createReqVO.getId());
    }

    @Override
    @LogRecord(type = DISHES_TYPE, subType = DISHES_UPDATE_SUB_TYPE, bizNo = "{{#id}}",
            success = DISHES_UPDATE_SUCCESS)
    public void updateDishes(DishesDO updateReqVO) {
        dishesMapper.updateById(updateReqVO);

        LogRecordContext.putVariable("name",updateReqVO.getFoodName());
        LogRecordContext.putVariable("id",updateReqVO.getId());
    }

    @Override
    public void deleteDishes(Long[] ids) {
        for (Long id : ids) {
            dishesMapper.deleteById(id);
        }
    }

    @Override
    public DishesDO getDishes(Long id) {
        return dishesMapper.selectById(id);
    }

    @Override
    public PageResult<DishesDO> getDishesPage(DishesPageReqVO pageReqVO) {
        return dishesMapper.selectPage(pageReqVO);
    }

    @Override
    public List<DishesDO> getDishesList(String foodName, Long restaurantId) {
        return dishesMapper.selectList(new LambdaQueryWrapperX<DishesDO>()
                .eqIfPresent(DishesDO::getRestaurantId,restaurantId)
                .likeIfPresent(DishesDO::getFoodName,foodName));
    }
}