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

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.math.BigDecimal;
import java.util.*;

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

import co.yixiang.yshop.module.distributor.convert.distributorwages.DistributorWagesConvert;
import co.yixiang.yshop.module.distributor.dal.mysql.distributorwages.DistributorWagesMapper;

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 DistributorWagesServiceImpl implements DistributorWagesService {

    @Resource
    private DistributorWagesMapper wagesMapper;

    @Override
    public Long createWages(DistributorWagesCreateReqVO createReqVO) {
        // 插入
        DistributorWagesDO wages = DistributorWagesConvert.INSTANCE.convert(createReqVO);
        wagesMapper.insert(wages);
        // 返回
        return wages.getId();
    }

    @Override
    public void updateWages(DistributorWagesUpdateReqVO updateReqVO) {
        // 校验存在
        validateWagesExists(updateReqVO.getId());
        // 更新
        DistributorWagesDO updateObj = DistributorWagesConvert.INSTANCE.convert(updateReqVO);
        wagesMapper.updateById(updateObj);
    }

    @Override
    public void deleteWages(Long id) {
        // 校验存在
        validateWagesExists(id);
        // 删除
        wagesMapper.deleteById(id);
    }

    private void validateWagesExists(Long id) {
        if (wagesMapper.selectById(id) == null) {
            throw exception(WAGES_NOT_EXISTS);
        }
    }

    @Override
    public DistributorWagesDO getWages(Long id) {
        return wagesMapper.selectById(id);
    }

    @Override
    public List<DistributorWagesDO> getWagesList(Collection<Long> ids) {
        return wagesMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<DistributorWagesDO> getWagesPage(DistributorWagesPageReqVO pageReqVO) {
        return wagesMapper.selectPage(pageReqVO);
    }

    @Override
    public List<DistributorWagesDO> getWagesList(DistributorWagesExportReqVO exportReqVO) {
        return wagesMapper.selectList(exportReqVO);
    }

    @Override
    public DistributorWagesDO getWagesByDistributorId(Long id) {
        return wagesMapper.selectOne(DistributorWagesDO::getDistributorId, id);
    }

    @Override
    public Boolean reduceAmount(Long id, BigDecimal amount) {
        int i = wagesMapper.reduceAmount(id, amount);
        return i > 0;
    }

    @Override
    public void reduceRefuseAmount(Long id, BigDecimal amount) {
        wagesMapper.reduceRefuseAmount(id, amount);
    }

    @Override
    public void increaseRefuseAmount(Long id, BigDecimal amount) {
        wagesMapper.increaseRefuseAmount(id, amount);
    }

    @Override
    public void settlementAmount(Long id, BigDecimal amount) {
        wagesMapper.settlementAmount(id, amount);
    }


    @Override
    public void increaseAmount(Long id, BigDecimal amount) {
        wagesMapper.increaseAmount(id, amount);
    }

}
