const system=require("../../system");
const ServiceBase=require("../sve.base");
class AuthService extends ServiceBase{
  constructor(){
     super(ServiceBase.getDaoName(AuthService));
  }

  //字符串数组参数
  async findAuthsByRole(rolecodestr,cmid){
    //{where:{id:{[this.db.Op.in]:ids}}}
    //var newattrs=rolecodestr.split(",");
    var aths=await this.dao.model.findAll({
      raw:true,
      attributes:["bizcode","authstrs","codepath"],
      where:{rolecode:{[this.db.Op.in]:rolecodestr},company_id:cmid}});
    return aths;
  }
  async saveAuths(auths,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.company_id=cmid;
         var objrtn=await self.dao.model.findOrCreate({
           defaults:tmpAuth,
           where:{rolecode:tmpAuth.rolecode,bizcode:tmpAuth.bizcode,company_id:cmid},//注意这里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,company_id:cmid},transaction:t})
         }
       }
       var aths=await self.dao.model.findAll({where:{rolecode:tmpAuth.rolecode,company_id:cmid},transaction:t});
       return aths;
     });
  }
}
module.exports=AuthService;
