const system = require("../../../system");
const ServiceBase = require("../../sve.base");
var settings = require("../../../../config/settings");
var RPCClient = require('@alicloud/pop-core').RPCClient;
var OSS = require('ali-oss');
const crypto = require('crypto');
class TradetransferService extends ServiceBase {
  constructor() {
    super("transfer", ServiceBase.getDaoName(TradetransferService));
    this.aliclient = system.getObject("util.aliApiClient");
  }

  //订单创建
  async createtransfer(obj) {
    if (!obj.BizId) {
      return {
        "errorCode": "error",
        "errorMsg": "订单号不能为空",
        "module": { "orderNumber": "" },
        "requestId": obj.requestid,
        "success": false
      }
    }
    if (!obj.UserName) {
      return {
        "errorCode": "error",
        "errorMsg": "用户不能为空",
        "module": { "orderNumber": "" },
        "requestId": obj.requestid,
        "success": false
      }
    }
    if (!obj.Mobile) {
      return {
        "errorCode": "error",
        "errorMsg": "用户手机号不能为空",
        "module": { "orderNumber": "" },
        "requestId": obj.requestid,
        "success": false
      }
    }
    if (!obj.RegisterNumber) {
      return {
        "errorCode": "error",
        "errorMsg": "注册号不能为空",
        "module": { "orderNumber": "" },
        "requestId": obj.requestid,
        "success": false
      }
    }
    if (!obj.Classification) {
      return {
        "errorCode": "error",
        "errorMsg": "大类不能为空",
        "module": { "orderNumber": "" },
        "requestId": obj.requestid,
        "success": false
      }
    }
    if (!obj.Price) {
      return {
        "errorCode": "error",
        "errorMsg": "价格不能为空",
        "module": { "orderNumber": "" },
        "requestId": obj.requestid,
        "success": false
      }
    }
    var transferinfo = await this.findOne({ ali_bizid: obj.BizId });
    var fq_ordernum = await this.getBusUid("ZR");
    if (transferinfo) {
      return {
        "errorCode": "error",
        "errorMsg": "订单已存在",
        "module": { "orderId": transferinfo.fq_ordernum },
        "requestId": obj.requestid,
        "success": false
      }
    } else {
      var source = {
        tm_number: obj.RegisterNumber,
        user_name: obj.UserName,
        ali_bizid: obj.BizId,
        mobile: obj.Mobile,
        price: obj.Price,
        ncl_one: obj.Classification,
        fq_ordernum: fq_ordernum
      }
      await this.create(source);
      return {
        "errorCode": "OK",
        "errorMsg": "",
        "module": { "orderNumber": fq_ordernum },
        "requestId": obj.requestid,
        "success": true
      }
    }
  }
  //订单查询
  async ordersel(obj) {
    if (!obj.BizId) {
      return {
        "errorCode": "error",
        "errorMsg": "订单号不能为空",
        "module": { "orderId": "", "orderStatus": "" },
        "requestId": obj.requestid,
        "success": false
      }
    }

    var transferinfo = await this.findOne({ ali_bizid: obj.BizId });
    if (transferinfo) {
      return {
        "errorCode": "OK",
        "errorMsg": "",
        "module": { "orderId": transferinfo.fq_ordernum, "orderStatus": transferinfo.status },
        "requestId": obj.requestid,
        "success": true
      }
    } else {
      return {
        "errorCode": "error",
        "errorMsg": "订单不存在",
        "module": { "orderId": "", "orderStatus": "" },
        "requestId": obj.requestid,
        "success": false
      }
    }

  }

  /*
  返回20位业务订单号
  prefix：业务前缀
  */
  async getBusUid(prefix) {
    prefix = (prefix || "");
    if (prefix) {
      prefix = prefix.toUpperCase();
    }
    var prefixlength = prefix.length;
    var subLen = 8 - prefixlength;
    var uidStr = "";
    if (subLen > 0) {
      uidStr = await this.getUidInfo(subLen, 60);
    }
    var timStr = moment().format("YYYYMMDDHHmm");
    return prefix + timStr + uidStr;
  }

  //订单关闭
  async orderclose(obj) {
    if (!obj.BizId) {
      return {
        "errorCode": "error",
        "errorMsg": "订单号不能为空",
        "requestId": obj.requestid,
        "success": false
      }
    }
    var transferinfo = await this.findOne({ ali_bizid: obj.BizId });
    if (transferinfo) {
      transferinfo = transferinfo.dataValues;
      if (transferinfo.status == "CLOSE_ORDER_REFUND") {
        return {
          "errorCode": "OK",
          "errorMsg": "",
          "requestId": obj.requestid,
          "success": true
        }
      } else {
        transferinfo.status = "CLOSE_ORDER_REFUND";
        await this.update(transferinfo);
        return {
          "errorCode": "OK",
          "errorMsg": "",
          "requestId": obj.requestid,
          "success": true
        }
      }
    } else {
      return {
        "errorCode": "error",
        "errorMsg": "订单不存在",
        "requestId": obj.requestid,
        "success": false
      }
    }
  }

  //业务员分配
  async fenpeiowner(obj) {
    if (!obj.BizId) {
      return {
        "errorCode": "error",
        "errorMsg": "订单号不能为空",
        "requestId": obj.requestid,
        "success": false
      }
    }
    var transferinfo = await this.findOne({ ali_bizid: obj.BizId });
  }


  //订单确认
  async confirmorder(obj) {
    if (!obj.ali_bizid) {
      return {
        status: "-1",
        msg: "参数异常"
      }
    }
    var BizId = obj.ali_bizid;
    var obj = {
      action: "ConfirmOrder",
      reqbody: {
        BizId: BizId
      }
    }
    return await this.aliclient.reqbyget(obj);
  }

  //取消订单
  async refuseorder(obj) {
    if (!obj.ali_bizid) {
      return {
        status: "-1",
        msg: "参数异常"
      }
    }
    var BizId = obj.ali_bizid;
    var obj = {
      action: "RefuseOrder",
      reqbody: {
        BizId: BizId
      }
    }
    return await this.aliclient.reqbyget(obj);
  }

  //补充材料
  async supplymaterail(obj) {
    var a = await this.generateuploadfilepolicy(obj);
    console.log("+++++++++++++++");
    console.log(a);
    return a;
  }

  //尾款支付---调取阿里云查询后修改订单
  async querytradeproducelist(obj) {
    if (!obj.ali_bizid) {
      return {
        status: "-1",
        msg: "参数异常"
      }
    }
    var BizId = obj.ali_bizid;
    var obj = { "parameter": ["&Action=QueryTradeProduceList", "&&BizId=" + BizId] }
    var tradeinfo = await this.getaliparameter(obj);

  }

  //服务商邮寄材料给用户
  async posttransfermatereial(obj) {
    if (!obj.ali_bizid) {
      return {
        status: "-1",
        msg: "参数异常"
      }
    }
    if (!obj.logistics) {
      return {
        status: "-1",
        msg: "参数异常"
      }
    }
    var BizId = obj.ali_bizid;
    var obj = {
      action: "PostTransferMaterial",
      reqbody: {
        BizId: BizId,
        Logistics: obj.logistics
      }
    }
    return await this.aliclient.reqbyget(obj);

  }

  //用户盖章后邮寄服务商
  async receivedtransfermaterail(obj) {
    if (!obj.ali_bizid) {
      return {
        status: "-1",
        msg: "参数异常"
      }
    }
    var BizId = obj.ali_bizid;
    var obj = {
      action: "ReceivedTransferMaterail",
      reqbody: {
        BizId: BizId
      }
    }
    return await this.aliclient.reqbyget(obj);
  }

  //服务商递交到商标局
  async submittedtransfermaterail(obj) {
    if (!obj.ali_bizid) {
      return {
        status: "-1",
        msg: "参数异常"
      }
    }
    var BizId = obj.ali_bizid;
    var obj = {
      action: "SubmittedTransferMaterail",
      reqbody: {
        BizId: BizId
      }
    }
    return await this.aliclient.reqbyget(obj);
  }

  //商标局受理
  async acceptedtransfermaterail(obj) {
    if (!obj.ali_bizid) {
      return {
        status: "-1",
        msg: "参数异常"
      }
    }
    var obj = {
      action: "AcceptedTransferMaterail",
      reqbody: {
        BizId: obj.ali_bizid
      }
    }
    return await this.aliclient.reqbyget(obj);
  }

  //商标局核准通过
  async approvedtransfer(obj) {
    if (!obj.ali_bizid) {
      return {
        status: "-1",
        msg: "参数异常"
      }
    }
    var obj = {
      action: "ApprovedTransfer",
      reqbody: {
        BizId: obj.ali_bizid
      }
    }
    return await this.aliclient.reqbyget(obj);
  }

  //商标局核准不通过
  async refusetransfer(obj) {
    if (!obj.ali_bizid) {
      return {
        status: "-1",
        msg: "参数异常"
      }
    }
    var obj = {
      action: "RefuseTransfer",
      reqbody: {
        BizId: obj.ali_bizid
      }
    }
    return await this.aliclient.reqbyget(obj);
  }

  //完成交易成功
  async tradesuccess(obj) {
    if (!obj.ali_bizid) {
      return {
        status: "-1",
        msg: "参数异常"
      }
    }
    var obj = {
      action: "TradeSuccess",
      reqbody: {
        BizId: obj.ali_bizid
      }
    }
    return await this.aliclient.reqbyget(obj);
  }

  //交易失败
  async tradefail(obj) {
    if (!obj.ali_bizid) {
      return {
        status: "-1",
        msg: "参数异常"
      }
    }
    var obj = {
      action: "TradeFail",
      reqbody: {
        BizId: obj.ali_bizid
      }
    }
    return await this.aliclient.reqbyget(obj);
  }

  //买卖家沟通记录
  async writecommunicationlog(obj) {

  }

  //获取签名
  async generateuploadfilepolicy(obj) {
    var self = this;;
    var gobj = {
      action: "GenerateUploadFilePolicy",
      reqbody: { FileType: "ATTORNEY" }
    }
    if (obj.BizId) {
      gobj["reqbody"] = { BizId: BizId, FilyType: "PARTNER_SYNC_FILE" };
    }
    return await self.aliclient.reqbyget(gobj);
  }

}
module.exports = TradetransferService;
