import request from '@/utils/request'
import settings from '@/settings'
const { systemPrefix } = settings
/**
 * 文件初始化加载查询
 * @param {*} data
 * @returns
 */
export function queryFileQuery(data) {
  data.systemNo = systemPrefix
  return request({
    url: `${systemPrefix}/com/system/fileHandler/queryFileQuery`,
    method: 'post',
    data: data,
    headers: {
      'Content-Type': 'application/json;charset=UTF-8'
    }
  })
}

/**
 * 文件下载
 * @param {*} data
 */
export function fileDownLoadHandler(data) {
  return request({
    url: `${systemPrefix}/com/system/fileHandler/downFile`,
    method: 'post',
    data: data,
    responseType: 'blob',
  })
}

/**
 * 文件删除操作
 * @param {} data
 */
export function fileRemoveHandler(data) {
  return request({
    url: `${systemPrefix}/com/system/fileHandler/deleteFile`,
    method: 'post',
    data: data,
  })
}

/**
 * 交易关联文件删除
 * @param {*} data
 */
export function traceRelFileRemoveHanlder(data) {
  return request({
    url: `${systemPrefix}/com/system/fileHandler/deleteYwFile`,
    method: 'post',
    data: data,
    headers: {
      'Content-Type': 'multipart/form-data'
    },
  })
}

/**
 * 删除文件片段信息
 * @param {*} data
 */
export function fileSplitFragment(data) {
  return request({
    url: `${systemPrefix}/com/system/fileHandler/removeFileFragement`,
    method: 'post',
    data: data
  })
}

/**
 * 文件预览转换处理
 * @param {*} data
 * @returns
 */
export function filePreviewHandler(data, uploaded) {
  return request({
    url: `${systemPrefix}/com/system/fileHandler/filePreview`,
    method: 'post',
    data: data,
    headers: {
      'Content-Type': uploaded ? 'application/x-www-form-urlencoded' : 'multipart/form-data'
    },
  })
}

/**
 * 加载缩略图
 * @param {*} data
 * @returns
 */
export function queryThumbUrl(data) {
  return new Promise((resolve, reject) => {
    request({
      url: `${systemPrefix}/com/system/fileHandler/getPreThumbUrl`,
      method: 'post',
      data: data,
      headers: {
        'Content-Type': "application/x-www-form-urlencoded"
      },
    }).then(function (resp) {
      resolve({ ...resp, paramter: data })
    }).catch(function (error) {
      reject({ ...error, paramter: data });
    })
  })
}


/***
 * 文件上传操作
 */
export function uploadFile(dataObj) {
  const { formData, fileInfo } = dataObj
  return new Promise((resolve, reject) => {
    request({
      url: `${systemPrefix}/com/system/fileHandler/multipleFileUpload`,
      method: 'post',
      data: formData,
      headers: {
        'Content-Type': 'multipart/form-data'
      },
    }).then(function (resp) {
      resolve({ ...resp, paramter: fileInfo })
    }).catch(function (error) {
      console.error('error uploadFile uploadFile  : ', error)
      reject({ ...error, paramter: fileInfo });
    })
  })
}
/**
 * 文件打包下载
 * @param {*} data
 */
export function multipleFileDown(data) {
  return new Promise((resolve, reject) => {
    request({
      url: `${systemPrefix}/com/system/fileHandler/downMultipleFile`,
      method: 'post',
      data: data,
      requestType: 'json',
      responseType: 'blob',
    }).then(resp => {
      resolve(resp)
    }).catch(err => {
      reject(err)
    })
  })
}


export function queryAllPartition(data) {
  return request({
    url: `${systemPrefix}/com/system/fileHandler/partition/trade/relationship`,
    method: 'post',
    data: data
  })
}

/**
 * 文件分区查询
 * @param {*} data
 * @returns
 */
export function queryPartition(data) {
  return request({
    url: `${systemPrefix}/com/system/fileHandler/partition/listwithpage`,
    method: 'post',
    data: data
  })
}

/**
 * 文件分区存储
 * @param {*} data
 * @returns
 */
export function insertPartition(data) {
  return request({
    url: `${systemPrefix}/com/system/fileHandler/partition/save`,
    method: 'post',
    data: data
  })
}

/**
 * 修改文件分区
 * @param {*} data
 * @returns
 */
export function updatePartition(data) {
  return request({
    url: `${systemPrefix}/com/system/fileHandler/partition/modify`,
    method: 'post',
    data: data
  })
}

/**
 * 文件分区删除
 * @param {*} data
 * @returns
 */
export function deletePartition(data) {
  return request({
    url: `${systemPrefix}/com/system/fileHandler/partition/delete`,
    method: 'post',
    data: { partitionNo: data.join() }
  })
}
/**
 *
 * @param {*} data
 * @returns
 */
export function savePartitionRel(data) {
  return request({
    url: `${systemPrefix}/com/system/fileHandler/partition/trade/addOrModify`,
    method: 'post',
    data: data,
    requestType: 'json'
  })
}

/**
 * 删除关联信息
 * @param {*} data
 * @returns
 */
export function deletePartitionRelApi(data) {
  return request({
    url: `${systemPrefix}/com/system/fileHandler/partition/trade/delete`,
    method: 'post',
    data: data,
    requestType: 'json'
  })
}

/**
 *
 * @param {*} data
 * @returns
 */
export function queryPartitionOrDefault(data) {
  return request({
    url: `${systemPrefix}/com/system/fileHandler/partition/queryPartitionOrDefault`,
    method: 'post',
    data: data,
    requestType: 'json'
  })
}
/**
 * 获取文件最大上传个数限制
 * @returns
 */
export function queryMaxFileSize() {
  return request({
    url: `${systemPrefix}/com/system/fileHandler/partition/queryMaxFileSize`,
    method: 'post',
    requestType: 'json'
  })
}

/**
 * pdf 文件下载
 * @param {} param
 */
export function downPdf(data) {
  return request({
    url: `${systemPrefix}/com/system/fileHandler/downPdf`,
    method: 'post',
    data: data,
    responseType: 'blob',
  })
}

/**
 * 查询附件个数
 * @param {*} data
 * @returns
 */
export function queryFileCount(data) {
  return request({
    url: `${systemPrefix}/com/system/fileHandler/queryFileCount`,
    method: 'post',
    data: data,
    requestType: 'json'
  })
}
