/*
 * @Author: code4eat awesomedema@gmail.com
 * @Date: 2025-04-15 14:49:07
 * @LastEditors: code4eat awesomedema@gmail.com
 * @LastEditTime: 2025-05-27 15:16:16
 * @FilePath: /pfmBackMana/src/pages/baseInfoMana/empMana/service.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { request } from 'umi';

export interface Employee {
  id: number; // 接口返回的 id
  code: string; // 工号
  name: string; // 员工姓名
  password?: string;
  tsType?: number;
  headUrl?: string;
  permission?: string;
  manageRole?: string;
  lastLoginPermission?: string;
}

export interface EmployeeAPIResponse {
  totalCount: number;
  pageSize: number;
  totalPage: number;
  currPage: number;
  list: Employee[];
}

// 角色接口数据结构（单个角色项）
export interface RoleItem {
  id: number; // 记录ID
  empId: number; // 职工ID
  roleId: number; // 角色ID
  empCode: string; // 职工工号
  empName: string; // 职工姓名
  roleCode: string; // 角色代码
  roleName: string; // 角色名称
}

// 查核单位接口数据结构（单个单位项）
export interface DeptItem {
  deptId: number; // 查核单位ID
  deptName: string; // 查核单位名称
  manager_id: string; // 单位负责人ID
  manager_name: string; // 单位负责人姓名
}

// 角色字典项
export interface RoleDictItem {
  roleId: number; // 角色ID
  roleCode: string; // 角色代码
  roleName: string; // 角色名称
}

// 部门字典项
export interface DeptDictItem {
  deptId: number; // 部门ID
  deptName: string; // 部门名称
  manager_id: string; // 单位负责人ID
  manager_name: string; // 单位负责人姓名
}

// 查核组接口数据结构（单个查核组项）
export interface GroupItem {
  id: number; // 记录ID
  employeeId: number; // 职工ID
  manager_id: number; // 查核组长ID
  checkGroupId: number; // 查核组id
  employeeName: string; // 职工姓名
  manager_name: string; // 查核组长姓名
  checkGroupName: string; // 查核组名称
}

// 查核组字典项
export interface GroupDictItem {
  manager_id: number; // 查核组长ID
  checkGroupId: number; // 查核组ID
  manager_name: string; // 查核组长姓名
  checkGroupName: string; // 查核组名称
}

export const fetchEmployeeList = (params?: any) => {
  return request<EmployeeAPIResponse>('/manager/Employee/getEmpList', {
    method: 'GET',
    params: { ...params },
  });
};

// 获取员工部门字典 - 直接返回部门数组 (已关联的)
export const fetchEmpDeptDict = (params?: { empId: number }) => {
  return request<DeptItem[]>('/manager/Employee/getEmpDept', {
    method: 'GET',
    params: { ...params },
  });
};

// 获取员工角色字典 - 直接返回角色数组 (已关联的)
export const fetchEmpRoleDict = (params?: { empId: number }) => {
  return request<RoleItem[]>('/manager/Employee/getEmpRole', {
    method: 'GET',
    params: { ...params },
  });
};

// 获取员工查核组 - 直接返回查核组数组 (已关联的)
export const fetchEmpGroup = (params?: { empId: number }) => {
  return request<GroupItem[]>('/manager/Employee/getEmpGroup', {
    method: 'GET',
    params: { ...params },
  });
};

// 获取可绑定的角色字典数据 - 用于TableSelecter
export const getEmpRoleDict = (params?: { empId: number; filter?: string }) => {
  return request<RoleDictItem[]>('/manager/Employee/getEmpRoleDict', {
    method: 'GET',
    params: { ...params },
  });
};

// 获取可绑定的部门字典数据 - 用于TableSelecter
export const getEmpDeptDict = (params?: { empId: number; filter?: string }) => {
  return request<DeptDictItem[]>('/manager/Employee/getEmpDeptDict', {
    method: 'GET',
    params: { ...params },
  });
};

// 获取可绑定的查核组字典数据 - 用于TableSelecter
export const getEmpGroupDict = (params?: {
  empId: number;
  filter?: string;
}) => {
  return request<GroupDictItem[]>('/manager/Employee/getEmpGroupDict', {
    method: 'GET',
    params: { ...params },
  });
};

// 删除员工角色
export const deleteEmpRole = (ids: number[]) => {
  return request('/manager/Employee/delEmpRole', {
    method: 'POST',
    data: ids,
  });
};

// 删除员工部门
export const deleteEmpDept = (items: { id: string; type: string }[]) => {
  return request('/manager/Employee/delEmpDept', {
    method: 'POST',
    data: items,
  });
};

// 删除员工查核组
export const deleteEmpGroup = (ids: number[]) => {
  return request('/manager/Employee/delEmpGroup', {
    method: 'POST',
    data: ids,
  });
};

// 添加员工角色关联
export const addEmpRole = (data: { id: string; mapIdList: string[] }) => {
  return request('/manager/Employee/addEmpRole', {
    method: 'POST',
    data,
  });
};

// 添加员工部门关联
export const addEmpDept = (data: { 
  id: string; 
  name: string; 
  mapObjList: { id: string; name: string; type: string; }[] 
}) => {
  return request('/manager/Employee/addEmpDept', {
    method: 'POST',
    data,
  });
};

// 添加员工查核组关联
export const addEmpGroup = (data: { id: string; mapIdList: string[] }) => {
  return request('/manager/Employee/addEmpGroup', {
    method: 'POST',
    data,
  });
};

// 同步/刷新员工列表
export const syncEmployeeList = () => {
  return request('/manager/Employee/synEmpList', {
    method: 'POST',
  });
};
