
package org.sky.scientific.controller;

import com.alibaba.fastjson.JSONObject;
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.XmExcel;
import org.sky.scientific.pojo.dto.XmDTO;
import org.sky.scientific.pojo.dto.XmUpdateDto;
import org.sky.scientific.pojo.entity.XmEntity;
import org.sky.scientific.pojo.entity.XmFinanceEntity;
import org.sky.scientific.pojo.vo.XmSummary;
import org.sky.scientific.pojo.vo.XmVO;
import org.sky.scientific.pojo.vo.XmYszbVO;
import org.sky.scientific.service.IXmFinanceService;
import org.sky.scientific.service.IXmService;
import org.springframework.web.bind.annotation.*;

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

/**
 * 科研项目 控制器
 *
 * @author Kd
 * @since 2025-06-29
 */
@RestController
@AllArgsConstructor
@RequestMapping("/xm")
@Tag(name = "科研项目", description = "科研项目接口")
public class XmController extends KdController {

	private final IXmService service;
	private final IXmFinanceService xmFinanceService;

	@GetMapping("/list")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "科研项目-列表", description  = "传入xm")
	public R<IPage<XmEntity>> list(XmDTO xm, Query query) {
		IPage<XmEntity> pages = service.selectXmList(Condition.getPage(query), xm);
		return R.data(pages);
	}

	@GetMapping("/list-year")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "年度科研项目-列表", description  = "传入xm")
	public R<List<XmEntity>> list(String year) {
		List<XmEntity> list = service.selectXmListByYear(year);
		return R.data(list);
	}

	@GetMapping("/assist/list")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "辅助核算-列表", description  = "yearAndMonth：2025")
	public R<IPage<JSONObject>> assistList(XmDTO xm, Query query) {
		IPage<JSONObject> pages = service.selectAssistList(Condition.getPage(query), xm);
		return R.data(pages);
	}

	@GetMapping("/detail")
	@ApiOperationSupport(order = 2)
	@Operation(summary = "科研项目-详情", description  = "传入xm")
	public R<XmVO> detail(XmEntity xm) {
		XmVO xmVO = service.selectByXmId(xm.getId());
		return R.data(xmVO);
	}

	@GetMapping("/summary")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "研发项目汇总表", description  = "传入xm")
	public R<IPage<XmSummary>> summary(XmDTO xm, Query query) {
		IPage<XmSummary> pages = service.summaryPage(Condition.getPage(query),xm);
		return R.data(pages);
	}

	@PostMapping("summary-export")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "研发项目汇总表-导出")
	public void exportSummary(@RequestBody XmDTO dto, HttpServletResponse response) {
		service.exportSummary(response, dto);
	}

	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@Operation(summary = "项目立项", description  = "传入xm")
	public R<JSONObject> save(@Valid @RequestBody XmDTO xm) {
		Long xmId = service.approval(xm);
		JSONObject result = new JSONObject();
		result.put("xmId", xmId);
		return R.data(result);
	}

	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@Operation(summary = "项目立项-修改", description  = "传入xm")
	public R<Boolean> update(@Valid @RequestBody XmUpdateDto xm) {
		return R.status(service.updateXm(xm));
	}

	@GetMapping("/approval")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "查询立项信息")
	public R<XmVO> selectApprovalInfo(Long xmId){
		return R.data(service.selectApprovalInfo(xmId));
	}

	@GetMapping("/export-xm")
	@ApiOperationSupport(order = 9)
	@Operation(summary = "导出数据", description  = "传入xm")
	public void exportXm(@Parameter(hidden = true) @RequestParam Map<String, Object> xm, KdUser kdUser, HttpServletResponse response) {
		QueryWrapper<XmEntity> queryWrapper = Condition.getQueryWrapper(xm, XmEntity.class);
		List<XmExcel> list = service.exportXm(queryWrapper);
		ExcelUtil.export(response, "科研项目数据" + DateUtil.time(), "科研项目数据表", list, XmExcel.class);
	}

	@GetMapping("yszb")
	@ApiOperationSupport(order = 10)
	@Operation(summary = "项目预算-预算总表", description  = "传入项目id")
	public R<XmYszbVO> selectYszb(@RequestParam("xmId") Long xmId) {
		XmYszbVO xmYszbVO = service.selectYszb(xmId);
		return R.data(xmYszbVO);
	}

	@GetMapping("finance/list")
	@ApiOperationSupport(order = 20)
	@Operation(summary = "会计/加计扣除/高新-研发项目清单-列表")
	public R<List<XmFinanceEntity>> selectXmFinancePage(XmFinanceEntity dto) {
		return R.data(xmFinanceService.selectXmFinanceList(dto));
	}

	@PostMapping("finance/fetch")
	@ApiOperationSupport(order = 21)
	@Operation(summary = "会计/加计扣除/高新-研发项目清单-引入", description = "传入yearAndMonth、type即可")
	public R<Boolean> fetchFinance(XmFinanceEntity entity){
		return R.status(xmFinanceService.fetchFinance(entity));
	}

	@PostMapping("finance/save")
	@ApiOperationSupport(order = 21)
	@Operation(summary = "会计/加计扣除/高新-研发项目清单-新增")
	public R<Boolean> saveFinance(@Valid @RequestBody XmFinanceEntity entity){
		return R.status(xmFinanceService.saveFinance(entity));
	}

	@PostMapping("finance/remove")
	@ApiOperationSupport(order = 22)
	@Operation(summary = "会计/加计扣除/高新-研发项目清单-批量删除", description = "传入ids")
	public R removeFinance(@Parameter(description = "主键集合", required = true) @RequestParam String ids) {
		return R.status(xmFinanceService.physicalDeleteByIds(Func.toLongList(ids)));
	}
}
