package com.xinelu.applet.controller.appletscreeningproject;

import com.xinelu.common.core.controller.BaseController;
import com.xinelu.common.core.domain.R;
import com.xinelu.manage.domain.screeningproject.ScreeningProject;
import com.xinelu.manage.dto.screeningrecord.ScreeningRecordDTO;
import com.xinelu.manage.service.screeningrecord.IScreeningRecordService;
import com.xinelu.manage.vo.screeningrecord.ScreeningRecordVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description: 项目控制器——小程序
 * @author: haown
 * @create: 2023-03-07 14:23
 **/
@RestController
@RequestMapping("/nurseApplet/screening/project")
@Api(tags = "项目控制器-小程序")
public class AppletScreeningProjectController extends BaseController {
	@Resource
	private IScreeningRecordService screeningRecordService;

	@ApiOperation("筛查项目列表")
	@GetMapping("list")
	public R<List<ScreeningProject>> list(ScreeningRecordDTO query) {
		// 查询居民预约过的项目
		List<ScreeningProject> projectList = new ArrayList<>();

		query.setScreeningType("1");
		List<ScreeningRecordVo> recordList = screeningRecordService.list(query);
		if (CollectionUtils.isNotEmpty(recordList)) {
			// 按照项目进行分组
			Map<String, List<ScreeningRecordVo>> groupByProject = recordList.stream()
				.collect(Collectors.groupingBy(ScreeningRecordVo::getProjectId));
			for (String projectId : groupByProject.keySet()) {
				ScreeningProject project = new ScreeningProject();
				project.setProjectId(projectId);
				project.setProjectName(groupByProject.get(projectId).get(0).getProjectName());
				project.setHospitalId(groupByProject.get(projectId).get(0).getHospitalId());
				project.setHospitalName(groupByProject.get(projectId).get(0).getHospitalName());
				project.setCreateTime(groupByProject.get(projectId).get(0).getApplyStartTime());
				projectList.add(project);
			}
		}
		return R.ok(projectList);
	}
}
