package com.hotent.project.controller;


import com.hotent.base.annotation.ApiGroup;
import com.hotent.base.constants.ApiGroupConsts;
import com.hotent.baseInfo.manager.AuditedUnitManager;
import com.hotent.baseInfo.model.CostCatalog;
import com.hotent.baseInfo.req.CostCatalogPageReq;
import com.hotent.baseInfo.req.CostTaskSearchReq;
import com.hotent.baseInfo.vo.CatalogTreeVo;
import com.hotent.baseInfo.vo.TaskTreeVo;
import com.hotent.common.CrudService;
import com.hotent.util.JsonUtil;
import com.hotent.util.treeutil.SumTree;
import com.hotent.util.treeutil.newtreeutil.TreeDotUtils;
import io.swagger.annotations.Api;
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.CostProjectTask;
import com.hotent.project.manager.CostProjectTaskManager;

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

/**
 * 项目任务表 前端控制器
 *
 * @company 山西清众科技股份有限公司
 * @author 超级管理员
 * @since 2025-10-03
 */
@RestController
@RequestMapping("/api/costProjectTask/v1/")
@Api(tags = "项目任务表")
@ApiGroup(group = {ApiGroupConsts.GROUP_COST})
public class CostProjectTaskController extends BaseController<CostProjectTaskManager, CostProjectTask> {

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





	/**
	 * 根据id获取项目任务表数据详情
	 * @param id
	 * @return
	 * @throws Exception
	 * ModelAndView
	 */
	@GetMapping(value="/getDetail")
	@ApiOperation(value="根据id获取项目任务表数据详情",httpMethod = "GET",notes = "根据id获取项目任务表数据详情")
	public CommonResult<CostProjectTask> getDetail(@ApiParam(name="id",value="业务对象主键", required = true)@RequestParam(required=true) String id) throws Exception{
		return CommonResult.<CostProjectTask>ok().value(baseService.getById(id));
	}
    /**
	 * 新增,更新项目任务表
	 * @param costProjectTask
	 * @throws Exception
	 * @return
	 * @exception
	 */
	@PostMapping(value="/save")
	@ApiOperation(value = "新增,更新项目任务表数据", httpMethod = "POST", notes = "新增,更新项目任务表数据")
	public CommonResult<String> save(@ApiParam(name="CostProjectTask",value="项目任务表对象", required = true)@RequestBody CostProjectTask costProjectTask) throws Exception{
		String msg = StringUtil.isEmpty(costProjectTask.getId()) ? "添加项目任务表成功" : "更新项目任务表成功";
		baseService.createOrUpdate(costProjectTask);
		return CommonResult.<String>ok().message(msg);
	}



	/**
	 * 目录列表查询
	 */
	@PostMapping("/taskList")
	@ApiOperation(value = "任务进度管理列表", notes = "任务进度管理列表")
	public CommonResult<List<SumTree<TaskTreeVo>>> getCatalogList(@RequestBody CostTaskSearchReq req) {
		List<CostProjectTask> taskList = baseService.getTaskList(req);
		List<TaskTreeVo> allList = JsonUtil.getJsonToList(taskList, TaskTreeVo.class);
		for (int i = 0; i < taskList.size(); i++) {
			CostProjectTask task = taskList.get(i);
			allList.get(i).setId(task.getId());
			allList.get(i).setParentId(task.getPid());
		}
		List<SumTree<TaskTreeVo>> trees = TreeDotUtils.convertListToTreeDotFilter(allList);
		CommonResult<List<SumTree<TaskTreeVo>>> result = CommonResult.ok();
		result.setValue(trees);
		return result;
	}



	/**
	 * 督办事项管理
	 */
	@PostMapping("/supervisorList")
	@ApiOperation(value = "督办事项管理列表", notes = "督办事项管理列表")
	public CommonResult<List<SumTree<TaskTreeVo>>> supervisorList(@RequestBody CostTaskSearchReq req) {
		List<CostProjectTask> taskList = baseService.getTaskList(req);
		List<TaskTreeVo> allList = JsonUtil.getJsonToList(taskList, TaskTreeVo.class);
		for (int i = 0; i < taskList.size(); i++) {
			CostProjectTask task = taskList.get(i);
			allList.get(i).setId(task.getId());
			allList.get(i).setParentId(task.getPid());
		}
		List<SumTree<TaskTreeVo>> trees = TreeDotUtils.convertListToTreeDotFilter(allList);
		CommonResult<List<SumTree<TaskTreeVo>>> result = CommonResult.ok();
		result.setValue(trees);
		return result;
	}
}
