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

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.activityvoucher.vo.ActivityVoucherPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.activityvoucher.vo.ActivityVoucherSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.activityvoucher.ActivityVoucherDO;
import cn.iocoder.yudao.module.member.dal.mysql.activityvoucher.ActivityVoucherMapper;
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_VOUCHER_NOT_EXISTS;

/**
 * 活动代金券 Service 实现类
 *
 * @author 我是秦俊旗
 */
@Service
@Validated
public class ActivityVoucherServiceImpl implements ActivityVoucherService {

    @Resource
    private ActivityVoucherMapper activityVoucherMapper;

    @Override
    public Long createActivityVoucher(ActivityVoucherSaveReqVO createReqVO) {
        // 插入
        ActivityVoucherDO activityVoucher = BeanUtils.toBean(createReqVO, ActivityVoucherDO.class);
        activityVoucherMapper.insert(activityVoucher);
        // 返回
        return activityVoucher.getId();
    }

    @Override
    public void updateActivityVoucher(ActivityVoucherSaveReqVO updateReqVO) {
        // 校验存在
        validateActivityVoucherExists(updateReqVO.getId());
        // 更新
        ActivityVoucherDO updateObj = BeanUtils.toBean(updateReqVO, ActivityVoucherDO.class);
        activityVoucherMapper.updateById(updateObj);
    }

    @Override
    public void deleteActivityVoucher(Long id) {
        // 校验存在
        validateActivityVoucherExists(id);
        // 删除
        activityVoucherMapper.deleteById(id);
    }

    private void validateActivityVoucherExists(Long id) {
        if (activityVoucherMapper.selectById(id) == null) {
            throw exception(ACTIVITY_VOUCHER_NOT_EXISTS);
        }
    }

    @Override
    public ActivityVoucherDO getActivityVoucher(Long id) {
        return activityVoucherMapper.selectById(id);
    }

    @Override
    public PageResult<ActivityVoucherDO> getActivityVoucherPage(ActivityVoucherPageReqVO pageReqVO) {
        return activityVoucherMapper.selectPage(pageReqVO);
    }

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