
package org.sky.scientific.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.sky.core.boot.ctrl.KdController;
import org.sky.core.mp.support.Condition;
import org.sky.core.mp.support.Query;
import org.sky.core.secure.utils.SecureUtil;
import org.sky.core.tool.api.R;
import org.sky.core.tool.utils.Func;
import org.sky.scientific.pojo.dto.CgQtDTO;
import org.sky.scientific.pojo.dto.CgRjzzqDTO;
import org.sky.scientific.pojo.entity.*;
import org.sky.scientific.pojo.vo.XmCGSummary;
import org.sky.scientific.service.*;
import org.sky.scientific.utils.ExportUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 科研成果-技术标准 控制器
 *
 * @author Kd
 * @since 2025-06-29
 */
@RestController
@AllArgsConstructor
@RequestMapping("/kycg")
@Tag(name = "科研成果管理", description = "科研成果管理")
public class KYCGController extends KdController {

	private final ICgJsbzService jsbzService;
	private final ICgKylwService kylwService;
	private final ICgZlService zlService;
	private final ICgRjzzqService rjzzqService;
	private final ICgQtService qtService;

	@GetMapping("/zl/list")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "专利-分页", description = "传入cgZl")
	public R<IPage<CgZlEntity>> zlList(CgZlEntity cgZl, Query query) {
		IPage<CgZlEntity> pages = zlService.selectPageByCondition(Condition.getPage(query), cgZl);
		return R.data(pages);
	}

	@PostMapping("/zl/save")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "专利-新增", description = "传入cgZl")
	public R saveZl(@Valid @RequestBody CgZlEntity cgZl) {
		return R.status(zlService.save(cgZl));
	}

	@PostMapping("/zl/update")
	@ApiOperationSupport(order = 4)
	@Operation(summary = "专利-修改", description = "传入cgZl")
	public R updateZl(@Valid @RequestBody CgZlEntity cgZl) {
		return R.status(zlService.update(cgZl));
	}

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

	@PostMapping("/zl/import")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "专利-导入", description = "传入excel")
	public R importZl(MultipartFile file) {
		zlService.importZl(file);
		return R.success("操作成功");
	}

	@SneakyThrows
	@GetMapping("/zl/export")
	@ApiOperationSupport(order = 7)
	@Operation(summary = "专利-导出")
	public void exportZl(HttpServletResponse response,CgZlEntity cgZl) {
		List<CgZlEntity> list = zlService.selectPageByCondition(Condition.getPage(new Query().setCurrent(1).setSize(Integer.MAX_VALUE)), cgZl).getRecords();
		if(Func.isEmpty(list)){
			ExportUtil.processWithNoData(response);
		}else {
			ExportUtil.processWithData(response, "科研项目-专利成果.xls");
			zlService.exportZl(response.getOutputStream(), list);
		}
	}

	@GetMapping("dashboard/zl")
	@ApiOperationSupport(order = 8)
	@Operation(summary = "首页-知识产权情况")
	public R<JSONObject> zlDashboard(String year) {
		return R.data(zlService.zlDashboard(SecureUtil.getTenantId(), year));
	}

	@GetMapping("/rjzzq/list")
	@ApiOperationSupport(order = 12)
	@Operation(summary = "软件著作-分页")
	public R<IPage<CgRjzzqEntity>> selectRjzzqList(CgRjzzqDTO dto, Query query) {
		IPage<CgRjzzqEntity> pages = rjzzqService.selectPageByCondition(Condition.getPage(query), dto);
		return R.data(pages);
	}

	@PostMapping("/rjzzq/save")
	@ApiOperationSupport(order = 13)
	@Operation(summary = "软件著作-新增", description = "传入cgRjzzq")
	public R saveRjzzq(@Valid @RequestBody CgRjzzqEntity cgRjzzq) {
		return R.status(rjzzqService.saveRjzzq(cgRjzzq));
	}

	@PostMapping("/rjzzq/update")
	@ApiOperationSupport(order = 14)
	@Operation(summary = "软件著作-修改", description = "传入cgRjzzq")
	public R updateRjzzq(@Valid @RequestBody CgRjzzqEntity cgRjzzq) {
		return R.status(rjzzqService.updateRjzzq(cgRjzzq));
	}

	@PostMapping("/rjzzq/remove")
	@ApiOperationSupport(order = 15)
	@Operation(summary = "软件著作-删除", description = "传入ids")
	public R removeRjzzq(@Parameter(description = "主键集合", required = true) @RequestParam String ids) {
		return R.status(rjzzqService.physicalDeleteByIds(Func.toLongList(ids)));
	}

	@PostMapping("/rjzzq/import")
	@ApiOperationSupport(order = 16)
	@Operation(summary = "软件著作-导入", description = "传入excel")
	public R importRjzzq(MultipartFile file) {
		rjzzqService.importRjzzq(file);
		return R.success("操作成功");
	}

	@SneakyThrows
	@GetMapping("/rjzzq/export")
	@ApiOperationSupport(order = 17)
	@Operation(summary = "软件著作-导出", description = "传入cgRjzzq")
	public void exportRjzzq(HttpServletResponse response, CgRjzzqDTO dto) {
		List<CgRjzzqEntity> list = rjzzqService.selectPageByCondition(Condition.getPage(new Query().setCurrent(1).setSize(Integer.MAX_VALUE)), dto).getRecords();
		if(Func.isEmpty(list)){
			ExportUtil.processWithNoData(response);
		}else {
			ExportUtil.processWithData(response, "科研成果-软件著作.xls");
			rjzzqService.export(response.getOutputStream(), list);
		}
	}

	@GetMapping("/jsbz/list")
	@ApiOperationSupport(order = 22)
	@Operation(summary = "技术标准-分页", description = "传入cgJsbz")
	public R<IPage<CgJsbzEntity>> listJsbz(CgJsbzEntity jsbz, Query query) {
		IPage<CgJsbzEntity> pages = jsbzService.selectPageByCondition(Condition.getPage(query), jsbz);
		return R.data(pages);
	}

	@PostMapping("/jsbz/save")
	@ApiOperationSupport(order = 23)
	@Operation(summary = "技术标准-新增", description = "传入cgJsbz")
	public R saveJsbz(@Valid @RequestBody CgJsbzEntity cgJsbz) {
		return R.status(jsbzService.save(cgJsbz));
	}

	@PostMapping("/jsbz/update")
	@ApiOperationSupport(order = 24)
	@Operation(summary = "技术标准-修改", description = "传入cgJsbz")
	public R updateJsbz(@Valid @RequestBody CgJsbzEntity cgJsbz) {
		return R.status(jsbzService.update(cgJsbz));
	}

	@PostMapping("/jsbz/remove")
	@ApiOperationSupport(order = 25)
	@Operation(summary = "技术标准-删除", description = "传入ids")
	public R removeJsbz(@Parameter(description = "主键集合", required = true) @RequestParam String ids) {
		return R.status(jsbzService.physicalDeleteByIds(Func.toLongList(ids)));
	}

	@PostMapping("/jsbz/import")
	@ApiOperationSupport(order = 26)
	@Operation(summary = "技术标准-导入", description = "传入excel")
	public R importJsbz(MultipartFile file) {
		jsbzService.importCgJsbz(file);
		return R.success("操作成功");
	}

	@SneakyThrows
	@GetMapping("/jsbz/export")
	@ApiOperationSupport(order = 27)
	@Operation(summary = "技术标准-导出", description = "传入cgJsbz")
	public void exportCgJsbz(CgJsbzEntity dto, HttpServletResponse response) {
		List<CgJsbzEntity> list = jsbzService.selectPageByCondition(Condition.getPage(new Query().setCurrent(1).setSize(Integer.MAX_VALUE)), dto).getRecords();
		if(Func.isEmpty(list)){
			ExportUtil.processWithNoData(response);
		}else {
			ExportUtil.processWithData(response, "科研成果-技术标准.xls");
			jsbzService.export(response.getOutputStream(), list);
		}
 	}

	@GetMapping("/kylw/list")
	@ApiOperationSupport(order = 31)
	@Operation(summary = "科研论文-分页", description = "传入cgKylw")
	public R<IPage<CgKylwEntity>> kylwList(CgKylwEntity kylw, Query query) {
		IPage<CgKylwEntity> pages = kylwService.selectPageByCondition(Condition.getPage(query), kylw);
		return R.data(pages);
	}

	@PostMapping("/kylw/save")
	@ApiOperationSupport(order = 32)
	@Operation(summary = "科研论文-新增", description = "传入cgKylw")
	public R saveKylw(@Valid @RequestBody CgKylwEntity cgKylw) {
		return R.status(kylwService.save(cgKylw));
	}

	@PostMapping("/kylw/update")
	@ApiOperationSupport(order = 33)
	@Operation(summary = "科研论文-修改", description = "传入cgKylw")
	public R updateKylw(@Valid @RequestBody CgKylwEntity cgKylw) {
		return R.status(kylwService.update(cgKylw));
	}


	@PostMapping("/kylw/remove")
	@ApiOperationSupport(order = 34)
	@Operation(summary = "科研论文-删除", description = "传入ids")
	public R removeKylw(@Parameter(description = "主键集合", required = true) @RequestParam String ids) {
		return R.status(kylwService.physicalDeleteByIds(Func.toLongList(ids)));
	}

	@PostMapping("/kylw/import")
	@ApiOperationSupport(order = 35)
	@Operation(summary = "科研论文-导入", description = "传入excel")
	public R importKylw(MultipartFile file) {
		kylwService.importCgKylw(file);
		return R.success("操作成功");
	}

	@SneakyThrows
	@GetMapping("kylw/export")
	@ApiOperationSupport(order = 36)
	@Operation(summary = "科研论文-导出")
	public void exportKylw(CgKylwEntity dto, HttpServletResponse response) {
		List<CgKylwEntity> list = kylwService.selectPageByCondition(Condition.getPage(new Query().setCurrent(1).setSize(Integer.MAX_VALUE)), dto).getRecords();
		if(Func.isEmpty(list)){
			ExportUtil.processWithNoData(response);
		}else {
			ExportUtil.processWithData(response, "科研成果-科研论文.xls");
			kylwService.export(response.getOutputStream(), list);
		}
	}

	@GetMapping("/qt/list")
	@ApiOperationSupport(order = 42)
	@Operation(summary = "其他成果-分页", description = "传入cgQt")
	public R<IPage<CgQtEntity>> qtList(CgQtDTO dto, Query query) {
		IPage<CgQtEntity> pages = qtService.selectPageByCondition(Condition.getPage(query), dto);
		return R.data(pages);
	}

	@PostMapping("/qt/save")
	@ApiOperationSupport(order = 43)
	@Operation(summary = "其他成果-新增", description = "传入cgQt")
	public R saveQt(@Valid @RequestBody CgQtEntity cgQt) {
		return R.status(qtService.save(cgQt));
	}

	@PostMapping("/qt/update")
	@ApiOperationSupport(order = 44)
	@Operation(summary = "其他成果-修改", description = "传入cgQt")
	public R updateQt(@Valid @RequestBody CgQtEntity cgQt) {
		return R.status(qtService.update(cgQt));
	}

	@PostMapping("/qt/remove")
	@ApiOperationSupport(order = 45)
	@Operation(summary = "其他成果-逻辑删除", description = "传入ids")
	public R removeQt(@Parameter(description = "主键集合", required = true) @RequestParam String ids) {
		return R.status(qtService.physicalDeleteByIds(Func.toLongList(ids)));
	}

	@PostMapping("/qt/import")
	@ApiOperationSupport(order = 46)
	@Operation(summary = "其他成果--导入", description = "传入excel")
	public R importQt(MultipartFile file) {
		qtService.importQt(file);
		return R.success("操作成功");
	}

	@SneakyThrows
	@GetMapping("/qt/export")
	@ApiOperationSupport(order = 47)
	@Operation(summary = "其他成果-导出数据", description = "传入cgQt")
	public void exportQt(HttpServletResponse response, CgQtDTO dto) {
		List<CgQtEntity> list = qtService.selectPageByCondition(Condition.getPage(new Query().setCurrent(1).setSize(Integer.MAX_VALUE)), dto).getRecords();
		if(Func.isEmpty(list)){
			ExportUtil.processWithNoData(response);
		}else {
			ExportUtil.processWithData(response, "科研成果-其他成果.xls");
			qtService.export(response.getOutputStream(), list);
		}
	}

	@GetMapping("/summary")
	@ApiOperationSupport(order = 80)
	@Operation(summary = "研发项目成果")
	public R<IPage<XmCGSummary>> xmCgSummary(Query query, Long xmId) {
		IPage<XmCGSummary> page = qtService.xmCgSummary(Condition.getPage(query), xmId);
		return R.data(page);
	}

	@SneakyThrows
	@GetMapping("/summary/export")
	@ApiOperationSupport(order = 82)
	@Operation(summary = "研发项目成果-导出")
	public void exportXmCgSummary(HttpServletResponse response, CgQtDTO dto) {
		List<XmCGSummary> list = qtService.xmCgSummary(Condition.getPage(new Query().setCurrent(1).setSize(Integer.MAX_VALUE)), dto.getXmId()).getRecords();
		if(Func.isEmpty(list)){
			ExportUtil.processWithNoData(response);
		}else {
			ExportUtil.processWithData(response, "项目预算-项目成果.xls");
			qtService.exportXmCgSummary(response.getOutputStream(), list);
		}
	}
}
