/*
 * @Author: YKH
 * @Date: 2022-02-17 15:14:39
 * @LastEditTime: 2022-02-23 15:23:29
 * @Description: 
 * @FilePath: \cool-admin\cool-admin-midway-master\src\app\modules\shuyou\controller\app\special_game_account.ts
 */
import { Get, Inject, Provide, Query, ALL } from '@midwayjs/decorator';
import { ILogger } from '@midwayjs/logger';
import { CoolController, BaseController } from 'midwayjs-cool-core';
import { ShuyouSpecialAccountEntity } from '../../entity/special_game_account';
import { ShuyouSpecialAccountService } from '../../service/special_game_account';
import * as _ from 'lodash';
import { ShuyouSpecialAccountConsumeService } from '../../service/special_account_consume';
import { InjectEntityModel } from '@midwayjs/orm';
import { Repository } from 'typeorm';
import { ShuyouSpecialAccountConsumeEntity } from '../../entity/special_account_consume';
// import { Utils } from '../../../../comm/utils';


/**
 * 商品
 */
@Provide()
@CoolController({
  // 添加通用CRUD接口
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  // 设置表实体
  entity: ShuyouSpecialAccountEntity,

})
export class ShuyouAppSpecialAccountController extends BaseController {
  @Inject()
  ShuyouSpecialAccountService: ShuyouSpecialAccountService;

  @Inject()
  ShuyouSpecialAccountConsumeService: ShuyouSpecialAccountConsumeService;

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

  @Inject()
  logger: ILogger;

  // @Inject()
  // utils: Utils;

  /**
   *获取QQ卡信息
   *http://127.0.0.1:8001/app/shuyou/special_game_account/getQQCard?task_id=1001&unit_price=1&account=111
   *http://ykhvip.fun/api/app/shuyou/special_game_account/getQQCard?task_id=1001&unit_price=1&account=111
   * @param {object} queryObject
   * @return {*} 
   * @memberof ShuyouAppSpecialAccountController
   */
  @Get('/getQQCard')
  async getQQCard(@Query(ALL) queryObject) {
    let { task_id, unit_price, account } = queryObject
    //unit_price付费单价
    //task_id任务id
    if (!unit_price || !task_id || !account) {
      return this.fail('缺少必要参数[task_id=任务id&unit_price=付费单价&account=账号]', -1)
    }
    const amount = parseFloat(unit_price)
    if (isNaN(amount)) {
      return this.fail('unit_price参数错误，必须为数字', -1)
    }
    const list = await this.ShuyouSpecialAccountService.getQQCard(unit_price);
    if (list.code == 1000) {
      const inserRecord = await this.ShuyouSpecialAccountConsumeEntity.createQueryBuilder()
        .insert()
        .into(ShuyouSpecialAccountConsumeEntity)
        .values({
          card_id: list.card_id,
          consume: unit_price,
          occupyTime: new Date(),
          occupy_task: task_id,
          occupy_acc: account,
          status: 2
        })
        .execute();
      let consume_id = ''
      if (!inserRecord) {
        return this.fail('插入记录表失败', -1)
      } else {
        consume_id = inserRecord.identifiers[0].id
      }
      let ret = {
        card_id: list.card_id,
        consume_id: consume_id,
        card_text: list.card_text,
        card_password: list.card_password,
        amount_balance: list.amount_balance
      }
      return this.ok(ret);
    } else {
      return this.fail('无符合条件的QQ卡', -1);
    }
  }

  /**
   *修改QQ卡余额以及状态{unit_price:单价，为0时表示余额不足}
   *http://127.0.0.1:8001/app/shuyou/special_game_account/updateQQCard?card_id=&consume_id=&unit_price=
   *http://ykhvip.fun/api/app/shuyou/special_game_account/updateQQCard?card_id=&consume_id=&unit_price=
   * @param {object} queryObject
   * @return {*} 
   * @memberof ShuyouAppSpecialAccountController
   */
  @Get('/updateQQCard')
  async updateQQCard(@Query(ALL) queryObject) {
    const { card_id, consume_id, unit_price } = queryObject
    const state = await this.ShuyouSpecialAccountService.updateRow(card_id, unit_price);
    if (state == 1000 || state == 5) {
      await this.ShuyouSpecialAccountConsumeService.updateRow(consume_id, state);
      return this.ok('修改成功');
    } else {
      return this.fail('修改失败', -1);
    }

  }


}
