const system=require("../../../system");
const ServiceBase=require("../../sve.base");
class AuthService extends ServiceBase{
  constructor(){
     super("auth",ServiceBase.getDaoName(AuthService));
  }
  //字符串数组参数
  async findAuthsByRole(rolecodestr,appid,comid){
    //{where:{id:{[this.db.Op.in]:ids}}}
    //var newattrs=rolecodestr.split(",");
    var aths=await this.dao.model.findAll({
      attributes:["bizcode","authstrs","codepath"],
      where:{rolecode:{[this.db.Op.in]:rolecodestr},app_id:appid,company_id:comid}});
    return aths;
  }
  async saveAuths(auths,appid,cmid){
     //先按照code 和 bizcode查询
     var self=this;
     console.log("yyyyyyyyyvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv");
     console.log(auths);
     return self.db.transaction(async function (t){
       for(var i=0;i<auths.length;i++){
         var tmpAuth=auths[i];
         tmpAuth.app_id=appid;
         tmpAuth.company_id=cmid;
         var objrtn=await self.dao.model.findOrCreate({
           defaults:tmpAuth,
           where:{rolecode:tmpAuth.rolecode,bizcode:tmpAuth.bizcode},//注意这里bizcode存储的是节点的code值
           transaction:t,
         });
         console.log("vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv");
         console.log(objrtn);
         if(!objrtn[1].created){
           //就执行更新操作
           await objrtn[0].update(tmpAuth,{where:{rolecode:tmpAuth.rolecode,bizcode:tmpAuth.bizcode,app_id:tmpAuth.app_id},transaction:t})
         }
       }
       var aths=await self.dao.model.findAll({where:{rolecode:tmpAuth.rolecode,app_id:tmpAuth.app_id},transaction:t});
       return aths;
     });
  }
}
module.exports=AuthService;
