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

import cn.newfeifan.mall.module.distri.dal.mysql.ptprofit.PtProfitMapper;
import cn.newfeifan.mall.module.distri.dal.mysql.ptprofitlog.PtProfitLogMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.newfeifan.mall.module.distri.controller.admin.ptprofitdailystatisticslog.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.ptprofitdailystatisticslog.PtProfitDailyStatisticsLogDO;
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.ptprofitdailystatisticslog.PtProfitDailyStatisticsLogMapper;

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 PtProfitDailyStatisticsLogServiceImpl implements PtProfitDailyStatisticsLogService {

    @Resource
    private PtProfitDailyStatisticsLogMapper ptProfitDailyStatisticsLogMapper;

    @Resource
    private PtProfitMapper ptProfitMapper;

    @Resource
    PtProfitLogMapper ptProfitLogMapper;

    @Override
    public Long createPtProfitDailyStatisticsLog(PtProfitDailyStatisticsLogSaveReqVO createReqVO) {
        // 插入
        PtProfitDailyStatisticsLogDO ptProfitDailyStatisticsLog = BeanUtils.toBean(createReqVO, PtProfitDailyStatisticsLogDO.class);
        ptProfitDailyStatisticsLogMapper.insert(ptProfitDailyStatisticsLog);
        // 返回
        return ptProfitDailyStatisticsLog.getId();
    }

    @Override
    public void updatePtProfitDailyStatisticsLog(PtProfitDailyStatisticsLogSaveReqVO updateReqVO) {
        // 校验存在
        validatePtProfitDailyStatisticsLogExists(updateReqVO.getId());
        // 更新
        PtProfitDailyStatisticsLogDO updateObj = BeanUtils.toBean(updateReqVO, PtProfitDailyStatisticsLogDO.class);
        ptProfitDailyStatisticsLogMapper.updateById(updateObj);
    }

    @Override
    public void deletePtProfitDailyStatisticsLog(Long id) {
        // 校验存在
        validatePtProfitDailyStatisticsLogExists(id);
        // 删除
        ptProfitDailyStatisticsLogMapper.deleteById(id);
    }

    private void validatePtProfitDailyStatisticsLogExists(Long id) {
        if (ptProfitDailyStatisticsLogMapper.selectById(id) == null) {
            throw exception(PT_PROFIT_DAILY_STATISTICS_LOG_NOT_EXISTS);
        }
    }

    @Override
    public PtProfitDailyStatisticsLogDO getPtProfitDailyStatisticsLog(Long id) {
        return ptProfitDailyStatisticsLogMapper.selectById(id);
    }

    @Override
    public PageResult<PtProfitDailyStatisticsLogDO> getPtProfitDailyStatisticsLogPage(PtProfitDailyStatisticsLogPageReqVO pageReqVO) {
        return ptProfitDailyStatisticsLogMapper.selectPage(pageReqVO);
    }

//    @Override
//    public void createPtProfitDailyStatisticsLog() {
//        LocalDate now = LocalDate.now();
//
//        //确保每天只有一条记录
//        PtProfitDailyStatisticsLogDO ptLog = ptProfitDailyStatisticsLogMapper.selectByEveryday(now);
//        if (ptLog != null) {
//            deletePtProfitDailyStatisticsLog(ptLog.getId());
//        }
//        //获取平台总收益
//        PtProfitDO ptProfitDO = ptProfitMapper.selectList().get(0);
//        //获取当天碰撞支出总和
//        List<PtProfitLogDO> ptProfitLogDOS = ptProfitLogMapper.selectList(new LambdaQueryWrapper<PtProfitLogDO>()
//                .between(PtProfitLogDO::getCreateTime, LocalDateTimeTool.startTime(now), LocalDateTimeTool.endTime(now))
//                .eq(PtProfitLogDO::getProfitStatus, AFTER_CRASH_CALC_PT_TOTAL_GROSS_QUOTA)
//        );
//        Long sum = ptProfitLogDOS.stream().mapToLong(PtProfitLogDO::getAmount).sum();
//
//        ptLog = new PtProfitDailyStatisticsLogDO();
//        ptLog.setEveryday(now);
//        ptLog.setTotalCollisionAmount(sum);
//        ptLog.setTotalRemainingAmount(ptProfitDO.getPtTotalAdd());
//
//        ptProfitDailyStatisticsLogMapper.insert(ptLog);
//    }

}