
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.secure.annotation.IsAdmin;
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.XmLeaseExcel;
import org.sky.scientific.pojo.entity.XmLeaseEntity;
import org.sky.scientific.service.IXmLeaseService;
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-17
 */
@RestController
@AllArgsConstructor
@RequestMapping("/lease")
@Tag(name = "研发项目-租赁管理")
public class XmLeaseController extends KdController {

	private final IXmLeaseService service;

	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "研发租赁资产使用记录和费用汇总表-分页")
	public R<IPage<XmLeaseEntity>> page(XmLeaseEntity dto, Query query) {
		IPage<XmLeaseEntity> pages = service.selectXmLeasePage(Condition.getPage(query), dto);
		return R.data(pages);
	}

	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "研发租赁资产使用记录和费用汇总表-新增或修改")
	public R submit(@Valid @RequestBody XmLeaseEntity xmLease) {
		return R.status(service.submit(xmLease));
	}

	@PostMapping("/import")
	@ApiOperationSupport(order = 4)
	@Operation(summary = "研发租赁资产使用记录和费用汇总表-导入")
	public R importXmLease(MultipartFile file, String yearAndMonth) {
		return R.data(service.importXmLease(file, yearAndMonth));
	}

	@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 = "研发租赁资产使用记录和费用汇总表-导出数据")
	public void export(@Parameter(hidden = true) @RequestParam Map<String, Object> xmLease, KdUser kdUser, HttpServletResponse response) {
		QueryWrapper<XmLeaseEntity> queryWrapper = Condition.getQueryWrapper(xmLease, XmLeaseEntity.class);
		List<XmLeaseExcel> list = service.exportXmLease(queryWrapper);
		ExcelUtil.export(response, "研发租赁资产使用记录和费用汇总表" + DateUtil.time(), "研发租赁资产使用记录和费用汇总表", list, XmLeaseExcel.class);
	}

	@GetMapping("yfzlzcfyfpb")
	@ApiOperationSupport(order = 15)
	@Operation(summary = "研发租赁资产费用分配表")
	public R<List<JSONObject>> yanfalingliaodan(XmLeaseEntity dto){
		return R.data(service.yanfazulinzichanfeiyongfenpeibiao(dto));
	}

}
