
import { Inject, Provide } from '@midwayjs/decorator';
import { BaseService, CoolCommException } from 'midwayjs-cool-core';
import * as _ from 'lodash';
import { InjectEntityModel } from '@midwayjs/orm';
import { ShuyouGameTaskEntity } from '../entity/game_task';
import { Utils } from '../../../comm/utils';
import { Repository } from 'typeorm';
const axios = require('axios')

@Provide()
export class ShuyouXJFUtilsService extends BaseService {

  @InjectEntityModel(ShuyouGameTaskEntity)
  ShuyouGameTaskEntity: Repository<ShuyouGameTaskEntity>;

  @Inject()
  utils: Utils;

  /**
   * 新增任务
   * @param {object} object
   * @memberof ShuyouXJFUtilsService
   */
  async createXJF(object: object) {
    var pm = new Promise(function (resolve, reject) {
      axios.post('http://xjf.lianyou.fun:8099/v1/task_statistics',
        object
      )
        .then((res: string) => {
          // console.log(res)
          resolve(res);
        })
        .catch((error: any) => {
          resolve(error)
          // reject(error)
        })
    });
    return pm;
  }
  /**
   * 修改任务
   * @param {object} object
   * @memberof ShuyouXJFUtilsService
   */
  async updateXJF(xjf_id: number, object: object) {
    var pm = new Promise(function (resolve, reject) {
      axios.put('http://xjf.lianyou.fun:8099/v1/task_statistics/' + xjf_id,
        object
      )
        .then((res: string) => {
          resolve(res);
        })
        .catch((error: any) => {
          // console.error(error)
          resolve(error)
        })
    });
    return pm;
  }
  /**
     * 删除任务
     * @param {xjf_id} number
     * @memberof ShuyouXJFUtilsService
     */
  async deleteXJF(xjf_id: number) {
    var pm = new Promise(function (resolve, reject) {
      axios.delete('http://xjf.lianyou.fun:8099/v1/task_statistics/' + xjf_id)
        .then((res: string) => {
          resolve(res);
        })
        .catch((error: any) => {
          // console.error(error)
          resolve(error)
        })
    });
    return pm;
  }
  /**
     * 开始任务
     * @param {game_id} number
     * @memberof ShuyouXJFUtilsService
     */
  async startXJF(game_id: number) {
    // console.log(game_id)
    var pm = new Promise(function (resolve, reject) {
      axios.get('http://xjf.lianyou.fun:8099/v1/install_info/updateinstallstatus?status=1&game_id=' + game_id
      )
        .then((res: string) => {
          // console.log(res)
          resolve(res);
        })
        .catch((error: any) => {
          resolve(error)
          // reject(error)
        })
    });
    return pm;
  }
  /**
     * 暂停任务
     * @param {game_id} number
     * @memberof ShuyouXJFUtilsService
     */
  async stopXJF(game_id: number) {
    var pm = new Promise(function (resolve, reject) {
      axios.get('http://xjf.lianyou.fun:8099/v1/install_info/updateinstallstatus?status=0&game_id=' + game_id
      )
        .then((res: string) => {
          // console.log(res)
          resolve(res);
        })
        .catch((error: any) => {
          resolve(error)
          // reject(error)
        })
    });
    return pm;
  }

  //根据gameid获取新机房信息
  async queryXjfId(game_id: number) {
    var pm = new Promise(function (resolve, reject) {
      axios.get('http://xjf.lianyou.fun:8099/v1/task_statistics/get_data_id?game_id=' + game_id
      )
        .then((res: string) => {
          // console.log(res)
          resolve(res);
        })
        .catch((error: any) => {
          resolve(-1)
          // reject(error)
        })
    });
    return pm;
  }

  //重新下发今日付费未成功的账号以及重置留存账号
  async resetPay(game_id: number) {
    var pm = new Promise(function (resolve, reject) {
      axios.get('http://xjf.lianyou.fun:8099/v1/task_statistics/rerun_retained?game_id=' + game_id
      )
        .then((res: string) => {
          // console.log(res)
          resolve(res);
        })
        .catch((error: any) => {
          resolve(error)
          // reject(error)
        })
    });
    return pm;
  }

  /**
   * xjf上传日志到机房
   * @param {string} game_id
   * @param {string} account
   * @param {string} action
   * @param {string} action_result
   * @param {string} action_state
   * @return {*} 
   * @memberof ShuyouXJFUtilsService
   */
  async uploadLog(game_id: string, account: string, action: string, action_result: string, action_state: string) {
    const url = 'http://xjf.lianyou.fun:8099/v1/device/setAccountLog?account=' + account + '&action=' + action + '&action_result=' + action_result + '&action_state=' + action_state + '&game_id=' + game_id
    // console.log(url)
    var http = require('http');
    var pm = new Promise(function (resolve, reject) {
      http.get(url, function (res) {
        var html = '';
        res.on('data', function (d) {
          html += d.toString()
        });
        res.on('end', function () {
          resolve(html);
        });
      }).on('error', function (e) {
        // reject(e)
        resolve(-1)
      });
    });
    return pm;
  }

  //TODO
  /**
   * 上传给机房后台QQ卡消耗记录
   * @param {string} game_id
   * @param {string} card_account
   * @param {string} card_balance
   * @return {*} 
   * @memberof ShuyouXJFUtilsService
   */
  async uploadQQconsume(game_id: string, card_account: string, card_balance: string) {
    //上传手动做付费的QQ卡
    // xjf.lianyou.fun:8099/v1/device/qq_card_hand?game_id=游戏ID&money=付费金额 &card_number=QQ卡号
    const url = 'http://xjf.lianyou.fun:8099/v1/device/qq_card_hand?game_id=' + game_id + '&card_number=' + card_account + '&money=' + card_balance
    // console.log(url)
    var http = require('http');
    var pm = new Promise(function (resolve, reject) {
      http.get(url, function (res) {
        var html = '';
        res.on('data', function (d) {
          html += d.toString()
        });
        res.on('end', function () {
          resolve({ code: 1, data: html });
        });
      }).on('error', function (error) {
        // reject(e)
        resolve({ code: -1, data: error })
      });
    });
    return pm;
  }

  //查询当前付费失败的账号//TODO
  async payFailed(task_id: string) {
    // console.log(task_id)
    const existsTask = await this.ShuyouGameTaskEntity.findOne({
      id: task_id,
    });
    if (_.isEmpty(existsTask)) {
      throw new CoolCommException('未查询到game_id~~~');
    }
    // console.log(existsTask)
    const game_id = existsTask.xjf_id
    const new_date = this.utils.formatDate(new Date())
    //
    //小绵羊游戏获取付费失败http://ss.kfzs.com/v1/web/system_add_user/bi_config/unused_account?game_id=1000605&day=2022-06-27
    var http = require('http');
    var url = 'http://xjf.lianyou.fun:8099/v1/device/get_log_by_game?date=' + new_date + '&action=付费失败账号&action_result=占位&game_id=' + game_id
    var pm = new Promise(function (resolve, reject) {
      http.get(url, function (res) {
        var html = '';
        res.on('data', function (d) {
          html += d.toString()
        });
        res.on('end', function () {
          resolve({ code: 1, data: html });
        });
      }).on('error', function (error) {
        // reject(e)
        resolve({ code: -1, data: error })
      });
    });
    return pm;
  }
  //查询今日可付费账号
  async unpaid(task_id: string) {
    // console.log(task_id)
    const existsTask = await this.ShuyouGameTaskEntity.findOne({
      id: task_id,
    });
    if (_.isEmpty(existsTask)) {
      throw new CoolCommException('未查询到game_id~~~');
    }
    // console.log(existsTask)
    const game_id = existsTask.xjf_id
    const new_date = this.utils.formatDate(new Date())
    //
    var http = require('http');
    var url = 'http://xjf.lianyou.fun:8099/v1/device/get_log_by_game?date=' + new_date + '&action=付费账号&action_result=占位&game_id=' + game_id
    var pm = new Promise(function (resolve, reject) {
      http.get(url, function (res) {
        var html = '';
        res.on('data', function (d) {
          html += d.toString()
        });
        res.on('end', function () {
          resolve({ code: 1, data: html });
        });
      }).on('error', function (error) {
        // reject(e)
        resolve({ code: -1, data: error })
      });
    });
    return pm;
  }

  //手动充值-魅族
  async handAddPayMz(xjf_id: string, account: string, pay_price: number) {
    //
    var http = require('http');
    var url = 'http://xjf.lianyou.fun:8099/v1/device/setAccountLog?account=' + account + '&action=付费充值&action_result=魅族加币&game_id=' + xjf_id + '&money=' + pay_price
    var pm = new Promise(function (resolve, reject) {
      http.get(url, function (res) {
        var html = '';
        res.on('data', function (d) {
          html += d.toString()
        });
        res.on('end', function () {
          resolve({ code: 1, data: html });
        });
      }).on('error', function (error) {
        // reject(e)
        resolve({ code: -1, data: error })
      });
    });
    return pm;
  }
  //强制手动充值-魅族
  async forceAddPayMz(xjf_id: string, account: string, pay_price: number) {
    //
    var http = require('http');
    var url = 'http://xjf.lianyou.fun:8099/v1/device/setAccountLog?account=' + account + '&action=付费充值&action_result=魅族加币无限制&game_id=' + xjf_id + '&money=' + pay_price
    var pm = new Promise(function (resolve, reject) {
      http.get(url, function (res) {
        var html = '';
        res.on('data', function (d) {
          html += d.toString()
        });
        res.on('end', function () {
          resolve({ code: 1, data: html });
        });
      }).on('error', function (error) {
        // reject(e)
        resolve({ code: -1, data: error })
      });
    });
    return pm;
  }


}

