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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zanxiang.game.back.base.pojo.dto.TencentOrderDTO;
import com.zanxiang.game.back.base.pojo.dto.TencentUserDTO;
import com.zanxiang.game.back.base.pojo.enums.OrderStatusEnum;
import com.zanxiang.game.back.base.rpc.ITencentMiniGameBackRpc;
import com.zanxiang.game.back.serve.pojo.entity.GameTencentMiniGameOrder;
import com.zanxiang.game.back.serve.pojo.entity.GameTencentMiniGameUser;
import com.zanxiang.game.back.serve.pojo.enums.BackStatusEnum;
import com.zanxiang.game.back.serve.service.IGameTencentMiniGameOrderService;
import com.zanxiang.game.back.serve.service.IGameTencentMiniGameUserService;
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 TencentMiniGameBackRpcImpl implements ITencentMiniGameBackRpc {

    @Autowired
    private IGameTencentMiniGameOrderService gameTencentMiniGameOrderService;
    @Autowired
    private IGameTencentMiniGameUserService gameTencentMiniGameUserService;

    @Override
    public ResultVO<Boolean> backOrder(TencentOrderDTO 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);
        }
        boolean isFirstOrder = gameTencentMiniGameOrderService.getOne(new LambdaQueryWrapper<GameTencentMiniGameOrder>()
                .select(GameTencentMiniGameOrder::getOrderId)
                .eq(GameTencentMiniGameOrder::getGameId, dto.getGameId())
                .eq(GameTencentMiniGameOrder::getWechatAppId, dto.getWechatAppId())
                .eq(GameTencentMiniGameOrder::getAdAccountId, dto.getAdAccountId())
                .eq(GameTencentMiniGameOrder::getOrderStatus, OrderStatusEnum.SUCCESS_PAY.getValue())
                .eq(GameTencentMiniGameOrder::getWechatOpenid, dto.getWechatOpenid())
                .last("limit 1")
        ) == null;
        GameTencentMiniGameOrder orderLog = GameTencentMiniGameOrder.builder()
                .adAccountId(dto.getAdAccountId())
                .gameId(dto.getGameId())
                .orderId(dto.getOrderId())
                .agentKey(dto.getChannel())
                .rechargeMoney(dto.getRechargeMoney())
                .rechargeTime(dto.getRechargeTime())
                .subscribeTime(dto.getSubscribeTime())
                .registerTime(dto.getRegisterTime())
                .wechatAppId(dto.getWechatAppId())
                .wechatOpenid(dto.getWechatOpenid())
                .orderStatus(dto.getOrderStatus())
                .payTime(dto.getPayTime())
                .createTime(LocalDateTime.now())
                .backStatus(BackStatusEnum.NO.getBackStatus())
                .backPolicyId(dto.getBackPolicyId())
                .clickId(dto.getClickId())
                .roleId(dto.getRoleId())
                .roleName(dto.getRoleName())
                .isFirstOrder(isFirstOrder)
                .build();
        gameTencentMiniGameOrderService.save(orderLog);
        return ResultVO.ok(gameTencentMiniGameOrderService.orderBack(orderLog, false, null));
    }

    @Override
    public ResultVO<Boolean> backUser(TencentUserDTO dto) {
        log.error("腾讯小游戏用户回传收到：{}", JsonUtil.toString(dto));
        GameTencentMiniGameUser userLog = GameTencentMiniGameUser.builder()
                .adAccountId(dto.getAdAccountId())
                .gameId(dto.getGameId())
                .agentKey(dto.getChannel())
                .subscribeTime(dto.getSubscribeTime())
                .registerTime(dto.getRegisterTime())
                .wechatAppId(dto.getWechatAppId())
                .wechatOpenid(dto.getWechatOpenid())
                .backStatus(BackStatusEnum.NO.getBackStatus())
                .clickId(dto.getClickId())
                .createTime(LocalDateTime.now())
                .build();
        gameTencentMiniGameUserService.save(userLog);
        return ResultVO.ok(gameTencentMiniGameUserService.userBack(userLog, false));
    }
}
