package com.zanxiang.game.back.serve.rpc.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zanxiang.game.back.base.pojo.dto.TtOrderRpcDTO;
import com.zanxiang.game.back.base.pojo.dto.TtUserActiveRpcDTO;
import com.zanxiang.game.back.base.pojo.enums.OrderStatusEnum;
import com.zanxiang.game.back.base.rpc.ITtMiniGameBackRpc;
import com.zanxiang.game.back.serve.pojo.entity.GameOceanengineOrderLog;
import com.zanxiang.game.back.serve.pojo.entity.GameOceanengineUserLog;
import com.zanxiang.game.back.serve.pojo.enums.BackStatusEnum;
import com.zanxiang.game.back.serve.service.IGameOceanengineOrderLogService;
import com.zanxiang.game.back.serve.service.IGameOceanengineUserLogService;
import com.zanxiang.module.util.JsonUtil;
import com.zanxiang.module.util.pojo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

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

@Slf4j
@DubboService
public class TtMiniGameBackRpcImpl implements ITtMiniGameBackRpc {
    @Autowired
    private IGameOceanengineUserLogService gameOceanengineUserLogService;
    @Autowired
    private IGameOceanengineOrderLogService gameOceanengineOrderLogService;

    @Override
    public ResultVO<Boolean> userActiveReport(TtUserActiveRpcDTO dto) {
        log.error("头条小游戏用户回传收到：{}", JsonUtil.toString(dto));
        GameOceanengineUserLog userLog = GameOceanengineUserLog.builder()
                .appId(dto.getWechatAppId())
                .openId(dto.getWechatOpenId())
                .unionId(dto.getWechatUnionId())
                .gameId(dto.getGameId())
                .accountId(dto.getAccountReport().getAccountId())
                .accountReportToken(dto.getAccountReport().getReportToken())
                .accountReportUrl(dto.getAccountReport().getReportUrl())
                .agentKey(dto.getAgentKey())
                .clueToken(dto.getClueToken())
                .projectId(dto.getProjectId())
                .promotionId(dto.getPromotionId())
                .reqId(dto.getReqId())
                .activeTime(dto.getActiveTime())
                .backStatus(BackStatusEnum.NO.getBackStatus())
                .createTime(LocalDateTime.now())
                .build();
        gameOceanengineUserLogService.save(userLog);
        return ResultVO.ok(gameOceanengineUserLogService.callback(userLog, false));
    }

    @Override
    public ResultVO<Boolean> orderReport(TtOrderRpcDTO dto) {
        if (Objects.equals(OrderStatusEnum.SUCCESS_PAY.getValue(), dto.getOrderStatus())) {
            log.error("头条小游戏订单回传收到：{}。", JsonUtil.toString(dto));
        } else {
            log.error("头条小游戏订单回传收到：{}。订单未支付，直接过滤", JsonUtil.toString(dto));
            return ResultVO.ok(true);
        }
        GameOceanengineOrderLog orderLog = GameOceanengineOrderLog.builder()
                .appId(dto.getWechatAppId())
                .openId(dto.getWechatOpenId())
                .unionId(dto.getWechatUnionId())
                .gameId(dto.getGameId())
                .backPolicyId(dto.getBackPolicyId())
                .accountId(dto.getAccountReport().getAccountId())
                .accountReportToken(dto.getAccountReport().getReportToken())
                .accountReportUrl(dto.getAccountReport().getReportUrl())
                .agentKey(dto.getAgentKey())
                .regTime(dto.getRegTime())
                .orderNo(dto.getOrderId())
                .amount(dto.getRechargeMoney())
                .orderStatus(dto.getOrderStatus())
                .createTime(dto.getCreateTime())
                .payTime(dto.getPayTime())
                .backStatus(BackStatusEnum.NO.getBackStatus())
                .roleId(dto.getRoleId())
                .roleName(dto.getRoleName())
                .build();
        boolean isFirstOrder = gameOceanengineOrderLogService.getOne(new LambdaQueryWrapper<GameOceanengineOrderLog>()
                .eq(GameOceanengineOrderLog::getGameId, orderLog.getGameId())
                .eq(GameOceanengineOrderLog::getAppId, orderLog.getAppId())
                .eq(GameOceanengineOrderLog::getAccountId, orderLog.getAccountId())
                .eq(GameOceanengineOrderLog::getOrderStatus, OrderStatusEnum.SUCCESS_PAY.getValue())
                .eq(GameOceanengineOrderLog::getOpenId, orderLog.getOpenId())
                .last("limit 1")
        ) == null;
        orderLog.setIsFirstOrder(isFirstOrder);
        gameOceanengineOrderLogService.save(orderLog);
        return ResultVO.ok(gameOceanengineOrderLogService.callback(orderLog, false, null));
    }
}
