/*
 * @Author: YKH
 * @Date: 2022-02-17 16:27:22
 * @LastEditTime: 2022-02-24 16:29:30
 * @Description: 
 * @FilePath: \cool-admin\cool-admin-midway-master\src\app\modules\shuyou\service\special_account_consume.ts
 */
import { Provide } from '@midwayjs/decorator';
import { BaseService } from 'midwayjs-cool-core';
import { InjectEntityModel } from '@midwayjs/orm';
import { Repository } from 'typeorm';
import * as _ from 'lodash';
import { ShuyouSpecialAccountConsumeEntity } from '../entity/special_account_consume';
// import { Utils } from '../../../comm/utils';

/**
 * 商品
 */
@Provide()
export class ShuyouSpecialAccountConsumeService extends BaseService {

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

  // @Inject()
  // utils: Utils;
  /**
    * 重写分页查询
    * @param query
    */
  async page(query) {
    const { card_text } = query;
    const sql = `
        SELECT
          a.id,a.createTime,a.updateTime,a.consume,a.occupyTime,a.occupy_task,a.occupy_acc,a.status,a.consumeTime,
          b.card_text as card_text,b.card_password as card_password,b.type_id as type_id
        FROM
          special_account_consume a
          LEFT JOIN special_account b ON a.card_id = b.id
        WHERE 1 = 1
          ${this.setSql(card_text, 'and b.card_text LIKE ?', [`%${card_text}%`])}
      `;
    return this.sqlRenderPage(sql, query);
  }


  /**
  *修改取卡记录
  */
  async updateRow(consume_id: number, state: number) {
    let obj = {
      id: consume_id
    }
    const consumeEntity = await this.ShuyouSpecialAccountConsumeEntity.findOne(obj);
    if (consumeEntity) {
      if (state == 1000) {
        consumeEntity.status = 3;
        consumeEntity.consumeTime = new Date();
      } else {
        consumeEntity.status = 5;
      }
      await this.ShuyouSpecialAccountConsumeEntity.save(consumeEntity);
      return 1000
    } else {
      return -1
    }
  }



}
