import axios from '@/common/services/axios-instance'

import loginService from '@/login/login-service'
const reqSuffix = '' // '/a.manage'
class OrgService {
  // 通用导入组件使用接口
  importFileData(url, params) {
    return axios({
      url: `${url}${reqSuffix}`,
      method: 'post',
      data: {
        ...params,
      },
    })
  }

  // 通用导入组件检测使用接口
  importFileDataCheck(url, params) {
    return axios({
      url: `${url}${reqSuffix}`,
      method: 'post',
      data: {
        ...params,
      },
    })
  }

  // 根据部门信息获取对应组织根节点信息
  getOrgRootByOrg(params) {
    return axios({
      url: `api/framework/v1/group-manager/group/root-node${reqSuffix}`,
      method: 'get',
      params,
    })
  }

  // 获取临时文件信息,导出时用到
  getTmpFileInfo(id) {
    return axios({
      url: `api/framework/v1/tmp-file/${id}${reqSuffix}`,
      method: 'post',
    })
  }

  // 修改根节点
  saveDepRoot(params) {
    return axios({
      url: `api/framework/v1/group-manager/root-group${reqSuffix}`,
      method: 'put',
      data: params,
    })
  }

  // 获取根
  getOrgDepRoot(params) {
    return axios({
      url: `api/framework/v1/group-manager/admin/group/root-node${reqSuffix}`, // `api/framework/v1/group-manager/group/root-node`,
      method: 'get',
    })
  }

  // 获取组织树
  getOrgDepTree(params) {
    return axios({
      url: `api/framework/v1/group-manager/admin/group/getChildren${reqSuffix}`, // `api/framework/v1/group-manager/group/getChildren`,
      method: 'get',
      params: {
        parent: params.id,
        hierarchical: 0,
      },
    })
  }

  // 获取组织列表
  getOrgDepList(data) {
    return axios({
      url: `api/framework/v1/group-manager/list${reqSuffix}`,
      method: 'post',
      data,
    })
  }

  // 删除部门
  deleteOrgDep(params) {
    return axios({
      url: `api/framework/v1/group-manager/group${reqSuffix}`,
      method: 'delete',
      params: {
        ids: params.join(','),
      },
    })
  }

  // 获取级别
  getOrgLevel(params) {
    return axios({
      url: `api/framework/v1/level-manager/list${reqSuffix}`,
      method: 'post',
      data: {
        expressions: [
          {
            name: 'type',
            op: 'eq',
            dataType: 'str',
            stringValue: params.code,
          },
        ],
        maxResults: -1,
        orderBy: 'index asc',
      },
    })
  }

  // 获取密级
  getClassifyLevel(params) {
    return axios({
      url: `api/framework/v1/user-classified/${reqSuffix}`,
      method: 'get',
    })
  }

  // 获取部门
  getOrgDep(params) {
    return axios({
      url: `api/framework/v1/group-manager/group/${params.id}${reqSuffix}`,
      method: 'get',
    })
  }

  // 新建组织
  createOrgDep(params) {
    return axios({
      url: `api/framework/v1/group-manager/group${reqSuffix}`,
      method: 'post',
      data: {
        ...params,
      },
    })
  }

  // 更新组织
  saveOrgDep(params) {
    return axios({
      url: `api/framework/v1/group-manager/group${reqSuffix}`,
      method: 'put',
      data: {
        ...params,
      },
    })
  }

  // 批量更新组织
  batchUpdateOrgDep(data) {
    return axios({
      url: `api/framework/v1/group-manager/groups`,
      method: 'put',
      data,
    })
  }

  // 获取部门分级权限开关情况
  getHieraSwitch(params) {
    return axios({
      url: `api/framework/v1/hiera-manage/switch/${params.id}${reqSuffix}`,
      method: 'get',
    })
  }

  // 保存部门分级权限开关情况
  saveHieraSwitch(params) {
    return axios({
      url: `api/framework/v1/hiera-manage/switch${reqSuffix}`,
      method: 'post',
      data: {
        ...params,
      },
    })
  }

  // 获取分权的管理员列表
  getHieraManager(params) {
    return axios({
      url: `api/framework/v1/hiera-manage/manager/${params.id}${reqSuffix}`,
      method: 'get',
    })
  }

  // 新增分权的管理员
  addHieraManager(params) {
    return axios({
      url: `api/framework/v1/hiera-manage/manager${reqSuffix}`,
      method: 'post',
      data: {
        ...params,
      },
    })
  }

  // 删除分权的管理员
  deleteHieraManager(params) {
    return axios({
      url: `api/framework/v1/hiera-manage/manager${reqSuffix}`,
      method: 'delete',
      params: {
        ids: params.join(','),
      },
    })
  }

  // 获取分权的角色列表
  getHieraRole(params) {
    return axios({
      url: `api/framework/v1/hiera-manage/roles/${params.id}${reqSuffix}`,
      method: 'get',
    })
  }

  // 新增分权的角色
  addHieraRole(params) {
    return axios({
      url: `api/framework/v1/hiera-manage/roles${reqSuffix}`,
      method: 'post',
      data: {
        ...params,
      },
    })
  }

  // 删除分权的角色
  deleteHieraRole(params) {
    return axios({
      url: `api/framework/v1/hiera-manage/roles${reqSuffix}`,
      method: 'delete',
      data: params,
    })
  }

  // 获取待选角色列表信息
  getHieraRoleList(params) {
    return axios({
      url: `api/framework/v1/hiera-manage/role-list/${params.id}${reqSuffix}`,
      method: 'get',
    })
  }

  // 分级设置菜单相关

  // 获取已下放的菜单根
  getOrgHieraMenuRoot(params) {
    return axios({
      url: `api/framework/v1/hiera-manage/auth-menu/root/${params.id}${reqSuffix}`,
      method: 'get',
    })
  }

  // 获取已下放的菜单按上级id
  getOrgHieraMenuTree(params) {
    return axios({
      url: `api/framework/v1/hiera-manage/auth-menu/children/${params.id}${reqSuffix}`,
      method: 'get',
    })
  }

  // 删除已下放菜单
  deleteHieraMenu(params) {
    return axios({
      url: `api/framework/v1/hiera-manage/menu${reqSuffix}`,
      method: 'delete',
      params,
    })
  }

  // 获取可供下放的菜单树
  getOrgHieraMenu(params) {
    return axios({
      url: `api/framework/v1/hiera-manage/menu-tree/${params.id}${reqSuffix}`,
      method: 'get',
    })
  }

  // 新增菜单
  addOrgHieraMenu(params) {
    return axios({
      url: `api/framework/v1/hiera-manage/menu${reqSuffix}`,
      method: 'post',
      data: {
        ...params,
      },
    })
  }

  // 分级设置流程
  // 获取已下放的流程按上级id
  getOrgHieraFlowTree(params) {
    return axios({
      url: `api/framework/v1/hiera-manage/flow/children${reqSuffix}`,
      method: 'get',
      params,
    })
  }

  // 获取已下放的流程
  getOrgHieraFlowList(params) {
    return axios({
      url: `api/framework/v1/hiera-manage/auth-flow/${params.id}${reqSuffix}`,
      method: 'get',
    })
  }

  // 设置或取消已下放的流程
  saveOrgHieraFlow(params) {
    return axios({
      url: `api/framework/v1/hiera-manage/auth-flow${reqSuffix}`,
      method: 'post',
      data: {
        ...params,
      },
    })
  }

  // 下放全部流程
  saveOrgHieraAllFlow(params) {
    return axios({
      url: `api/framework/v1/hiera-manage/auth-all-flow${reqSuffix}`,
      method: 'post',
      data: {
        ...params,
      },
    })
  }

  // 获取用户列表
  getOrgUserList(data) {
    return axios({
      url: `api/framework/v1/user-manager/all/users_query`,
      method: 'post',
      data,
    })
  }

  // 获取用户信息
  getOrgUser(params) {
    return axios({
      url: `api/framework/v1/user-manager/users/id/${params.id}${reqSuffix}`,
      method: 'get',
    })
  }

  // 新建用户
  createOrgUser(params) {
    return axios.get('api/ott').then((res) => {
      const ott = res.data
      return axios({
        url: `api/framework/v1/user-manager/users${reqSuffix}`,
        method: 'post',
        data: {
          ...params,
          password: loginService.encryptBySm4(ott.id, ott.token, params.password),
          repassword: loginService.encryptBySm4(ott.id, ott.token, params.repassword),
        },
      })
    })
  }

  // 更新用户
  saveOrgUser(params) {
    if (params.password && params.password !== '') {
      return axios.get('api/ott').then((res) => {
        const ott = res.data
        return axios({
          url: `api/framework/v1/user-manager/users/${params.account}${reqSuffix}`,
          method: 'put',
          data: {
            ...params,
            password: loginService.encryptBySm4(ott.id, ott.token, params.password),
            repassword: loginService.encryptBySm4(ott.id, ott.token, params.repassword),
          },
        })
      })
    } else {
      return axios({
        url: `api/framework/v1/user-manager/users/${params.account}${reqSuffix}`,
        method: 'put',
        data: {
          ...params,
        },
      })
    }
  }

  // 批量更新用户
  batchUpdateOrgUser(data) {
    return axios({
      url: `api/framework/v1/user-manager/users`,
      method: 'put',
      data,
    })
  }

  // 删除用户
  deleteOrgUser(params) {
    return axios({
      url: `api/framework/v1/user-manager/users${reqSuffix}`,
      method: 'delete',
      params: {
        ids: params.join(','),
      },
    })
  }

  exportDep = {
    // 导出压缩包格式
    exportZip(params) {
      return axios({
        url: `api/framework/v1/group-manager/export${reqSuffix}`,
        method: 'post',
        // data: {
        //   ...params,
        // },
        params: {
          ids: params.join(','),
        },
      })
    },

    // 导出excel部门
    exportDepExcel(params) {
      return axios({
        url: `api/framework/v1/group-manager/export-excel${reqSuffix}`,
        method: 'post',
        // data: {
        //   ...params,
        // },
        params: {
          ids: params.join(','),
        },
      })
    },

    // 导出excel用户
    exportUserExcel(params) {
      return axios({
        url: `api/framework/v1/group-manager/export-user-excel${reqSuffix}`,
        method: 'post',
        // data: {
        //   ...params,
        // },
        params: {
          ids: params.join(','),
        },
      })
    },

    // 导出压缩包格式
    importZip(params) {
      return axios({
        url: `api/framework/v1/group-manager/import${reqSuffix}`,
        method: 'post',
        data: {
          ...params,
        },
      })
    },

    // 导出excel部门
    importDepExcel(params) {
      return axios({
        url: `api/framework/v1/group-manager/import-excel${reqSuffix}`,
        method: 'post',
        data: {
          ...params,
        },
      })
    },
  }

  exportUser = {
    // 导出压缩包格式
    exportZip(params) {
      return axios({
        url: `api/framework/v1/user-manager/export${reqSuffix}`,
        method: 'post',
        // data: {
        //   ...params,
        // },
        params: {
          ids: params.join(','),
        },
      })
    },

    // 导出excel用户
    exportUserExcel(params) {
      return axios({
        url: `api/framework/v1/user-manager/export-excel${reqSuffix}`,
        method: 'post',
        // data: {
        //   ...params,
        // },
        params: {
          ids: params.join(','),
        },
      })
    },

    // 导出压缩包格式
    importZip(params) {
      return axios({
        url: `api/framework/v1/group-manager/import${reqSuffix}`,
        method: 'post',
        data: {
          ...params,
        },
      })
    },

    // 导出excel用户
    importUserExcel(params) {
      return axios({
        url: `api/framework/v1/user-manager/import-excel${reqSuffix}`,
        method: 'post',
        data: {
          ...params,
        },
      })
    },
  }

  // 导入待完善代码
  importApp(params) {
    return axios({
      url: `api/framework/v1/user-manager/users${reqSuffix}`,
      method: 'delete',
      params: {
        ids: params.join(','),
      },
    })
  }

  group = {
    // 获取列表
    getList(params) {
      return axios({
        url: `api/framework/v1/usergroup-manager/query/usergroups${reqSuffix}`,
        method: 'post',
        data: {
          expressions: [
            {
              name: 'parentId',
              op: 'eq',
              dataType: 'str',
              stringValue: params.id,
            },
          ],
          orderBy: 'weight asc,id asc',
        },
      })
    },

    // 删除
    deleteDocs(params) {
      return axios({
        url: `api/framework/v1/usergroup-manager/userGroups-batch${reqSuffix}`,
        method: 'delete',
        params: {
          ids: params.join(','),
        },
      })
    },

    // 获取
    getDoc(params) {
      return axios({
        url: `api/framework/v1/usergroup-manager/userGroup/${params.id}${reqSuffix}`,
        method: 'get',
      })
    },

    // 新建
    createDoc(params) {
      return axios({
        url: `api/framework/v1/usergroup-manager/userGroup${reqSuffix}`,
        method: 'post',

        ...params,
      })
    },

    // 更新
    updateDoc(params) {
      return axios({
        url: `api/framework/v1/usergroup-manager/userGroup${reqSuffix}`,
        method: 'put',
        data: {
          ...params,
        },
      })
    },

    // 获取成员信息
    getUsers(params) {
      return axios({
        url: `api/framework/v1/usergroup-manager/userGroup/users/${params.id}${reqSuffix}`,
        method: 'get',
      })
    },

    // 添加成员信息
    addUsers(params) {
      return axios({
        url: `api/framework/v1/usergroup-manager/userGroup/users-batch${reqSuffix}`,
        method: 'post',
        ...params,
      })
    },

    // 删除成员信息
    deleteUsers(params) {
      return axios({
        url: `api/framework/v1/usergroup-manager/userGroup/users-batch${reqSuffix}`,
        method: 'delete',
        ...params,
      })
    },
  }

  classify = {
    // 获取列表
    // 已通过列表数据源获取

    // 删除
    deleteDocs(params) {
      return axios({
        url: `api/framework/v1/user-classified/${params.join(',')}${reqSuffix}`,
        method: 'delete',
        // params: {
        //   ids: params.join(','),
        // },
      })
    },

    // 获取
    getDoc(params) {
      return axios({
        url: `api/framework/v1/user-classified/${params.id}${reqSuffix}`,
        method: 'get',
      })
    },

    // 新建 or 更新
    saveDoc(params) {
      return axios({
        url: `api/framework/v1/user-classified/${reqSuffix}`,
        method: 'post',
        data: {
          ...params,
        },
      })
    },

    // 设置用户密级
    setAssignedUser(params) {
      return axios({
        url: `api/framework/v1/user-classified/set${reqSuffix}`,
        method: 'get',
        params: {
          ...params,
        },
      })
    },

    // 获取当前登录用户密级
    getByCurUser(params) {
      return axios({
        url: `api/framework/v1/user-classified/set${reqSuffix}`,
        method: 'get',
        data: {
          ...params,
        },
      })
    },
  }

  roles = {
    // 获取列表
    // 已通过列表数据源获取

    // 删除
    deleteDocs(params) {
      return axios({
        url: `api/framework/v1/role-manager/roles/${params.join(',')}${reqSuffix}`,
        method: 'delete',
        // params: {
        //   ids: params.join(','),
        // },
      })
    },

    // 获取
    getDoc(params) {
      return axios({
        url: `api/framework/v1/role-manager/roles/${params.id}/info${reqSuffix}`,
        method: 'get',
      })
    },

    // 新建 or 更新
    saveDoc(params) {
      return axios({
        url: `api/framework/v1/role-manager/roles${reqSuffix}`,
        method: 'post',
        data: {
          ...params,
        },
      })
    },

    // 添加成员用户
    addUsers(params) {
      return axios({
        url: `api/framework/v1/role-manager/roles/${params.id}/assign-users${reqSuffix}`,
        method: 'post',
        params: {
          accounts: params.account,
        },
      })
    },

    // 删除成员信息
    deleteUsers(params) {
      return axios({
        url: `api/framework/v1/role-manager/roles/${params.id}/deassign-users${reqSuffix}`,
        method: 'post',
        params: {
          accounts: params.account,
        },
      })
    },

    // 获取子角色
    getChildRoles(params) {
      return axios({
        url: `api/framework/v1/role-manager/roles/${params.id}/roles${reqSuffix}`,
        method: 'get',
      })
    },

    // 添加成员角色
    addRoles(params) {
      return axios({
        url: `api/framework/v1/role-manager/roles/${params.id}/auth-roles${reqSuffix}`,
        method: 'post',
        params: {
          roleCodes: params.account,
        },
      })
    },

    // 获取父角色
    getParentRoles(params) {
      return axios({
        url: `api/framework/v1/role-manager/roles/${params.id}/assigned-roles${reqSuffix}`,
        method: 'get',
      })
    },
    // 被添加成员角色
    beAddRoles(params) {
      return axios({
        url: `api/framework/v1/role-manager/roles/${params.id}/assign-roles${reqSuffix}`,
        method: 'post',
        params: {
          roleCodes: params.account,
        },
      })
    },

    /// 导出压缩包格式
    exportZip(params) {
      return axios({
        url: `api/framework/v1/role-manager/export${reqSuffix}`,
        method: 'post',

        params: {
          codes: params.join(','),
        },
      })
    },

    // 导出excel用户
    exportExcel(params) {
      return axios({
        url: `api/framework/v1/role-manager/export-excel${reqSuffix}`,
        method: 'post',
        params: {
          codes: params.join(','),
        },
      })
    },

    // 角色列表已通过sd-data-table数据源直接实现

    // 获取角色树数据
    getTree(params) {
      return axios({
        url: `api/framework/v1/role-manager/query/node-roles/${reqSuffix}`,
        method: 'post',
        data: {
          ...params,
        },
      })
    },
  }

  // 获取用户组
  getOrgUsea(params) {
    return true
  }

  // 组织排序
  sortOrgDep(data) {
    return axios({
      url: 'api/framework/v1/group-manager/groups/weight',
      method: 'put',
      data,
    })
  }

  // 用户排序
  sortOrgUser(data) {
    return axios({
      url: 'api/framework/v1/user-manager/users/weight',
      method: 'put',
      data,
    })
  }

  // 群组成员排序
  sortOrgGroup(data) {
    return axios({
      url: 'api/framework/v1/usergroup-manager/userGroup/user/weight',
      method: 'put',
      data,
    })
  }

  queryUserByCode(userCodes, ids = [], options = {}) {
    const param = {
      expressions: [
        {
          dataType: 'str',
          name: 'account',
          op: 'in',
          stringValues: userCodes,
        },
      ],
      ids,
    }
    if (options.secretLevel) {
      param.securityLevel = options.secretLevel
    }
    param.hierarchical = options.hierarchical

    if (param.hierarchical === undefined) {
      param.hierarchical = ''
    } else if (param.hierarchical) {
      param.hierarchical = '1'
    } else {
      param.hierarchical = '0'
    }
    param.rootIds = options.rootIds
    return axios.post(`api/framework/v1/user-manager/users_query`, param).then((res) => {
      return res.data
    })
  }

  // 分级：获取应用分类树（分类+应用）
  getAppTransferTree(params) {
    return axios({
      url: `api/framework/v1/we-app-transfer/tree`,
      method: 'get',
      params,
    })
  }

  // 应用分级：下放
  appIssued(params) {
    return axios({
      url: `api/framework/v1/we-app-transfer/create`,
      method: 'post',
      params,
    })
  }

  // 应用分级：回收
  appTakeBack(params) {
    return axios({
      url: `api/framework/v1/we-app-transfer/delete`,
      method: 'post',
      params,
    })
  }
}

export default new OrgService()
