const TaskBase = require("../../task.base");
const settings = require("../../../../config/settings");
var excel = require('exceljs');
var archiver = require('archiver');
var xlsx = require('node-xlsx');
const system = require("../../../system");
const fs = require('fs');
const COS = require('cos-nodejs-sdk-v5');
const request = require('request');
var child_process = require('child_process');
class IcpGenerateTask extends TaskBase {
  constructor() {
    super(TaskBase.getServiceName(IcpGenerateTask));
    this.cosgetsecreturl = settings.cosGetSecreturl();
    this.icpMaterialurl = settings.icpMaterialurl();
    this.restclient = system.getObject("util.restClient");
  }


  async subBeforeTask(params) {

    //创建目录,判断文件目录是否存在，不存在就创建  /tmp/icp
    var icpPath = '/tmp/icp'
    if (!fs.existsSync(icpPath)) {
      fs.mkdirSync(icpPath);
    }
    //请求材料数据
    var materiadata = [];
    try {
      var icpGetMaterialurl = this.icpMaterialurl + '/web/delivery/materialCtl/waitGenerate';
      console.log(icpGetMaterialurl);
      var rtn = await this.restclient.execPost({}, icpGetMaterialurl);
      if (!rtn || !rtn.stdout) {
        return;
      }
      var result = JSON.parse(rtn.stdout);
      if (result.status == 0) {
        materiadata = result.data;
      } else {
        return;
      }
    } catch (error) {
      // console.log(error);
      return;
    }
    //遍历数据
    var datalist = materiadata;
    for (var j = 0; j < datalist.length; j++) {
      try {
        var exceldata = datalist[j];
        //编号id
        var Id = exceldata.id;
        var type = exceldata.type;
        var id_dir = '/tmp/icp/' + Id;
        var registeredAddress_dir = id_dir + '/01-注册认证';
        var implementationPlan_dir = id_dir + '/02-发展和实施计划';
        var licence_dir = id_dir + '/03-增值电信申请表';
        var companyPersonnel_dir = id_dir + '/04-公司及人员情况';
        var safety_dir = id_dir + '/05-网络与信息安全';
        var shareholders_dir = id_dir + '/06-股东信息';
        var telecommunication_dir = id_dir + '/07-电信业务承诺书';
        var rotherMaterials_dir = id_dir + '/08-其他材料';
        //创建目录,判断文件目录是否存在，不存在就创建  /tmp/icp
        if (!fs.existsSync(id_dir)) {
          fs.mkdirSync(id_dir);
        }
        //复制文件夹及文件
        child_process.spawn('cp', ['-r', 'template/01-注册认证', id_dir]);
        child_process.spawn('cp', ['-r', 'template/02-发展和实施计划', id_dir]);
        child_process.spawn('cp', ['-r', 'template/03-增值电信申请表', id_dir]);
        child_process.spawn('cp', ['-r', 'template/04-公司及人员情况', id_dir]);
        child_process.spawn('cp', ['-r', 'template/05-网络与信息安全', id_dir]);
        child_process.spawn('cp', ['-r', 'template/06-股东信息', id_dir]);
        child_process.spawn('cp', ['-r', 'template/07-电信业务承诺书', id_dir]);
        child_process.spawn('cp', ['-r', 'template/08-其他材料', id_dir]);
        // child_process.spawn('cp', ['template/材料清单.xlsx', id_dir]);
        //------------------------创建  01-注册认证 ------------------------
        if (fs.existsSync(registeredAddress_dir)) {
          var registeredAddressData = exceldata.registeredAddressData;
          var workbook = new excel.Workbook();
          await workbook.xlsx.readFile(registeredAddress_dir + "/实名认证.xlsx").then(function () {
            var worksheet = workbook.getWorksheet("实名认证"); //获取第一个worksheet
            // console.log("-------------------", workbook.worksheets);
            worksheet.eachRow(function (row, rowNumber) {
              // console.log('Row ' + rowNumber + ' = ' + JSON.stringify(row.values));
              // console.log(row.values);
              if (rowNumber == 2) {
                row.eachCell(function (cell, colNumber) {
                  // if (cell.type == 6) {
                  //   var value = cell.result;
                  // } else {
                  //   var value = cell.value;
                  // }
                  if (colNumber == 2) {
                    // console.log('Cell ' + colNumber + ' = ' + cell.type + " " + value);
                    cell.value = registeredAddressData.authentication.personType;
                  }

                });
              }
              if (rowNumber == 3) {
                row.eachCell(function (cell, colNumber) {
                  if (colNumber == 2) {
                    cell.value = registeredAddressData.authentication.companyType;
                  }

                });
              }
              if (rowNumber == 4) {
                row.eachCell(function (cell, colNumber) {
                  if (colNumber == 2) {
                    cell.value = registeredAddressData.authentication.enterpriseCode;
                  }

                });
              }
              if (rowNumber == 5) {
                row.eachCell(function (cell, colNumber) {
                  if (colNumber == 2) {
                    cell.value = registeredAddressData.authentication.name;
                  }

                });
              }
              if (rowNumber == 6) {
                row.eachCell(function (cell, colNumber) {
                  if (colNumber == 2) {
                    cell.value = registeredAddressData.authentication.legalTypes;
                  }

                });
              }
              if (rowNumber == 7) {
                row.eachCell(function (cell, colNumber) {
                  if (colNumber == 2) {
                    cell.value = registeredAddressData.authentication.legalName;
                  }

                });
              }
              if (rowNumber == 8) {
                row.eachCell(function (cell, colNumber) {
                  if (colNumber == 2) {
                    cell.value = registeredAddressData.authentication.legalCertificateId;
                  }

                });
              }
              if (rowNumber == 9) {
                row.eachCell(function (cell, colNumber) {
                  if (colNumber == 2) {
                    cell.value = registeredAddressData.authentication.legalCertificateType;
                  }

                });
              }
              if (rowNumber == 10) {
                row.eachCell(function (cell, colNumber) {
                  if (colNumber == 2) {
                    cell.value = registeredAddressData.authentication.contactAddress;
                  }

                });
              }

            });
            workbook.xlsx.writeFile(registeredAddress_dir + "/实名认证.xlsx");
          });
          //下载文件
          var files = registeredAddressData.files;
          await this.downFile(files, registeredAddress_dir);
        }
        //------------------------02-发展和实施计划 ------------------------
        if (fs.existsSync(implementationPlan_dir)) {
          var implementationPlanData = exceldata.implementationPlanData;
          if (type == "EDI") {
            fs.unlinkSync(implementationPlan_dir + "/发展和实施计划-ICP.xlsx");
            var implementationPlan_workbook = new excel.Workbook();
            await implementationPlan_workbook.xlsx.readFile(implementationPlan_dir + "/发展和实施计划-EDI.xlsx").then(function () {
              var worksheet = implementationPlan_workbook.getWorksheet("发展和实施计划-EDI"); //获取第一个worksheet
              // console.log("---------------------",workbook.worksheets);
              worksheet.eachRow(function (row, rowNumber) {
                // console.log('Row ' + rowNumber + ' = ' + JSON.stringify(row.values));
                // console.log(row.values);
                if (rowNumber == 3) {
                  row.eachCell(function (cell, colNumber) {
                    if (colNumber == 2) {
                      cell.value = implementationPlanData.EDI.electronicExchange;
                    }

                  });
                }
                if (rowNumber == 4) {
                  row.eachCell(function (cell, colNumber) {
                    if (colNumber == 2) {
                      cell.value = implementationPlanData.EDI.transactionProcessing;
                    }

                  });
                }
                if (rowNumber == 5) {
                  row.eachCell(function (cell, colNumber) {
                    if (colNumber == 2) {
                      cell.value = implementationPlanData.EDI.dataProcessing;
                    }

                  });
                }
                if (rowNumber == 6) {
                  row.eachCell(function (cell, colNumber) {
                    if (colNumber == 2) {
                      cell.value = implementationPlanData.EDI.targetUser;
                    }

                  });
                }
                if (rowNumber == 7) {
                  row.eachCell(function (cell, colNumber) {
                    if (colNumber == 2) {
                      cell.value = implementationPlanData.EDI.profitableWay;
                    }
                  });
                }

              });
              implementationPlan_workbook.xlsx.writeFile(implementationPlan_dir + "/发展和实施计划-EDI.xlsx").then(function () { });;
            });
          } else if (type == "ICP") {
            fs.unlinkSync(implementationPlan_dir + "/发展和实施计划-EDI.xlsx");
            var icp_workbook = new excel.Workbook();
            await icp_workbook.xlsx.readFile(implementationPlan_dir + "/发展和实施计划-ICP.xlsx").then(function () {
              var worksheet = icp_workbook.getWorksheet("发展和实施计划-ICP"); //获取第一个worksheet
              // console.log(workbook.worksheets);
              worksheet.eachRow(function (row, rowNumber) {
                // console.log('Row ' + rowNumber + ' = ' + JSON.stringify(row.values));
                // console.log(row.values);
                if (rowNumber == 3) {
                  row.eachCell(function (cell, colNumber) {
                    if (colNumber == 2) {
                      cell.value = implementationPlanData.ICP.releaseAndDelivery;
                    }

                  });
                }
                if (rowNumber == 4) {
                  row.eachCell(function (cell, colNumber) {
                    if (colNumber == 2) {
                      cell.value = implementationPlanData.ICP.community;
                    }

                  });
                }
                if (rowNumber == 5) {
                  row.eachCell(function (cell, colNumber) {
                    if (colNumber == 2) {
                      cell.value = implementationPlanData.ICP.protectiveProcessing;
                    }

                  });
                }
                if (rowNumber == 6) {
                  row.eachCell(function (cell, colNumber) {
                    if (colNumber == 2) {
                      cell.value = implementationPlanData.ICP.interactive;
                    }

                  });
                }
                if (rowNumber == 7) {
                  row.eachCell(function (cell, colNumber) {
                    if (colNumber == 2) {
                      cell.value = implementationPlanData.ICP.search;
                    }
                  });
                }
                if (rowNumber == 8) {
                  row.eachCell(function (cell, colNumber) {
                    if (colNumber == 2) {
                      cell.value = implementationPlanData.ICP.targetUser;
                    }
                  });
                }
                if (rowNumber == 9) {
                  row.eachCell(function (cell, colNumber) {
                    if (colNumber == 2) {
                      cell.value = implementationPlanData.ICP.profitableWay;
                    }
                  });
                }

              });
              icp_workbook.xlsx.writeFile(implementationPlan_dir + "/发展和实施计划-ICP.xlsx").then(function () { });;
            });
          }
          //网站或APP信息
          var webappdata = implementationPlanData.webApp;
          var webapp_workbook = new excel.Workbook();
          await webapp_workbook.xlsx.readFile(implementationPlan_dir + "/网站或APP信息.xlsx").then(function () {
            var worksheet = webapp_workbook.getWorksheet("网站或APP信息"); //获取第一个worksheet
            for (var i = 0; i < webappdata.length; i++) {
              var wd = webappdata[i];
              var rw = worksheet.addRow([wd.name, wd.type, wd.domain, wd.appStoreName, wd.serverAddress]);
              rw.eachCell(function (cell) {
                // cell.fill = {
                //   type: 'pattern',
                //   pattern: 'solid',
                //   fgColor: { argb: 'deeded' }
                // };
                // cell.font = { name: 'Arial', size: 14 };
                cell.alignment = { horizontal: 'center' };
                cell.border = {
                  top: { style: "thin" },
                  left: { style: "thin" },
                  bottom: { style: "thin" },
                  right: { style: "thin" }
                };

              });
            }
            webapp_workbook.xlsx.writeFile(implementationPlan_dir + "/网站或APP信息.xlsx").then(function () { });
          });
          //下载文件
          var files = implementationPlanData.files;
          await this.downFile(files, implementationPlan_dir);

        }
        //------------------------03-增值电信申请表 ------------------------
        if (fs.existsSync(licence_dir)) {
          var licenceData = exceldata.licenceData;
          var licence_workbook = new excel.Workbook();
          await licence_workbook.xlsx.readFile(licence_dir + "/增值电信业务经营许可证申请表.xlsx").then(function () {
            var worksheet = licence_workbook.getWorksheet("增值电信业务经营许可证申请表.xlsx"); //获取第一个worksheet
            // console.log("-------------------", workbook.worksheets);
            worksheet.eachRow(function (row, rowNumber) {
              if (rowNumber == 2) {
                row.eachCell(function (cell, colNumber) {
                  if (colNumber == 2) {
                    // console.log('Cell ' + colNumber + ' = ' + cell.type + " " + value);
                    cell.value = licenceData.licence.companyName;
                  }
                });
              }
              if (rowNumber == 3) {
                row.eachCell(function (cell, colNumber) {
                  if (colNumber == 2) {
                    cell.value = licenceData.licence.enterpriseCode;
                  }
                });
              }
              if (rowNumber == 4) {
                row.eachCell(function (cell, colNumber) {
                  if (colNumber == 2) {
                    cell.value = licenceData.licence.registeredCapital;
                  }
                });
              }
              if (rowNumber == 5) {
                row.eachCell(function (cell, colNumber) {
                  if (colNumber == 2) {
                    cell.value = licenceData.licence.registerAddress;
                  }
                });
              }
              if (rowNumber == 6) {
                row.eachCell(function (cell, colNumber) {
                  if (colNumber == 2) {
                    cell.value = licenceData.licence.fixedTelephone;
                  }
                });
              }
              if (rowNumber == 7) {
                row.eachCell(function (cell, colNumber) {
                  if (colNumber == 2) {
                    cell.value = licenceData.licence.workAddress;
                  }
                });
              }
              if (rowNumber == 8) {
                row.eachCell(function (cell, colNumber) {
                  if (colNumber == 2) {
                    cell.value = licenceData.licence.zipCode;
                  }

                });
              }
              if (rowNumber == 9) {
                row.eachCell(function (cell, colNumber) {
                  if (colNumber == 2) {
                    cell.value = licenceData.licence.comapnyNature;
                  }
                });
              }
              if (rowNumber == 10) {
                row.eachCell(function (cell, colNumber) {
                  if (colNumber == 2) {
                    cell.value = licenceData.licence.ifListed;
                  }
                });
              }

            });
            licence_workbook.xlsx.writeFile(licence_dir + "/增值电信业务经营许可证申请表.xlsx").then(function () { });
          });
          //下载文件
          var files = licenceData.files;
          await this.downFile(files, licence_dir);

        }
        //------------------------04-公司及人员情况 ------------------------
        if (fs.existsSync(companyPersonnel_dir)) {
          var companyPersonnelData = exceldata.companyPersonnelData;
          var companyPersonnel_workbook = new excel.Workbook();
          await companyPersonnel_workbook.xlsx.readFile(companyPersonnel_dir + "/公司人员情况.xlsx").then(function () {
            var worksheet = companyPersonnel_workbook.getWorksheet("公司及人员情况"); //获取第一个worksheet
            // console.log("-------------------", workbook.worksheets);
            worksheet.eachRow(function (row, rowNumber) {
              if (rowNumber == 3) {
                row.eachCell(function (cell, colNumber) {
                  if (colNumber == 1) {
                    // console.log('Cell ' + colNumber + ' = ' + cell.value);
                    cell.value = companyPersonnelData.personnelInfo[0].title;
                  }
                  if (colNumber == 2) {
                    cell.value = companyPersonnelData.personnelInfo[0].name;
                  }
                  if (colNumber == 3) {
                    cell.value = companyPersonnelData.personnelInfo[0].certificateType;
                  }
                  if (colNumber == 4) {
                    cell.value = companyPersonnelData.personnelInfo[0].certificateId;
                  }
                  if (colNumber == 5) {
                    cell.value = companyPersonnelData.personnelInfo[0].phone;
                  }
                  if (colNumber == 6) {
                    cell.value = companyPersonnelData.personnelInfo[0].email;
                  }
                });
              }
              if (rowNumber == 4) {
                row.eachCell(function (cell, colNumber) {
                  if (colNumber == 1) {
                    cell.value = companyPersonnelData.personnelInfo[1].title;
                  }
                  if (colNumber == 2) {
                    cell.value = companyPersonnelData.personnelInfo[1].name;
                  }
                  if (colNumber == 3) {
                    cell.value = companyPersonnelData.personnelInfo[1].certificateType;
                  }
                  if (colNumber == 4) {
                    cell.value = companyPersonnelData.personnelInfo[1].certificateId;
                  }
                  if (colNumber == 5) {
                    cell.value = companyPersonnelData.personnelInfo[1].phone;
                  }
                  if (colNumber == 6) {
                    cell.value = companyPersonnelData.personnelInfo[1].email;
                  }
                });
              }
              if (rowNumber == 5) {
                row.eachCell(function (cell, colNumber) {
                  if (colNumber == 1) {
                    cell.value = companyPersonnelData.personnelInfo[2].title;
                  }
                  if (colNumber == 2) {
                    cell.value = companyPersonnelData.personnelInfo[2].name;
                  }
                  if (colNumber == 3) {
                    cell.value = companyPersonnelData.personnelInfo[2].certificateType;
                  }
                  if (colNumber == 4) {
                    cell.value = companyPersonnelData.personnelInfo[2].certificateId;
                  }
                  if (colNumber == 5) {
                    cell.value = companyPersonnelData.personnelInfo[2].phone;
                  }
                  if (colNumber == 6) {
                    cell.value = companyPersonnelData.personnelInfo[2].email;
                  }
                });
              }
              if (rowNumber == 6) {
                row.eachCell(function (cell, colNumber) {
                  if (colNumber == 1) {
                    cell.value = companyPersonnelData.personnelInfo[3].title;
                  }
                  if (colNumber == 2) {
                    cell.value = companyPersonnelData.personnelInfo[3].name;
                  }
                  if (colNumber == 3) {
                    cell.value = companyPersonnelData.personnelInfo[3].certificateType;
                  }
                  if (colNumber == 4) {
                    cell.value = companyPersonnelData.personnelInfo[3].certificateId;
                  }
                  if (colNumber == 5) {
                    cell.value = companyPersonnelData.personnelInfo[3].phone;
                  }
                  if (colNumber == 6) {
                    cell.value = companyPersonnelData.personnelInfo[3].email;
                  }
                });
              }

              if (rowNumber == 8) {
                row.eachCell(function (cell, colNumber) {
                  if (colNumber == 2) {
                    cell.value = companyPersonnelData.companyInfo.bussinessDirection;
                  }
                });
              }
              if (rowNumber == 9) {
                row.eachCell(function (cell, colNumber) {
                  if (colNumber == 2) {
                    cell.value = companyPersonnelData.companyInfo.staffSize;
                  }
                });
              }
              if (rowNumber == 10) {
                row.eachCell(function (cell, colNumber) {
                  if (colNumber == 2) {
                    cell.value = companyPersonnelData.companyInfo.businessScale;
                  }
                });
              }

            });
            companyPersonnel_workbook.xlsx.writeFile(companyPersonnel_dir + "/公司人员情况.xlsx").then(function () { });
          });
          //下载文件
          var files = companyPersonnelData.files;
          await this.downFile(files, companyPersonnel_dir);

        }
        //------------------------05-网络与信息安全 ------------------------
        if (fs.existsSync(safety_dir)) {
          var safetyData = exceldata.safetyData;
          var safety_workbook = new excel.Workbook();
          await safety_workbook.xlsx.readFile(safety_dir + "/网络与信息安全保障措施.xlsx").then(function () {
            var worksheet = safety_workbook.getWorksheet("网络与信息安全保障措施"); //获取第一个worksheet
            // console.log("-------------------", workbook.worksheets);
            worksheet.eachRow(function (row, rowNumber) {
              if (rowNumber == 2) {
                row.eachCell(function (cell, colNumber) {
                  if (colNumber == 2) {
                    // console.log('Cell ' + colNumber + ' = ' + cell.type + " " + value);
                    cell.value = safetyData.safety.qualification;
                  }
                });
              }
              if (rowNumber == 3) {
                row.eachCell(function (cell, colNumber) {
                  if (colNumber == 2) {
                    cell.value = safetyData.safety.responsibility;
                  }
                });
              }
            });
            safety_workbook.xlsx.writeFile(safety_dir + "/网络与信息安全保障措施.xlsx").then(function () { });
          });
          //下载文件
          var files = safetyData.files;
          await this.downFile(files, safety_dir);
        }
        //------------------------06-股东信息 ------------------------
        if (fs.existsSync(shareholders_dir)) {
          var shareholdersData = exceldata.shareholdersData;
          var shareholders_workbook = new excel.Workbook();
          await shareholders_workbook.xlsx.readFile(shareholders_dir + "/股东信息.xlsx").then(function () {
            var worksheet = shareholders_workbook.getWorksheet("股权信息"); //获取第一个worksheet
            for (var i = 0; i < shareholdersData.shareholdes.length; i++) {
              // console.log(shareholdersData.shareholdes[i]);
              var shareholde = shareholdersData.shareholdes[i];
              var rw = worksheet.addRow([shareholde.name, shareholde.type, shareholde.idNumber, shareholde.holdProportion, shareholde.superiorShareholder, shareholde.superiorIdNumber, shareholde.companyCode, shareholde.declareCompany, shareholde.waysInvestment, shareholde.investmentAmount, shareholde.currencyType, shareholde.address]);
              rw.eachCell(function (cell) {
                // cell.fill = {
                //   type: 'pattern',
                //   pattern: 'solid',
                //   fgColor: { argb: 'deeded' }
                // };
                // cell.font = { name: 'Arial', size: 14 };
                cell.alignment = { horizontal: 'center' };
                cell.border = {
                  top: { style: "thin" },
                  left: { style: "thin" },
                  bottom: { style: "thin" },
                  right: { style: "thin" }
                };

              });
            }
            shareholders_workbook.xlsx.writeFile(shareholders_dir + "/股东信息.xlsx").then(function () { });
          });

          //下载文件
          var files = shareholdersData.files;
          await this.downFile(files, shareholders_dir);
        }
        //------------------------07-电信业务承诺书 ------------------------
        if (fs.existsSync(telecommunication_dir)) {
          //下载文件
          var files = exceldata.telecommunicationServiceData.files;
          await this.downFile(files, telecommunication_dir);
        }
        //------------------------08-其他材料 ------------------------
        if (fs.existsSync(rotherMaterials_dir)) {
          //下载文件
          var files = exceldata.otherMaterialsData.files;
          await this.downFile(files, rotherMaterials_dir);
        }

        //打包压缩
        var zipdir = id_dir + '.zip';
        var output = fs.createWriteStream(zipdir);
        var archive = archiver('zip');
        archive.on('error', function (err) {
          throw err;
        });
        archive.pipe(output);
        archive.directory(id_dir, false);
        await archive.finalize();

        var resultJson = {
          "id": Id,
          "url": ""
        }
        //把打包文件上传到cos
        var bucket = 'test-1258715722'; /* 必须 */
        var begion = 'ap-beijing';   /* 必须 */
        var that = this;
        var cos = new COS({
          getAuthorization: function (options, callback) {
            // 异步获取临时密钥
            request({
              url: that.cosgetsecreturl,
              data: {
                // 可从 options 取需要的参数
                // Bucket: 'test-1258715722', /* 必须 */
                // Region: 'ap-beijing',    /* 必须 */
              }
            }, function (err, response, body) {
              try {
                var resultdata = JSON.parse(body);
                if (resultdata.status == 1) {
                  var data = resultdata.data;
                  var credentials = data.credentials;
                  Bucket = credentials.tmpBucket;
                  Region = credentials.tmpRegion;
                }
              } catch (e) { }
              if (!data || !credentials) return console.error('credentials invalid');
              callback({
                TmpSecretId: credentials.tmpSecretId,        // 临时密钥的 tmpSecretId
                TmpSecretKey: credentials.tmpSecretKey,      // 临时密钥的 tmpSecretKey
                XCosSecurityToken: credentials.sessionToken, // 临时密钥的 sessionToken
                ExpiredTime: data.expiredTime,               // 临时密钥失效时间戳，是申请临时密钥时，时间戳加 durationSeconds
              });
            });
          }
        });

        var getParam = await new Promise(function (resv, rej) {
          cos.putObject({
            Bucket: bucket, /* 必须 */
            Region: begion,    /* 必须 */
            Key: zipdir,       /* 必须 */
            Body: fs.createReadStream(zipdir),
          }, function (err, data) {
            if (err) {
              rej(false);
            } else {
              if (data.statusCode == 200) {
                resv(data.Location);
              } else {
                resv(false);
              }
            }
          });
        });
        if (getParam) {
          resultJson.url = 'http://' + getParam;
          var icpUpMaterialurl = this.icpMaterialurl + '/web/delivery/materialCtl/updateUrl';
          //请求返回接口，传输上传接口
          await this.restclient.execPost(resultJson, icpUpMaterialurl);
        }

      } catch (error) {
        continue;
      }


    }
    // 删除文件夹
    for (var j = 0; j < datalist.length; j++) {
      var exceldata = datalist[j];
      //编号id
      var Id = exceldata.id;
      //文件夹
      var id_dir = '/tmp/icp/' + Id;
      //压缩包
      var id_zip = id_dir + '.zip';
      if (fs.existsSync(id_dir)) {
        // delete
        child_process.spawn('rm', ['-r', id_dir]);

      }
      if (fs.existsSync(id_zip)) {
        child_process.spawn('rm', [id_zip]);
      }
    }

  }
  //下载文件函数
  async downFile(data, dir) {
    // console.log(data, dir);
    //下载文件
    var bucket = 'test-1258715722'; /* 必须 */
    var region = 'ap-beijing';   /* 必须 */
    var that = this;
    var cos = new COS({
      getAuthorization: function (options, callback) {
        // 异步获取临时密钥
        request({
          url: that.cosgetsecreturl,
          data: {
            // 可从 options 取需要的参数
            // Bucket: "brg-1257943044",   /* 必须 */
            // Region: "ap-nanjing",       /* 必须 */
          }
        }, function (err, response, body) {
          try {
            var resultdata = JSON.parse(body);
            if (resultdata.status == 1) {
              var data = resultdata.data;
              var credentials = data.credentials;
              Bucket = credentials.tmpBucket;
              Region = credentials.tmpRegion;
            }
          } catch (e) { }
          if (!data || !credentials) return console.error('credentials invalid');
          callback({
            TmpSecretId: credentials.tmpSecretId,        // 临时密钥的 tmpSecretId
            TmpSecretKey: credentials.tmpSecretKey,      // 临时密钥的 tmpSecretKey
            XCosSecurityToken: credentials.sessionToken, // 临时密钥的 sessionToken
            ExpiredTime: data.expiredTime,               // 临时密钥失效时间戳，是申请临时密钥时，时间戳加 durationSeconds
          });
        });
      }
    });
    var downfiles = data;
    for (var i = 0; i < downfiles.length; i++) {
      var filedata = downfiles[i];
      var file_url = filedata.file.url;
      var arr = file_url.split("myqcloud.com/")
      if (arr.length > 1) {
        var key = arr[arr.length - 1];
        var key_arr = key.split(".")
        var key_type = key_arr[key_arr.length - 1];
        var title = filedata.title;
        if (title) {
          var titlename = title + '.' + key_type;
          var output = dir + '/' + titlename;
          await this.downLoadFile(bucket, region, cos, key, output);
        }
      }
    }
  }
  downLoadFile(bucket, region, cos, key, output) {
    return new Promise((resolve, reject) => {
      cos.getObject({
        Bucket: bucket, /* 必须 */
        Region: region,    /* 必须 */
        Key: key,
        Output: output,
      }, function (err, data) {
        // console.log(err || data);
        if (err) {
          reject(false);
        } else {
          resolve(true);
        }
      });
    })
  }

  async subDoTask(params) {
    console.log(params);
    console.log("TestTask1.....");
  }
}
module.exports = IcpGenerateTask;