package com.hotent.project.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hotent.base.annotation.ApiGroup;
import com.hotent.base.constants.ApiGroupConsts;
import com.hotent.base.exception.BaseException;
import com.hotent.base.util.UniqueIdUtil;
import com.hotent.project.manager.CostProjectApprovalManager;
import com.hotent.project.manager.CostProjectTaskManager;
import com.hotent.project.model.CostProjectApproval;
import com.hotent.project.model.CostProjectTask;
import com.hotent.project.req.CostProjectBasePageReq;
import com.hotent.project.req.CostProjectDocumentReq;
import com.hotent.uc.api.model.IUser;
import com.hotent.uc.util.ContextUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.hotent.base.model.CommonResult;
import com.hotent.base.util.StringUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.RestController;
import com.hotent.base.controller.BaseController;
import com.hotent.project.model.CostProjectDeliberate;
import com.hotent.project.manager.CostProjectDeliberateManager;

import java.time.LocalDateTime;

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

	@Autowired
	private CostProjectApprovalManager costProjectApprovalManager;

	@Autowired
	private CostProjectTaskManager costProjectTaskManager;
	/**
	 * 根据id获取监审项目集体审议表数据详情
	 * @param id
	 * @return
	 * @throws Exception 
	 * ModelAndView
	 */
	@GetMapping(value="/getDetail")
	@ApiOperation(value="根据id获取监审项目集体审议表数据详情",httpMethod = "GET",notes = "根据id获取监审项目集体审议表数据详情")
	public CommonResult<CostProjectDeliberate> getDetail(@ApiParam(name="id",value="业务对象主键", required = true)@RequestParam(required=true) String id) throws Exception{
		return CommonResult.<CostProjectDeliberate>ok().value(baseService.getDetail(id));
	}
	//新增
	@PostMapping(value="/save")
	@ApiOperation(value="新增save",httpMethod = "POST",notes = "新增save")
	public CommonResult<String> save(@RequestBody CostProjectDeliberate req) throws Exception {
		if (req.getProjectId()==null) {
			throw new BaseException("项目id不能为空");
		}
		if (req.getTaskId()==null) {
			throw new BaseException("任务id不能为空");
		}
		CostProjectApproval approval = costProjectApprovalManager.getById(req.getProjectId());
		if (approval==null) {
			throw new BaseException("根据id：【"+req.getProjectId()+"】未查询到项目信息");
		}
		CostProjectTask task = costProjectTaskManager.getById(req.getTaskId());
		if (task==null) {
			throw new BaseException("根据id：【"+req.getTaskId()+"】未查询到任务信息");
		}
		IUser iUser = ContextUtil.getCurrentUser();
		req.setCreateBy(iUser.getAccount());
		req.setCreateTime(LocalDateTime.now());
		req.setId(UniqueIdUtil.getSuid());
		baseService.create(req);
		return CommonResult.<String>ok().message("保存成功");
	}


	//新增
	@PostMapping(value="/update")
	@ApiOperation(value="update",httpMethod = "POST",notes = "新增save")
	public CommonResult<String> update(@RequestBody CostProjectDeliberate req) throws Exception {
		if (req.getProjectId()==null) {
			throw new BaseException("项目id不能为空");
		}
		if (req.getTaskId()==null) {
			throw new BaseException("任务id不能为空");
		}
		CostProjectDeliberate deliberate = this.getById(req.getId());
		if (deliberate==null) {
			throw new BaseException("根据id：【"+req.getId()+"】未查询到实体信息");
		}
		CostProjectApproval approval = costProjectApprovalManager.getById(req.getProjectId());
		if (approval==null) {
			throw new BaseException("根据id：【"+req.getProjectId()+"】未查询到项目信息");
		}
		CostProjectTask task = costProjectTaskManager.getById(req.getTaskId());
		if (task==null) {
			throw new BaseException("根据id：【"+req.getTaskId()+"】未查询到任务信息");
		}
		IUser iUser = ContextUtil.getCurrentUser();
		req.setUpdateBy(iUser.getAccount());
		req.setUpdateTime(LocalDateTime.now());
		baseService.updateById(req);
		return CommonResult.<String>ok().message("保存成功");
	}

	/**
	 * 分页查询
	 */
	@PostMapping(value="/pageList")
	@ApiOperation(value="分页查询pageList",httpMethod = "POST",notes = "分页查询pageList")
	public CommonResult<IPage<CostProjectDeliberate>> pageList(@RequestBody CostProjectBasePageReq req) {
		IPage<CostProjectDeliberate> pageResult = baseService.pageList(req);
		return CommonResult.<IPage<CostProjectDeliberate>>ok().value(pageResult);
	}

}
