package cn.iocoder.yudao.module.member.service.activityrestaurant;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.member.controller.admin.activityrestaurant.vo.ActivityRestaurantPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.activityrestaurant.vo.ActivityRestaurantSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.activityrestaurant.ActivityRestaurantDO;
import cn.iocoder.yudao.module.member.dal.mysql.activityrestaurant.ActivityRestaurantMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.ACTIVITY_RESTAURANT_NOT_EXISTS;

/**
 * 活动用餐 Service 实现类
 *
 * @author 我是秦俊旗
 */
@Service
@Validated
public class ActivityRestaurantServiceImpl implements ActivityRestaurantService {

    @Resource
    private ActivityRestaurantMapper activityRestaurantMapper;

    @Override
    public Long createActivityRestaurant(ActivityRestaurantSaveReqVO createReqVO) {
        // 插入
        ActivityRestaurantDO activityRestaurant = BeanUtils.toBean(createReqVO, ActivityRestaurantDO.class);
        activityRestaurantMapper.insert(activityRestaurant);
        // 返回
        return activityRestaurant.getId();
    }

    @Override
    public void updateActivityRestaurant(ActivityRestaurantSaveReqVO updateReqVO) {
        // 校验存在
        validateActivityRestaurantExists(updateReqVO.getId());
        // 更新
        ActivityRestaurantDO updateObj = BeanUtils.toBean(updateReqVO, ActivityRestaurantDO.class);
        activityRestaurantMapper.updateById(updateObj);
    }

    @Override
    public void deleteActivityRestaurant(Long id) {
        // 校验存在
        validateActivityRestaurantExists(id);
        // 删除
        activityRestaurantMapper.deleteById(id);
    }

    private void validateActivityRestaurantExists(Long id) {
        if (activityRestaurantMapper.selectById(id) == null) {
            throw exception(ACTIVITY_RESTAURANT_NOT_EXISTS);
        }
    }

    @Override
    public ActivityRestaurantDO getActivityRestaurant(Long id) {
        return activityRestaurantMapper.selectById(id);
    }

    @Override
    public PageResult<ActivityRestaurantDO> getActivityRestaurantPage(ActivityRestaurantPageReqVO pageReqVO) {
        return activityRestaurantMapper.selectPage(pageReqVO);
    }

    @Override
    public void deleteByActivityId(Long activityId) {
        activityRestaurantMapper.delete(Wrappers.lambdaQuery(ActivityRestaurantDO.class).eq(ActivityRestaurantDO::getActivityId,activityId));
    }

    @Override
    public ActivityRestaurantDO getByActivityId(Long activityId) {
       return activityRestaurantMapper.selectOne(Wrappers.lambdaQuery(ActivityRestaurantDO.class).eq(ActivityRestaurantDO::getActivityId,activityId));
    }
}