
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 lombok.SneakyThrows;
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.core.tool.utils.Func;
import org.sky.scientific.pojo.dto.XmDesignCostQuery;
import org.sky.scientific.pojo.entity.XmDesignCostEntity;
import org.sky.scientific.pojo.vo.XmDesignCostVO;
import org.sky.scientific.service.IXmDesignCostService;
import org.sky.scientific.utils.ExportUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 项目-设计费用、装备调试费用与试验费用基本信息台账 控制器
 *
 * @author Kd
 * @since 2025-07-28
 */
@RestController
@AllArgsConstructor
@RequestMapping("/xm/design-cost")
@Tag(name = "项目-设计费用、装备调试费用与试验费用基本信息台账")
public class XmDesignCostController extends KdController {

	private final IXmDesignCostService service;

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

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

	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "新增或修改")
	public R submit(@Valid @RequestBody XmDesignCostEntity xmDesignCost) {
		return R.status(service.submit(xmDesignCost));
	}

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

	@SneakyThrows
	@PostMapping("/export-xmDesignCost")
	@ApiOperationSupport(order = 9)
	@Operation(summary = "导出", description = "传入xmDesignCost")
	public void exportXmDesignCost(HttpServletResponse response, @RequestBody XmDesignCostQuery dto) {
		List<XmDesignCostVO> list = service.selectXmDesignCostPage(Condition.getPage(new Query().setCurrent(1).setSize(Integer.MAX_VALUE)), dto).getRecords();
		if (Func.isEmpty(list)) {
			ExportUtil.processWithNoData(response);
		} else {
			ExportUtil.processWithData(response, "5和6.设计费用、装备调试费用与试验费用.xls");
			service.exportXmDesignCost(response.getOutputStream(), list, dto.getYearAndMonth());
		}
	}

}
