package com.jojubanking.boot.module.system.service.refundapply;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.jojubanking.boot.module.system.controller.admin.refundapply.vo.*;
import com.jojubanking.boot.module.system.dal.dataobject.refundapply.RefundApplyDO;
import com.jojubanking.boot.framework.common.pojo.PageResult;

import com.jojubanking.boot.module.system.convert.refundapply.RefundApplyConvert;
import com.jojubanking.boot.module.system.dal.mysql.refundapply.RefundApplyMapper;

import static com.jojubanking.boot.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.jojubanking.boot.module.system.enums.ErrorCodeConstants.*;

/**
 * 就诊卡退费申请 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class RefundApplyServiceImpl implements RefundApplyService {

    @Resource
    private RefundApplyMapper refundApplyMapper;

    @Override
    public Long createRefundApply(RefundApplyCreateReqVO createReqVO) {
        // 插入
        RefundApplyDO refundApply = RefundApplyConvert.INSTANCE.convert(createReqVO);
        refundApplyMapper.insert(refundApply);
        // 返回
        return refundApply.getId();
    }

    @Override
    public void updateRefundApply(RefundApplyUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateRefundApplyExists(updateReqVO.getId());
        // 更新
        RefundApplyDO updateObj = RefundApplyConvert.INSTANCE.convert(updateReqVO);
        refundApplyMapper.updateById(updateObj);
    }

    @Override
    public void deleteRefundApply(Long id) {
        // 校验存在
        this.validateRefundApplyExists(id);
        // 删除
        refundApplyMapper.deleteById(id);
    }

    private void validateRefundApplyExists(Long id) {
        if (refundApplyMapper.selectById(id) == null) {
            throw exception(REFUND_APPLY_NOT_EXISTS);
        }
    }

    @Override
    public RefundApplyDO getRefundApply(Long id) {
        return refundApplyMapper.selectById(id);
    }

    @Override
    public List<RefundApplyDO> getRefundApplyList(Collection<Long> ids) {
        return refundApplyMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<RefundApplyDO> getRefundApplyPage(RefundApplyPageReqVO pageReqVO) {
        return refundApplyMapper.selectPage(pageReqVO);
    }

    @Override
    public List<RefundApplyDO> getRefundApplyList(RefundApplyExportReqVO exportReqVO) {
        return refundApplyMapper.selectList(exportReqVO);
    }

}
