package com.op.mes.controller;

import com.op.common.core.domain.BaseFileData;
import com.op.common.core.utils.poi.ExcelUtil;
import com.op.common.core.web.controller.BaseController;
import com.op.common.core.web.domain.AjaxResult;
import com.op.common.core.web.page.TableDataInfo;
import com.op.common.log.annotation.Log;
import com.op.common.log.enums.BusinessType;
import com.op.common.security.annotation.RequiresPermissions;

import com.op.mes.domain.BaseFile;
import com.op.mes.service.IBaseFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 附件Controller
 *
 * @author Open Platform
 * @date 2023-07-10
 */
@RestController
@RequestMapping("/file")
public class BaseFileController extends BaseController {
	@Autowired
	private IBaseFileService baseFileService;

	/**
	 * 查询附件列表
	 */
	@RequiresPermissions("system:file:list")
	@GetMapping("/list")

	public TableDataInfo list(BaseFile baseFile) {
		startPage();
		List<BaseFile> list = baseFileService.selectBaseFileList(baseFile);
		return getDataTable(list);
	}

	/**
	 * 导出附件列表
	 */
	@RequiresPermissions("system:file:export")
	@Log(title = "附件", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	public void export(HttpServletResponse response, BaseFile baseFile) {
		List<BaseFile> list = baseFileService.selectBaseFileList(baseFile);
		ExcelUtil<BaseFile> util = new ExcelUtil<BaseFile>(BaseFile.class);
		util.exportExcel(response, list, "附件数据");
	}

	/**
	 * 获取附件详细信息
	 */
	@RequiresPermissions("system:file:query")
	@GetMapping(value = "/{fileId}")
	public AjaxResult getInfo(@PathVariable("fileId") String fileId) {
		return success(baseFileService.selectBaseFileByFileId(fileId));
	}

	/**
	 * 新增附件
	 */
	@RequiresPermissions("system:file:add")
	@Log(title = "附件", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody BaseFile baseFile) {
		return toAjax(baseFileService.insertBaseFile(baseFile));
	}

	/**
	 * 修改附件
	 */
	@RequiresPermissions("system:file:edit")
	@Log(title = "附件", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody BaseFile baseFile) {
		return toAjax(baseFileService.updateBaseFile(baseFile));
	}

	/**
	 * 删除附件
	 */
	@RequiresPermissions("system:file:remove")
	@Log(title = "附件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{fileIds}")
	public AjaxResult remove(@PathVariable String[] fileIds) {
		return toAjax(baseFileService.deleteBaseFileByFileIds(fileIds));
	}


	@Log(title = "批量上传附件", businessType = BusinessType.INSERT)
	@PostMapping("/upLoadFile")
	public Boolean add(@RequestBody List<BaseFileData> baseFiles) {
		return baseFileService.insertBaseFileBatch(baseFiles);
	}

}
