package com.zanxiang.game.platform.serve.service.platform;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.zanxiang.game.platform.serve.enums.GamePlatformEnum;
import com.zanxiang.game.platform.serve.pojo.entity.GamePlatform;
import com.zanxiang.game.platform.serve.pojo.entity.GamePlatformAccount;
import com.zanxiang.game.platform.serve.service.IGamePlatformAccountService;
import com.zanxiang.game.platform.serve.service.IGamePlatformService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author : lingfeng
 * @time : 2023-05-24
 * @description : 超类
 */
@Slf4j
public abstract class PlatformBaseService {

    @Lazy
    @Autowired
    private IGamePlatformService getByPlatformKey;

    @Lazy
    @Autowired
    private IGamePlatformAccountService gamePlatformAccountService;

    public void syncPlatformOrder(List<Long> accountIds, LocalDateTime startDate, LocalDateTime endDate) {
        GamePlatform gamePlatform = getByPlatformKey.getByPlatformKey(getGamePlatformEnum().getPlatformKey());
        List<GamePlatformAccount> gamePlatformAccountList;
        //未指定账号id
        if (CollectionUtils.isEmpty(accountIds)) {
            gamePlatformAccountList = gamePlatformAccountService.list(new LambdaQueryWrapper<GamePlatformAccount>()
                    .eq(GamePlatformAccount::getPlatformKey, gamePlatform.getPlatformKey())
                    .eq(GamePlatformAccount::getStatus, GamePlatformAccount.USE_STATUS));
        } else {
            //指定账号id
            gamePlatformAccountList = gamePlatformAccountService.listByIds(accountIds);
        }
        for (GamePlatformAccount account : gamePlatformAccountList) {
            try {
                syncOrderByAccount(gamePlatform, account, startDate, endDate);
            } catch (Exception e) {
                log.error("同步游戏平台订单失败！accountId: {}, error : {}", account.getId(), e.getMessage());
            }
        }
    }

    public void syncPlatformUser(List<Long> accountIds, LocalDateTime startDate, LocalDateTime endDate) {
        GamePlatform gamePlatform = getByPlatformKey.getByPlatformKey(getGamePlatformEnum().getPlatformKey());
        List<GamePlatformAccount> gamePlatformAccountList;
        //指定账号id
        if (CollectionUtils.isNotEmpty(accountIds)) {
            gamePlatformAccountList = gamePlatformAccountService.listByIds(accountIds);
        } else {
            //未指定账号id
            gamePlatformAccountList = gamePlatformAccountService.list(new LambdaQueryWrapper<GamePlatformAccount>()
                    .eq(GamePlatformAccount::getPlatformKey, gamePlatform.getPlatformKey())
                    .eq(GamePlatformAccount::getStatus, GamePlatformAccount.USE_STATUS));
        }
        for (GamePlatformAccount account : gamePlatformAccountList) {
            try {
                syncUserByAccount(gamePlatform, account, startDate, endDate);
            } catch (Exception e) {
                log.error("同步游戏平台用户失败！accountId: {}, error : {}", account.getId(), e.getMessage());
            }
        }
    }

    /**
     * 获取游戏平台枚举信息
     *
     * @return {@link GamePlatformEnum}
     */
    public abstract GamePlatformEnum getGamePlatformEnum();

    /**
     * 游戏平台订单同步
     *
     * @param account      账户
     * @param startDate    开始日期
     * @param endDate      结束日期
     * @param gamePlatform 游戏平台
     */
    public abstract void syncUserByAccount(GamePlatform gamePlatform, GamePlatformAccount account, LocalDateTime startDate, LocalDateTime endDate);

    /**
     * 游戏平台同步用户
     *
     * @param account      账户
     * @param startDate    开始日期
     * @param endDate      结束日期
     * @param gamePlatform 游戏平台
     */
    public abstract void syncOrderByAccount(GamePlatform gamePlatform, GamePlatformAccount account, LocalDateTime startDate, LocalDateTime endDate);
}
