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

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.activitymenu.vo.ActivityMenuPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.activitymenu.vo.ActivityMenuSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.activitymenu.ActivityMenuDO;
import cn.iocoder.yudao.module.member.dal.mysql.activitymenu.ActivityMenuMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

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

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

/**
 * 活动套餐 Service 实现类
 *
 * @author 我是秦俊旗
 */
@Service
@Validated
public class ActivityMenuServiceImpl implements ActivityMenuService {

    @Resource
    private ActivityMenuMapper activityMenuMapper;

    @Override
    public Long createActivityMenu(ActivityMenuSaveReqVO createReqVO) {
        // 插入
        ActivityMenuDO activityMenu = BeanUtils.toBean(createReqVO, ActivityMenuDO.class);
        activityMenuMapper.insert(activityMenu);
        // 返回
        return activityMenu.getId();
    }

    @Override
    public void updateActivityMenu(ActivityMenuSaveReqVO updateReqVO) {
        // 校验存在
        validateActivityMenuExists(updateReqVO.getId());
        // 更新
        ActivityMenuDO updateObj = BeanUtils.toBean(updateReqVO, ActivityMenuDO.class);
        activityMenuMapper.updateById(updateObj);
    }

    @Override
    public void deleteActivityMenu(Long id) {
        // 校验存在
        validateActivityMenuExists(id);
        // 删除
        activityMenuMapper.deleteById(id);
    }

    private void validateActivityMenuExists(Long id) {
        if (activityMenuMapper.selectById(id) == null) {
            throw exception(ACTIVITY_MENU_NOT_EXISTS);
        }
    }

    @Override
    public ActivityMenuDO getActivityMenu(Long id) {
        return activityMenuMapper.selectById(id);
    }

    @Override
    public PageResult<ActivityMenuDO> getActivityMenuPage(ActivityMenuPageReqVO pageReqVO) {
        return activityMenuMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ActivityMenuDO> getListByIds(List<Long> ids) {
        return  activityMenuMapper.selectList(Wrappers.<ActivityMenuDO>lambdaQuery()
                .in(ActivityMenuDO::getId, ids));
    }

    @Override
    public List<ActivityMenuDO> getListByMerchantId(Long merchantId) {
        return activityMenuMapper.selectList(Wrappers.<ActivityMenuDO>lambdaQuery()
                .eq(ActivityMenuDO::getMerchantId, merchantId));
    }
}