
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.XmWwxmCostQuery;
import org.sky.scientific.pojo.dto.XmWwxmDTO;
import org.sky.scientific.pojo.entity.XmWwxmCost;
import org.sky.scientific.pojo.entity.XmWwxmEntity;
import org.sky.scientific.pojo.vo.XmWwxmCostVO;
import org.sky.scientific.pojo.vo.XmWwxmVO;
import org.sky.scientific.service.IXmWwxmCostService;
import org.sky.scientific.service.IXmWwxmService;
import org.sky.scientific.utils.ExportUtil;
import org.sky.scientific.wrapper.XmWwxmWrapper;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 项目-委外项目 控制器
 *
 * @author Kd
 * @since 2025-06-29
 */
@RestController
@AllArgsConstructor
@RequestMapping("/xm/wwxm")
@Tag(name = "研发项目管理-委外项目")
public class XmWwxmController extends KdController {

	private final IXmWwxmService service;
	private final IXmWwxmCostService costService;

	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "详情", description = "传入xmWwxm")
	public R<XmWwxmVO> detail(XmWwxmEntity xmWwxm) {
		XmWwxmEntity detail = service.getOne(Condition.getQueryWrapper(xmWwxm));
		return R.data(XmWwxmWrapper.build().entityVO(detail));
	}

	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "分页", description = "传入xmWwxm")
	public R<IPage<XmWwxmVO>> page(XmWwxmDTO wwxmDTO, Query query) {
		IPage<XmWwxmVO> pages = service.selectPage(Condition.getPage(query), wwxmDTO);
		return R.data(pages);
	}

	@GetMapping("/list")
	@ApiOperationSupport(order = 4)
	@Operation(summary = "列表")
	public R<List<XmWwxmVO>> simpleList(String yearAndMonth) {
		List<XmWwxmVO> list = service.selectSimpleList(yearAndMonth);
		return R.data(list);
	}

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

	@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("/cost/page")
	@ApiOperationSupport(order = 33)
	@Operation(summary = "委托外部研发费用-基本信息台账-分页")
	public R<IPage<XmWwxmCostVO>> pageWwxmCost(XmWwxmCostQuery dto, Query query) {
		IPage<XmWwxmCostVO> pages = costService.selectPage(Condition.getPage(query), dto);
		return R.data(pages);
	}

	@PostMapping("/cost/import")
	@ApiOperationSupport(order = 34)
	@Operation(summary = "委托外部研发费用-基本信息台账-导入", description = "传入excel")
	public R importWwxmCost(MultipartFile file, String yearAndMonth) {
		costService.importWwxmCost(file, yearAndMonth);
		return R.success("操作成功");
	}

	@PostMapping("/cost/submit")
	@ApiOperationSupport(order = 35)
	@Operation(summary = "委托外部研发费用-基本信息台账-新增或修改")
	public R submitWwxmCost(@Valid @RequestBody XmWwxmCost cost) {
		return R.status(costService.submit(cost));
	}

	@PostMapping("/cost/remove")
	@ApiOperationSupport(order = 37)
	@Operation(summary = "委托外部研发费用-基本信息台账-删除", description = "传入ids")
	public R removeWwxmCost(@Parameter(description = "主键集合", required = true) @RequestParam String ids) {
		return R.status(costService.physicalDeleteByIds(Func.toLongList(ids)));
	}

	@SneakyThrows
	@GetMapping("/cost/export")
	@ApiOperationSupport(order = 39)
	@Operation(summary = "委托外部研发费用-基本信息台账-导出")
	public void exportWwxmCost(HttpServletResponse response, XmWwxmCostQuery dto) {
		List<XmWwxmCostVO> list = costService.selectPage(Condition.getPage(new Query().setCurrent(1).setSize(Integer.MAX_VALUE)), dto).getRecords();
		if (Func.isEmpty(list)) {
			ExportUtil.processWithNoData(response);
		} else {
			ExportUtil.processWithData(response, "委托外部研究开发费用.xlsx");
			costService.export(response.getOutputStream(), list, dto.getYearAndMonth());
		}
	}

}
