const TaskBase=require("../../task.base");
const settings=require("../../../../config/settings");
var excel = require('exceljs');
const system = require("../../../system");
const fs = require('fs');
class ParseRptTask extends TaskBase{
  constructor(){
    super(TaskBase.getServiceName(ParseRptTask));
    this.cacheData=[];
    this.restData=[];
    this.ossC=system.getObject("util.ossClient");
  }
  async subBeforeTask(params){
    console.log("前置操作......",this.serviceName);
    //this.isThrough=true;
    //console.log(this.cacheManager);
    this.isDaemon=true;
 }
  async subTaskHandle(channel, message){
    var x=await this.redisClient.rpop(message);
    if(x){
      this.cacheData.push(x);
    }
    setImmediate(async ()=>{
      await this.parseXls();
    });
  }
  //读取模板文件
async  readxls(path) {
  var promise = new Promise((resv, rej) => {
      var workbook = new excel.Workbook();
      workbook.properties.date1904 = true;
      workbook.xlsx.readFile(path)
          .then(function () {
              return resv(workbook);
          }).catch(function (e) {
              return rej(e);
          });
  });
  return promise;
}
  async parseXls(){
    var pv=this.cacheData.pop();
    if(pv){
      console.log(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.",pv);
      var tohandleurls=pv.split("~");
      var rptid=tohandleurls[0];
      var bid=tohandleurls[1];
      var rptDate=tohandleurls[2];
      var rptCode=tohandleurls[3];
      var urldownload=tohandleurls[4];
      //如果是pdf,那么就调用pdf 解析接口
      if(urldownload.indexOf(".pdf")>=0){
        
        return;
      }
      //下载文件
      var keyindex=urldownload.lastIndexOf("/");
      var keystr=urldownload.substring(keyindex+1);
      var result= await this.ossC.downfile(keystr);
      var localpath="/tmp/"+keystr;
      try{
        var wb= await this.readxls(localpath);
      }catch(e){
        console.log(localpath,'下载文件不存在...........................')
        console.log(e)
      }
      console.log("//////////////////////////68................")
      //是否解析excell中的报表
      var toparsedate={isparsedate:false,rptDate:'notparse',fp:urldownload};
      var ind=rptCode.lastIndexOf("_");
      var parsefunc=rptCode;
      if(ind>=0){
        var st2=rptCode.substring(ind+1);
        console.log("//////////////////////////74................")
        if(st2=="N"){//是非必传的文件
           parsefunc=rptCode.substring(0,ind);
           toparsedate.isparsedate=true;
        }
      }
      console.log("//////////////////////////79................")
      var parserByCode=null;
      if(parsefunc=="ITY"){//年报所得税7个sheet
        //里面存在7个sheet,构造7个sheet history data
        //A100000--ITY--1
        parsefunc="ITY";
        parserByCode=require("./rptHandler/"+parsefunc);
        var rowsITY=await parserByCode(wb);
        this.restData.push({
          rptid:rptid,
          batchid:bid,
          rptdate:rptDate,
          rptcode:rptCode,
          hisdata:rowsITY
        });
        //A101010--ITY_INCOME_DESC-2
        parsefunc="ITY_INCOME_DESC";
        parserByCode=require("./rptHandler/"+parsefunc);
        var rows2=await parserByCode(wb);
        this.restData.push({
          rptid:rptid,
          batchid:bid,
          rptdate:rptDate,
          rptcode:rptCode,
          hisdata:rows2
        });
        //A102010-ITY_COST---3
        parsefunc="ITY_COST";
        parserByCode=require("./rptHandler/"+parsefunc);
        var rows3=await parserByCode(wb);
        this.restData.push({
          rptid:rptid,
          batchid:bid,
          rptdate:rptDate,
          rptcode:rptCode,
          hisdata:rows3
        });
        //A104000--ITY_PERIOD--4
        parsefunc="ITY_PERIOD";
        parserByCode=require("./rptHandler/"+parsefunc);
        var rows4=await parserByCode(wb);
        this.restData.push({
          rptid:rptid,
          batchid:bid,
          rptdate:rptDate,
          rptcode:rptCode,
          hisdata:rows4
        });

        //A105060--ITY_ADV--5
        parsefunc="ITY_ADV";
        parserByCode=require("./rptHandler/"+parsefunc);
        var rows5=await parserByCode(wb);
        this.restData.push({
          rptid:rptid,
          batchid:bid,
          rptdate:rptDate,
          rptcode:rptCode,
          hisdata:rows5
        });
        //A105080--ITY_DISCOUNT_ADJUST--6
        parsefunc="ITY_DISCOUNT_ADJUST";
        parserByCode=require("./rptHandler/"+parsefunc);
        var rows6=await parserByCode(wb);
        this.restData.push({
          rptid:rptid,
          batchid:bid,
          rptdate:rptDate,
          rptcode:rptCode,
          hisdata:rows6
        });
        //A106000--ITY_FILL_LOST--7
        parsefunc="ITY_FILL_LOST";
        parserByCode=require("./rptHandler/"+parsefunc);
        var rows7=await parserByCode(wb);
        this.restData.push({
          rptid:rptid,
          batchid:bid,
          rptdate:rptDate,
          rptcode:rptCode,
          hisdata:rows7
        });
      }else{
        parserByCode=require("./rptHandler/"+parsefunc);
        var rows=await parserByCode(wb,toparsedate);
        this.restData.push({
          rptid:rptid,
          batchid:bid,
          rptdate:toparsedate.isparsedate?toparsedate.rptDate:rptDate,
          rptcode:rptCode,
          hisdata:rows
        });
      }
      this.parseXls();
    }else{
      if(this.restData && this.restData .length>0){
        this.apiCallWithAk(settings.fkweb(),this.restData);
        this.restData.length=0;
        this.restData=[];
      }
    }
  }
  async subDoTask(params){
    console.log(params);
    console.log("TestTask1.....");
  }
}
module.exports=ParseRptTask;