/*
 * @Author: YKH
 * @Date: 2021-12-10 09:04:02
 * @LastEditTime: 2022-09-20 21:18:16
 * @Description: 
 * @FilePath: \cool-admin-3.x\cool-admin-midway-master\src\app\modules\shuyou\controller\admin\data_active.ts
 */
import { Provide, Inject, Post, Body, ALL } from '@midwayjs/decorator';
import { CoolController, BaseController } from 'midwayjs-cool-core';
import { Utils } from '../../../../comm/utils';
import { ShuyouDataActiveEntity } from '../../entity/data_active';
import { ShuyouDataActiveService } from '../../service/data_active';
import { ILogger } from '@midwayjs/logger';

/**
 * 游戏负责人
 */
@Provide()
@CoolController({
  // 添加通用CRUD接口
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  // 设置表实体
  entity: ShuyouDataActiveEntity,
  service: ShuyouDataActiveService,

  before: ctx => {
    //前段数据转JSON格式
    const data = ctx.request.body;
    console.log(data.agentOrDirector)
    if (data.agentOrDirector == undefined) {
      data.agentOrDirector = 0
    }
    if (typeof (data.new_date) == "undefined") {
      data.new_date = [new Date(), new Date()]
    }
    ctx.request.body.data = JSON.stringify(data);
  },
  // 分页查询配置
  // pageQueryOp: {
  //   // 让字段支持模糊查询
  //   keyWordLikeFields: ['directorName'],
  //   // 让字段支持筛选
  //   fieldEq: ['directorName'],
  //   // 添加排序
  //   addOrderBy: {
  //     // 排序字段及排序方式
  //     id: 'desc',
  //   },
  // },
})
export class ShuyouDataActiveController extends BaseController {

  @Inject()
  utils: Utils;

  @Inject()
  logger: ILogger;

  @Inject()
  ShuyouDataActiveService: ShuyouDataActiveService;


  @Post('/queryByDay')
  async queryByDay() {
    let new_date = this.utils.formatDate(new Date())
    let yest_date = this.utils.formatDate(new Date().getTime() - 24 * 60 * 60 * 1000)
    let today = await this.ShuyouDataActiveService.queryByDay(new_date);
    let yesterday = await this.ShuyouDataActiveService.queryByDay(yest_date);
    let today_sum_new = 0
    let today_sum_pay = 0
    let today_sum_active = 0
    let yest_sum_new = 0
    let yest_sum_pay = 0
    let yest_sum_active = 0
    let growth_new = '0%'
    let growth_pay = '0%'
    let growth_active = '0%'
    if(today.length == 0){
      return { code: -1, message: '未查询到今日数据' };
    }
    if (today[0].new_date) {
      today_sum_new = today[0].sum_new
      today_sum_pay = today[0].sum_pay
      today_sum_active = today[0].sum_active
    } else {
      return { code: -1, message: '未查询到今日数据' };
    }
    if (yesterday[0].new_date) {
      yest_sum_new = yesterday[0].sum_new
      yest_sum_pay = yesterday[0].sum_pay
      yest_sum_active = yesterday[0].sum_active
      growth_new = Number(((today_sum_new - yest_sum_new) / yest_sum_new) * 100).toFixed(2);
      growth_pay = Number(((today_sum_pay - yest_sum_pay) / yest_sum_pay) * 100).toFixed(2);
      growth_active = Number(((today_sum_active - yest_sum_active) / yest_sum_active) * 100).toFixed(2);
    }
    let res = {
      today: new_date,
      today_sum_new: today_sum_new,
      today_sum_pay: today_sum_pay,
      today_sum_active: today_sum_active,
      yest_sum_new: yest_sum_new,
      yest_sum_pay: yest_sum_pay,
      yest_sum_active: yest_sum_active,
      growth_new: growth_new,
      growth_pay: growth_pay,
      growth_active: growth_active
    }
    // this.logger.warn(res)
    return await this.ok({ code: 1, data: res });
  }
  /**
   *
   * 获取月数据
   * @return {*} 
   * @memberof ShuyouDataActiveController
   */
  @Post('/queryByMonth')
  async queryByMonth() {
    let monthData = await this.ShuyouDataActiveService.queryByMonth();
    if (!monthData) {
      return { code: -1, message: '未查询到数据' };
    }
    // this.logger.warn(monthData)
    return await this.ok({ code: 1, data: monthData });
  }
  /**
   *
   * 查询今日操作次数
   * @return {*} 
   * @memberof ShuyouDataActiveController
   */
  @Post('/queryOperationCount')
  async queryOperationCount() {
    let operationCount = await this.ShuyouDataActiveService.queryOperationCount();
    if (!operationCount) {
      return { code: -1, message: '未查询到数据' };
    }
    return await this.ok({ code: 1, data: operationCount });
  }
  /**
   *
   * 查询今日IP访问次数
   * @return {*} 
   * @memberof ShuyouDataActiveController
   */
  @Post('/queryIpCount')
  async queryIpCount() {
    let ipCount = await this.ShuyouDataActiveService.queryIpCount();
    if (!ipCount) {
      return { code: -1, message: '未查询到数据' };
    }
    return await this.ok({ code: 1, data: ipCount });
  }

  /**
   *
   *异常统计-根据负责人和未完成原因
   * @param {*} queryObject
   * @return {*} 
   * @memberof ShuyouDataActiveController
   */
  @Post('/queryAbnormalStatisticsByDirectorReason')
  async queryAbnormalStatisticsByDirectorReason(@Body(ALL) queryObject: any) {
    // console.log('异常统计-根据负责人和未完成原因')
    let { date } = queryObject
    if (typeof (date) == "undefined") {
      date = [new Date(), new Date()]
    }
    let queryAbnormalStatisticsByDirectorReason = await this.ShuyouDataActiveService.queryAbnormalStatisticsByDirectorReason(date);
    // console.log(queryAbnormalStatisticsByDirectorReason)
    if (!queryAbnormalStatisticsByDirectorReason) {
      return { code: -1, message: '未查询到数据' };
    }
    return await this.ok({ code: 1, data1: queryAbnormalStatisticsByDirectorReason });
  }
  //
  @Post('/queryGameCountByDirectorGameAgent')
  async queryGameCountByDirectorGameAgent(@Body(ALL) queryObject: any) {
    let { date } = queryObject
    if (typeof (date) == "undefined") {
      date = [new Date(), new Date()]
    }
    let queryGameCountByDirectorGameAgent = await this.ShuyouDataActiveService.queryGameCountByDirectorGameAgent(date);
    // console.log(queryGameCountByDirectorGameAgent)
    if (!queryGameCountByDirectorGameAgent) {
      return { code: -1, message: '未查询到数据' };
    }
    let queryGameCountByDirector = await this.ShuyouDataActiveService.queryGameCountByDirector(date);
    // console.log(queryGameCountByDirector)
    if (!queryGameCountByDirector) {
      return { code: -1, message: '未查询到数据' };
    }
    let queryGameCountByGameAgent = await this.ShuyouDataActiveService.queryGameCountByGameAgent(date);
    // console.log(queryGameCountByGameAgent)
    if (!queryGameCountByGameAgent) {
      return { code: -1, message: '未查询到数据' };
    }
    let queryGameCountByDirectorReal = await this.ShuyouDataActiveService.queryGameCountByDirectorReal(date);
    // console.log(queryGameCountByGameAgent)
    if (!queryGameCountByDirectorReal) {
      return { code: -1, message: '未查询到数据' };
    }
    return await this.ok({ code: 1, data1: queryGameCountByDirectorGameAgent, data2: queryGameCountByDirector, data3: queryGameCountByGameAgent, data4: queryGameCountByDirectorReal });
  }
  /**
   *查询今日游戏付费top5
   *
   * @return {*} 
   * @memberof ShuyouDataActiveController
   */
  @Post('/queryTopFive')
  async queryTopFive() {
    let topFive = await this.ShuyouDataActiveService.queryTopFive();
    if (!topFive) {
      return { code: -1, message: '未查询到数据' };
    }
    return await this.ok({ code: 1, data: topFive });
  }

}
