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 salesmanInfo = await this.userService.addChance(datajson);
        var salesmanInfo = {
          salesmanOpcode = "1",
          salesmanId = "1",
          clerkName = "zhangsan",
          clerkPhone = "11111"
        }
        var rc = system.getObject("util.execClient");
        var requrl = this.icUrl + "/bizoptCtl/insertInfo";
        var params =  {
          "businessMode": datajson.needNum,
          "businessType":datajson.consultType,
          "servicerCode":datajson.servicerCode,
          "servicerName":datajson.servicerName,
          "serviceCode":datajson.regionId,
          "serviceName":datajson.regionName,
          "contactsName":datajson.contactsName,
          "contactsPhone":datajson.contactsMoblie,
          "businessType":datajson.consultType,
          "businessName":datajson.consultTypeName,
          "salesmanOpcode":salesmanInfo.salesmanOpcode,
          "salesmanId":salesmanInfo.salesmanId,
          "clerkName":salesmanInfo.clerkName,
          "clerkPhone":salesmanInfo.clerkPhone
        }   
        var rtn = await rc.execPost(params, requrl);
        var j = JSON.parse(rtn.stdout);
        return j;
      }
      catch(error){
        return  system.getResultError(error);
      }
      
  }

  //退回商机处理
  async needClose(datajson){
    try{
      var rc = system.getObject("util.execClient");
      var requrl = this.icUrl + "/schemeCtl/updateStatusByDemandCode";
      var params = {
        "businessMode": datajson.needNum,
        "currentStatus":"已退回",
        "statusReason":datajson.desc
      }
      var rtn = await rc.execPost(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){
    try{
      //获取分配的业务员信息
      //var salesmanInfo = await this.userService.addChance(datajson);
      var salesmanInfo = {
        salesmanOpcode = "1",
        salesmanId = "1",
        clerkName = "zhangsan",
        clerkPhone = "11111",
        skuCode = "1111",
        costPrice = "2000"
      }
      var rc = system.getObject("util.execClient");
      var requrl = this.icUrl + "/deliverybillCtl/insertInfo";
      var params = {
        
      }
      var rtn = await rc.execPost(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();