/*
 * @Author: YKH
 * @Date: 2021-12-22 13:59:30
 * @LastEditTime: 2022-01-24 13:23:46
 * @Description: 
 * @FilePath: \cool-admin\cool-admin-midway-master\src\app\modules\shuyou\service\game_account.ts
 */
import { Inject, Provide } from '@midwayjs/decorator';
import { BaseService } from 'midwayjs-cool-core';
import { InjectEntityModel } from '@midwayjs/orm';
import { Repository } from 'typeorm';
import { ShuyouGameAccountEntity } from '../entity/game_account';
import * as _ from 'lodash';
import { Utils } from '../../../comm/utils';

/**
 * 
 */
@Provide()
export class ShuyouGameAccountService extends BaseService {
  @InjectEntityModel(ShuyouGameAccountEntity)
  ShuyouGameAccountEntity: Repository<ShuyouGameAccountEntity>;

  @Inject()
  utils: Utils;
  /***
   * 批量插入数据库
   */
  async batchAdd(id_card: any) {
    let idCard = id_card
    // const returnMsg = await this.ShuyouGameAccountEntity
    //   .createQueryBuilder()
    //   .insert()
    //   .into(ShuyouGameAccountEntity)
    //   .values(idCard)
    //   .execute();
    const sqlBatchAdd = `
      insert 
        IGNORE into 
        shuyou_game_account (account_text,
          password_text,
          vpn_text,
          manufacturer,
          model,
          pnumber,
          imei,
          imsi,
          simserial,
          androidid,
          mac,
          type_id,
          status,
          remark) 
        values
    `+ idCard;
    return await this.nativeQuery(sqlBatchAdd);
  }

  /**
    * 重写分页查询
    * @param query
    */
  async page(query) {
    const { type_name, account_text, password_text } = query;
    const sql = `
        SELECT
          a.id,a.createTime,a.updateTime,a.remark,a.account_text,a.password_text,a.vpn_text,a.manufacturer,a.model,a.pnumber,a.imei,
          a.imsi,a.simserial,a.androidid,a.mac,a.status,a.frozenTime,a.getTime,a.occupy_taskId,a.releaseTime,a.type_id,
          b.name AS type_name
        FROM
        shuyou_game_account a
          LEFT JOIN shuyou_account_type b ON a.type_id = b.id
        WHERE 1 = 1
          ${this.setSql(type_name, 'and b.name = ?', [type_name])}
          ${this.setSql(account_text, 'and a.account_text LIKE ?', [`%${account_text}%`])}
          ${this.setSql(password_text, 'and a.account_text LIKE ?', [`%${password_text}%`])}
      `;
    return this.sqlRenderPage(sql, query);
  }

  /**
   *
   * 修改账号被占用的任务id
   * @param {number} accountId
   * @param {string} task_id
   * @return {*} 
   * @memberof ShuyouGameAccountService
   */
  async updateOccupy(accountId: number, task_id: string) {
    const updateResult = await this.ShuyouGameAccountEntity
      .createQueryBuilder()
      .update()
      .set({
        occupy_taskId: task_id,
        getTime: new Date()
      })
      .where('id = :id', {
        id: accountId
      })
      .execute();
    if (updateResult.affected >= 1) {
      return { code: 1, message: '修改成功!' }
    } else {
      return { code: -1, message: '修改失败!' }
    }
  }

  /**
   *
   * 修改GameAccount表账号状态
   * @param {*} account_id
   * @param {number} status 0.正常 1.冻结 2.密错 3.禁用 5.人脸验证
   * @return {*} 
   * @memberof ShuyouGameAccountService
   */
  async updateAccountState(account_id, status: number) {
    // status 0.正常 1.冻结 2.密错 3.禁用 5.人脸验证
    const exists = await this.ShuyouGameAccountEntity.findOne({
      id: account_id
    });
    if (exists) {
      exists.occupy_taskId = '0'
      if (status == 1) {
        exists.status = 1
        exists.frozenTime = new Date()
      } else if (status == 2) {
        exists.status = 2
        exists.frozenTime = new Date()
      } else if (status == 3) {
        exists.status = 3
        exists.frozenTime = new Date()
      } else if (status == 5) {
        exists.status = 5
        exists.frozenTime = new Date()
      }
      await this.ShuyouGameAccountEntity.save(exists);
      return 1
    } else {
      return -1
    }
  }


}
