package org.stm.doc.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.stm.doc.entity.DocFileLink;
import org.stm.doc.entity.DocFilesDir;
import org.stm.doc.service.IDocFileLinkService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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
 */
@Api(tags="doc_file_link")
@RestController
@RequestMapping("/doc/docFileLink")
@Slf4j
public class DocFileLinkController extends JeecgController<DocFileLink, IDocFileLinkService> {
	@Autowired
	private IDocFileLinkService docFileLinkService;

	/**
	 *   添加
	 *
	 * @param docFileLink
	 * @return
	 */
	@AutoLog(value = "doc_file_link-添加")
	@ApiOperation(value="doc_file_link-添加", notes="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-编辑")
	@ApiOperation(value="doc_file_link-编辑", notes="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删除")
	@ApiOperation(value="doc_file_link-通过id删除", notes="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-批量删除")
	@ApiOperation(value="doc_file_link-批量删除", notes="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);
	}

}
