var system = require("../../../../system");
class TxHandler {
  constructor() {
    this.icUrl = "http://ic.com:8000/web/bizchance";
    this.userService = system.getObject("service.auth.userSve");
  }
  /**
   * 需要调用生成者接口，把数据丢入队列
   * 组装生产者需要的数据结构
   * @param {*} datajson 
   */
  //新商机处理
  async addChance(datajson){
      console.log("put in queue",datajson);
      try{
        //获取分配的业务员信息
        var ConsultTypeName = datajson.actionBody.ConsultTypeName.split("/")[2];
        var salesmanInfo = await this.userService.getBizUserForBizChance(datajson.actionBody.ContactsMoblie, datajson.actionBody.ServicerName, ConsultTypeName,datajson.actionBody.RegionName);
        console.log("salesman   " + salesmanInfo);
        var rc = system.getObject("util.execClient");
        var requrl = this.icUrl + "/bizoptCtl/insertInfo";
        var params =  {
          "businessMode": datajson.actionBody.NeedNum,
          "servicerCode":datajson.actionBody.ServicerCode,
          "servicerName":datajson.actionBody.ServicerName,
          "serviceCode":datajson.actionBody.RegionId,
          "serviceName":datajson.actionBody.RegionName,
          "contactsName":datajson.actionBody.ContactsName,
          "contactsPhone":datajson.actionBody.ContactsMoblie,
          "businessType":datajson.actionBody.ConsultType,
          "businessName":datajson.actionBody.ConsultTypeName
        } 
        if (datajson.actionBody.Notes && datajson.actionBody.Notes != 'undefined'){
          params.memoInfo = datajson.actionBody.Notes;
        }
        if (salesmanInfo && salesmanInfo != 'undefined'){
          params.salesmanOpcode = salesmanInfo.opath;
          params.salesmanId = salesmanInfo.userId;
          params.clerkName = salesmanInfo.userName;
          params.clerkPhone = salesmanInfo.mobile
        }  
        var rtn = await rc.execPost3(params, requrl);
        var j = JSON.parse(rtn.stdout);
        console.log(JSON.stringify(j),"RRRRRRRRRRRRRRR");
        return j;
      }
      catch(error){
        console.log("TTTTTTTTTTTTTT");
        console.log(error);
        return  system.getResultError(error);
      }
      
  }

  //退回商机处理
  async needClose(datajson){
    console.log("put in queue",datajson);
    try{
      var rc = system.getObject("util.execClient");
      var requrl = this.icUrl + "/schemeCtl/updateStatusByDemandCode";
      var params = {
        "businessMode": datajson.actionBody.needNum,
        "currentStatus":"已退回",
        "statusReason":datajson.actionBody.refusalContent
      }
      var rtn = await rc.execPost3(params, requrl);
      var j = JSON.parse(rtn.stdout);
      if (j.status == 0) {
        return {
          "status":1,    //1代表成功，否则失败
          "msg":"",
          "data":"",
          "requestId":""
        }
      }
      else{
        return j;
      }
    }
    catch(error){
      return  system.getResultError(error);
    }

  }

  //交付单处理
  async orderSubmit(datajson){
    console.log("put in queue",datajson);
    try{
      //获取分配的业务员信息
      var salesmanInfo = await this.userService.getBizUserForDelivery(datajson.actionBody.contactsMoblie, datajson.actionBody.servicerName, datajson.actionBody.productTypeName, datajson.actionBody.skucode);
      var rc = system.getObject("util.execClient");
      var requrl = this.icUrl + "/deliverybillCtl/insertInfo";
      var params = {
        "deliverNumber":datajson.actionBody.orderNum,
        "servicerCode":datajson.actionBody.servicerCode,
        "servicerName":datajson.actionBody.servicerName,
        "serviceCode":datajson.actionBody.regionId,
        "serviceName":datajson.actionBody.regionName,
        "businessType":datajson.actionBody.consultType,
        "businessName":datajson.actionBody.consultTypeName,
        "skuCode":datajson.actionBody.skucode,
        "deliveryInfo":{
          "txOrderNum":datajson.actionBody.txOrderNum,
          "isAdviser":"已分配",//是否分配顾问
          "contactsName":datajson.actionBody.contactsName,
          "contactsPhone":datajson.actionBody.contactsMoblie,
          "payStatus":"待支付"
        },
        "companyInfo":{}
      }
      if (salesmanInfo && salesmanInfo != 'undefined'){
        params.salesmanOpcode = salesmanInfo.opath;
        params.salesmanId = salesmanInfo.userId;
        params.clerkName = salesmanInfo.userName;
        params.clerkPhone = salesmanInfo.mobile;
        params.costPrice = salesmanInfo.cost
      } 
      if (datajson.actionBody.order_snapshot.needNum && datajson.actionBody.order_snapshot.needNum != 'undefined'){
        params.businessMode = datajson.actionBody.order_snapshot.needNum;//商机编号
      }
      if (datajson.actionBody.order_snapshot.sellingPrice && datajson.actionBody.order_snapshot.sellingPrice != 'undefined'){
        params.sellingPrice = datajson.actionBody.order_snapshot.sellingPrice;//价格
      }
      if (datajson.actionBody.order_snapshot.channelSource && datajson.actionBody.order_snapshot.channelSource != 'undefined'){
        params.channelSource = datajson.actionBody.order_snapshot.channelSource;//渠道名称
      }
      if (datajson.actionBody.order_snapshot.channelNumber && datajson.actionBody.order_snapshot.channelNumber != 'undefined'){
        params.channelNumber = datajson.actionBody.order_snapshot.channelNumber;//渠道编码
      }
      if (datajson.actionBody.order_snapshot.isWhether && datajson.actionBody.order_snapshot.isWhether != 'undefined'){
        params.deliveryInfo.isWhether = datajson.actionBody.order_snapshot.isWhether;//是否刻章
      }
      if (datajson.actionBody.order_snapshot.financial && datajson.actionBody.order_snapshot.financial != 'undefined'){
        params.deliveryInfo.financial = datajson.actionBody.order_snapshot.financial;//财税服务
      }
      if (datajson.actionBody.order_snapshot.officialSeal && datajson.actionBody.order_snapshot.officialSeal != 'undefined'){
        params.deliveryInfo.officialSeal = datajson.actionBody.order_snapshot.officialSeal;//公章扫描件
      }
      if (datajson.actionBody.order_snapshot.officialSeal && datajson.actionBody.order_snapshot.officialSeal != 'undefined'){
        params.deliveryInfo.officialSeal = datajson.actionBody.order_snapshot.officialSeal;//公章扫描件
      }
      if (datajson.actionBody.order_snapshot.businessLicense && datajson.actionBody.order_snapshot.businessLicense != 'undefined'){
        params.deliveryInfo.businessLicense = datajson.actionBody.order_snapshot.businessLicense;//营业执照扫描件
      }
      if (datajson.actionBody.order_snapshot.taxpayerType && datajson.actionBody.order_snapshot.taxpayerType != 'undefined'){
        params.deliveryInfo.taxpayerType = datajson.actionBody.order_snapshot.taxpayerType;//纳税人类型
      }
      if (datajson.actionBody.order_snapshot.buyDuration && datajson.actionBody.order_snapshot.buyDuration != 'undefined'){
        params.deliveryInfo.buyDuration = datajson.actionBody.order_snapshot.buyDuration;//购买时长
      }
      if (datajson.actionBody.order_snapshot.buyTime && datajson.actionBody.order_snapshot.buyTime != 'undefined'){
        params.deliveryInfo.buyTime = datajson.actionBody.order_snapshot.buyTime;//购买时间
      }
      if (datajson.actionBody.order_snapshot.isRenew && datajson.actionBody.order_snapshot.isRenew != 'undefined'){
        params.deliveryInfo.isRenew = datajson.actionBody.order_snapshot.isRenew;//是否自动续费
      }
      if (datajson.actionBody.order_snapshot.whetherType && datajson.actionBody.order_snapshot.whetherType != 'undefined'){
        params.deliveryInfo.whetherType = datajson.actionBody.order_snapshot.whetherType;//刻章类型
      }
      if (datajson.actionBody.order_snapshot.subjectType && datajson.actionBody.order_snapshot.subjectType != 'undefined'){
        params.deliveryInfo.subjectType = datajson.actionBody.order_snapshot.subjectType;//代理记账主体类型
      }
      if (datajson.actionBody.order_snapshot.companyName && datajson.actionBody.order_snapshot.companyName != 'undefined'){
        params.deliveryInfo.companyName = datajson.actionBody.order_snapshot.companyName;//公司名称
        params.companyInfo.companyName = datajson.actionBody.order_snapshot.companyName;
      }
      if (datajson.actionBody.order_snapshot.companyProperties && datajson.actionBody.order_snapshot.companyProperties != 'undefined'){
        params.companyInfo.companyProperties = datajson.actionBody.order_snapshot.companyProperties;//公司性质
      }
      if (datajson.actionBody.order_snapshot.fullAddress && datajson.actionBody.order_snapshot.fullAddress != 'undefined'){
        params.companyInfo.fullAddress = datajson.actionBody.order_snapshot.fullAddress;//详细地址
      }
      if (datajson.actionBody.order_snapshot.engagedIndustry && datajson.actionBody.order_snapshot.engagedIndustry != 'undefined'){
        params.companyInfo.engagedIndustry = datajson.actionBody.order_snapshot.engagedIndustry;//从事行业
      }
      if (datajson.actionBody.order_snapshot.businessScope && datajson.actionBody.order_snapshot.businessScope != 'undefined'){
        params.companyInfo.businessScope = datajson.actionBody.order_snapshot.businessScope;//经营范围
      }
      if (datajson.actionBody.order_snapshot.operatingPeriod && datajson.actionBody.order_snapshot.operatingPeriod != 'undefined'){
        params.companyInfo.operatingPeriod = datajson.actionBody.order_snapshot.operatingPeriod;//经营期限
      }
      if (datajson.actionBody.order_snapshot.registeredCapital && datajson.actionBody.order_snapshot.registeredCapital != 'undefined'){
        params.companyInfo.registeredCapital = datajson.actionBody.order_snapshot.registeredCapital;//注册资本
      }
      if (datajson.actionBody.order_snapshot.creditCode && datajson.actionBody.order_snapshot.creditCode != 'undefined'){
        params.companyInfo.creditCode = datajson.actionBody.order_snapshot.creditCode;//统一社会信用代码
      }
      if (datajson.actionBody.order_snapshot.companyType && datajson.actionBody.order_snapshot.companyType != 'undefined'){
        params.companyInfo.companyType = datajson.actionBody.order_snapshot.companyType;//公司类型
      }
      if (datajson.actionBody.order_snapshot.establishedTime && datajson.actionBody.order_snapshot.establishedTime != 'undefined'){
        params.companyInfo.establishedTime = datajson.actionBody.order_snapshot.establishedTime;//成立时间
      }
      if (datajson.actionBody.order_snapshot.shareholderName && datajson.actionBody.order_snapshot.shareholderName != 'undefined'){
        params.companyInfo.shareholderName = datajson.actionBody.order_snapshot.shareholderName;//法人代表
      }

      var rtn = await rc.execPost3(params, requrl);
      var j = JSON.parse(rtn.stdout);
      if (j.status == 0) {
        return {
          "status":1,    //1代表成功，否则失败
          "msg":"",
          "data":"",
          "requestId":""
        }
      }
      else{
        return j;
      }
    }
    catch(error){
      return system.getResultError(error);
    }
  }

    //关闭交付单处理
  async needClose(datajson){
    console.log("put in queue",datajson);
    try{
      var rc = system.getObject("util.execClient");
      var requrl = this.icUrl + "/deliverybillCtl/updateStatusByDeliverCode";
      var params = {
        "deliverNumber": datajson.actionBody.orderNum,
        "deliverStatus":"closed",
        "statusReason":datajson.actionBody.refusalContent
      }
      var rtn = await rc.execPost3(params, requrl);
      var j = JSON.parse(rtn.stdout);
      if (j.status == 0) {
        return {
          "status":1,    //1代表成功，否则失败
          "msg":"",
          "data":"",
          "requestId":""
        }
      }
      else{
        return j;
      }
    }
    catch(error){
      return  system.getResultError(error);
    }
  
  }
}

module.exports = new TxHandler();

// (async ()=>{
//   var task = new TxHandler();
//   var ConsultTypeName = "/工商服务/公司注册/".split("/")[2];
//   console.log(ConsultTypeName);
//   var d = await task.userService.getBizUserForBizChance("1880156771","公司宝","公司注册","北京");
//   console.log("ddddddddddddd");
//   console.log(JSON.stringify(d));
//   console.log("dddddddddddddd");
// })()

