package org.jeecg.modules.stm.doc.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.stm.doc.entity.DocFileLink;
import org.jeecg.modules.stm.doc.service.IDocFileLinkService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
 
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: doc_file_link
 * @Author: jeecg-boot
 * @Date:   2025-07-06
 * @Version: V1.0
 */
  
@RestController
@RequestMapping("/doc/docFileLink")
@Slf4j
public class DocFileLinkController extends JeecgController<DocFileLink, IDocFileLinkService> {
	@Autowired
	private IDocFileLinkService docFileLinkService;

	/**
	 *   添加
	 *
	 * @param docFileLink
	 * @return
	 */
	@AutoLog(value = "doc_file_link-添加")
	  
	@RequiresPermissions("doc:doc_file_link:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody DocFileLink docFileLink) {
		docFileLinkService.save(docFileLink);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param docFileLink
	 * @return
	 */
	@AutoLog(value = "doc_file_link-编辑")
	  
	@RequiresPermissions("doc:doc_file_link:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody DocFileLink docFileLink) {
		docFileLinkService.updateById(docFileLink);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "doc_file_link-通过id删除")
	  
	@RequiresPermissions("doc:doc_file_link:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		docFileLinkService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "doc_file_link-批量删除")
	  
	@RequiresPermissions("doc:doc_file_link:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.docFileLinkService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}


	 @GetMapping(value = "/findLinkFile")
	 public Result<List<Integer>> listAll(DocFileLink docFileLink) {
		 QueryWrapper<DocFileLink> queryWrapper = new QueryWrapper<>();
		 queryWrapper.eq("sid",docFileLink.getSid());
		 List<DocFileLink> retList =  docFileLinkService.list(queryWrapper);
		 List<Integer> ids= new ArrayList<>();
		 for(DocFileLink link :retList) {
			 ids.add(link.getTid());
		 }
		 return Result.OK(ids);
	 }

	@PostMapping(value = "/addBatch")
	public Result<String> addBatch(@RequestBody DocFileLink docFileLink) {
		List<Integer> tids = docFileLink.getTids();
		Integer sid = docFileLink.getSid();
		List<DocFileLink>  fileList = new ArrayList<>();
		for(Integer tid:tids) {
			DocFileLink newList = new DocFileLink();
			newList.setSid(sid);
			newList.setTid(tid);
			fileList.add(newList);
		}

		deleteBySid(sid);
		docFileLinkService.saveBatch(fileList);
		return Result.OK("添加成功！");
	}


	private void deleteBySid(Integer sid) {
		QueryWrapper<DocFileLink> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("sid",sid);
		docFileLinkService.remove(queryWrapper);
	}

}
