package com.hotent.project.controller;


import com.hotent.base.annotation.ApiGroup;
import com.hotent.base.constants.ApiGroupConsts;
import com.hotent.base.query.PageList;
import com.hotent.common.CrudService;
import com.hotent.project.model.CostProjectSurvey;
import com.hotent.project.req.CostProjectApprovalPageReq;
import com.hotent.project.req.CostProjectApprovalPublishReq;
import com.hotent.project.resp.CostProjectApprovalDetailResp;
import com.hotent.project.resp.CostProjectMembersResp;
import com.hotent.resp.PageResp;
import com.hotent.uc.manager.UserManager;
import com.hotent.uc.model.User;
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.CostProjectApproval;
import com.hotent.project.manager.CostProjectApprovalManager;

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

/**
 * 成本监审立项主表 前端控制器
 *
 * @company 山西清众科技股份有限公司
 * @author 超级管理员
 * @since 2025-09-22
 */
@RestController
@RequestMapping("/api/costProjectApproval/v1/")
@Api(tags = "成本监审立项")
@ApiGroup(group = {ApiGroupConsts.GROUP_COST})
public class CostProjectApprovalController extends BaseController<CostProjectApprovalManager, CostProjectApproval> {
	@Autowired
	UserManager userService;
	// 创建通用服务实例
	private final CrudService crudService = new CrudService();

	@Autowired
	private CostProjectApprovalManager costProjectApprovalManager;
	/**
	 * 根据id获取成本监审立项主表数据详情
	 * @param id
	 * @return
	 * @throws Exception
	 * ModelAndView
	 */
	@GetMapping(value="/getDetail")
	@ApiOperation(value="根据id获取成本监审立项主表数据详情",httpMethod = "GET",notes = "根据id获取成本监审立项主表数据详情")
	public CommonResult<CostProjectApprovalDetailResp> getDetail(@ApiParam(name="id",value="业务对象主键", required = true)@RequestParam(required=true) String id) throws Exception{
		return CommonResult.<CostProjectApprovalDetailResp>ok().value(baseService.getDetail(id));
	}
	//分页查询
	/*@PostMapping(value="/pageList")
	@ApiOperation(value="分页查询pageList",httpMethod = "POST",notes = "分页查询pageList")
	public CommonResult<PageResp<CostProjectApproval>> pageList(@RequestBody CostProjectApprovalPageReq req) {
		return CommonResult.<PageResp<CostProjectApproval>>ok().value(crudService.pageQuery(req, baseService::pageList));
	}*/


	@PostMapping(value="/pageList")
	@ApiOperation(value="分页查询pageList",httpMethod = "POST",notes = "分页查询pageList")
	public PageList<CostProjectApproval> pageList(@RequestBody CostProjectApprovalPageReq req) throws Exception {
		return costProjectApprovalManager.pageList(req);
	}




	//任务发布
	@PostMapping(value="/publish")
	@ApiOperation(value="任务发布",httpMethod = "POST",notes = "任务发布")
	public CommonResult<String> publish(@RequestBody CostProjectApprovalPublishReq req) {
		baseService.publish(req);
		return CommonResult.<String>ok();
	}


	@PostMapping(value = "list")
	@ApiOperation(value = "查询list", httpMethod = "POST", notes = "查询list")
	public CommonResult<List<CostProjectApproval>> list() {
		return CommonResult.<List<CostProjectApproval>>ok().value(baseService.getList());
	}


	@PostMapping(value = "analyze/list")
	@ApiOperation(value = "查询analyzeList", httpMethod = "POST", notes = "查询analyzeList")
	public CommonResult<List<CostProjectApproval>> analyzeList() {
		return CommonResult.<List<CostProjectApproval>>ok().value(baseService.getAnalyzeList());
	}

	/**
	 * 根据id获取成本监审立项主办人员
	 * @param id
	 * @return
	 * @throws Exception
	 * ModelAndView
	 */
	@GetMapping(value="/getMembers")
	@ApiOperation(value="根据id获取成本监审立项主办人员",httpMethod = "GET",notes = "根据id获取成本监审立项主办人员")
	public CommonResult<List<CostProjectMembersResp>> getMembers(@ApiParam(name="id",value="业务对象主键", required = true)@RequestParam() String id){
		CostProjectApprovalDetailResp costProjectApprovalDetailResp = baseService.getDetail(id);
		List<String> members = Arrays.asList(costProjectApprovalDetailResp.getProjectMembers().split(","));
		List<User> users = userService.getAll();
		List<CostProjectMembersResp> membersResp =  users.stream().filter(u -> members.contains(u.getId())).map(u->{
			CostProjectMembersResp costProjectMembersResp = new CostProjectMembersResp();
			costProjectMembersResp.setId(u.getId());
			costProjectMembersResp.setName(u.getFullname());
			return costProjectMembersResp;
		}).collect(Collectors.toList());
		return CommonResult.<List<CostProjectMembersResp>>ok().value(membersResp);
	}

}
