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


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zanxiang.game.back.base.pojo.dto.TencentOrderDTO;
import com.zanxiang.game.back.base.pojo.dto.TencentRoleRegisterRpcDTO;
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.ITencentUserActionBackRpc;
import com.zanxiang.game.back.serve.pojo.entity.GameTencentOrder;
import com.zanxiang.game.back.serve.pojo.entity.GameTencentRoleRegister;
import com.zanxiang.game.back.serve.pojo.entity.GameTencentUser;
import com.zanxiang.game.back.serve.pojo.enums.BackStatusEnum;
import com.zanxiang.game.back.serve.service.IGameTencentOrderService;
import com.zanxiang.game.back.serve.service.IGameTencentRoleRegisterService;
import com.zanxiang.game.back.serve.service.IGameTencentUserService;
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 TencentUserActionBackRpcImpl implements ITencentUserActionBackRpc {

    @Autowired
    private IGameTencentOrderService gameTencentOrderService;
    @Autowired
    private IGameTencentUserService gameTencentUserService;
    @Autowired
    private IGameTencentRoleRegisterService gameTencentRoleRegisterService;

    @Override
    public ResultVO<Boolean> backOrder(TencentOrderDTO dto) {
        if (Objects.equals(OrderStatusEnum.SUCCESS_PAY.getValue(), dto.getOrderStatus())) {
            log.error("腾讯H5订单回传收到：{}。", JsonUtil.toString(dto));
        } else {
            log.error("腾讯H5订单回传收到：{}。订单未支付，直接过滤", JsonUtil.toString(dto));
            return ResultVO.ok(true);
        }
        boolean isFirstOrder = gameTencentOrderService.getOne(new LambdaQueryWrapper<GameTencentOrder>()
                .select(GameTencentOrder::getOrderId)
                .eq(GameTencentOrder::getGameId, dto.getGameId())
                .eq(GameTencentOrder::getAdAccountId, dto.getAdAccountId())
                .eq(GameTencentOrder::getWechatAppId, dto.getWechatAppId())
                .eq(GameTencentOrder::getWechatOpenid, dto.getWechatOpenid())
                .eq(GameTencentOrder::getOrderStatus, OrderStatusEnum.SUCCESS_PAY.getValue())
                .last("limit 1")
        ) == null;
        //订单保存
        GameTencentOrder gameTencentOrder = GameTencentOrder.builder()
                .adAccountId(dto.getAdAccountId())
                .gameId(dto.getGameId())
                .orderId(dto.getOrderId())
                .channel(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())
                .isBack(BackStatusEnum.NO.getBackStatus())
                .backPolicyId(dto.getBackPolicyId())
                .userActionSetId(dto.getUserActionSetId())
                .roleId(dto.getRoleId())
                .roleName(dto.getRoleName())
                .isFirstOrder(isFirstOrder)
                .build();
        gameTencentOrderService.save(gameTencentOrder);
        return ResultVO.ok(gameTencentOrderService.orderBack(gameTencentOrder));
    }

    @Override
    public ResultVO<Boolean> backUser(TencentUserDTO dto) {
        log.error("腾讯H5用户回传收到：{}", JsonUtil.toString(dto));
        GameTencentUser gameTencentUser = GameTencentUser.builder()
                .adAccountId(dto.getAdAccountId())
                .gameId(dto.getGameId())
                .channel(dto.getChannel())
                .subscribeTime(dto.getSubscribeTime())
                .registerTime(dto.getRegisterTime())
                .wechatAppId(dto.getWechatAppId())
                .wechatOpenid(dto.getWechatOpenid())
                .isBack(BackStatusEnum.NO.getBackStatus())
                .userActionSetId(dto.getUserActionSetId())
                .createTime(LocalDateTime.now())
                .build();
        gameTencentUserService.save(gameTencentUser);
        return ResultVO.ok(true);
        // 激活现在默认不回传了，等创角的时候一起回传
        // return ResultVO.ok(gameTencentUserService.userBack(gameTencentUser, false));
    }

    @Override
    public ResultVO<Boolean> backRoleRegister(TencentRoleRegisterRpcDTO dto) {
        log.error("腾讯H5创角回传收到：{}", JsonUtil.toString(dto));
        GameTencentRoleRegister roleRegister;
        GameTencentRoleRegister oldRole = gameTencentRoleRegisterService.getOne(new LambdaQueryWrapper<GameTencentRoleRegister>()
                .eq(GameTencentRoleRegister::getGameId, dto.getGameId())
                .eq(GameTencentRoleRegister::getWechatAppId, dto.getWechatAppId())
                .eq(GameTencentRoleRegister::getWechatOpenid, dto.getWechatOpenid())
                .eq(GameTencentRoleRegister::getRoleId, dto.getRoleId())
                .last("limit 1")
        );
        if (oldRole != null) {
            roleRegister = oldRole;
            if (dto.getRoleLevel() != null && (oldRole.getRoleLevel() == null || oldRole.getRoleLevel() < dto.getRoleLevel())) {
                gameTencentRoleRegisterService.update(new LambdaUpdateWrapper<GameTencentRoleRegister>()
                        .set(GameTencentRoleRegister::getRoleLevel, dto.getRoleLevel())
                        .set(GameTencentRoleRegister::getRoleName, dto.getRoleName())
                        .eq(GameTencentRoleRegister::getId, oldRole.getId())
                );
            }
            if (Objects.equals(oldRole.getIsBack(), BackStatusEnum.SUCCESS.getBackStatus())) {
                // 创角已回传。不重复传了
                return ResultVO.ok(true);
            }
        } else {
            roleRegister = GameTencentRoleRegister.builder()
                    .backPolicyId(dto.getBackPolicyId())
                    .channel(dto.getChannel())
                    .gameId(dto.getGameId())
                    .adAccountId(dto.getAdAccountId())
                    .registerTime(dto.getRegisterTime())
                    .wechatAppId(dto.getWechatAppId())
                    .wechatOpenid(dto.getWechatOpenid())
                    .isBack(BackStatusEnum.NO.getBackStatus())
                    .userActionSetId(dto.getUserActionSetId())
                    .createTime(LocalDateTime.now())
                    .roleId(dto.getRoleId())
                    .roleName(dto.getRoleName())
                    .roleLevel(dto.getRoleLevel())
                    .build();
            gameTencentRoleRegisterService.save(roleRegister);
        }
        return ResultVO.ok(gameTencentRoleRegisterService.roleRegisterBack(roleRegister));
    }
}
