const system = require("../../../system")
const settings = require("../../../../config/settings");
const CtlBase = require("../../ctl.base");
const moment = require('moment');

var cacheBaseComp = null;
class PatentAnalysisCtl extends CtlBase {
  constructor() {
    super("report", CtlBase.getServiceName(PatentAnalysisCtl));
    this.DOC_DIC = {
      "100000": "德温特专利数据库"
    }
    this.redisClient = system.getObject("util.redisClient");
  }

  async docDic(body, query, req, res) {
    let dic = [];
    for (let id in this.DOC_DIC) {
      dic.push({id: id, name: this.DOC_DIC[id]});
    }
    return system.getResultSuccess(dic);
  }
  async save(body, query, req, res) {
    let technology_name = this.trim(body.technology_name);
    let topic_name = this.trim(body.topic_name);
    let topic_doc_id = this.trim(body.topic_doc_id);
    let search_time = this.trim(body.search_time);
    let topic_overview = this.trim(body.topic_overview);
    let data_file = this.trim(body.data_file);
    if (!technology_name) {
      return system.getResult(null, "请填写技术名称");
    }
    if (!data_file) {
      return system.getResult(null, "请上传文件");
    }
    if (!topic_name) {
      return system.getResult(null, "请填写课题名称");
    }
    if (topic_name.length > 50) {
      return system.getResult(null, "课题名称不允许超过50个字");
    }
    if (!topic_doc_id) {
      return system.getResult(null, "请选择课题文献");
    }
    let topic_doc_name = this.DOC_DIC[topic_doc_id];
    if (!topic_doc_name) {
      return system.getResult(null, "课题文献选择错误，请刷新重试");
    }
    if (!search_time) {
      return system.getResult(null, "请选择检索时间");
    }
    try {
      // search_time = moment(search_time, "YYYY-MM");
    } catch (e) {
      return system.getResult(null, "时间格式错误");
    }
    if (!topic_overview) {
      return system.getResult(null, "请填写发展概况");
    }
    if (topic_overview.length > 5000) {
      return system.getResult(null, "发展概况不允许超过5000字");
    }

    try {
      // let exists = this.service.findOne({technology_name: technology_name});
      // if (exists) {
      //   return system.getResult(null, `${technology_name}名称重复`);
      // }
      let obj = {
        autoIncrement: true,
        technology_name: technology_name,
        data_file: data_file,
        topic_doc_name: topic_doc_name,
        topic_name: topic_name,
        topic_doc_id: topic_doc_id,
        search_time: search_time,
        topic_overview: topic_overview,
        status: 1,
      };
      obj = await this.service.create(obj);
      return system.getResultSuccess();
    } catch (error) {
      console.log(error);
      return system.getResultFail(500, "接口异常：" + error.stack);
    }
  }

  async page(body, query, req, res) {
    try {
      let page = await this.service.pageByCondition(body);
      return page;
    } catch (error) {
      console.log(error);
      return system.getResultFail(500, "接口异常：" + error.stack);
    }
  }
}
module.exports = PatentAnalysisCtl;