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.IssueService;
import com.saye.hrs.service.SymptomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
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;

/**
 * @description: 导诊问题管理
 * @author: Mr.zs
 * @create: 2024-04-09 13:02
 **/
@Controller
@RequestMapping("/issue")
public class IssueController {

    @Autowired
    IssueService issueService;

    @Autowired
    SymptomService symptomService;

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


    @RequestMapping("/findAllQuestions")
    @ResponseBody
    public TemplatePage findAllQuestions(Integer page, Integer limit, String questions, String symptomName, String bodyArea, String gender, String adultOrChild) {
        System.out.println("questions is :" + questions);
        HashMap<Object, Object> searchMap = new HashMap<>();
        searchMap.put("questions", questions);
        searchMap.put("symptom", symptomName);
        searchMap.put("bodyArea", bodyArea);
        searchMap.put("gender", gender);
        searchMap.put("adultOrChild", adultOrChild);
        PageHelper.startPage(page, limit);
        PageInfo<HashMap<Object, Object>> appsPageInfo = new PageInfo<>(issueService.findAllQuestions(searchMap));
        return PageUtil.loadJsonPage(appsPageInfo);
    }

    @RequestMapping("/getSymptomByAreaType")
    @ResponseBody
    public HashMap<Object, Object> getSymptomByAreaType(ModelMap modelMap, String bodyAreaType) {
        HashMap<Object, Object> resultMap = new HashMap<>();
        String errCode = "0";
        String errMsg = "";
        HashMap<Object, Object> searchMap = new HashMap<>();
        searchMap.put("bodyAreaType", bodyAreaType);
        try {
            List<HashMap<Object, Object>> result = symptomService.getSymptomByAreaType(searchMap);
            resultMap.put("data", result);
        } catch (Exception e) {
            e.printStackTrace();
            errCode = "999";
            errMsg = "查询症状失败，原因：" + e.getMessage();
        }
        resultMap.put("errCode", errCode);
        resultMap.put("errMsg", errMsg);
        return resultMap;
    }

    @RequestMapping("/addQuestions")
    @ResponseBody
    public HashMap<Object, Object> addQuestions(@RequestBody HashMap<Object, Object> map) {
        String errCode = "0";
        String errMsg = "";
        try {
            issueService.addQuestions(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("/delQuestions")
    @ResponseBody
    public HashMap<Object, Object> delQuestions(@RequestBody HashMap<Object, Object> map) {
        String errCode = "0";
        String errMsg = "";
        try {
            issueService.delQuestions(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("/modifyQuestions")
    @ResponseBody
    public HashMap<Object, Object> modifyQuestions(@RequestBody HashMap<Object, Object> map) {
        String errCode = "0";
        String errMsg = "";
        try {
            issueService.modifyQuestions(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;
    }
}
