package com.zanxiang.game.module.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zanxiang.game.module.base.pojo.enums.DeleteEnum;
import com.zanxiang.game.module.base.pojo.enums.StatusEnum;
import com.zanxiang.game.module.base.util.DateUtils;
import com.zanxiang.game.module.manage.pojo.dto.GameDTO;
import com.zanxiang.game.module.manage.pojo.params.MiPaySumListParam;
import com.zanxiang.game.module.manage.pojo.vo.MiPaySumListVO;
import com.zanxiang.game.module.manage.service.IGameService;
import com.zanxiang.game.module.manage.service.IMiPaySumService;
import com.zanxiang.game.module.mybatis.entity.MiPaySum;
import com.zanxiang.game.module.mybatis.mapper.MiPaySumMapper;
import com.zanxiang.module.util.bean.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * @author : lingfeng
 * @time : 2023-06-21
 * @description : 米大师支付统计
 */
@Slf4j
@Service
public class MiPaySumServiceImpl extends ServiceImpl<MiPaySumMapper, MiPaySum> implements IMiPaySumService {

    @Autowired
    private IGameService gameService;

    @Override
    public void add(Long gameId, String appId, String miPayAppId) {
        long count = super.count(new LambdaQueryWrapper<MiPaySum>()
                .eq(MiPaySum::getGameId, gameId)
                .eq(MiPaySum::getAppId, appId)
                .eq(MiPaySum::getMiPayAppId, miPayAppId));
        if (count > 0) {
            return;
        }
        super.save(MiPaySum.builder()
                .gameId(gameId)
                .appId(appId)
                .miPayAppId(miPayAppId)
                .maxPayLock(StatusEnum.YES.getCode())
                .status(StatusEnum.YES.getCode())
                .isDelete(DeleteEnum.NO.getCode())
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .build());
    }

    @Override
    public IPage<MiPaySumListVO> pageList(MiPaySumListParam param) {
        //返回查询结果
        return page(param.toPage(), new LambdaQueryWrapper<MiPaySum>()
                .eq(param.getGameId() != null, MiPaySum::getGameId, param.getGameId())
                .eq(param.getStatus() != null, MiPaySum::getStatus, param.getStatus())
                .orderByDesc(MiPaySum::getCreateTime)
        ).convert(this::toVO);
    }

    private MiPaySumListVO toVO(MiPaySum miPaySum) {
        MiPaySumListVO miPaySumListVO = BeanUtil.copy(miPaySum, MiPaySumListVO.class);
        if (miPaySumListVO == null) {
            return null;
        }
        //判断日更新日期是否为今天
        if (!DateUtils.isToday(miPaySum.getDayUpdateDate())) {
            //日已用额度归零
            miPaySumListVO.setDayPayAmount(BigDecimal.ZERO);
        }
        //判断周更新日期是否为本周
        if (!DateUtils.isThisWeek(miPaySum.getMouthUpdateDate())) {
            //周已用额度归零
            miPaySumListVO.setWeakerPayAmount(BigDecimal.ZERO);
        }
        //判断月更新日期是否为本月
        if (!DateUtils.isThisMonth(miPaySum.getMouthUpdateDate())) {
            //月已用额度归零
            miPaySumListVO.setMouthPayAmount(BigDecimal.ZERO);
        }
        //获取游戏名字
        GameDTO gameDTO = gameService.getById(miPaySumListVO.getGameId());
        if (gameDTO != null) {
            miPaySumListVO.setGameName(gameDTO.getName());
        }
        return miPaySumListVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean statusUpdate(Long id, Integer status) {
        return super.update(new LambdaUpdateWrapper<MiPaySum>()
                .set(MiPaySum::getStatus, status)
                .set(MiPaySum::getUpdateTime, LocalDateTime.now())
                .eq(MiPaySum::getId, id));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean maxPayCountUpdate(Long id, BigDecimal maxPayCount) {
        return super.update(new LambdaUpdateWrapper<MiPaySum>()
                .set(MiPaySum::getMaxPayAmount, maxPayCount)
                .set(MiPaySum::getUpdateTime, LocalDateTime.now())
                .eq(MiPaySum::getId, id));
    }
}
