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

import cn.newfeifan.mall.module.distri.enums.CaclEnum;
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.ptprofitlog.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.ptprofitlog.PtProfitLogDO;
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.ptprofitlog.PtProfitLogMapper;

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 PtProfitLogServiceImpl implements PtProfitLogService {

    @Resource
    private PtProfitLogMapper ptProfitLogMapper;


    @Override
    public Long createPtProfitLog(PtProfitLogSaveReqVO createReqVO) {
        // 插入
        PtProfitLogDO ptProfitLog = BeanUtils.toBean(createReqVO, PtProfitLogDO.class);
        ptProfitLogMapper.insert(ptProfitLog);
        // 返回
        return ptProfitLog.getId();
    }

    @Override
    public void updatePtProfitLog(PtProfitLogSaveReqVO updateReqVO) {
        // 校验存在
        validatePtProfitLogExists(updateReqVO.getId());
        // 更新
        PtProfitLogDO updateObj = BeanUtils.toBean(updateReqVO, PtProfitLogDO.class);
        ptProfitLogMapper.updateById(updateObj);
    }

    @Override
    public void deletePtProfitLog(Long id) {
        // 校验存在
        validatePtProfitLogExists(id);
        // 删除
        ptProfitLogMapper.deleteById(id);
    }

    private void validatePtProfitLogExists(Long id) {
        if (ptProfitLogMapper.selectById(id) == null) {
            throw exception(PT_PROFIT_LOG_NOT_EXISTS);
        }
    }

    @Override
    public PtProfitLogDO getPtProfitLog(Long id) {
        return ptProfitLogMapper.selectById(id);
    }

    @Override
    public PageResult<PtProfitLogDO> getPtProfitLogPage(PtProfitLogPageReqVO pageReqVO) {
        return ptProfitLogMapper.selectPage(pageReqVO);
    }

    @Override
    public void saveBatch(List<PtProfitLogSaveReqVO> ptProfitLogSaveReqVOS) {
        List<PtProfitLogDO> ptProfitLog = BeanUtils.toBean(ptProfitLogSaveReqVOS, PtProfitLogDO.class);
        ptProfitLogMapper.insertBatch(ptProfitLog);
    }

    @Override
    public void addMessage(Long userId, CaclEnum caclEnum, Integer amount, Integer afterAmount, String percentTemplate) {
        PtProfitLogSaveReqVO ptProfitLog = PtProfitLogSaveReqVO.builder()
                .afterAmount(afterAmount)
                .amount(amount)
                .percentTemplate(percentTemplate)
                .userId(userId)
                .profitStatus(caclEnum.getType())
                .build();
        ptProfitLogMapper.insert(BeanUtils.toBean(ptProfitLog, PtProfitLogDO.class));

    }

}