package co.yixiang.yshop.module.order.service.rechargepackage;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.*;

import co.yixiang.yshop.module.order.controller.admin.rechargepackage.vo.*;
import co.yixiang.yshop.module.order.dal.dataobject.rechargepackage.RechargePackageDO;
import co.yixiang.yshop.framework.common.pojo.PageResult;

import co.yixiang.yshop.module.order.convert.rechargepackage.RechargePackageConvert;
import co.yixiang.yshop.module.order.dal.mysql.rechargepackage.RechargePackageMapper;

import static co.yixiang.yshop.framework.common.exception.util.ServiceExceptionUtil.exception;
import static co.yixiang.yshop.module.order.enums.ErrorCodeConstants.*;

/**
 * 充值套餐配置 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class RechargePackageServiceImpl implements RechargePackageService {

    @Resource
    private RechargePackageMapper rechargePackageMapper;

    @Override
    public Long createRechargePackage(RechargePackageCreateReqVO createReqVO) {
        // 插入
        RechargePackageDO rechargePackage = RechargePackageConvert.INSTANCE.convert(createReqVO);
        rechargePackageMapper.insert(rechargePackage);
        // 返回
        return rechargePackage.getId();
    }

    @Override
    public void updateRechargePackage(RechargePackageUpdateReqVO updateReqVO) {
        // 校验存在
        validateRechargePackageExists(updateReqVO.getId());
        // 更新
        RechargePackageDO updateObj = RechargePackageConvert.INSTANCE.convert(updateReqVO);
        rechargePackageMapper.updateById(updateObj);
    }

    @Override
    public void deleteRechargePackage(Long id) {
        // 校验存在
        validateRechargePackageExists(id);
        // 删除
        rechargePackageMapper.deleteById(id);
    }

    private void validateRechargePackageExists(Long id) {
        if (rechargePackageMapper.selectById(id) == null) {
            throw exception(RECHARGE_PACKAGE_NOT_EXISTS);
        }
    }

    @Override
    public RechargePackageDO getRechargePackage(Long id) {
        return rechargePackageMapper.selectById(id);
    }

    @Override
    public List<RechargePackageDO> getRechargePackageList(Collection<Long> ids) {
        return rechargePackageMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<RechargePackageDO> getRechargePackagePage(RechargePackagePageReqVO pageReqVO) {
        return rechargePackageMapper.selectPage(pageReqVO);
    }

    @Override
    public List<RechargePackageDO> getRechargePackageList(RechargePackageExportReqVO exportReqVO) {
        return rechargePackageMapper.selectList(exportReqVO);
    }

}
