/*
 * @Author: YKH
 * @Date: 2021-12-08 10:26:27
 * @LastEditTime: 2022-03-11 13:45:02
 * @Description: 
 * @FilePath: \cool-admin\cool-admin-midway-master\src\app\modules\shuyou\service\computer_usage.ts
 */
import { Provide } from '@midwayjs/decorator';
import { BaseService, CoolCommException } from 'midwayjs-cool-core';
import { InjectEntityModel } from '@midwayjs/orm';
import { Repository } from 'typeorm';
import * as _ from 'lodash';
import { ShuyouComputerUsageEntity } from '../entity/computer_usage';

/**
 * 游戏目标
 */
@Provide()
export class ShuyouComputerUsageService extends BaseService {

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

  /**
   * 重载新增接口
   */
  async add(param) {
    const exists = await this.ShuyouComputerUsageEntity.findOne({
      task_id: param.task_id,
      pc_num: param.pc_num
    });
    if (!_.isEmpty(exists)) {
      throw new CoolCommException('该任务已添加过该电脑~');
    }
    param.createTime = new Date()
    param.updateTime = new Date()
    // console.log(param)
    return super.add(param);

  }

  /**
  * 重写分页查询
  * @param query
  */
  async page(query) {
    const { task_id, pc_num } = query;
    // console.log(query)
    const sql = `
            SELECT
            a.id,a.createTime,a.updateTime,a.remark,
            b.id AS task_id,
            b.task_name AS task_name,
            c.id AS pc_num,
            c.pc_name AS pc_name
          FROM
            shuyou_computer_usage a
            LEFT JOIN shuyou_game_task b ON a.task_id = b.id
            LEFT JOIN shuyou_game_computer c ON a.pc_num = c.id
          WHERE 1 = 1
            ${this.setSql(task_id, 'and a.task_id = ?', [task_id])}
            ${this.setSql(pc_num, 'and a.pc_num LIKE ?', [`%${pc_num}%`])}
        `;
    return this.sqlRenderPage(sql, query);
  }


}
