package com.saye.hrs.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.saye.hrs.commons.log.ExceptionDUtil;
import com.saye.hrs.commons.log.LogUtil;
import com.saye.hrs.commons.page.PageUtil;
import com.saye.hrs.commons.page.TemplatePage;
import com.saye.hrs.model.BodyDivision;
import com.saye.hrs.service.SymptomService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
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.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 智能导诊症状管理
 * @author: Mr.zs
 * @create: 2024-04-09 12:49
 **/
@Controller
@RequestMapping("/symptom")
@Slf4j
public class SymptomController {


    @Autowired
    SymptomService symptomService;

    @RequestMapping("/toSymptomManager")
    public String toSymptomManager(ModelMap modelMap) {
//        List<BodyDivision> bodyDivisions = symptomService.findAllBodyArea();
//        modelMap.put("bodyAreaList", bodyDivisions);
        return "guidance/symptomManager";
    }

    @RequestMapping("/findAllSymptom")
    @ResponseBody
    public TemplatePage findAllSymptom(Integer page, Integer limit, String symptom, String bodyArea, String gender, String adultOrChild) {
        Map<String, String> searchMap = new HashMap<>();
        searchMap.put("symptom", symptom);
        searchMap.put("bodyArea", bodyArea);
        searchMap.put("gender", gender);
        searchMap.put("adultOrChild", adultOrChild);
        PageHelper.startPage(page, limit);
        PageInfo<HashMap<Object, Object>> appsPageInfo = new PageInfo<>(symptomService.findAllSymptom(searchMap));
        return PageUtil.loadJsonPage(appsPageInfo);
    }

    @PostMapping("/addSymptom")
    @ResponseBody
    public HashMap<Object, Object> addSymptom(@RequestBody HashMap<Object, Object> map) {
        String errCode = "0";
        String errMsg = "";

        String bodyArea = "";

        List<BodyDivision> bodyDivisions = symptomService.findAllBodyArea();
        Integer i = symptomService.findMaxId();
        Integer newId = i + 1;

        map.put("childId", newId + "_" + map.get("bodyAreaType").toString());
        for (BodyDivision bodyDivision : bodyDivisions) {
            if (bodyDivision.getBODY_AREA_TYPE().equals(map.get("bodyAreaType"))) {
                bodyArea = bodyDivision.getBODY_AREA();
            }
        }
        map.put("bodyArea", bodyArea);
        try {
            symptomService.addSymptom(map);
        } catch (Exception e) {
            errCode = "999";
            errMsg = "新增症状失败";
            LogUtil.error(this.getClass(), "新增症状失败，原因:" + ExceptionDUtil.getDetailExceptionMsg(e));
        }

        HashMap<Object, Object> resultMap = new HashMap<Object, Object>();
        resultMap.put("errCode", errCode);
        resultMap.put("errMsg", errMsg);
        return resultMap;
    }
//    @RequestMapping("/findAllBodyArea")
//    @ResponseBody
//    public  List<HashMap<Object,Object>> findAllBodyArea (){
//
//    }

    @RequestMapping("/modifySymptom")
    @ResponseBody
    public HashMap<Object, Object> modifySymptom(@RequestBody HashMap<Object, Object> map) {
        String errCode = "0";
        String errMsg = "";

        try {
            symptomService.modifySymptom(map);
        } catch (Exception e) {
            errCode = "999";
            errMsg = "修改症状失败";
            LogUtil.error(this.getClass(), "修改症状失败，原因:" + ExceptionDUtil.getDetailExceptionMsg(e));
        }
        HashMap<Object, Object> resultMap = new HashMap<Object, Object>();
        resultMap.put("errCode", errCode);
        resultMap.put("errMsg", errMsg);
        return resultMap;
    }

    @RequestMapping("/delSymptom")
    @ResponseBody
    public HashMap<Object, Object> delSymptom(@RequestBody HashMap<Object, Object> map) {
        String errCode = "0";
        String errMsg = "";

        try {
            symptomService.delSymptom(map);
        } catch (Exception e) {
            errCode = "999";
            errMsg = "修改症状失败";
            LogUtil.error(this.getClass(), "修改症状失败，原因:" + ExceptionDUtil.getDetailExceptionMsg(e));
        }
        HashMap<Object, Object> resultMap = new HashMap<Object, Object>();
        resultMap.put("errCode", errCode);
        resultMap.put("errMsg", errMsg);
        return resultMap;
    }

    @RequestMapping("/getAreaTypeBySexAdult")
    @ResponseBody
    public HashMap<Object, Object> getAreaTypeBySexAdult(@RequestBody HashMap<String, String> searchMap) {
        HashMap<Object, Object> resultMap = new HashMap<>();
        String errCode = "0";
        String errMsg = "";
        log.info("searchMap is :" + searchMap);
//        Map<String, String> searchMap = new HashMap<>();
//        searchMap.put("sex", sex);
//        searchMap.put("isAdult", isAdult);
        try {
            List<BodyDivision> bodyDivisions = symptomService.findAreaTypeBySexAdult(searchMap);
            resultMap.put("data", bodyDivisions);
        } catch (Exception e) {
            e.printStackTrace();
            errCode = "999";
            errMsg = "查询身体区域失败，原因：" + e.getMessage();
        }
        resultMap.put("errCode", errCode);
        resultMap.put("errMsg", errMsg);
        return resultMap;

    }
}
