import Vue from 'vue'
import co from 'co'
import { HttpResetClass } from 'vue-client'

/**
 * 取得所有授权策略
 */
let getAllDataGen = function * () {
  let response = yield Vue.resetpost('/rs/logic/getLicenseData', {data:{}}, {resolveMsg: null, rejectMsg: null})
  return response.data
}

export function getAllData () {
  let gen = getAllDataGen()
  return co(gen)
}
//获取具体授权信息
let getAllRightGen = function * (val) {
  let response = yield Vue.resetpost('/rs/sql/getLicenseById', {data:{
      condition: "resourceid ='"+val.id+"'"
    }}, {resolveMsg: null, rejectMsg: null})
  return response.data
}

//获取具体授权信息
let getRightMsgGen = function * (val) {
  let http = new HttpResetClass()
  return http.load('POST', '/rs/search', {data:{
    source: 'this.getRights().where(row.getType() == $function$))',
    userid: val.id
  }}, {resolveMsg: null, rejectMsg: null})
}

export function getAllRight () {
  let gen = getAllRightGen()
  return co(gen)
}

/**
 * 取得资源的所有授权策略
 * @param res
 */
let getDataGen = function * (res) {
  let response = yield Vue.resetpost('/rs/license/getData', {parentid: res.id}, {resolveMsg: null, rejectMsg: null})
  return response.data
}

export function getData (res) {
  let gen = getDataGen(res)
  return co(gen)
}

/**
 * 取得所有被授权资源名称路径串，用","分割
 * @param res
 * @returns {*}
 */
let getDataNamesGen = function * (res) {
  let response = yield Vue.resetpost('/rs/license/getData', {parentid: res.id}, {resolveMsg: null, rejectMsg: null})
  let result = ''
  for (let license of response.data) {
    if (result !== '') {
      result += '|'
    }
    result += license.names
  }
  return result
}

export function getDataNames (res) {
  let gen = getDataNamesGen(res)
  return co(gen)
}
//获取具体授权信息
export function getDataRights (res) {
  let gen = getAllRightGen(res)
  return co(gen)
}

export function getRightsMsg (res) {
  let gen = getRightMsgGen(res)
  return co(gen)
}

