package cn.newfeifan.mall.module.distri.service.ptprofitlogdetails;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import java.util.*;
import cn.newfeifan.mall.module.distri.controller.admin.ptprofitlogdetails.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.ptprofitlogdetails.PtProfitLogDetailsDO;
import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.common.util.object.BeanUtils;

import cn.newfeifan.mall.module.distri.dal.mysql.ptprofitlogdetails.PtProfitLogDetailsMapper;

import static cn.newfeifan.mall.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.newfeifan.mall.module.distri.enums.ErrorCodeConstants.*;

/**
 * 平台利润记录详情 Service 实现类
 *
 * @author 非繁人
 */
@Service
@Validated
public class PtProfitLogDetailsServiceImpl implements PtProfitLogDetailsService {

    @Resource
    private PtProfitLogDetailsMapper ptProfitLogDetailsMapper;

    @Override
    public Long createPtProfitLogDetails(PtProfitLogDetailsSaveReqVO createReqVO) {
        // 插入
        PtProfitLogDetailsDO ptProfitLogDetails = BeanUtils.toBean(createReqVO, PtProfitLogDetailsDO.class);
        ptProfitLogDetailsMapper.insert(ptProfitLogDetails);
        // 返回
        return ptProfitLogDetails.getId();
    }

    @Override
    public void updatePtProfitLogDetails(PtProfitLogDetailsSaveReqVO updateReqVO) {
        // 校验存在
        validatePtProfitLogDetailsExists(updateReqVO.getId());
        // 更新
        PtProfitLogDetailsDO updateObj = BeanUtils.toBean(updateReqVO, PtProfitLogDetailsDO.class);
        ptProfitLogDetailsMapper.updateById(updateObj);
    }

    @Override
    public void deletePtProfitLogDetails(Long id) {
        // 校验存在
        validatePtProfitLogDetailsExists(id);
        // 删除
        ptProfitLogDetailsMapper.deleteById(id);
    }

    private void validatePtProfitLogDetailsExists(Long id) {
        if (ptProfitLogDetailsMapper.selectById(id) == null) {
            throw exception(PT_PROFIT_LOG_DETAILS_NOT_EXISTS);
        }
    }

    @Override
    public PtProfitLogDetailsDO getPtProfitLogDetails(Long id) {
        return ptProfitLogDetailsMapper.selectById(id);
    }

    @Override
    public PageResult<PtProfitLogDetailsDO> getPtProfitLogDetailsPage(PtProfitLogDetailsPageReqVO pageReqVO) {
        return ptProfitLogDetailsMapper.selectPage(pageReqVO);
    }

    @Override
    public void saveBatch(List<PtProfitLogDetailsSaveReqVO> ptProfitLogDetailsSaveReqVOS) {
        List<PtProfitLogDetailsDO> ptProfitLogDetailsDOList = BeanUtils.toBean(ptProfitLogDetailsSaveReqVOS, PtProfitLogDetailsDO.class);
        ptProfitLogDetailsMapper.insertBatch(ptProfitLogDetailsDOList);
    }

}