
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.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
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.tool.api.R;
import org.sky.scientific.pojo.dto.HoursSyncDto;
import org.sky.scientific.pojo.dto.TechnicianDTO;
import org.sky.scientific.pojo.vo.TechnicianHoursMonthlyVO;
import org.sky.scientific.pojo.vo.XmTechnicianHoursVO;
import org.sky.scientific.service.IHoursSyncService;
import org.sky.scientific.service.IXmTechnicianHoursService;
import org.sky.scientific.utils.ExportUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 研发人员研发工时记录表 控制器
 *
 * @author Kd
 * @since 2025-06-29
 */
@RestController
@AllArgsConstructor
@RequestMapping("xm/technician/hours")
@Tag(name = "研发人员研发工时记录表")
public class XmTechnicianHoursController extends KdController {

	private final IXmTechnicianHoursService service;
	private final IHoursSyncService hoursSyncService;

	@GetMapping("/page")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "分页")
	public R<IPage<XmTechnicianHoursVO>> selectPage(XmTechnicianHoursVO dto, Query query) {
		return R.data(service.selectPage(dto, Condition.getPage(query)));
	}

	@PostMapping("/import")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "导入", description = "传入excel")
	public R importHours(MultipartFile file, String yearAndMonth) {
		service.importXmTechnicianHours(file, yearAndMonth);
		return R.success("操作成功");
	}

	@GetMapping("/fetch")
	@ApiOperationSupport(order = 4)
	@Operation(summary = "引用当月参研人员名单")
	public R<Boolean> fetch(String yearAndMonth) {
		return R.status(service.fetch(yearAndMonth));
	}

	@PostMapping("/submit/batch")
	@ApiOperationSupport(order = 5)
	@Operation(summary = "新增/修改")
	public R submit(@RequestBody XmTechnicianHoursVO dto) {
		return R.status(service.batchSubmit(dto));
	}

	@PostMapping("/clear/batch")
	@ApiOperationSupport(order = 7)
	@Operation(summary = "清除工时", description = "{\"yearAndMonth\":\"202507\",\"xmAndUnicodeList\":[{\"xmId\":\"1943241918440747010\",\"unicode\":\"ccd86a42-2369-4517-832e-004307e5237e\"}]}")
	public R clear(@RequestBody JSONObject dto) {
		return R.status(service.clearHours(dto));
	}

	@DeleteMapping("/delete")
	@ApiOperationSupport(order = 8)
	@Operation(summary = "删除", description = "数据结构和 清除接口一致")
	public R deleteByXmUnicodeAndMonth(@RequestBody JSONObject dto) {
		return R.status(service.deleteHours(dto));
	}

	@PostMapping("/export")
	@ApiOperationSupport(order = 12)
	@Operation(summary = "导出")
	public void export(HttpServletResponse response, @RequestBody XmTechnicianHoursVO param) throws IOException {
		List<XmTechnicianHoursVO> list = service.selectPage(param, Condition.getPage(new Query().setCurrent(1).setSize(Integer.MAX_VALUE))).getRecords();
		ExportUtil.processWithData(response, "每月研发工时打卡.xls");
		service.export(response.getOutputStream(), list, param.getYearAndMonth());
	}

	@GetMapping("/list/print")
	@ApiOperationSupport(order = 15)
	@Operation(summary = "自动生成-打印")
	public R<List<JSONObject>> selectListForPrint(XmTechnicianHoursVO dto) {
		return R.data(service.selectListForPrint(dto));
	}

	@GetMapping("/myyfgsfpb")
	@ApiOperationSupport(order = 20)
	@Operation(summary = "每月研发工时分配表")
	public R<IPage<TechnicianHoursMonthlyVO>> meiyueyanfagongshifenpeibiao(TechnicianDTO dto, Query query) {
		return R.data(service.meiyueyanfagongshifenpeibiao(Condition.getPage(query), dto));
	}

	@PostMapping("/myyfgsfpb/export")
	@ApiOperationSupport(order = 33)
	@Operation(summary = "每月研发工时分配表-导出")
	public void exportMyyfgsfpb(HttpServletResponse response, @RequestBody TechnicianDTO dto) {
		service.exportMeiyueyanfagongshifenpeibiao(response, dto);
	}

	@PostMapping("/sync")
	@ApiOperationSupport(order = 40)
	@Operation(summary = "同步至工时系统")
	public R<Boolean> sync(@RequestBody HoursSyncDto dto) {
		return R.status(hoursSyncService.syncHoursData(dto));
	}

	@PostMapping("accept")
	@ApiOperationSupport(order = 50)
	@Operation(summary = "同步至本系统")
	public R<Boolean> acceptRdData(@RequestBody HoursSyncDto dto) {
		return R.status(hoursSyncService.acceptRdData(dto));
	}

}
