package co.yixiang.yshop.module.distributor.service.distributorwithdrawalmethod;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.*;

import co.yixiang.yshop.module.distributor.controller.admin.distributorwithdrawalmethod.vo.*;
import co.yixiang.yshop.module.distributor.dal.dataobject.distributorwithdrawalmethod.DistributorWithdrawalMethodDO;
import co.yixiang.yshop.framework.common.pojo.PageResult;

import co.yixiang.yshop.module.distributor.convert.distributorwithdrawalmethod.DistributorWithdrawalMethodConvert;
import co.yixiang.yshop.module.distributor.dal.mysql.distributorwithdrawalmethod.DistributorWithdrawalMethodMapper;

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

/**
 * 分销商提现方式 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class DistributorWithdrawalMethodServiceImpl implements DistributorWithdrawalMethodService {

    @Resource
    private DistributorWithdrawalMethodMapper withdrawalMethodMapper;

    @Override
    public Long createWithdrawalMethod(DistributorWithdrawalMethodCreateReqVO createReqVO) {
        // 插入
        DistributorWithdrawalMethodDO withdrawalMethod = DistributorWithdrawalMethodConvert.INSTANCE.convert(createReqVO);
        withdrawalMethodMapper.insert(withdrawalMethod);
        // 返回
        return withdrawalMethod.getId();
    }

    @Override
    public void updateWithdrawalMethod(DistributorWithdrawalMethodUpdateReqVO updateReqVO) {
        // 校验存在
        validateWithdrawalMethodExists(updateReqVO.getId());
        // 更新
        DistributorWithdrawalMethodDO updateObj = DistributorWithdrawalMethodConvert.INSTANCE.convert(updateReqVO);
        withdrawalMethodMapper.updateById(updateObj);
    }

    @Override
    public void deleteWithdrawalMethod(Long id) {
        // 校验存在
        validateWithdrawalMethodExists(id);
        // 删除
        withdrawalMethodMapper.deleteById(id);
    }

    private void validateWithdrawalMethodExists(Long id) {
        if (withdrawalMethodMapper.selectById(id) == null) {
            throw exception(WITHDRAWAL_METHOD_NOT_EXISTS);
        }
    }

    @Override
    public DistributorWithdrawalMethodDO getWithdrawalMethod(Long id) {
        return withdrawalMethodMapper.selectById(id);
    }

    @Override
    public List<DistributorWithdrawalMethodDO> getWithdrawalMethodList(Collection<Long> ids) {
        return withdrawalMethodMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<DistributorWithdrawalMethodDO> getWithdrawalMethodPage(DistributorWithdrawalMethodPageReqVO pageReqVO) {
        return withdrawalMethodMapper.selectPage(pageReqVO);
    }

    @Override
    public List<DistributorWithdrawalMethodDO> getWithdrawalMethodList(DistributorWithdrawalMethodExportReqVO exportReqVO) {
        return withdrawalMethodMapper.selectList(exportReqVO);
    }

}
