
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.XmCqdtfyExcel;
import org.sky.scientific.pojo.dto.XmCqdtfyDTO;
import org.sky.scientific.pojo.entity.XmCqdtfyEntity;
import org.sky.scientific.pojo.vo.XmCqdtfyVO;
import org.sky.scientific.service.IXmCqdtfyService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 长期待摊费用分配表 控制器
 *
 * @author Kd
 * @since 2025-07-24
 */
@RestController
@AllArgsConstructor
@RequestMapping("/xm/cqdtfy")
@Tag(name = "研发费用管理-长期待摊费用分配表")
public class XmCqdtfyController extends KdController {

	private final IXmCqdtfyService service;

	@GetMapping("/page")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "分页", description  = "传入xmCqdtfy")
	public R<IPage<XmCqdtfyVO>> page(XmCqdtfyDTO dto, Query query) {
		IPage<XmCqdtfyVO> pages = service.selectPageByCondition(Condition.getPage(query), dto);
		return R.data(pages);
	}

	@PostMapping("import")
	@ApiOperationSupport(order = 2)
	@Operation(summary = "导入")
	public R importCqdtfy(MultipartFile file, String yearAndMonth) {
		service.importData(file, yearAndMonth);
		return R.success("操作成功");
	}

	@GetMapping("/fetchLastMonthData")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "读取上个月数据", description = "传入选中的年月，比如2025-06，则读取2025-05的数据到06月")
	public R<Boolean> fetchLastMonthData(String yearAndMonth) {
		return R.status(service.fetchLastMonthData(yearAndMonth));
	}

	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "新增或修改", description  = "传入xmCqdtfy")
	public R submit(@Valid @RequestBody XmCqdtfyEntity xmCqdtfy) {
		return R.status(service.submit(xmCqdtfy));
	}

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

 	@GetMapping("/export-xmCqdtfy")
	@ApiOperationSupport(order = 9)
	@Operation(summary = "导出数据", description  = "传入xmCqdtfy")
	public void exportXmCqdtfy(@Parameter(hidden = true) @RequestParam Map<String, Object> xmCqdtfy, KdUser kdUser, HttpServletResponse response) {
		QueryWrapper<XmCqdtfyEntity> queryWrapper = Condition.getQueryWrapper(xmCqdtfy, XmCqdtfyEntity.class);
		List<XmCqdtfyExcel> list = service.exportXmCqdtfy(queryWrapper);
		ExcelUtil.export(response, "长期待摊费用分配表数据" + DateUtil.time(), "长期待摊费用分配表数据表", list, XmCqdtfyExcel.class);
	}

}
