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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

import cn.newfeifan.mall.module.distri.controller.app.ptdailywithdrawal.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.ptdailywithdrawal.PtDailyWithdrawalDO;
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.ptdailywithdrawal.PtDailyWithdrawalMapper;

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 PtDailyWithdrawalServiceImpl implements PtDailyWithdrawalService {

    @Resource
    private PtDailyWithdrawalMapper ptDailyWithdrawalMapper;

    @Override
    public Long createPtDailyWithdrawal(AppPtDailyWithdrawalSaveReqVO createReqVO) {
        // 插入
        PtDailyWithdrawalDO ptDailyWithdrawal = BeanUtils.toBean(createReqVO, PtDailyWithdrawalDO.class);
        ptDailyWithdrawalMapper.insert(ptDailyWithdrawal);
        // 返回
        return ptDailyWithdrawal.getId();
    }

    @Override
    public void updatePtDailyWithdrawal(AppPtDailyWithdrawalSaveReqVO updateReqVO) {
        // 校验存在
        validatePtDailyWithdrawalExists(updateReqVO.getId());
        // 更新
        PtDailyWithdrawalDO updateObj = BeanUtils.toBean(updateReqVO, PtDailyWithdrawalDO.class);
        ptDailyWithdrawalMapper.updateById(updateObj);
    }

    @Override
    public void deletePtDailyWithdrawal(Long id) {
        // 校验存在
        validatePtDailyWithdrawalExists(id);
        // 删除
        ptDailyWithdrawalMapper.deleteById(id);
    }

    private void validatePtDailyWithdrawalExists(Long id) {
        if (ptDailyWithdrawalMapper.selectById(id) == null) {
            throw exception(PT_DAILY_WITHDRAWAL_NOT_EXISTS);
        }
    }

    @Override
    public PtDailyWithdrawalDO getPtDailyWithdrawal(Long id) {
        return ptDailyWithdrawalMapper.selectById(id);
    }

    @Override
    public PageResult<PtDailyWithdrawalDO> getPtDailyWithdrawalPage(AppPtDailyWithdrawalPageReqVO pageReqVO) {
        return ptDailyWithdrawalMapper.selectPage(pageReqVO);
    }

    @Override
    public PtDailyWithdrawalDO getPtDailyWithdrawalToDay() {
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();

        // 获取今天下午十点
        LocalDateTime todayFivePM = LocalDateTime.of(LocalDate.now(), LocalTime.of(22, 0));

        // 获取昨天下午十点
        LocalDateTime yesterdayFivePM = LocalDate.now().minusDays(1).atTime(22, 0);

        // 判断当前时间是否在昨天下午十点和今天下午十点之间
        if (now.isAfter(yesterdayFivePM) && now.isBefore(todayFivePM)) {
            PtDailyWithdrawalDO ptDailyWithdrawalDO = ptDailyWithdrawalMapper.selectOne(new LambdaQueryWrapper<PtDailyWithdrawalDO>()
                    .eq(PtDailyWithdrawalDO::getStartTime, yesterdayFivePM)
                    .eq(PtDailyWithdrawalDO::getEndTime, todayFivePM)
            );

            // 如果没有记录那就插入一条新的
            if (ptDailyWithdrawalDO == null) {
                ptDailyWithdrawalDO = PtDailyWithdrawalDO.builder()
                        .startTime(yesterdayFivePM)
                        .endTime(todayFivePM)
                        .build();

                ptDailyWithdrawalMapper.insert(ptDailyWithdrawalDO);

                return ptDailyWithdrawalMapper.selectById(ptDailyWithdrawalDO.getId());
            }

            return ptDailyWithdrawalDO;

        } else {
            // 获取明天下午十点的时间
            LocalDateTime tomorrowFivePM = LocalDate.now().plusDays(1).atTime(22, 0);

            PtDailyWithdrawalDO ptDailyWithdrawalDO = ptDailyWithdrawalMapper.selectOne(new LambdaQueryWrapper<PtDailyWithdrawalDO>()
                    .eq(PtDailyWithdrawalDO::getStartTime, todayFivePM)
                    .eq(PtDailyWithdrawalDO::getEndTime, tomorrowFivePM)
            );

            // 如果没有记录那就插入一条新的
            if (ptDailyWithdrawalDO == null) {
                ptDailyWithdrawalDO = PtDailyWithdrawalDO.builder()
                        .startTime(todayFivePM)
                        .endTime(tomorrowFivePM)
                        .build();

                ptDailyWithdrawalMapper.insert(ptDailyWithdrawalDO);

                return ptDailyWithdrawalMapper.selectById(ptDailyWithdrawalDO.getId());
            }

            return ptDailyWithdrawalDO;
        }
    }

}