const system=require("../../../system");
const ServiceBase=require("../../sve.base");
class AuthService extends ServiceBase{
  constructor(){
     super("auth",ServiceBase.getDaoName(AuthService));
  }
  //字符串数组参数
  //前端角色查询，按照来源应用和来源公司查询
  async findAuthsByRole(roleids,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:{role_id:{[this.db.Op.in]:roleids},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:{role_id:tmpAuth.role_id,bizcode:tmpAuth.bizcode},//注意这里bizcode存储的是节点的code值
           transaction:t,
         });
         console.log("vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv");
         console.log(objrtn);
         if(!objrtn[1].created){
           //就执行更新操作
           await objrtn[0].update(tmpAuth,{where:{role_id:tmpAuth.role_id,bizcode:tmpAuth.bizcode},transaction:t})
         }
       }
       var aths=await self.dao.model.findAll({where:{role_id:tmpAuth.role_id,app_id:tmpAuth.app_id},transaction:t});
       return aths;
     });
  }
}
module.exports=AuthService;
