const system = require("../../../system");
const ServiceBase = require("../../sve.base");
var settings = require("../../../../config/settings");
var RPCClient = require('@alicloud/pop-core').RPCClient;
var OSS = require('ali-oss');
const crypto = require('crypto');
var fs = require('fs');
class TmjsonfileService extends ServiceBase {
  constructor() {
    super("transfer", ServiceBase.getDaoName(TmjsonfileService));
    this.connectionigirl = system.getObject("db.common.connection").getConigirl();

  }

  async createjsonfile() {
    var lastsql = "select * from tm_jsonfile order by id desc limit 1";
    var lastinfo = await this.dao.customQuery(lastsql);
    var lasttime=0
    if(lastinfo){
      lasttime=lastinfo[0].updated_at.toISOString().split("T")[0];
    }
    var sql = "select * from bi_trademarktransaction where updated_at>='"+lasttime+"'";
    // var sql = "select * from bi_trademarktransaction where id=175342";
    var tminfos = await this.connectionigirl.query(sql);
    var sources = [];
    if (tminfos[0]) {
      for (var i = 0; i < tminfos[0].length; i++) {
        var tmg = tminfos[0][i].tm_group;
        var status=1;
        if(tminfos[0][i].deleted_at||"lowershelf"==tminfos[0][i].publish_status){
          status=0;
        }
        var strlist = null;
        if (tmg) {
          strlist = tmg.replace(/"/g, "").replace(/\[/g, "").replace(/\]/g, "").split(",");
          for (var n = 0; n < strlist.length; n++) {
            if (!strlist[n]) {
              strlist.shift();
            }
          }
        }
        var des = "";
        if (tminfos[0][i].tm_ncl_third) {
          des = tminfos[0][i].tm_ncl_third.replace(/\[/g, "").replace(/\]/g, "").replace(/\'/g, "");
        }
        var source = {
          beginTime: 1574388139000,
          classificationCode: tminfos[0][i].ncl_one_code,
          description: des,
          endTime: 1668089537981,
          label: "商标标签",
          originalPrice: parseInt(tminfos[0][i].platform_quoted_price),
          ownerEnName: "",
          ownerName: tminfos[0][i].tm_applier,
          partnerCode: "gong_si_bao",
          regAnnDate: tminfos[0][i].tm_regist_day * 1000,
          secondaryClassification: strlist,
          status: status,
          thirdClassification: tminfos[0][i].tm_ncl_third,
          tmIcon: tminfos[0][i].pic_url,
          tmName: tminfos[0][i].name,
          tmNumber: tminfos[0][i].code
        }
        sources.push(source);
      }
      var newdate = new Date();
      var strdate = ""
      if (newdate.getHours() > 12) {
        strdate = newdate.toISOString().split("T")[0].replace(/\-/g, "") + "_2.json";
      } else {
        strdate = newdate.toISOString().split("T")[0].replace(/\-/g, "") + "_1.json";
      }
      var jsonstr = JSON.stringify(sources, null, "\t");
      return new Promise(function (resv, rej) {
        fs.writeFile('/tmp/' + strdate, jsonstr, function (err) {
          if (err) {
            console.error(err);
          }
          console.log('--------------------修改成功11111111111111');
          return resv({ status: 0, msg: "成功" })
        })
      }
      )

      console.log('--------------------修改成功');
    }

  }



}
module.exports = TmjsonfileService;
