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

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.rechargeconfig.vo.*;
import co.yixiang.yshop.module.order.dal.dataobject.rechargeconfig.RechargeConfigDO;
import co.yixiang.yshop.framework.common.pojo.PageResult;

import co.yixiang.yshop.module.order.convert.rechargeconfig.RechargeConfigConvert;
import co.yixiang.yshop.module.order.dal.mysql.rechargeconfig.RechargeConfigMapper;

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 RechargeConfigServiceImpl implements RechargeConfigService {

    @Resource
    private RechargeConfigMapper rechargeConfigMapper;

    @Override
    public Long createRechargeConfig(RechargeConfigCreateReqVO createReqVO) {
        // 插入
        RechargeConfigDO rechargeConfig = RechargeConfigConvert.INSTANCE.convert(createReqVO);
        rechargeConfigMapper.insert(rechargeConfig);
        // 返回
        return rechargeConfig.getId();
    }

    @Override
    public void updateRechargeConfig(RechargeConfigUpdateReqVO updateReqVO) {
        // 校验存在
        validateRechargeConfigExists(updateReqVO.getId());
        // 更新
        RechargeConfigDO updateObj = RechargeConfigConvert.INSTANCE.convert(updateReqVO);
        rechargeConfigMapper.updateById(updateObj);
    }

    @Override
    public void deleteRechargeConfig(Long id) {
        // 校验存在
        validateRechargeConfigExists(id);
        // 删除
        rechargeConfigMapper.deleteById(id);
    }

    private void validateRechargeConfigExists(Long id) {
        if (rechargeConfigMapper.selectById(id) == null) {
            throw exception(RECHARGE_CONFIG_NOT_EXISTS);
        }
    }

    @Override
    public RechargeConfigDO getRechargeConfig(Long id) {
        return rechargeConfigMapper.selectById(id);
    }

    @Override
    public List<RechargeConfigDO> getRechargeConfigList(Collection<Long> ids) {
        return rechargeConfigMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<RechargeConfigDO> getRechargeConfigPage(RechargeConfigPageReqVO pageReqVO) {
        return rechargeConfigMapper.selectPage(pageReqVO);
    }

    @Override
    public List<RechargeConfigDO> getRechargeConfigList(RechargeConfigExportReqVO exportReqVO) {
        return rechargeConfigMapper.selectList(exportReqVO);
    }

}
