
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.secure.annotation.IsAdmin;
import org.sky.core.secure.utils.AuthUtil;
import org.sky.core.tool.api.R;
import org.sky.core.tool.constant.KdConstant;
import org.sky.core.tool.utils.DateUtil;
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.entity.TechnicianEntity;
import org.sky.scientific.pojo.vo.TechnicianVO;
import org.sky.scientific.service.ITechnicianService;
import org.sky.scientific.wrapper.TechnicianWrapper;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

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

	private final ITechnicianService technicianService;

	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@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("/detail")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "详情", description = "传入technician")
	public R<TechnicianVO> detail(TechnicianEntity technician) {
		TechnicianEntity detail = technicianService.getOne(Condition.getQueryWrapper(technician));
		return R.data(TechnicianWrapper.build().entityVO(detail));
	}

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

//	@IsAdmin
	@PostMapping("import")
	@ApiOperationSupport(order = 12)
	@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("/update")
	@ApiOperationSupport(order = 5)
	@Operation(summary = "修改", description = "传入technician")
	public R update(@Valid @RequestBody TechnicianEntity technician) {
		return R.status(technicianService.updateTechnician(technician));
	}

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

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

//	@IsAdmin
	@PostMapping("/export")
	@ApiOperationSupport(order = 9)
	@Operation(summary = "导出数据", description = "传入technician")
	public void exportTechnician(@RequestBody TechnicianDTO technician, HttpServletResponse response) {
		List<TechnicianExcel> list = technicianService.exportTechnician(technician);
		ExcelUtil.export(response, "技术人员花名册数据" + DateUtil.time(), "技术人员花名册数据表", list, TechnicianExcel.class);
	}

	@GetMapping("/fetchLastMonthData")
	@ApiOperationSupport(order = 10)
	@Operation(summary = "读取上个月人员名单")
	public R fetchLastMonthData(TechnicianDTO technician) {
		return R.status(technicianService.fetchLastMonthData(technician));
	}

}
