var system=require("../../system")
var settings=require("../../../config/settings");
const CtlBase = require("../ctl.base");
const uuidv4 = require('uuid/v4');
class PartnerInfoCtl extends CtlBase{
  constructor(){
    super(CtlBase.getServiceName(PartnerInfoCtl));
    this.roleS=system.getObject("service.roleSve");
    this.tradeSve=system.getObject("service.tradeSve");
  }
  async createWork(queryobj,pobj,req){
    console.log("createWork****************");
    if(pobj.status==1){
      pobj.status='0';
    }else{
      pobj.status='1';
    }
    pobj.version+=1;
    await this.service.update(pobj);
    //TODO:创建容器启动或停用脚本==，成功后赋值status，status状态为：0停用，1启动

    var result={

    }
    return system.getResult2(result,null);
  }
  async getPartnerInfo(gobj,pobj,req){//合伙人申请
    var user=req.session.user;
    if(user==null){
      return null;
    }else {
      return user.partnerinfo;
    }
  }
async addPartnerInfo(gobj,pobj,req){//合伙人申请
      var user=req.session.user;
      if(user==null){
        return {code:-1,msg:"用户信息为空"}
      }
      if(pobj==null||pobj.partnerInfo==null){
        return {code:-1,msg:"请求信息为空"}
      }
      if(pobj.partnerInfo.apply_type==null||pobj.partnerInfo.apply_type==""){
        return {code:-1,msg:"请求信息类型为空"}
      }
       //银行卡验证
      var realName=pobj.partnerInfo.realName;
      var identityCard=pobj.partnerInfo.identityCard;
      var cardNo=pobj.partnerInfo.cardNo;
      if( realName == null || identityCard == null || cardNo == null ){
        return {code:-1,msg:"请完善信息"}
      }
      var result=await this.tradeSve.verifyBankcard(cardNo,identityCard,realName);
      if(result.code<0){
        return {code:-1,msg:"用于提现三要素，身份证号码、银行卡号、开户人姓名验证未通过"}
      }
      var role= await this.roleS.findOneByCode(pobj.partnerInfo.apply_type);
      if(role==null){
        return {code:-1,msg:"角色错误"}
      }
       var result= await this.service.createOrUpdatePartnerInfo(user,role.id,pobj);
       if(result==null){
         return {code:-1,msg:"数据操作失败"}
       }
       req.session.user.partnerinfo=result;
       return {code:"1",msg:"ok"};
   }

}
module.exports=PartnerInfoCtl;
