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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zanxiang.game.module.base.pojo.enums.PayWayEnum;
import com.zanxiang.game.module.mybatis.entity.GameExt;
import com.zanxiang.game.module.mybatis.entity.GameRemitLog;
import com.zanxiang.game.module.mybatis.entity.User;
import com.zanxiang.game.module.mybatis.mapper.GameRemitLogMapper;
import com.zanxiang.game.module.sdk.pojo.param.GameRemitLogParam;
import com.zanxiang.game.module.sdk.service.IGameExtService;
import com.zanxiang.game.module.sdk.service.IGameRemitLogService;
import com.zanxiang.game.module.sdk.service.IUserService;
import com.zanxiang.game.module.sdk.util.SignUtil;
import com.zanxiang.module.util.DateUtil;
import com.zanxiang.module.util.exception.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @author : lingfeng
 * @time : 2023-07-17
 * @description : 游戏提现记录
 */
@Slf4j
@Service
public class GameRemitLogServiceImpl extends ServiceImpl<GameRemitLogMapper, GameRemitLog> implements IGameRemitLogService {

    @Autowired
    private IGameExtService gameExtService;

    @Autowired
    private IUserService userService;

    @Override
    public boolean addOrUpdate(GameRemitLogParam param) {
        GameExt gameExt = gameExtService.getByGameAppId(param.getAppId());
        StringBuilder sb = new StringBuilder();
        sb.append("payKey=").append(gameExt.getPayKey());
        sb.append("&appId=").append(param.getAppId());
        sb.append("&userId=").append(param.getUserId());
        sb.append("&merchantOrderNo=").append(param.getMerchantOrderNo());
        sb.append("&createdTime=").append(param.getCreatedTime());
        String mySign;
        try {
            mySign = SignUtil.MD5(sb.toString());
        } catch (Exception e) {
            log.error("md5工具类加密异常, str : {}, e : {}", sb.toString(), e.getMessage());
            throw new BaseException("MD5加密异常");
        }
        //签名错误
        if (!Objects.equals(mySign, param.getSign())) {
            log.error("游戏提现签名验证失败, mySign : {}, sign : {}", mySign, param.getSign());
            throw new BaseException("签名验证失败");
        }
        User user = userService.getById(param.getUserId());
        GameRemitLog gameRemitLog = super.getOne(new LambdaQueryWrapper<GameRemitLog>()
                .eq(GameRemitLog::getMerchantOrderNo, param.getMerchantOrderNo()));
        if (gameRemitLog == null) {
            gameRemitLog = this.transform(param, user, gameExt.getGameId());
        } else {
            gameRemitLog.setUserId(param.getUserId());
            gameRemitLog.setAgentId(user == null ? null : user.getAgentId());
            gameRemitLog.setGameId(gameExt.getGameId());
            gameRemitLog.setAmount(param.getAmount());
            gameRemitLog.setPayPlatform(PayWayEnum.getKeyByPayWayId(param.getPayPlatform()));
            gameRemitLog.setPayAccount(param.getPayAccount());
            gameRemitLog.setCreatedTime(DateUtil.milliToLocalDateTime(param.getCreatedTime()));
            gameRemitLog.setUpdateTime(LocalDateTime.now());
        }
        return super.saveOrUpdate(gameRemitLog);
    }

    private GameRemitLog transform(GameRemitLogParam param, User user, Long gameId) {
        return GameRemitLog.builder()
                .userId(param.getUserId())
                .agentId(user == null ? null : user.getAgentId())
                .gameId(gameId)
                .merchantOrderNo(param.getMerchantOrderNo())
                .amount(param.getAmount())
                .payPlatform(PayWayEnum.getKeyByPayWayId(param.getPayPlatform()))
                .payAccount(param.getPayAccount())
                .createdTime(DateUtil.milliToLocalDateTime(param.getCreatedTime()))
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .build();
    }

}
