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

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.activityawards.vo.ActivityAwardsPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.activityawards.vo.ActivityAwardsSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.activityawards.ActivityAwardsDO;
import cn.iocoder.yudao.module.member.dal.mysql.activityawards.ActivityAwardsMapper;
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.Collections;
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_AWARDS_NOT_EXISTS;

/**
 * 活动奖项 Service 实现类
 *
 * @author 我是秦俊旗
 */
@Service
@Validated
public class ActivityAwardsServiceImpl implements ActivityAwardsService {

    @Resource
    private ActivityAwardsMapper activityAwardsMapper;

    @Override
    public Long createActivityAwards(ActivityAwardsSaveReqVO createReqVO) {
        // 插入
        ActivityAwardsDO activityAwards = BeanUtils.toBean(createReqVO, ActivityAwardsDO.class);
        activityAwardsMapper.insert(activityAwards);
        // 返回
        return activityAwards.getId();
    }

    @Override
    public void updateActivityAwards(ActivityAwardsSaveReqVO updateReqVO) {
        // 校验存在
        validateActivityAwardsExists(updateReqVO.getId());
        // 更新
        ActivityAwardsDO updateObj = BeanUtils.toBean(updateReqVO, ActivityAwardsDO.class);
        activityAwardsMapper.updateById(updateObj);
    }

    @Override
    public void deleteActivityAwards(Long id) {
        // 校验存在
        validateActivityAwardsExists(id);
        // 删除
        activityAwardsMapper.deleteById(id);
    }

    private void validateActivityAwardsExists(Long id) {
        if (activityAwardsMapper.selectById(id) == null) {
            throw exception(ACTIVITY_AWARDS_NOT_EXISTS);
        }
    }

    @Override
    public ActivityAwardsDO getActivityAwards(Long id) {
        return activityAwardsMapper.selectById(id);
    }

    @Override
    public PageResult<ActivityAwardsDO> getActivityAwardsPage(ActivityAwardsPageReqVO pageReqVO) {
        return activityAwardsMapper.selectPage(pageReqVO);
    }

    @Override
    public void insertActivityAwards(List<ActivityAwardsDO> activityAwardsDOList) {
        activityAwardsMapper.insertBatch(activityAwardsDOList);
    }

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

    @Override
    public List<ActivityAwardsDO> getListByActivityId(Long activityId) {

        return activityAwardsMapper.selectList(Wrappers.lambdaQuery(ActivityAwardsDO.class)
                .eq(ActivityAwardsDO::getActivityId, activityId)
                .orderByAsc(ActivityAwardsDO::getSortNum));
    }

    @Override
    public List<ActivityAwardsDO> getListByIds(List<Long> ids) {
        return activityAwardsMapper.selectList(Wrappers.lambdaQuery(ActivityAwardsDO.class)
                .in(ActivityAwardsDO::getId, ids)
                .orderByAsc(ActivityAwardsDO::getSortNum));
    }

    @Override
    public List<ActivityAwardsDO> getListByActivityIdAndAwardsStatus(Long activityId, String limitType) {
        return activityAwardsMapper.selectList(Wrappers.lambdaQuery(ActivityAwardsDO.class)
                .eq(ActivityAwardsDO::getActivityId, activityId)
                .eq(ActivityAwardsDO::getLimitType, limitType));
    }

    @Override
    public List<ActivityAwardsDO> getListByActivity(Long activityId) {
        return activityAwardsMapper.selectList(Wrappers.lambdaQuery(ActivityAwardsDO.class)
                .eq(ActivityAwardsDO::getActivityId, activityId));
    }
}