
package org.sky.scientific.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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 org.sky.core.boot.ctrl.KdController;
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.tool.api.R;
import org.sky.core.tool.utils.DateUtil;
import org.sky.core.tool.utils.Func;
import org.sky.scientific.excel.*;
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.*;
import org.sky.scientific.service.*;
import org.sky.scientific.wrapper.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 科研成果-技术标准 控制器
 *
 * @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/detail")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "专利-详情", description  = "传入cgZl")
	public R<CgZlVO> zlDetail(CgZlEntity cgZl) {
		CgZlEntity detail = zlService.getOne(Condition.getQueryWrapper(cgZl));
		return R.data(CgZlWrapper.build().entityVO(detail));
	}

	@GetMapping("/zl/list")
	@ApiOperationSupport(order = 2)
	@Operation(summary = "专利-分页", description  = "传入cgZl")
	public R<IPage<CgZlVO>> zlList(@Parameter(hidden = true) @RequestParam Map<String, Object> cgZl, Query query) {
		IPage<CgZlEntity> pages = zlService.page(Condition.getPage(query), Condition.getQueryWrapper(cgZl, CgZlEntity.class));
		return R.data(CgZlWrapper.build().pageVO(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("操作成功");
	}

	@GetMapping("/zl/export")
	@ApiOperationSupport(order = 7)
	@Operation(summary = "专利-导出", description  = "传入cgZl")
	public void exportZl(@Parameter(hidden = true) @RequestParam Map<String, Object> cgZl, KdUser kdUser, HttpServletResponse response) {
		QueryWrapper<CgZlEntity> queryWrapper = Condition.getQueryWrapper(cgZl, CgZlEntity.class);
		List<CgZlExcel> list = zlService.export(queryWrapper);
		ExcelUtil.export(response, "科研成果-专利数据" + DateUtil.time(), "科研成果-专利数据表", list, CgZlExcel.class);
	}

	@GetMapping("/rjzzq/detail")
	@ApiOperationSupport(order = 11)
	@Operation(summary = "软件著作-详情", description  = "传入cgRjzzq")
	public R<CgRjzzqVO> rjzzqDetail(CgRjzzqEntity cgRjzzq) {
		CgRjzzqEntity detail = rjzzqService.getOne(Condition.getQueryWrapper(cgRjzzq));
		return R.data(CgRjzzqWrapper.build().entityVO(detail));
	}

	@GetMapping("/rjzzq/list")
	@ApiOperationSupport(order = 12)
	@Operation(summary = "软件著作-分页", description  = "传入cgRjzzq")
	public R<IPage<CgRjzzqVO>> rjzzqList(CgRjzzqDTO dto, Query query) {
		IPage<CgRjzzqVO> pages = rjzzqService.selectRjzzqPage(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.save(cgRjzzq));
	}

	@PostMapping("/rjzzq/update")
	@ApiOperationSupport(order = 14)
	@Operation(summary = "软件著作-修改", description  = "传入cgRjzzq")
	public R updateRjzzq(@Valid @RequestBody CgRjzzqEntity cgRjzzq) {
		return R.status(rjzzqService.update(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("操作成功");
	}

 	@GetMapping("/rjzzq/export")
	@ApiOperationSupport(order = 17)
	@Operation(summary = "软件著作-导出", description  = "传入cgRjzzq")
	public void exportRjzzq(@Parameter(hidden = true) @RequestParam Map<String, Object> cgRjzzq, KdUser kdUser, HttpServletResponse response) {
		QueryWrapper<CgRjzzqEntity> queryWrapper = Condition.getQueryWrapper(cgRjzzq, CgRjzzqEntity.class);
		List<CgRjzzqExcel> list = rjzzqService.export(queryWrapper);
		ExcelUtil.export(response, "科研成果-软件著作数据" + DateUtil.time(), "科研成果-软件著作数据表", list, CgRjzzqExcel.class);
	}

	@GetMapping("/jsbz/detail")
	@ApiOperationSupport(order = 21)
	@Operation(summary = "技术标准-详情", description  = "传入cgJsbz")
	public R<CgJsbzVO> detailJsbz(CgJsbzEntity jsbz) {
		CgJsbzEntity detail = jsbzService.getOne(Condition.getQueryWrapper(jsbz));
		return R.data(CgJsbzWrapper.build().entityVO(detail));
	}

	@GetMapping("/jsbz/list")
	@ApiOperationSupport(order = 22)
	@Operation(summary = "技术标准-分页", description  = "传入cgJsbz")
	public R<IPage<CgJsbzVO>> listJsbz(@Parameter(hidden = true) @RequestParam Map<String, Object> cgJsbz, Query query) {
		IPage<CgJsbzEntity> pages = jsbzService.page(Condition.getPage(query), Condition.getQueryWrapper(cgJsbz, CgJsbzEntity.class));
		return R.data(CgJsbzWrapper.build().pageVO(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("操作成功");
	}

	@GetMapping("/jsbz/export")
	@ApiOperationSupport(order = 27)
	@Operation(summary = "技术标准-导出", description  = "传入cgJsbz")
	public void exportCgJsbz(@Parameter(hidden = true) @RequestParam Map<String, Object> cgJsbz, KdUser kdUser, HttpServletResponse response) {
		QueryWrapper<CgJsbzEntity> queryWrapper = Condition.getQueryWrapper(cgJsbz, CgJsbzEntity.class);
		List<CgJsbzExcel> list = jsbzService.export(queryWrapper);
		ExcelUtil.export(response, "科研成果-技术标准数据" + DateUtil.time(), "科研成果-技术标准数据表", list, CgJsbzExcel.class);
	}


	@GetMapping("/kylw/detail")
	@ApiOperationSupport(order = 30)
	@Operation(summary = "科研论文-详情", description  = "传入cgKylw")
	public R<CgKylwVO> kylwDetail(CgKylwEntity cgKylw) {
		CgKylwEntity detail = kylwService.getOne(Condition.getQueryWrapper(cgKylw));
		return R.data(CgKylwWrapper.build().entityVO(detail));
	}

	@GetMapping("/kylw/list")
	@ApiOperationSupport(order = 31)
	@Operation(summary = "科研论文-分页", description  = "传入cgKylw")
	public R<IPage<CgKylwVO>> kylwList(@Parameter(hidden = true) @RequestParam Map<String, Object> cgKylw, Query query) {
		IPage<CgKylwEntity> pages = kylwService.page(Condition.getPage(query), Condition.getQueryWrapper(cgKylw, CgKylwEntity.class));
		return R.data(CgKylwWrapper.build().pageVO(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("操作成功");
	}

	@GetMapping("kylw/export")
	@ApiOperationSupport(order = 36)
	@Operation(summary = "科研论文-导出", description  = "传入cgKylw")
	public void exportKylw(@Parameter(hidden = true) @RequestParam Map<String, Object> cgKylw, KdUser kdUser, HttpServletResponse response) {
		QueryWrapper<CgKylwEntity> queryWrapper = Condition.getQueryWrapper(cgKylw, CgKylwEntity.class);
		List<CgKylwExcel> list = kylwService.export(queryWrapper);
		ExcelUtil.export(response, "科研成果-科研论文数据" + DateUtil.time(), "科研成果-科研论文数据表", list, CgKylwExcel.class);
	}

	@GetMapping("/qt/detail")
	@ApiOperationSupport(order = 41)
	@Operation(summary = "其他成果-详情", description  = "传入cgQt")
	public R<CgQtVO> qtDetail(CgQtEntity cgQt) {
		CgQtEntity detail = qtService.getOne(Condition.getQueryWrapper(cgQt));
		return R.data(CgQtWrapper.build().entityVO(detail));
	}

	@GetMapping("/qt/list")
	@ApiOperationSupport(order = 42)
	@Operation(summary = "其他成果-分页", description  = "传入cgQt")
	public R<IPage<CgQtVO>> qtList(CgQtDTO dto, Query query) {
		IPage<CgQtVO> pages = qtService.selectQtPage(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("操作成功");
	}

 	@GetMapping("/qt/export")
	@ApiOperationSupport(order = 47)
	@Operation(summary = "其他成果-导出数据", description  = "传入cgQt")
	public void exportQt(@Parameter(hidden = true) @RequestParam Map<String, Object> cgQt, KdUser kdUser, HttpServletResponse response) {
		QueryWrapper<CgQtEntity> queryWrapper = Condition.getQueryWrapper(cgQt, CgQtEntity.class);
		List<CgQtExcel> list = qtService.export(queryWrapper);
		ExcelUtil.export(response, "科研成果-其他成果数据" + DateUtil.time(), "科研成果-其他成果数据表", list, CgQtExcel.class);
	}

	@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);
	}
}
