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

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.rechargeamount.vo.RechargeAmountPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.rechargeamount.vo.RechargeAmountSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.rechargeamount.RechargeAmountDO;
import cn.iocoder.yudao.module.member.dal.mysql.rechargeamount.RechargeAmountMapper;
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.RECHARGE_AMOUNT_NOT_EXISTS;

/**
 * 充值金额配置 Service 实现类
 *
 * @author 开发账号
 */
@Service
@Validated
public class RechargeAmountServiceImpl implements RechargeAmountService {

    @Resource
    private RechargeAmountMapper rechargeAmountMapper;

    @Override
    public Long createRechargeAmount(RechargeAmountSaveReqVO createReqVO) {
        // 插入
        RechargeAmountDO rechargeAmount = BeanUtils.toBean(createReqVO, RechargeAmountDO.class);
        rechargeAmountMapper.insert(rechargeAmount);
        // 返回
        return rechargeAmount.getId();
    }

    @Override
    public void updateRechargeAmount(RechargeAmountSaveReqVO updateReqVO) {
        // 校验存在
        validateRechargeAmountExists(updateReqVO.getId());
        // 更新
        RechargeAmountDO updateObj = BeanUtils.toBean(updateReqVO, RechargeAmountDO.class);
        rechargeAmountMapper.updateById(updateObj);
    }

    @Override
    public void deleteRechargeAmount(Long id) {
        // 校验存在
        validateRechargeAmountExists(id);
        // 删除
        rechargeAmountMapper.deleteById(id);
    }

    private void validateRechargeAmountExists(Long id) {
        if (rechargeAmountMapper.selectById(id) == null) {
            throw exception(RECHARGE_AMOUNT_NOT_EXISTS);
        }
    }

    @Override
    public RechargeAmountDO getRechargeAmount(Long id) {
        return rechargeAmountMapper.selectById(id);
    }

    @Override
    public PageResult<RechargeAmountDO> getRechargeAmountPage(RechargeAmountPageReqVO pageReqVO) {
        return rechargeAmountMapper.selectPage(pageReqVO);
    }

    @Override
    public List<RechargeAmountDO> getAmount() {
        List<RechargeAmountDO> list = rechargeAmountMapper.selectList(Wrappers.<RechargeAmountDO>lambdaQuery()
                .eq(RechargeAmountDO::getStatus, RechargeAmountDO.ON)
                .orderByAsc(RechargeAmountDO::getMoney));
        return list;
    }
}