/*
 * @Author: YKH
 * @Date: 2022-06-08 15:37:04
 * @LastEditTime: 2022-06-28 14:19:50
 * @Description: 
 * @FilePath: \cool-admin\cool-admin-midway-master\src\app\modules\shuyou\service\data_abnormal_rate.ts
 */
import { Inject, Provide } from '@midwayjs/decorator';
import { BaseService } from 'midwayjs-cool-core';
import * as _ from 'lodash';
import { Utils } from '../../../comm/utils';

/**
 * 游戏目标
 */
@Provide()
export class ShuyouAbnormalRateService extends BaseService {


  @Inject()
  utils: Utils;

  async queryAbnormalRate(new_date: Date) {
    let sql = ``
    if (this.utils.formatDate(new Date()) == this.utils.formatDate(new Date(new_date))) { //当天数据处理
      let hour = new Date().getHours()
      if (new Date().getMinutes() <= 30) {
        hour = hour - 1
      }
      sql = `
      SELECT
        a.task_id,a.task_name,a.new_date,a.num_frozen,a.num_pull_account,a.num_start_simulator,a.num_ip,a.num_start_script,a.num_start_game,
        a.num_login_success,a.num_into_game,a.num_new_success,a.num_lc_success,a.num_pay_success,a.rate_all_step,a.num_pull_pay,
        a.num_account_lc,a.num_could_pay,a.order_success_rate,
        b.name as director_name
      FROM
        data_abnormal_rate a
        LEFT JOIN shuyou_game_director b ON a.directorId = b.id 
      WHERE
        1 = 1
        ${this.setSql(hour, 'and hour = ?', hour)}
        ${this.setSql(new_date, 'and new_date = ?', this.utils.formatDate(new Date(new_date)))}
      ORDER BY task_id
      `;
    } else {
      sql = `
      SELECT
      a.task_id,a.task_name,a.new_date,a.num_frozen,a.num_pull_account,a.num_start_simulator,a.num_ip,a.num_start_script,a.num_start_game,
      a.num_login_success,a.num_into_game,a.num_new_success,a.num_lc_success,a.num_pay_success,a.rate_all_step,a.num_pull_pay,
      a.num_account_lc,a.num_could_pay,a.order_success_rate,
      b.name as director_name
      FROM
        data_abnormal_rate a
        LEFT JOIN shuyou_game_director b ON a.directorId = b.id 
      WHERE
        1 = 1
        and hour = 23
        ${this.setSql(new_date, 'and new_date = ?', this.utils.formatDate(new Date(new_date)))}
      ORDER BY task_id
      `;
    }

    return await this.nativeQuery(sql);
  }
  //查询七天数据
  async querySeven(task_id: string, new_date: Date) {
    //计算时间段
    let new_date2 = this.utils.getNextDate(new_date, -7)

    if (this.utils.formatDate(new Date()) == this.utils.formatDate(new Date(new_date))) { //今日数据因为不准确所以不查询
      new_date = new Date(this.utils.getNextDate(new_date, -1))
    }
    let sql = `
      SELECT
      a.task_id,a.task_name,a.new_date,a.num_frozen,a.num_pull_account,a.num_start_simulator,a.num_ip,a.num_start_script,a.num_start_game,
      a.num_login_success,a.num_into_game,a.num_new_success,a.num_lc_success,a.num_pay_success,a.rate_all_step,a.num_pull_pay,
      a.num_account_lc,a.num_could_pay,a.order_success_rate,
      b.name as director_name
      FROM
        data_abnormal_rate a
        LEFT JOIN shuyou_game_director b ON a.directorId = b.id 
      WHERE
        1 = 1
        and hour = 23
        ${this.setSql(task_id, 'and task_id = ?', task_id)}
        ${this.setSql(new_date, 'and new_date >= ? and new_date <= ?', [this.utils.formatDate(new Date(new_date2)), this.utils.formatDate(new Date(new_date))])}
      ORDER BY task_id,a.new_date
      `;
    return await this.nativeQuery(sql);
  }
  //查询一段时间数据
  async querySomeTime(task_id: string, new_date: Date[]) {

    if (this.utils.formatDate(new Date()) == this.utils.formatDate(new Date(new_date[1]))) { //今日数据因为不准确所以不查询
      new_date[1] = new Date(this.utils.getNextDate(new_date[1], -1))
    }
    let sql = `
      SELECT
      a.task_id,a.task_name,a.new_date,a.num_frozen,a.num_pull_account,a.num_start_simulator,a.num_ip,a.num_start_script,a.num_start_game,
      a.num_login_success,a.num_into_game,a.num_new_success,a.num_lc_success,a.num_pay_success,a.rate_all_step,a.num_pull_pay,
      a.num_account_lc,a.num_could_pay,a.order_success_rate,
      b.name as director_name
      FROM
        data_abnormal_rate a
        LEFT JOIN shuyou_game_director b ON a.directorId = b.id 
      WHERE
        1 = 1
        and hour = 23
        ${this.setSql(task_id, 'and task_id = ?', task_id)}
        ${this.setSql(new_date, 'and new_date >= ? and new_date <= ?', [this.utils.formatDate(new Date(new_date[0])), this.utils.formatDate(new Date(new_date[1]))])}
      ORDER BY task_id,a.new_date
      `;
    return await this.nativeQuery(sql);
  }

}
