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

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

import co.yixiang.yshop.module.distributor.convert.distributor.DistributorConvert;
import co.yixiang.yshop.module.distributor.dal.mysql.distributor.DistributorMapper;

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 DistributorServiceImpl implements DistributorService {

    @Resource
    private DistributorMapper distributorMapper;

    @Override
    public Long create(DistributorCreateReqVO createReqVO) {
        // 插入
        DistributorDO distributorDO = DistributorConvert.INSTANCE.convert(createReqVO);
        distributorMapper.insert(distributorDO);
        // 返回
        return distributorDO.getId();
    }

    @Override
    public Long save(DistributorDO distributorDO) {
        distributorMapper.insert(distributorDO);
        // 返回
        return distributorDO.getId();
    }

    @Override
    public void update(DistributorUpdateReqVO updateReqVO) {
        // 校验存在
        validateExists(updateReqVO.getId());
        // 更新
        DistributorDO updateObj = DistributorConvert.INSTANCE.convert(updateReqVO);
        distributorMapper.updateById(updateObj);
    }

    @Override
    public void update(DistributorDO distributorDO) {
        validateExists(distributorDO.getId());
        distributorMapper.updateById(distributorDO);
    }

    @Override
    public void delete(Long id) {
        // 校验存在
        validateExists(id);
        // 删除
        distributorMapper.deleteById(id);
    }

    private void validateExists(Long id) {
        if (distributorMapper.selectById(id) == null) {
            throw exception(DISTRIBUTOR_NOT_EXISTS);
        }
    }

    @Override
    public DistributorDO get(Long id) {
        return distributorMapper.selectById(id);
    }

    @Override
    public List<DistributorDO> getList(Collection<Long> ids) {
        return distributorMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<DistributorDO> getPage(DistributorPageReqVO pageReqVO) {
        return distributorMapper.selectPage(pageReqVO);
    }

    @Override
    public List<DistributorDO> getList(DistributorExportReqVO exportReqVO) {
        return distributorMapper.selectList(exportReqVO);
    }

    @Override
    public DistributorDO getDistributorByUserId(Long userId) {
        return distributorMapper.selectOne(DistributorDO::getUserId, userId);
    }

    @Override
    public void clearance(Long id) {
        distributorMapper.updateP1(id);
        distributorMapper.updateP2(id);
    }

    @Override
    public void addUpDistributor(Long id) {
        distributorMapper.addUpDistributor(id);
    }

    @Override
    public void addAmount(Long id, BigDecimal productPrice, BigDecimal addUpWages) {
        distributorMapper.addAmount(id, productPrice, addUpWages);
    }

}
