
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.XmWwxmExcel;
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.wrapper.XmWwxmWrapper;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 项目-委外项目 控制器
 *
 * @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("/export")
	@ApiOperationSupport(order = 9)
	@Operation(summary = "导出数据", description  = "传入xmWwxm")
	public void export(@Parameter(hidden = true) @RequestParam Map<String, Object> xmWwxm, KdUser kdUser, HttpServletResponse response) {
		QueryWrapper<XmWwxmEntity> queryWrapper = Condition.getQueryWrapper(xmWwxm, XmWwxmEntity.class);
		List<XmWwxmExcel> list = service.exportXmWwxm(queryWrapper);
		ExcelUtil.export(response, "项目-委外项目数据" + DateUtil.time(), "项目-委外项目数据表", list, XmWwxmExcel.class);
	}

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

 	@GetMapping("/cost/export")
	@ApiOperationSupport(order = 39)
	@Operation(summary = "委托外部研发费用-基本信息台账-导出")
	public void exportWwxmCost(HttpServletResponse response,XmWwxmCostQuery dto) {
		costService.export(response, dto);
	}

}
