var system = require("../../../system")
const http = require("http")
const querystring = require('querystring');
var settings = require("../../../../config/settings");
const CtlBase = require("../../ctl.base");
const logCtl = system.getObject("web.common.oplogCtl");

var cacheBaseComp = null;
class TradetransferCtl extends CtlBase {
  constructor() {
    super("transfer", CtlBase.getServiceName(TradetransferCtl));
    this.aliclient = system.getObject("util.aliApiClient");
  }
  async update(p, q, req) {
    if (req && req.session && req.session.user) {
      p.onlyCode = req.session.user.unionId;
    }
    if (req.codepath) {
      p.codepath = req.codepath;
    }
    var a = null;

    if (p.status == "CONFIRM_ORDER") {
      p.aliorder_status = "1",
        a = await this.service.confirmorder(p)
    }
    if (p.status == "CLOSE_ORDER_ONLY") {
      p.aliorder_status = "11",
        a = await this.service.refuseorder(p)
    }
    if (p.status == "PROVIDE_MATERIAL") {
      // if (p.tm_cert_url != null && transfer_cert_url != null){

      // }
      p.aliorder_status = "3",
        a = await this.service.supplymaterail(p)
    }
    if (p.status == "BUYER_EXPRESS") {
      p.aliorder_status = "5",
        a = await this.service.posttransfermatereial(p)
    }
    if (p.status == "BUYER_PROVIDE_MATERIAL") {
      p.aliorder_status = "6",
        a = await this.service.receivedtransfermaterail(p)
    }
    if (p.status == "SUBMIT_TO_SBJ") {
      p.aliorder_status = "7",
        a = await this.service.submittedtransfermaterail(p)
    }
    if (p.status == "SBJ_ACCEPT") {
      p.aliorder_status = "8",
        a = await this.service.acceptedtransfermaterail(p)
    }

    if (p.status == "SBJ_SUCCESS") {
      p.aliorder_status = "8",
        a = await this.service.approvedtransfer(p)
    }
    if (p.status == "SBJ_FAIL") {
      p.aliorder_status = "８",
        a = await this.service.refusetransfer(p)
    }

    if (p.status == "TRANSFER_SUCCESS") {
      p.aliorder_status = "12",
        a = await this.service.tradesuccess(p)
    }
    if (p.status == "TRANSFER_FAIL") {
      p.aliorder_status = "10",
        a = await this.service.tradefail(p)
    }
    if (!a) {
      var rd = await this.service.update(p);
      return system.getResult(rd);
    } else {
      return system.getResult(null, "阿里请求失败");
    }
  }

  //获取签名
  async generateuploadfilepolicy(obj) {
    var self = this;;
    var gobj = {
      action: "GenerateUploadFilePolicy",
      reqbody: { FileType: "LEGAL_NOTICE" }
    }
    
    var rst=await self.aliclient.reqbyget(gobj);
    var source={
        OSSAccessKeyId:rst.AccessId,
        policy:rst.EncodedPolicy,
        Signature:rst.Signature,
        Bucket:'trade-mark-user-upload',
        url:"https://trade-mark-user-upload.oss-cn-beijing.aliyuncs.com",
        filedir:rst.FileDir
    }
    return source;
  }
}
module.exports = TradetransferCtl;
