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

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

import co.yixiang.yshop.module.distributor.convert.distributordynamics.DistributorDynamicsConvert;
import co.yixiang.yshop.module.distributor.dal.mysql.distributordynamics.DistributorDynamicsMapper;

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 DistributorDynamicsServiceImpl implements DistributorDynamicsService {

    @Resource
    private DistributorDynamicsMapper dynamicsMapper;

    @Override
    public Long createDynamics(DistributorDynamicsCreateReqVO createReqVO) {
        // 插入
        DistributorDynamicsDO dynamics = DistributorDynamicsConvert.INSTANCE.convert(createReqVO);
        dynamicsMapper.insert(dynamics);
        // 返回
        return dynamics.getId();
    }

    @Override
    public void updateDynamics(DistributorDynamicsUpdateReqVO updateReqVO) {
        // 校验存在
        validateDynamicsExists(updateReqVO.getId());
        // 更新
        DistributorDynamicsDO updateObj = DistributorDynamicsConvert.INSTANCE.convert(updateReqVO);
        dynamicsMapper.updateById(updateObj);
    }

    @Override
    public void deleteDynamics(Long id) {
        // 校验存在
        validateDynamicsExists(id);
        // 删除
        dynamicsMapper.deleteById(id);
    }

    private void validateDynamicsExists(Long id) {
        if (dynamicsMapper.selectById(id) == null) {
            throw exception(DYNAMICS_NOT_EXISTS);
        }
    }

    @Override
    public DistributorDynamicsDO getDynamics(Long id) {
        return dynamicsMapper.selectById(id);
    }

    @Override
    public List<DistributorDynamicsDO> getDynamicsList(Collection<Long> ids) {
        return dynamicsMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<DistributorDynamicsDO> getDynamicsPage(DistributorDynamicsPageReqVO pageReqVO) {
        return dynamicsMapper.selectPage(pageReqVO);
    }

    @Override
    public List<DistributorDynamicsDO> getDynamicsList(DistributorDynamicsExportReqVO exportReqVO) {
        return dynamicsMapper.selectList(exportReqVO);
    }

}
