const system = require("../../../system");
const ServiceBase = require("../../sve.base")
const settings = require("../../../../config/settings")
class PatentAnalysisService extends ServiceBase {
  constructor() {
    super("report", ServiceBase.getDaoName(PatentAnalysisService));
    this.dic = {
      1: "报告生成中...",
      2: "已生成",
    }
  }

  async reportFile(params) {
    let id = this.trim(params.id);
    let report_file = this.trim(params.report_file);
    if (!id) {
      return system.getResult(null, "参数缺少id");
    }
    if (!report_file) {
      return system.getResult(null, "参数缺少report_file");
    }

    let analysis = await this.dao.findById(Number(params.id));
    if (!analysis) {
      return system.getResult(null, `记录【${params.id}】, 不存在`);
    }
    analysis.report_file = report_file;
    analysis.report_time = new Date();
    analysis.status = 2;
    await analysis.save();
  }

  async all(params) {
    let list = await this.dao.all(params);
    this.doFields(list);
    return list;
  }

  async pageByCondition(params) {
    let page = {
      count: 0,
      rows: []
    };
    params.currentPage = Number(params.currentPage || 1);
    params.pageSize = Number(params.pageSize || 10);
    params.startRow = (params.currentPage - 1) * params.pageSize;

    page.count = await this.dao.countByCondition(params);
    if (page.count == 0) {
      return system.getResultSuccess(page);
    }

    page.rows = await this.dao.listByCondition(params);
    this.doFields(page.rows);
    return system.getResultSuccess(page);
  }

  doFields(list) {
    if (!list || list.length == 0) {
      return;
    }
    for (let row of list) {
      this.handleDate(row, ["created_at", "report_time"], "YYYY-MM-DD HH:mm:ss", 0);
      this.handleDate(row, ["search_time"], "YYYY-MM", 0);
      row.status_name = this.dic[Number(row.status || 0)] || "";
    }
  }
}
module.exports = PatentAnalysisService;
// var task=new PatentAnalysisService();
// task.getUserStatisticGroupByApp().then(function(result){
//   console.log((result));
// }).catch(function(e){
//   console.log(e);
// });
