/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.security.trans.common.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.security.trans.common.service.IFileService;
import org.security.ht.vo.common.FileVO;
import org.security.ht.vo.common.UploadVO;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * 公共-附件表 控制器
 *
 * @author BladeX
 * @since 2022-07-04
 */
@RestController
@AllArgsConstructor
@RequestMapping("/file")
@Api(value = "公共-附件表", tags = "公共-附件表接口")
public class FileController extends BladeController {

	private final IFileService fileService;


	/**
	 * 附件显示列表处删除附件
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(fileService.removeFileData2(Func.toStrList(ids)));
	}

	@PostMapping("/update")
	public R update(@RequestParam MultipartFile file,
					@RequestParam String id) throws IOException {
		if (Objects.isNull(file) || file.isEmpty()) {
			return R.fail("文件不能为空");
		}
		if (StringUtils.isBlank(id)) {
			return R.fail("附件ID不能为空");
		}
		return R.status(fileService.update(file, id));
	}

	@GetMapping("/list")
	@ApiOperation(value = "附件list", notes = "传入linkId")
	public R<List<UploadVO>> list(@RequestParam String linkId,@RequestParam String groupName) {
		List<UploadVO> attachments = fileService.getAttachments(linkId,groupName);
		return R.data(attachments);
	}
}
