/*
 * @Author: YKH
 * @Date: 2021-12-08 10:26:27
 * @LastEditTime: 2022-09-29 15:24:36
 * @Description:
 * @FilePath: \cool-admin-3.x\cool-admin-midway-master\src\app\modules\shuyou\service\rent_computer.ts
 */
import { Provide, Inject } from '@midwayjs/decorator';
import { BaseService } from 'midwayjs-cool-core';
import { InjectEntityModel } from '@midwayjs/orm';
import { Repository } from 'typeorm';
import * as _ from 'lodash';
import { ShuyouRentLedgerComputerEntity } from '../entity/rent_ledger_computer';
import { ShuyouRentComputerUsageEntity } from '../entity/rent_computer_game_record';
import { Utils } from '../../../comm/utils';

/**
 * 租机
 */
@Provide()
export class ShuyouRentLedgerComputerService extends BaseService {
  @InjectEntityModel(ShuyouRentLedgerComputerEntity)
  ShuyouRentLedgerComputerEntity: Repository<ShuyouRentLedgerComputerEntity>;
  @InjectEntityModel(ShuyouRentComputerUsageEntity)
  ShuyouRentComputerUsageEntity: Repository<ShuyouRentComputerUsageEntity>;
  @Inject()
  utils: Utils;
  /**
   * 重写分页查询
   * @param query
   */
  async page(query) {
    const { pc_num, new_date, shop_id } = query;
    const sql = `
          SELECT
            a.id,DATE_FORMAT(a.new_date,'%Y-%m-%d') AS new_date,a.pc_num,a.pc_name,a.shop_id,a.set_meal,a.rent_start_then,a.rent_duration_then,
            a.rent_end_then,a.price_type,a.rent_price_then,a.rent_price_day,a.pay_method,a.is_expire,a.rent_price_used_then,a.remark,
            a.updateTime,
            b.name as shop_name
          FROM
            shuyou_rent_ledger_computer a
            LEFT JOIN shuyou_rent_computer_shop b ON a.shop_id = b.id
          WHERE 1 = 1
            ${this.setSql(pc_num, 'and a.pc_num = ?', [pc_num])}
            ${this.setSql(shop_id, 'and a.shop_id = ?', [shop_id])}
            ${this.setSql(new_date, 'and a.new_date >= ? and a.new_date <= ?', [
              this.utils.formatDate(new Date(new_date[0])),
              this.utils.formatDate(new Date(new_date[1])),
            ])}
          GROUP BY a.id
        `;
    return this.sqlRenderPage(sql, query);
  }
}
