package org.stm.audit.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.stm.audit.entity.AuditItemLink;
import org.stm.audit.service.IAuditItemLinkService;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: audit_item_link
 * @Author: jeecg-boot
 * @Date:   2025-07-06
 * @Version: V1.0
 */
@Api(tags="audit_item_link")
@RestController
@RequestMapping("/audit/auditItemLink")
@Slf4j
public class AuditItemLinkController extends JeecgController<AuditItemLink, IAuditItemLinkService> {
	@Autowired
	private IAuditItemLinkService auditItemLinkService;

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

	@PostMapping(value = "/addBatch")
	public Result<String> addBatch(@RequestBody AuditItemLink auditItemLink) {
		List<Integer> tids = auditItemLink.getTids();
		Integer sid = auditItemLink.getSid();
		List<AuditItemLink>  dataList = new ArrayList<>();
		for(Integer tid:tids) {
			AuditItemLink newList = new AuditItemLink();
			newList.setSid(sid);
			newList.setTid(tid);
			dataList.add(newList);
		}
		deleteBySid(sid);
		auditItemLinkService.saveBatch(dataList);
		return Result.OK("保存成功！");
	}


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



}
