package com.hotent.project.controller;


import com.aliyun.tea.utils.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hotent.base.annotation.ApiGroup;
import com.hotent.base.constants.ApiGroupConsts;
import com.hotent.baseInfo.model.CostDocumentTemplateFile;
import com.hotent.common.CrudService;
import com.hotent.config.EipConfig;
import com.hotent.constant.BaseConstant;
import com.hotent.project.manager.CostProjectDocumentFileManager;
import com.hotent.project.model.CostProjectApproval;
import com.hotent.project.model.CostProjectDocumentFile;
import com.hotent.project.req.CostProjectApprovalPageReq;
import com.hotent.project.req.CostProjectBasePageReq;
import com.hotent.project.req.CostProjectDocumentPageReq;
import com.hotent.project.req.CostProjectDocumentReq;
import com.hotent.project.resp.CostProjectDocumentResp;
import com.hotent.req.IdReq;
import com.hotent.req.PageReq;
import com.hotent.resp.PageResp;
import com.hotent.util.FileUploadUtil;
import com.hotent.util.wordexcelutils.DocumentProcessor;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.hotent.base.model.CommonResult;
import com.hotent.base.util.StringUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.hotent.base.controller.BaseController;
import com.hotent.project.model.CostProjectDocument;
import com.hotent.project.manager.CostProjectDocumentManager;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 监审项目文书表 前端控制器
 *
 * @company 山西清众科技股份有限公司
 * @author 超级管理员
 * @since 2025-09-26
 */
@RestController
@RequestMapping("/api/costProjectDocument/v1/")
@Api(tags = "监审项目文书")
@ApiGroup(group = {ApiGroupConsts.GROUP_COST})
public class CostProjectDocumentController extends BaseController<CostProjectDocumentManager, CostProjectDocument> {

	// 创建通用服务实例
	private final CrudService crudService = new CrudService();

	@Autowired
	private CostProjectDocumentManager costProjectDocumentManager;

	@Autowired
	private CostProjectDocumentFileManager costProjectDocumentFileManager;
	/**
	 * 根据id获取监审项目文书表数据详情
	 * @param id
	 * @return
	 * @throws Exception 
	 * ModelAndView
	 */
	@GetMapping(value="/getDetail")
	@ApiOperation(value="根据id获取监审项目文书表数据详情",httpMethod = "GET",notes = "根据id获取监审项目文书表数据详情")
	public CommonResult<CostProjectDocument> getDetail(@ApiParam(name="id",value="业务对象主键", required = true)@RequestParam(required=true) String id) throws Exception{
		return CommonResult.<CostProjectDocument>ok().value(baseService.getDetail(id));
	}




	//分页查询
	@PostMapping(value="/pageList")
	@ApiOperation(value="分页查询pageList",httpMethod = "POST",notes = "分页查询pageList")
	public CommonResult<PageResp<CostProjectDocument>> pageList(@RequestBody CostProjectDocumentPageReq req) throws Exception{
		IPage<CostProjectDocument> costProjectDocumentIPage = baseService.pageList(req);
		PageResp<CostProjectDocument> success = PageResp.success(costProjectDocumentIPage);
		return CommonResult.<PageResp<CostProjectDocument>>ok().value(success);

	}

	//新增
	@PostMapping(value="/save")
	@ApiOperation(value="新增save",httpMethod = "POST",notes = "新增save")
	public CommonResult<String> save(@RequestBody CostProjectDocumentReq req) throws Exception {
		String id= costProjectDocumentManager.create(req);
		return CommonResult.<String>ok().value(id).message("保存成功");

	}

	//修改
	@PutMapping(value="/update")
	@ApiOperation(value="修改update",httpMethod = "Put",notes = "修改update")
	public CommonResult<String> update(@RequestBody CostProjectDocumentReq req) {
		costProjectDocumentManager.updateProjectDocument(req);
		return CommonResult.<String>ok().message("修改成功");
	}

	//修改
	@PostMapping(value="/feedback/document/url")
	@ApiOperation(value="修改update",httpMethod = "POST",notes = "修改update")
	public CommonResult<String> feedbackDocumentUrl(@RequestBody CostProjectDocumentReq req) {
		baseService.feedbackDocumentUrl(req);
		return CommonResult.<String>ok().message("修改成功");
	}

	@PostMapping(value="/deleteByIds")
	@ApiOperation(value="根据id删除数据",httpMethod = "POST",notes = "根据id删除数据")
	public CommonResult<String> cleanByIds(@ApiParam(name="ids", value="id集合") @RequestBody IdReq req){
		costProjectDocumentManager.deleteByIds(req.getIds());
		return CommonResult.<String>ok().message("删除文书成功");
	}

	@GetMapping(value="/getByProjectId")
	@ApiOperation(value="根据项目id获取项目文书内容",httpMethod = "GET",notes = "根据项目id获取项目文书内容")
	public CommonResult<CostProjectDocumentResp> getByProjectId(@ApiParam(name="projectId", value="项目id") @RequestParam String projectId){
		return CommonResult.<CostProjectDocumentResp>ok().value(costProjectDocumentManager.getByProjectId(projectId));
	}

	/**
	 * 根据id生成文书并且下载
	 * @param id
	 * @return
	 * @throws Exception
	 * ModelAndView
	 */
	@GetMapping(value="/downDocument")
	@ApiOperation(value="根据id生成文书并且下载",httpMethod = "GET",notes = "根据id生成文书并且下载")
	public CommonResult<String> downDocument(@ApiParam(name="id",value="业务对象主键", required = true)@RequestParam(required=true) String id) throws Exception{
		return CommonResult.<String>ok().value(costProjectDocumentManager.createDocument(id));
	}


	//上传扫描
	@PostMapping(value="/updateScan")
	@ApiOperation(value="",httpMethod = "POST",notes = "修改update")
	public CommonResult<String> updateScan(@RequestBody CostProjectDocument req) {
		CostProjectDocument entity = costProjectDocumentManager.get(req.getId());
		entity.setScanDocumentUrl(req.getScanDocumentUrl());
		costProjectDocumentManager.updateById(entity);
		return CommonResult.<String>ok().message("修改成功");

	}



}
