import { Provide, Post, Inject, Body, ALL } from '@midwayjs/decorator';
import { CoolController, BaseController } from 'midwayjs-cool-core';
import { ShuyouGameTaskEntity } from '../../entity/game_task';
import { ShuyouGameTaskService } from '../../service/game_task';
import { ILogger } from '@midwayjs/logger';
import { ShuyouGameTargetService } from '../../service/game_target';
import { ShuyouGameCompleteService } from '../../service/game_complete';
import { Utils } from '../../../../comm/utils';
import { ShuyouGameAgentService } from '../../service/game_agent';
import { ShuyouLoModeService } from '../../service/lo_mode';
import { ShuyouGameDirectorService } from '../../service/game_director';
// import * as qiNiu from 'qiniu';
/**
 * 游戏负责人
 */
@Provide()
@CoolController({
  // 添加通用CRUD接口
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  // 设置表实体
  entity: ShuyouGameTaskEntity,
  service: ShuyouGameTaskService,
  // // 向表插入当前登录用户ID
  // insertParam: async (ctx: Context) => {
  //   return {
  //     userId: ctx.admin.userId,
  //   };
  // },
  // // info接口忽略价格字段
  // infoIgnoreProperty: ['price'],

  before: ctx => {
    //前段数据转JSON格式
    const data = ctx.request.body;
    ctx.request.body.data = JSON.stringify(data);
  },
  // 分页查询配置
  pageQueryOp: {
    // 让字段支持模糊查询
    keyWordLikeFields: ['id'],
    // 让字段支持筛选
    fieldEq: ['id'],
    // 添加排序
    addOrderBy: {
      // 排序字段及排序方式
      id: 'asc',
    },
  },
})
export class ShuyouAdminGameTaskController extends BaseController {

  @Inject()
  logger: ILogger;

  @Inject()
  ShuyouGameTaskService: ShuyouGameTaskService;

  @Inject()
  ShuyouGameTargetService: ShuyouGameTargetService;

  @Inject()
  ShuyouGameCompleteService: ShuyouGameCompleteService;

  @Inject()
  ShuyouGameAgentService: ShuyouGameAgentService;

  @Inject()
  ShuyouLoModeService: ShuyouLoModeService;

  @Inject()
  ShuyouGameDirectorService: ShuyouGameDirectorService;

  @Inject()
  utils: Utils;
  /**
   *
   * 停止任务
   * @param {object} queryObject
   * @return {*} 
   * @memberof ShuyouGameTaskController
   */
  @Post('/stopTask')
  async stopTask(@Body(ALL) queryObject) {
    // this.logger.info(queryObject)
    let { id, stopTime, stopRemark, game_agentId, login_modeId, directorId } = queryObject
    let reqmsg = await this.ShuyouGameTaskService.stopTask(id, stopTime, stopRemark);
    //把stopTime的game_target表里的目标改为0，删掉之后的目标
    stopTime = this.utils.formatDate(stopTime)
    await this.ShuyouGameTargetService.updateTarget(id, stopTime);
    // this.logger.warn(reqtarget)
    // 端口对应的游戏数量-1
    // this.logger.warn(game_agentId)
    await this.ShuyouGameAgentService.reduceGameNumByname(game_agentId)
    // 登录方式对应游戏数量-1
    await this.ShuyouLoModeService.reduceGameNumByname(login_modeId)
    // 对应负责人的游戏数量-1
    await this.ShuyouGameDirectorService.reduceGameNumById(directorId)
    // this.logger.warn(returnMsg)
    //
    if (reqmsg == 1000) {
      return await this.ok({ code: 1, message: '任务已停止' });
    } else {
      return await this.fail('任务异常', -1);
    }
  }

  @Post('/startTask')
  async startTask(@Body(ALL) queryObject: ShuyouGameTaskEntity) {
    // this.logger.info(queryObject)
    let { id, game_agentId, login_modeId, directorId } = queryObject
    let reqmsg = await this.ShuyouGameTaskService.startTask(id);
    //添加任务目标
    await this.ShuyouGameTargetService.addTarget(queryObject);
    await this.ShuyouGameCompleteService.addComplete(queryObject);
    // 端口对应的游戏数量+1
    await this.ShuyouGameAgentService.addGameNumByname(game_agentId)
    // 登录方式对应游戏数量+1
    await this.ShuyouLoModeService.addGameNumByname(login_modeId)
    // 对应负责人的游戏数量+1
    await this.ShuyouGameDirectorService.addGameNumById(directorId)
    // this.logger.info(returnMsg)
    if (reqmsg == 1000) {
      return await this.ok({ code: 1, message: '任务已开启' });
    } else {
      return await this.fail('任务异常', -1);
    }
  }

  /**
   *根据任务id删除任务
   *
   * @param {*} queryObject
   * @return {*} 
   * @memberof ShuyouAdminGameTaskController
   */
  @Post('/deleteById')
  async deleteById(@Body(ALL) queryObject) {
    // this.logger.info(queryObject)
    let { id } = queryObject
    // console.log(id)
    let reqmsg = await this.ShuyouGameTaskService.deleteById(id);
    if (reqmsg == 1000) {
      return await this.ok({ code: 1, message: '任务删除成功' });
    } else {
      return await this.fail('任务删除失败', -1);
    }
  }

  @Post('/taskList')
  async taskList() {
    let aa =  await this.ShuyouGameTaskService.taskList();
    return await this.ok({ code: 1, data: aa });
  }

}
