
package org.sky.scientific.controller;

import com.alibaba.fastjson.JSONObject;
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.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.AssetDTO;
import org.sky.scientific.pojo.dto.XmAssetCostQueryParam;
import org.sky.scientific.pojo.dto.XmAssetHoursDto;
import org.sky.scientific.pojo.dto.XmAssetQueryParam;
import org.sky.scientific.pojo.entity.AssetEntity;
import org.sky.scientific.pojo.entity.XmFixedAsset;
import org.sky.scientific.pojo.entity.XmIntangibleAsset;
import org.sky.scientific.pojo.vo.*;
import org.sky.scientific.service.IAssetService;
import org.sky.scientific.service.IXmAssetHoursService;
import org.sky.scientific.service.IXmFixedAssetService;
import org.sky.scientific.service.IXmIntangibleAssetService;
import org.sky.scientific.wrapper.ZcWrapper;
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("/asset")
@Tag(name = "科研资产管理", description = "资产表接口")
public class AssetController extends KdController {

	private final IAssetService service;
	private final IXmFixedAssetService fixedAssetService;
	private final IXmIntangibleAssetService intangibleAssetService;
	private final IXmAssetHoursService hoursService;

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

	@GetMapping("/simple/list")
	@ApiOperationSupport(order = 2)
	@Operation(summary = "列表-所有不分页", description = "")
	public R<List<AssetEntity>> list() {
		List<AssetEntity> list = service.selectAssetList();
		return R.data(list);
	}

	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "分页", description = "传入zc")
	public R<IPage<AssetVO>> page(AssetDTO dto, Query query) {
		IPage<AssetVO> pages = service.selectAssetPage(Condition.getPage(query), dto);
		return R.data(pages);
	}

	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@Operation(summary = "新增", description = "传入zc")
	public R save(@Valid @RequestBody AssetEntity asset) {
		return R.status(service.saveAsset(asset));
	}

	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@Operation(summary = "修改", description = "传入zc")
	public R update(@Valid @RequestBody AssetEntity asset) {
		return R.status(service.updateAsset(asset));
	}

	@PostMapping("/update-status")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "修改状态", description = "传入zc")
	public R updateStatus(@RequestBody AssetEntity asset) {
		return R.status(service.updateStatus(asset));
	}

	@PostMapping("/recover")
	@ApiOperationSupport(order = 7)
	@Operation(summary = "恢复")
	public R recover(@RequestBody AssetEntity asset) {
		return R.status(service.recover(asset));
	}

	@GetMapping("/fetchLastMonthData")
	@ApiOperationSupport(order = 10)
	@Operation(summary = "读取上个月资产名单", description = "传入选中的年月，比如2025-06，则读取2025-05的数据到06月")
	public R<Boolean> fetchLastMonthData(AssetDTO asset) {
		return R.status(service.fetchLastMonthData(asset));
	}

	@PostMapping("/remove")
	@ApiOperationSupport(order = 15)
	@Operation(summary = "删除", description = "传入ids")
	public R remove(@Parameter(description = "主键集合", required = true) @RequestParam String ids) {
		return R.status(service.physicalDeleteByIds(Func.toLongList(ids)));
	}

	@PostMapping("import")
	@ApiOperationSupport(order = 18)
	@Operation(summary = "导入", description = "传入excel")
	public R importAsset(MultipartFile file, String yearAndMonth) {
		service.importAsset(file, yearAndMonth);
		return R.success("操作成功");
	}

	@PostMapping("/export")
	@ApiOperationSupport(order = 19)
	@Operation(summary = "导出数据", description = "传入zc")
	public void exportZc(HttpServletResponse response, @RequestBody AssetDTO dto) {
 		service.exportAsset(response, dto);
 	}

	@GetMapping("/keyanyiqishebeimingxibiao/page")
	@ApiOperationSupport(order = 20)
	@Operation(summary = "科研仪器设备明细表-分页")
	public R<IPage<XmFixedAsset>> pageYiqishebei(XmAssetQueryParam param, Query query) {
		IPage<XmFixedAsset> pages = fixedAssetService.selectXmFixAssetPage(Condition.getPage(query), param, "仪器设备");
		return R.data(pages);
	}

	@PostMapping("/keyanyiqishebeimingxibiao/save")
	@ApiOperationSupport(order = 21)
	@Operation(summary = "科研仪器设备明细表-新增")
	public R saveKeyanyiqishebeimingxibiao(@Valid @RequestBody XmFixedAsset xmAsset) {
		return R.status(fixedAssetService.saveXmFixedAsset(xmAsset, "仪器设备"));
	}

	@PostMapping("/keyanyiqishebeimingxibiao/import")
	@ApiOperationSupport(order = 21)
	@Operation(summary = "科研仪器设备明细表-导入")
	public R importKeyanyiqishebeimingxibiao(MultipartFile file, String yearAndMonth) {
		fixedAssetService.xmFixAssetImport(file, yearAndMonth, "仪器设备");
		return R.success("操作成功");
	}

	@GetMapping("/keyanyiqishebeimingxibiao/fetchData")
	@ApiOperationSupport(order = 22)
	@Operation(summary = "科研仪器设备明细表-直接引用")
	public R<Boolean> fetchYiqishebeiData(String yearAndMonth) {
		return R.status(fixedAssetService.xmFixAsset_zhijieyinyong(yearAndMonth, "仪器设备"));
	}

	@PostMapping("/keyanyiqishebeimingxibiao/remove")
	@ApiOperationSupport(order = 24)
	@Operation(summary = "科研仪器设备明细表-删除", description = "传入ids")
	public R removeKeyanyiqishebeimingxibiao(@Parameter(description = "主键集合", required = true) @RequestParam String ids) {
		return R.status(fixedAssetService.physicalDeleteByIds(Func.toLongList(ids)));
	}

	@PostMapping("/hours/import")
	@ApiOperationSupport(order = 30)
	@Operation(summary = "科研仪器设备工时登记表-导入", description = "传入excel")
	public R importXmAssetHours(MultipartFile file, String yearAndMonth) {
		hoursService.importXmAssetHours(file, yearAndMonth);
		return R.success("操作成功");
	}

	@GetMapping("/hours/page")
	@ApiOperationSupport(order = 31)
	@Operation(summary = "科研仪器设备工时登记表-分页")
	public R<IPage<XmAssetHoursVO>> selectXmAssetHourspage(XmAssetHoursDto dto, Query query) {
		return R.data(hoursService.selectXmAssetHoursPage(dto, Condition.getPage(query)));
	}

	@PostMapping("/hours/submit/batch")
	@ApiOperationSupport(order = 32)
	@Operation(summary = "科研仪器设备工时登记表-编辑")
	public R submitAssetHours(@RequestBody XmAssetHoursDto dto) {
		return R.status(hoursService.batchSubmitXmAssetHours(dto));
	}

	@PostMapping("/hours/clear/batch")
	@ApiOperationSupport(order = 33)
	@Operation(summary = "科研仪器设备工时登记表-清除")
	public R clearXmAssetHours(@RequestBody JSONObject dto) {
		return R.status(hoursService.clearXmAssetHours(dto));
	}


	@GetMapping("/kyyqsbzjfyfpb")
	@ApiOperationSupport(order = 35)
	@Operation(summary = "科研仪器设备折旧费用分配表")
	public R<IPage<XmFixedAssetCostVo>> keyanyiqishebeizhejiufeiyongfenpeibiao(Query query, XmAssetQueryParam dto) {
		return R.data(fixedAssetService.keyanyiqishebeizhejiufeiyongfenpeibiao(Condition.getPage(query), dto));
	}


	@GetMapping("/fangwujianzhuwu/page")
	@ApiOperationSupport(order = 40)
	@Operation(summary = "科研房屋建筑物明细表-分页")
	public R<IPage<XmFixedAsset>> pageFangwujianzhuwu(XmAssetQueryParam xmAsset, Query query) {
		IPage<XmFixedAsset> pages = fixedAssetService.selectXmFixAssetPage(Condition.getPage(query), xmAsset, "房屋建筑物");
		return R.data(pages);
	}

	@GetMapping("/fangwujianzhuwu/fetchData")
	@ApiOperationSupport(order = 41)
	@Operation(summary = "科研房屋建筑物明细表-直接引用")
	public R<Boolean> fetchFangwujianzhuwuData(String yearAndMonth) {
		return R.status(fixedAssetService.xmFixAsset_zhijieyinyong(yearAndMonth, "房屋建筑物"));
	}

	@PostMapping("/fangwujianzhuwu/save")
	@ApiOperationSupport(order = 42)
	@Operation(summary = "科研房屋建筑物明细表-新增")
	public R saveFangwujianzhuwu(@Valid @RequestBody XmFixedAsset xmAsset) {
		return R.status(fixedAssetService.saveXmFixedAsset(xmAsset, "房屋建筑物"));
	}

	@PostMapping("/fangwujianzhuwu/import")
	@ApiOperationSupport(order = 43)
	@Operation(summary = "科研房屋建筑物明细表-导入")
	public R importFangwujianzhuwu(MultipartFile file, String yearAndMonth) {
		fixedAssetService.xmFixAssetImport(file, yearAndMonth, "房屋建筑物");
		return R.success("操作成功");
	}

	@PostMapping("/fangwujianzhuwu/remove")
	@ApiOperationSupport(order = 44)
	@Operation(summary = "科研房屋建筑物明细表-删除", description = "传入ids")
	public R removeFangwujianzhuwu(@Parameter(description = "主键集合", required = true) @RequestParam String ids) {
		return R.status(fixedAssetService.physicalDeleteByIds(Func.toLongList(ids)));
	}

	@GetMapping("/kyfwjzwzjfyfpb")
	@ApiOperationSupport(order = 46)
	@Operation(summary = "科研房屋建筑物折旧费用分配表")
	public R<IPage<XmFixedAssetCostVo>> keyanfangwujianzhuwuzhejiufeiyongfenpeibiao(Query query, XmAssetQueryParam dto) {
		return R.data(fixedAssetService.keyanfangwujianzhuwuzhejiufeiyongfenpeibiao(Condition.getPage(query), dto));
	}

	@PutMapping("/kyfwjzwzjfyfpb")
	@ApiOperationSupport(order = 47)
	@Operation(summary = "科研房屋建筑物折旧费用分配表-填写 研发分摊率、分摊率确定依据")
	public R<Boolean> keyanfangwujianzhuwuzhejiufeiyongfenpeibiaoEdit( @RequestBody XmFixedAssetCostVo dto) {
		return R.status(fixedAssetService.keyanfangwujianzhuwuzhejiufeiyongfenpeibiaoEdit(dto));
	}

	@GetMapping("/intangible/page")
	@ApiOperationSupport(order = 50)
	@Operation(summary = "科研无形资产明细表-分页")
	public R<IPage<XmIntangibleAsset>> pageIntangibleAsset(XmAssetQueryParam xmAsset, Query query) {
		IPage<XmIntangibleAsset> pages = intangibleAssetService.selectPage(Condition.getPage(query), xmAsset);
		return R.data(pages);
	}

	@GetMapping("/intangible/fetchData")
	@ApiOperationSupport(order = 51)
	@Operation(summary = "科研无形资产明细表-直接引用")
	public R<Boolean> fetchIntangibleAssetData(String yearAndMonth) {
		return R.status(intangibleAssetService.zhijieyinyong(yearAndMonth));
	}

	@PostMapping("/intangible/save")
	@ApiOperationSupport(order = 52)
	@Operation(summary = "科研无形资产明细表-新增")
	public R saveIntangibleAsset(@Valid @RequestBody XmIntangibleAsset xmAsset) {
		return R.status(intangibleAssetService.saveXmIntangibleAsset(xmAsset));
	}

	@PostMapping("/intangible/update")
	@ApiOperationSupport(order = 52)
	@Operation(summary = "科研无形资产明细表-修改")
	public R updateIntangibleAsset(@Valid @RequestBody XmIntangibleAsset xmAsset) {
		return R.status(intangibleAssetService.updateXmIntangibleAsset(xmAsset));
	}

	@PostMapping("/intangible/import")
	@ApiOperationSupport(order = 53)
	@Operation(summary = "科研无形资产明细表-导入")
	public R importIntangibleAsset(MultipartFile file, String yearAndMonth) {
		intangibleAssetService.xmIntangibleAssetImport(file, yearAndMonth);
		return R.success("操作成功");
	}

	@PostMapping("/intangible/remove")
	@ApiOperationSupport(order = 54)
	@Operation(summary = "科研无形资产明细表-删除", description = "传入ids")
	public R removeIntangibleAsset(@Parameter(description = "主键集合", required = true) @RequestParam String ids) {
		return R.status(intangibleAssetService.physicalDeleteByIds(Func.toLongList(ids)));
	}

	@GetMapping("/kywxzctxfyfpb")
	@ApiOperationSupport(order = 58)
	@Operation(summary = "科研无形资产摊销费用分配表")
	public R<IPage<XmIntangibleAssetCostVo>> keyanwuxingzichantanxiaofeiyongfenpeibiao(Query query, XmAssetQueryParam dto) {
		return R.data(intangibleAssetService.keyanwuxingzichantanxiaofeiyongfenpeibiao(Condition.getPage(query), dto));
	}

	@GetMapping("/zj-cqdt-wxzctx-fy-hzb")
	@ApiOperationSupport(order = 60)
	@Operation(summary = "折旧费用、长期待摊费用、无形资产摊销费用汇总表")
	public R<IPage<XmAssetCostSummary>> zjCqdtWxzctxFyHzb(Query query, XmAssetCostQueryParam dto){
		return R.data(service.zjCqdtWxzctxFyHzb(Condition.getPage(query), dto));
	}
}
