const system=require("../../system");
const ServiceBase=require("../sve.base");
class AuthService extends ServiceBase{
  constructor(){
     super(ServiceBase.getDaoName(AuthService));
  }
  //字符串数组参数
  async findAuthsByRole(rolecodestr){
    //{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}}});
    return aths;
  }
  async saveAuths(auths){
     //先按照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];
         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},transaction:t})
         }
       }
       var aths=await self.dao.model.findAll({where:{rolecode:tmpAuth.rolecode},transaction:t});
       return aths;
     });
  }
}
module.exports=AuthService;
