import { Provide } from '@midwayjs/decorator';
import { BaseService } from 'midwayjs-cool-core';
import { InjectEntityModel } from '@midwayjs/orm';
import { Repository } from 'typeorm';
import { ShuyouSpecialAccountEntity } from '../entity/special_game_account';
import * as _ from 'lodash';
// import { ShuyouSpecialAccountConsumeEntity } from '../entity/special_account_consume';

/**
 * 商品
 */
@Provide()
export class ShuyouSpecialAccountService extends BaseService {
  @InjectEntityModel(ShuyouSpecialAccountEntity)
  ShuyouSpecialAccountEntity: Repository<ShuyouSpecialAccountEntity>;

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

  /**
    * 重写分页查询
    * @param query
    */
  async page(query) {
    const { type_name, card_text } = query;
    const sql = `
        SELECT
          a.id,a.createTime,a.updateTime,a.remark,a.card_text,a.card_password,
          a.amount_all,a.amount_balance,a.get_time,a.status,a.releaseTime,
          b.name AS type_name
        FROM
          special_account a
          LEFT JOIN special_account_type b ON a.type_id = b.id
        WHERE 1 = 1
          ${this.setSql(type_name, 'and b.name = ?', [type_name])}
          ${this.setSql(card_text, 'and a.card_text LIKE ?', [`%${card_text}%`])}
      `;
    return this.sqlRenderPage(sql, query);
  }

  /***
   * 批量插入数据库
   */
  async batchAdd(card: any) {
    let cardT = card
    const sqlBatchAdd = `
    insert into special_account (card_text, card_password, type_id, amount_all, amount_balance, remark) 
      values ` + cardT + `
    on duplicate key update 
      amount_all=values(amount_all), amount_balance=values(amount_balance), remark=values(remark)
    `;
    // console.log(sqlBatchAdd)
    return await this.nativeQuery(sqlBatchAdd);
  }

  /**
  *获取
  */
  async getQQCard(unit_price: number) {
    const sql = `
    SELECT id, card_text, card_password, amount_balance
    FROM special_account 
    WHERE type_id = 1 and status <= 3 
      ${this.setSql(unit_price, ' and amount_balance >= ?', [unit_price])}
    ORDER BY amount_balance LIMIT 1
    `;
    // console.log(sql)
    const list = await this.nativeQuery(sql);
    if (!_.isEmpty(list)) {
      let obj = {
        code: 1000,
        card_id: list[0].id,
        card_text: list[0].card_text,
        card_password: list[0].card_password,
        amount_balance: list[0].amount_balance
      }
      return obj
    } else {
      return {
        code: -1,
        card_id: '',
        card_text: '',
        card_password: '',
        amount_balance: ''
      }
    }

  }

  /**
   *
   *
   * @param {string} card_text
   * @param {number} unit_price
   * @return {*} 
   * @memberof ShuyouSpecialAccountService
   */
  async updateRow(card_id: number, unit_price: number) {
    let obj = {
      id: card_id
    }
    const cardEntity = await this.ShuyouSpecialAccountEntity.findOne(obj);
    let ret = 1000
    if (cardEntity) {
      if (unit_price > 0) {
        cardEntity.amount_balance = cardEntity.amount_balance - unit_price;
      } else {
        cardEntity.amount_balance = 0
        cardEntity.status = 5
        ret = 5
      }
      await this.ShuyouSpecialAccountEntity.save(cardEntity);
      return ret
    } else {
      return -1
    }
  }


}
