/*
 * @Author: YKH
 * @Date: 2022-04-06 15:38:44
 * @LastEditTime: 2022-11-24 15:33:08
 * @Description:
 * @FilePath: \cool-admin\cool-admin-midway-master\src\app\modules\shuyou\service\file_list.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 { ShuyouFileListEntity } from '../entity/file_list';

/**
 * 文件列表
 */
@Provide()
export class ShuyouFileListService extends BaseService {
  @InjectEntityModel(ShuyouFileListEntity)
  ShuyouFileListEntity: Repository<ShuyouFileListEntity>;

  /**
   * 重写分页查询
   * @param query
   */
  async page(query) {
    const { task_id } = query;
    // console.log(query)
    const sql = `
          SELECT
            a.id,
            a.createTime,
            a.updateTime,
            a.task_id,
            a.fileName,
            a.fileVersion,
            a.qiniuAddress,
            a.qiniuHash,
            a.qiniuKey,
            a.type
          FROM
            shuyou_file_list a
          WHERE
            1 = 1
            ${this.setSql(task_id, 'and a.task_id = ?', [task_id])}
        `;
    return this.sqlRenderPage(sql, query);
  }

  /**
   * 重载新增接口
   */
  async add(param) {
    const exists = await this.ShuyouFileListEntity.findOne({
      task_id: param.task_id,
      fileName: param.fileName,
    });
    // console.log(exists)
    if (!_.isEmpty(exists)) {
      throw new CoolCommException('已存在相同文件~');
    }
    // console.log(param)
    return super.add(param);
  }
  /**
   * 删除接口
   * @param {*} id
   * @memberof ShuyouFileListService
   */
  async deleteById(id) {
    const deleteResult = await this.ShuyouFileListEntity.createQueryBuilder()
      .delete()
      .where('id = :id', {
        id: id,
      })
      .execute();
    // console.log(deleteResult)
    if (deleteResult.affected >= 0) {
      return 1000;
    } else {
      return -1;
    }
  }
  /**
   * 查询某task_id下某一类型文件最新一个渠道号
   * @param {*} task_id
   * @param {*} type
   * @return {*}
   * @memberof ShuyouFileListService
   */
  async queryVersionByType(task_id, type) {
    const res = await this.ShuyouFileListEntity.createQueryBuilder()
      .where('task_id = :task_id AND type = :type', {
        task_id: task_id,
        type: type,
      })
      .orderBy('fileVersion', 'DESC')
      .getOne();
    // console.log(deleteResult)
    return res;
  }

  /**
   * 查询所有
   *
   * @return {*}
   * @memberof ShuyouFileListService
   */
  async queryAll() {
    const sql = `
      SELECT
        task_id,
        type,
        qiniuAddress,
        qiniuHash,
        fileVersion
      FROM
        shuyou_file_list
        ORDER BY task_id,type,fileVersion 
    `;
    return this.nativeQuery(sql);
  }
  /**
   * 删除数据库记录
   * @param {*} task_id
   * @return {*}
   * @memberof ShuyouFileListService
   */
  async deleteAllByTaskId(task_id) {
    const exists = await this.ShuyouFileListEntity.find({
      task_id: task_id,
    });
    if (!_.isEmpty(exists)) {
      //删除数据库记录
      await this.ShuyouFileListEntity.createQueryBuilder()
        .delete()
        .where('task_id = :task_id', {
          task_id: task_id,
        })
        .execute();
      return { code: 1, data: exists };
    } else {
      return { code: -1 };
    }
  }
}
