/*
 * @Author: code4eat awesomedema@gmail.com
 * @Date: 2024-01-01 10:00:00
 * @LastEditors: code4eat awesomedema@gmail.com
 * @LastEditTime: 2025-06-30 10:30:00
 * @FilePath: /pfmBackMana/src/pages/checkItemMana/service.ts
 * @Description: 查核项管理页面接口服务
 */

import { request } from 'umi';

// 查核项数据类型定义
export type CheckItemType = {
  id: number;
  name: string;
  rightAnswerText: string;
  rightAnswerImage: string | null;
  checkModeList: string;
  checkModeNameList: string; // 查核方式名称列表
  moreDeduction: number;
  score: number | null;
  subtotal: number | null;
};

// 缺陷项数据类型定义
export type DefectItemType = {
  id: number;
  checkItemId: number; // 查核项ID
  resultName: string; // 缺陷项名称
  percentScore: number; // 分数
};

// 查核结果数据类型定义
export type CheckResultItemType = {
  id: number;
  checkItemId: number; // 查核项ID
  name: string; // 查核结果名称
  score: number; // 分值
  scoreType: number; // 分值类型：0-数值，1-占比
  defaultFlag: number; // 默认项标识：0-否，1-是
  fastFlag: number; // 快捷项标识：0-否，1-是
};

// 查核项分类树节点类型
export type CheckItemTreeNode = {
  key: string;
  title: string;
  children?: CheckItemTreeNode[];
  type?: string;
  score?: number;
  checkMethod?: string;
  isStatistics?: boolean;
};

// 查核方式类型定义
export type CheckModeType = {
  itemCode: string;
  itemName: string;
};

// 查核要点数据类型定义
export type CheckPointItemType = {
  id: number;
  checkItemId: number; // 查核项ID
  name: string; // 查核要点名称
  checkPointId: string; // 查核要点ID（显示用）
  checkPointName: string; // 查核要点名称（显示用）
};

// 获取查核项列表数据
export const getData = (params?: any) => {
  return request<{
    totalCount: number;
    pageSize: number;
    totalPage: number;
    currPage: number;
    list: CheckItemType[];
  }>('/manager/CheckItem/getCheckItem', {
    method: 'GET',
    params: {
      current: 1,
      pageSize: 10,
      filter: '',
      ...params,
    },
  });
};

// 获取缺陷项列表数据（无分页）
export const getDefectData = (params?: any) => {
  return request<DefectItemType[]>('/manager/CheckItem/getCheckItemDefect', {
    method: 'GET',
    params: {
      checkItemId: '',
      filter: '',
      ...params,
    },
  });
};

// 获取查核结果列表数据（无分页）
export const getCheckResultData = (params?: any) => {
  return request<CheckResultItemType[]>(
    '/manager/CheckItem/getCheckItemResult',
    {
      method: 'GET',
      params: {
        checkItemId: '',
        filter: '',
        ...params,
      },
    },
  );
};

// 获取查核项分类树
export const getCheckItemTree = () => {
  return request<{
    data: CheckItemTreeNode[];
    success: boolean;
  }>('/manager/checkItem/getCheckItemTree', {
    method: 'GET',
  });
};

// 新增查核项
export type AddCheckItemType = {
  name: string; // 查核项名称
  rightAnswerText: string; // 应知应会文字
  rightAnswerImage?: string; // 应知应会图片
  checkModeList: string; // 查核方式代码
  moreDeduction: number; // 缺陷项类型：0单选 1多选
  score: number; // 该查核项总分
  subtotal: number; // 是否统计小计：0不是 1是
};

export const addData = (data: AddCheckItemType) => {
  return request('/manager/CheckItem/addCheckItem', {
    method: 'POST',
    data,
  });
};

// 新增缺陷项
export type AddDefectItemType = {
  checkItemId: number; // 查核项ID
  resultName: string; // 缺陷项名称
  percentScore: number; // 分数
};

export const addDefectData = (data: AddDefectItemType) => {
  return request('/manager/CheckItem/addCheckItemDefect', {
    method: 'POST',
    data,
  });
};

// 新增查核结果
export type AddCheckResultItemType = {
  checkItemId: number; // 查核项ID
  name: string; // 查核结果名称
  score: number; // 分值
  scoreType: number; // 分值类型：0-数值，1-占比
  defaultFlag: number; // 默认项标识：0-否，1-是
  fastFlag: number; // 快捷项标识：0-否，1-是
};

export const addCheckResultData = (data: AddCheckResultItemType) => {
  return request('/manager/CheckItem/addCheckItemResult', {
    method: 'POST',
    data,
  });
};

// 编辑查核项
export type UpdateCheckItemType = {
  id: number; // 查核项ID
  name: string; // 查核项名称
  rightAnswerText: string; // 应知应会文字
  rightAnswerImage?: string; // 应知应会图片
  checkModeList: string; // 查核方式代码
  moreDeduction: number; // 缺陷项类型：0单选 1多选
  score: number; // 该查核项总分
  subtotal: number; // 是否统计小计：0不是 1是
};

export const editData = (data: UpdateCheckItemType) => {
  return request('/manager/CheckItem/updateCheckItem', {
    method: 'POST',
    data,
  });
};

// 编辑缺陷项
export type UpdateDefectItemType = {
  id: number; // 缺陷项ID
  checkItemId: number; // 查核项ID
  resultName: string; // 缺陷项名称
  percentScore: number; // 分数
};

export const editDefectData = (data: UpdateDefectItemType) => {
  return request('/manager/CheckItem/updateCheckItemDefect', {
    method: 'POST',
    data,
  });
};

// 编辑查核结果
export type UpdateCheckResultItemType = {
  id: number; // 查核结果ID
  checkItemId: number; // 查核项ID
  name: string; // 查核结果名称
  score: number; // 分值
  scoreType: number; // 分值类型：0-数值，1-占比
  defaultFlag: number; // 默认项标识：0-否，1-是
  fastFlag: number; // 快捷项标识：0-否，1-是
};

export const editCheckResultData = (data: UpdateCheckResultItemType) => {
  return request('/manager/CheckItem/updateCheckItemResult', {
    method: 'POST',
    data,
  });
};

// 删除查核项
export const delData = (ids: Array<string | number>) => {
  return request('/manager/CheckItem/deleteCheckItem', {
    method: 'POST',
    data: ids,
  });
};

// 删除缺陷项
export const delDefectData = (ids: Array<string | number>) => {
  return request('/manager/CheckItem/delCheckItemDefect', {
    method: 'POST',
    data: ids,
  });
};

// 删除查核结果
export const delCheckResultData = (ids: Array<string | number>) => {
  return request('/manager/CheckItem/delCheckItemResult', {
    method: 'POST',
    data: ids,
  });
};

// 获取查核方式列表
export const getCheckItemModeList = () => {
  return request<CheckModeType[]>('/manager/CheckItem/getCheckItemModeList', {
    method: 'GET',
  });
};

// 复制查核结果
export type CopyCheckItemResultType = {
  sourceId: string; // 来源查核项ID
  targetIdList: string[]; // 目标查核项ID列表
};

export const copyCheckItemResult = (data: CopyCheckItemResultType) => {
  return request('/manager/CheckItem/copyBatchCheckItemResult', {
    method: 'POST',
    data,
  });
};

// 获取查核项分类详情
export const getCategoryDetail = (categoryId: string) => {
  return request<{
    id: string;
    name: string;
    type: string;
    score: number;
    checkMethod: string;
    isStatistics: boolean;
    description: string;
  }>('/manager/checkItem/getCategoryDetail', {
    method: 'GET',
    params: { categoryId },
  });
};

// 获取查核要点列表数据（无分页）
export const getCheckPointData = (params?: any) => {
  return request<CheckPointItemType[]>('/manager/CheckItem/getCheckItemCheckpoint', {
    method: 'GET',
    params: {
      checkItemId: '',
      filter: '',
      ...params,
    },
  });
};

// 新增查核要点
export type AddCheckPointItemType = {
  checkItemId: number; // 查核项ID
  name: string; // 查核要点名称
};

export const addCheckPointData = (data: AddCheckPointItemType) => {
  return request('/manager/CheckItem/addCheckItemCheckpoint', {
    method: 'POST',
    data,
  });
};

// 编辑查核要点
export type UpdateCheckPointItemType = {
  id: number; // 查核要点ID
  checkItemId: number; // 查核项ID
  name: string; // 查核要点名称
};

export const editCheckPointData = (data: UpdateCheckPointItemType) => {
  return request('/manager/CheckItem/updateCheckItemCheckpoint', {
    method: 'POST',
    data,
  });
};

// 删除查核要点
export const delCheckPointData = (ids: Array<string | number>) => {
  return request('/manager/CheckItem/delCheckItemCheckpoint', {
    method: 'POST',
    data: ids,
  });
};

// 获取待添加查核要点列表数据
export const getItemPendingPageCheckpoint = (params?: any) => {
  return request<{
    totalCount: number;
    pageSize: number;
    totalPage: number;
    currPage: number;
    list: Array<{
      id: string;
      name: string;
      pointCategory: number;
      pointCategoryName: string;
    }>;
  }>('/manager/CheckItem/getItemPendingPageCheckpoint', {
    method: 'GET',
    params: {
      current: 1,
      pageSize: 10,
      checkItemId: '',
      filter: '',
      ...params,
    },
  });
};

// 添加查核要点到查核项
export type AddCheckItemCheckpointType = {
  id: string; // 查核项ID
  name: string; // 查核项名称
  mapObjList: Array<{
    id: string; // 查核要点ID
    name: string; // 查核要点名称
  }>;
};

export const addCheckItemCheckpoint = (data: AddCheckItemCheckpointType) => {
  return request('/manager/CheckItem/addCheckItemCheckpoint', {
    method: 'POST',
    data,
  });
};
