
package org.sky.scientific.controller;

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.tool.api.R;
import org.sky.core.tool.utils.Func;
import org.sky.scientific.excel.TechnicianExcel;
import org.sky.scientific.excel.TechnicianImporter;
import org.sky.scientific.pojo.dto.TechnicianDTO;
import org.sky.scientific.pojo.dto.XmSalaryDto;
import org.sky.scientific.pojo.dto.XmTechnicianDto;
import org.sky.scientific.pojo.entity.TechnicianEntity;
import org.sky.scientific.pojo.entity.XmTechnicianEntity;
import org.sky.scientific.pojo.vo.*;
import org.sky.scientific.service.ITechnicianService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 技术人员花名册 控制器
 *
 * @author sky
 * @since 2025-06-10
 */
@RestController
@AllArgsConstructor
@RequestMapping("/technician")
@Tag(name = "科研人员花名册")
public class TechnicianController extends KdController {

	private final ITechnicianService technicianService;

	@GetMapping("/page")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "分页", description = "传入technician")
	public R<IPage<TechnicianVO>> page(TechnicianDTO technician, Query query) {
		IPage<TechnicianVO> pages = technicianService.selectTechnicianPage(Condition.getPage(query), technician);
		return R.data(pages);
	}

	@GetMapping("/page/date-interval")
	@ApiOperationSupport(order = 2)
	@Operation(summary = "分页-主要用于查询项目周期内的技术人员数据,传入项目的开始和结束月份，startDate, endDate, 格式：202505")
	public R<IPage<TechnicianEntity>> pageByDateInterval(@RequestParam HashMap<String, String> param, Query query) {
		IPage<TechnicianEntity> pages = technicianService.selectTechnicianPageByDateInterval(Condition.getPage(query), param);
		return R.data(pages);
	}

	@PostMapping("import")
	@ApiOperationSupport(order = 5)
	@Operation(summary = "导入", description = "传入excel")
	public R importUser(MultipartFile file, String yearAndMonth) {
		TechnicianImporter importer = new TechnicianImporter(technicianService, yearAndMonth);
		ExcelUtil.save(file, importer, TechnicianExcel.class);
		return R.success("操作成功");
	}

	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@Operation(summary = "新增", description = "传入technician")
	public R save(@Valid @RequestBody TechnicianDTO technician) {
		return R.status(technicianService.saveOrUpdateTechnician(technician));
	}

	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@Operation(summary = "修改", description = "传入technician")
	public R update(@Valid @RequestBody TechnicianEntity technician) {
		return R.status(technicianService.updateById(technician));
	}

	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@Operation(summary = "删除", description = "数组，每个元素传入id、yearAndMonth")
	public R remove(@RequestBody List<TechnicianEntity> technicianList) {
		return R.status(technicianService.batchDelete(technicianList));
	}

	@PostMapping("/export")
	@ApiOperationSupport(order = 9)
	@Operation(summary = "导出数据", description = "传入technician")
	public void exportTechnician(HttpServletResponse response, @RequestBody TechnicianDTO technician) {
		technicianService.exportTechnician(response, technician);
	}


	@GetMapping("export-template")
	@ApiOperationSupport(order = 8)
	@Operation(summary = "导出模板")
	public void exportTechnicianTemplate(HttpServletResponse response) {
		List<TechnicianExcel> list = new ArrayList<>();
		ExcelUtil.export(response, "技术人员花名册模板", "用户数据表", list, TechnicianExcel.class);
	}

	@GetMapping("/fetchLastMonthData")
	@ApiOperationSupport(order = 10)
	@Operation(summary = "读取上个月人员名单", description = "传入选中的年月，比如2025-06，则读取2025-05的数据到06月")
	public R<Boolean> fetchLastMonthData(TechnicianDTO technician) {
		return R.status(technicianService.fetchLastMonthData(technician));
	}

	@GetMapping("xm/monthly/page")
	@ApiOperationSupport(order = 20)
	@Operation(summary = "每月实际参研人员名单-分页")
	public R<IPage<XmTechnicianEntity>> meiyueshijicanyuanrenyuanmingdan(XmTechnicianDto entity, Query query) {
		return R.data(technicianService.meiyueshijicanyuanrenyuanmingdan(Condition.getPage(query), entity));
	}

	@GetMapping("xm/monthly/yyysbmd")
	@ApiOperationSupport(order = 21)
	@Operation(summary = "每月实际参研人员名单-直接引用预算表名单", description = "传入xmId、yearAndMonth")
	public R<Boolean> zhijieyinyongyusuanbiaomingdan(String yearAndMonth) {
		technicianService.zhijieyinyongyusuanbiaomingdan(yearAndMonth);
		return R.success();
	}

	@PostMapping("xm/monthly/submit")
	@ApiOperationSupport(order = 22)
	@Operation(summary = "每月实际参研人员名单-新增")
	public R<Boolean> submitXmTechnicianMonthly(@Valid @RequestBody XmTechnicianEntity entity) {
		return R.status(technicianService.submitXmTechnicianMonthly(entity));
	}

	@DeleteMapping("xm/monthly/remove")
	@ApiOperationSupport(order = 23)
	@Operation(summary = "每月实际参研人员名单-删除")
	public R<Boolean> removeXmTechnicianMonthly(@Parameter(description = "主键集合", required = true) @RequestParam String ids) {
		return R.status(technicianService.physicalDeleteXmTechnicianMonthlyByIds(Func.toLongList(ids)));
	}

	@PostMapping("xm/monthly/export")
	@ApiOperationSupport(order = 24)
	@Operation(summary = "每月实际参研人员名单-导出")
	public void exportXmTechnicianMonthly(@RequestBody XmTechnicianDto dto, HttpServletResponse response) {
		technicianService.exportXmTechnicianMonthly(response, dto);
	}

	@GetMapping("cyryhmc")
	@ApiOperationSupport(order = 25)
	@Operation(summary = "参研人员花名册")
	public R<IPage<XmTechnicianVO>> canyanrenyuanhuamingce(XmTechnicianDto entity, Query query) {
		return R.data(technicianService.canyanrenyuanhuamingce(Condition.getPage(query), entity));
	}
	@PostMapping("cyryhmc-export")
	@ApiOperationSupport(order = 24)
	@Operation(summary = "参研人员花名册-导出")
	public void exportCyryhmc(@RequestBody XmTechnicianDto dto, HttpServletResponse response) {
		technicianService.exportCyryhmc(response, dto);
	}

	@GetMapping("xmcyryhzb")
	@ApiOperationSupport(order = 26)
	@Operation(summary = "项目参研人员汇总表")
	public R<IPage<XmTechnicianSummary>> xiangmucanyanrenyuanhuizongbiao(XmTechnicianEntity entity, Query query) {
		return R.data(technicianService.xiangmucanyanrenyuanhuizongbiao(Condition.getPage(query), entity));
	}
	@PostMapping("xmcyryhzb-export")
	@ApiOperationSupport(order = 27)
	@Operation(summary = "项目参研人员汇总表-导出")
	public void exportXmcyryhzb(@RequestBody XmTechnicianEntity dto, HttpServletResponse response) {
		technicianService.exportXmcyryhzb(response, dto);
	}

	@GetMapping("ndgkjryhzb")
	@ApiOperationSupport(order = 28)
	@Operation(summary = "年度科技人员汇总表")
	public R<IPage<TechnicianVO>> niandugaokejirenyuanhuizongbiao(TechnicianDTO dto, Query query) {
		return R.data(technicianService.niandugaokejirenyuanhuizongbiao(Condition.getPage(query), dto));
	}
	@PostMapping("ndgkjryhzb-export")
	@ApiOperationSupport(order = 29)
	@Operation(summary = "年度科技人员汇总表-导出")
	public void exportNdgkjryhzb(@RequestBody TechnicianDTO dto, HttpServletResponse response) {
		technicianService.exportNdgkjryhzb(response, dto);
	}

	@GetMapping("myyfryrgfyfpb")
	@ApiOperationSupport(order = 30)
	@Operation(summary = "每月研发人员人工费用分配表")
	public R<IPage<XmTechnicianSalaryVO>> meiyueyanfarengongfeiyongfeifenpeibiao(XmTechnicianDto dto, Query query) {
		return R.data(technicianService.meiyueyanfarengongfeiyongfeifenpeibiao(Condition.getPage(query), dto));
	}

	@GetMapping("myyfxmrgfyhzb")
	@ApiOperationSupport(order = 31)
	@Operation(summary = "每月研发项目人工分配汇总表")
	public R<IPage<XmSalaryVO>> meiyueyanfaxiangmurengongfenpeihuizongbiao(XmSalaryDto dto, Query query) {
		return R.data(technicianService.meiyueyanfaxiangmurengongfenpeihuizongbiao(Condition.getPage(query), dto));
	}
}
