
package org.sky.scientific.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import jakarta.validation.Valid;

import org.sky.core.excel.util.ExcelUtil;
import org.sky.core.mp.support.Condition;
import org.sky.core.mp.support.Query;
import org.sky.core.secure.KdUser;
import org.sky.core.secure.annotation.IsAdmin;
import org.sky.core.tool.api.R;
import org.sky.core.tool.utils.DateUtil;
import org.sky.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.sky.scientific.pojo.entity.YfzdEntity;
import org.sky.scientific.pojo.vo.YfzdVO;
import org.sky.scientific.excel.YfzdExcel;
import org.sky.scientific.wrapper.YfzdWrapper;
import org.sky.scientific.service.IYfzdService;
import org.sky.core.boot.ctrl.KdController;

import java.util.Map;
import java.util.List;

import jakarta.servlet.http.HttpServletResponse;

/**
 * 研发制度 控制器
 *
 * @author Kd
 * @since 2025-06-29
 */
@RestController
@AllArgsConstructor
@RequestMapping("/yfzd")
@Tag(name = "研发制度", description = "研发制度接口")
public class YfzdController extends KdController {

	private final IYfzdService service;

	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "获取指定年度的制度文件", description = "传入yfzd")
	public R<Map<String, List<YfzdEntity>>> selectYfzd(YfzdEntity yfzd) {
		Map<String, List<YfzdEntity>> map = service.selectYfzd(yfzd);
		return R.data(map);
	}

	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@Operation(summary = "新增或修改", description = "传入yfzd")
	public R saveOrUpdate(@Valid @RequestBody YfzdEntity yfzd) {
		return R.status(service.editYfzd(yfzd));
	}

	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@Operation(summary = "删除", description = "传入ids")
	public R remove(@Parameter(description = "主键集合", required = true) @RequestParam String ids) {
		return R.status(service.physicalDeleteByIds(Func.toLongList(ids)));
	}

 	@GetMapping("/export-yfzd")
	@ApiOperationSupport(order = 9)
	@Operation(summary = "导出数据", description = "传入yfzd")
	public void exportYfzd(@Parameter(hidden = true) @RequestParam Map<String, Object> yfzd, KdUser kdUser, HttpServletResponse response) {
		QueryWrapper<YfzdEntity> queryWrapper = Condition.getQueryWrapper(yfzd, YfzdEntity.class);
		List<YfzdExcel> list = service.exportYfzd(queryWrapper);
		ExcelUtil.export(response, "研发制度数据" + DateUtil.time(), "研发制度数据表", list, YfzdExcel.class);
	}

//	public static void main(String[] args) {
//		JSONArray jsonArray = new JSONArray();
//		JSONObject jsonObject = new JSONObject();
//		jsonObject.put("url", "http://sxdhyig3c.hn-bkt.clouddn.com/upload/20250702/864da644cc7041f5414c55c1a6f625c7.png");
//		jsonObject.put("fileName", "111.png");
//		jsonArray.add(jsonObject);
//		jsonObject = new JSONObject();
//		jsonObject.put("url", "http://sxdhyig3c.hn-bkt.clouddn.com/upload/20250702/864da644cc7041f5414c55c1a6f625c7.png");
//		jsonObject.put("fileName", "222.png");
//		jsonArray.add(jsonObject);
//		jsonObject = new JSONObject();
//		jsonObject.put("url", "http://sxdhyig3c.hn-bkt.clouddn.com/upload/20250702/864da644cc7041f5414c55c1a6f625c7.png");
//		jsonObject.put("fileName", "333.png");
//		jsonArray.add(jsonObject);
//		System.out.println(jsonArray.toJSONString());
//
////		"[{\"fileName\":\"111.png\",\"url\":\"http://sxdhyig3c.hn-bkt.clouddn.com/upload/20250702/864da644cc7041f5414c55c1a6f625c7.png\"},{\"fileName\":\"222.png\",\"url\":\"http://sxdhyig3c.hn-bkt.clouddn.com/upload/20250702/864da644cc7041f5414c55c1a6f625c7.png\"},{\"fileName\":\"333.png\",\"url\":\"http://sxdhyig3c.hn-bkt.clouddn.com/upload/20250702/864da644cc7041f5414c55c1a6f625c7.png\"}]\n"
//	}

}
