package com.joju.datamanager.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.joju.datamanager.common.annotation.EleganceLog;
import com.joju.datamanager.common.result.Result;
import com.joju.datamanager.common.result.ResultUtil;
import com.joju.datamanager.model.guidance.IntelligentGuidanceCategory;
import com.joju.datamanager.model.guidance.IntelligentGuidanceQuestions;
import com.joju.datamanager.service.IntelligentGuidanceCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @description: 智能导诊
 * @author: Mr.zs
 * @create: 2024-03-21 14:00
 **/
@RestController
@RequestMapping("/IntelligentGuidance")
public class IntelligentGuidanceController {

    @Autowired
    IntelligentGuidanceCategoryService intelligentGuidanceCategoryService;


    @GetMapping("/testGuidance")
    public Result testInterface() {
        return ResultUtil.successData("ok");
    }

    @GetMapping("/getSymptom")
    @EleganceLog(description = "导诊根据身体部位获取症状")
    public Result getSymptomByBodyAreaType(String bodyAreaType) {

        List<IntelligentGuidanceCategory> intelligentGuidanceCategoryList = intelligentGuidanceCategoryService.getSymptomByBodyAreaType(bodyAreaType);


        Map<String, List<IntelligentGuidanceCategory>> listMap = intelligentGuidanceCategoryList.stream().collect(Collectors.groupingBy(IntelligentGuidanceCategory::getBodyArea));
        String bodyArea = listMap.keySet().iterator().next();
        JSONObject result = JSONUtil.createObj().put("bodyArea", bodyArea).put("categoryList", intelligentGuidanceCategoryList);
        JSONUtil.toJsonStr(result);
        return ResultUtil.successData(JSONUtil.toJsonStr(result));
    }


    @GetMapping("/getQuestions")
    @EleganceLog(description = "导诊获取症状对应的子问题")
    public Result getQuestionsByChildId(String childId) {

        List<IntelligentGuidanceQuestions> intelligentGuidanceQuestionsList = intelligentGuidanceCategoryService.getQuestionsByChildId(childId);

        List<IntelligentGuidanceQuestions> intelligentGuidanceQuestions = intelligentGuidanceQuestionsList.stream().sorted(Comparator.comparing(IntelligentGuidanceQuestions::getSortId)).collect(Collectors.toList());
        return ResultUtil.successData(intelligentGuidanceQuestions);

    }

    @GetMapping("/getAllBodyArea")
    @EleganceLog(description = "导诊获取身体区域划分")
    public Result getAllBodyArea() {

        List<IntelligentGuidanceCategory> list = intelligentGuidanceCategoryService.getAllBodyArea();
        return ResultUtil.successData(list);

    }

}
