const system=require("../../../system");
const ServiceBase=require("../../sve.base")
const settings=require("../../../../config/settings")
class RptTypeService extends ServiceBase{
  constructor(){
     super("auth",ServiceBase.getDaoName(RptTypeService));
     this.redisClient=system.getObject("util.redisClient");
  }
  async saveRpt(upinfo){
    return this.db.transaction(async t=>{
         //构建批量对象
          var company={
              code:upinfo.companyTaxNum,
              name:upinfo.companyName,
          }
          var comp=await this.db.models.company.findOne({where:{code:upinfo.companyTaxNum}});
          if(!comp){
            comp=await this.db.models.company.create(company,{transaction:t});
          }
          var batchobj={
            batchdate:upinfo.date,
            companyname:upinfo.companyName,
            companyCode:upinfo.companyTaxNum,
            name:companyname+"_"+upinfo.date+"_"+upinfo.accountingType,
            batchstatuscode:"parserpt",
            taxtypecode:upinfo.valueAddedTax,
            timetypecode:upinfo.dateType,
            accountstandardcode:upinfo.accountingType,
            company_id:comp.id,
          }
          //保存批量
          var batchtemp=await this.db.models.uploadbatch.create(batchobj,{transaction:t});
          var rpts=[];
          Object.keys(upinfo).forEach(k=>{
              {
                if(k.indexOf("Report")>=0){
                  var rpts=upinfo[k];
                  rpts.forEach(rpt=>{
                      var rptupload={
                        companyname:upinfo.companyName,
                        companyCode:upinfo.companyTaxNum,
                        rpttypecode:rpt.code,
                        timetypecode:upinfo.dateType,
                        urlpath:rpt.url,
                        rptdate:rpt.date,
                        company_id:comp.id,
                        uploadbatch_id:batchtemp.id,
                      }
                      rpts.push(rptupload);
                  });
                }
             }
          });
          //保存报表
        await this.db.models.uploadrpt.bulkCreate(rpts,{transaction:t});
        try{
          rpts.forEach((rpt)=>{
            await this.redisClient.notifyConsume("k8stask","docurls",rpt.rpttypecode+"~"+rpt.urlpath);
          });
        }catch(e){
          //需要记录要清理的批号历史数据
        }
        return batchtemp;
    });
  }
}
module.exports=RptTypeService;

