package cn.hnthyy.thmz.controller;

import cn.hnthyy.thmz.comment.UserLoginToken;
import cn.hnthyy.thmz.service.his.MzPatientMiService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

@Slf4j
@RestController
public class MzPatientMiController {
    @Autowired
    private MzPatientMiService mzPatientMiService;

    /**
     * 根据病人就诊卡号查询病人信息
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getByIcCardNo", method = {RequestMethod.GET})
    public Map<String, Object> getByIcCardNo(@RequestParam("icCardNo") String icCardNo) {
        log.info("根据病人就诊卡号查询病人信息");
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if(StringUtils.isBlank(icCardNo)){
                resultMap.put("code", -1);
                resultMap.put("message", "就诊卡号不能为空");
                return resultMap;
            }
            resultMap.put("code", 0);
            resultMap.put("message", "查询病人信息成功");
            resultMap.put("data", mzPatientMiService.queryByIcCardNo(icCardNo));
            return resultMap;
        }catch (Exception e){
            resultMap.put("code", -1);
            resultMap.put("message", "系统出错，请联系管理员");
            log.error("系统异常，错误信息{}",e.getMessage());
            return resultMap;
        }
    }



    /**
     * 根据病人姓名查询病人信息
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getPatientMiByName", method = {RequestMethod.GET})
    public Map<String, Object> getPatientMiByName(@RequestParam("name") String name,@RequestParam("pageIndex") Integer pageIndex) {
        log.info("根据病人姓名查询病人信息");
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if(StringUtils.isBlank(name)){
                name=null;
            }
            if(pageIndex==null){
                pageIndex=1;
            }
            resultMap.put("code", 0);
            resultMap.put("message", "查询病人信息成功");
            resultMap.put("data", mzPatientMiService.queryByNameFuzzy(name,pageIndex,10));
            return resultMap;
        }catch (Exception e){
            resultMap.put("code", -1);
            resultMap.put("message", "系统出错，请联系管理员");
            log.error("系统异常，错误信息{}",e.getMessage());
            return resultMap;
        }
    }


    /**
     * 根据病人ID查询病人信息
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getByPatientId", method = {RequestMethod.GET})
    public Map<String, Object> getByPatientId(@RequestParam("patientId") String patientId) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if(StringUtils.isBlank(patientId)){
                resultMap.put("code", -1);
                resultMap.put("message", "病人id不能为空");
                return resultMap;
            }
            resultMap.put("code", 0);
            resultMap.put("message", "根据病人ID查询病人信息成功");
            resultMap.put("data", mzPatientMiService.queryByPatientId(patientId));
            return resultMap;
        }catch (Exception e){
            resultMap.put("code", -1);
            resultMap.put("message", "系统出错，请联系管理员");
            log.error("系统异常，错误信息{}",e.getMessage());
            return resultMap;
        }
    }
}
