package com.yihu.wlyy.web.doctor.archives;

import com.yihu.wlyy.aop.ObserverRequired;
import com.yihu.wlyy.service.app.archives.PatientEventService;
import com.yihu.wlyy.service.app.archives.PatientRecordService;
import com.yihu.wlyy.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
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.ResponseBody;

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

/**
 * Created by hzp on 2016/11/24.
 */
@Controller
@RequestMapping(value = "/doctor/archives", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(description = "医生端-健康档案服务接口")
public class DoctorArchivesController extends BaseController {

    @Autowired
    private PatientRecordService patientRecordService;

    @Autowired
    private PatientEventService patientEventService;


    @ApiOperation("获取门诊记录/住院记录(基卫+APP)")
    @RequestMapping(value = "/event", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    @ResponseBody
    public String getAllEvent(@ApiParam(name="patient",value="患者代码",defaultValue = "")
                              @RequestParam(value="patient",required = false) String patient,
                              @ApiParam(name="type",value="类型",defaultValue = "")
                              @RequestParam(value="type",required = false) String type,
                              @ApiParam(name="page",value="第几页",defaultValue = "1")
                              @RequestParam(value="page",required = true) String page,
                              @ApiParam(name="pageSize",value="每页几行",defaultValue = "10")
                              @RequestParam(value="pageSize",required = true) String pageSize,
                              @ApiParam(name="lastTime",value="最后一条时间",defaultValue = "2010-10-10 00:00:00")
                              @RequestParam(value="lastTime",required = false) String lastTime) {
        try {
            List<Map<String,String>> result = patientRecordService.getAllEvent(patient,type,page,pageSize,lastTime);

            return write(200, "获取就诊记录成功！", "data", result);
        } catch (Exception e) {
            if(e.getMessage().indexOf("卡号尚未在医疗机构注册过")>0)
            {
                return error(-1,"对不起，您的社保卡有变更，请改天再试");
            }
            else if(e.getMessage().startsWith("error"))
            {
                return error(-1,e.getMessage());
            }
            else{
                return error(-1,"获取门/急诊数据失败！");
            }
        }
    }

    @RequestMapping(value = "/event/healthData", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("获取健康档案详情（基卫）")
    public String getHealthData(@ApiParam(name="patient",value="患者代码",defaultValue = "")
                                @RequestParam(value="patient",required = false) String patient,
                                @ApiParam(name="event",value="事件ID",defaultValue = "")
                                @RequestParam(value="event",required = true) String event,
                                @ApiParam(name="catalog",value="档案类型",defaultValue = "")
                                @RequestParam(value="catalog",required = true) String catalog,
                                @ApiParam(name="serial",value="该类别顺序号，默认填1",defaultValue = "1")
                                @RequestParam(value="serial",required = true) String serial) {
        try {
            String result = patientRecordService.getHealthData(patient, event, catalog, serial);

            return write(200, "获取健康档案详情成功！", "data", result);
        } catch (Exception e) {
            if(e.getMessage().indexOf("卡号尚未在医疗机构注册过")>0)
            {
                return error(-1,"对不起，您的社保卡有变更，请改天再试");
            }
            else if(e.getMessage().startsWith("error"))
            {
                return error(-1,e.getMessage());
            }
            else{
                return error(-1,"获取健康档案详情失败！");
            }
        }
    }

    @RequestMapping(value = "/event/catalog", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("通过事件号获取档案类型列表（基卫）")
    public String getEventCatalog(@ApiParam(name="patient",value="患者代码",defaultValue = "")
                                  @RequestParam(value="patient",required = false) String patient,
                                  @ApiParam(name="event",value="事件ID",defaultValue = "")
                                  @RequestParam(value="event",required = true) String event) {
        try {
            List<Map<String,String>> result = patientRecordService.getEventCatalog(patient,event);

            return write(200, "通过事件号获取档案类型列表成功！", "data", result);
        } catch (Exception e) {
            if(e.getMessage().indexOf("卡号尚未在医疗机构注册过")>0)
            {
                return error(-1,"对不起，您的社保卡有变更，请改天再试");
            }
            else if(e.getMessage().startsWith("error"))
            {
                return error(-1,e.getMessage());
            }
            else{
                return error(-1,"获取档案类型列表失败！");
            }
        }
    }


    @RequestMapping(value = "/event/drug", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("获取用药记录（基卫）")
    public String getDrugsList(@ApiParam(name="patient",value="患者代码",defaultValue = "")
                               @RequestParam(value="patient",required = false) String patient,
                               @ApiParam(name="page",value="第几页",defaultValue = "1")
                               @RequestParam(value="page",required = true) String page,
                               @ApiParam(name="pageSize",value="每页几行",defaultValue = "10")
                               @RequestParam(value="pageSize",required = true) String pageSize) {
        try {
            String result = patientRecordService.getDrugsListPage(patient, page, pageSize);

            return write(200, "获取用药记录成功！", "data", result);
        } catch (Exception e) {
            if(e.getMessage().indexOf("卡号尚未在医疗机构注册过")>0)
            {
                return error(-1,"对不起，您的社保卡有变更，请改天再试");
            }
            else if(e.getMessage().startsWith("error"))
            {
                return error(-1,e.getMessage());
            }
            else{
                return error(-1,"获取用药记录失败！");
            }
        }
    }

    @ApiOperation("获取检查检验报告(基卫+APP)")
    @RequestMapping(value = "/event/report", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    @ResponseBody
    public String getReportList(@ApiParam(name="patient",value="患者代码",defaultValue = "")
                                @RequestParam(value="patient",required = false) String patient,
                                @ApiParam(name="page",value="第几页",defaultValue = "1")
                                @RequestParam(value="page",required = true) String page,
                                @ApiParam(name="pageSize",value="每页几行",defaultValue = "10")
                                @RequestParam(value="pageSize",required = true) String pageSize,
                                @ApiParam(name="lastTime",value="最后一条时间",defaultValue = "2010-10-10 00:00:00")
                                    @RequestParam(value="lastTime",required = false) String lastTime)
    {
        try {
            List<Map<String,String>> result = patientRecordService.getExamAndLabReport(patient, page, pageSize,lastTime);

            return write(200, "获取检查检验报告成功！", "data", result);
        } catch (Exception e) {
            if(e.getMessage().indexOf("卡号尚未在医疗机构注册过")>0)
            {
                return error(-1,"对不起，您的社保卡有变更，请改天再试");
            }
            else if(e.getMessage().startsWith("error"))
            {
                return error(-1,e.getMessage());
            }
            else{
                return error(-1,"获取检查检验报告失败！");
            }
        }
    }

    /******************************************* 就诊事件管理 **********************************************************/
    @RequestMapping(value = "/event/detail", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("获取就诊事件详情")
    public String getEventDetail(@ApiParam(name="event",value="事件ID",defaultValue = "")
                                 @RequestParam(value="event",required = true) String event)
    {
        try {
            JSONObject result = patientEventService.getEventDetail(event);

            return write(200, "获取就诊事件详情成功！", "data", result);
        } catch (Exception e) {

            return invalidUserException(e, -1, "获取就诊事件详情失败！");
        }
    }

    @RequestMapping(value = "/event/save", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("保存就诊事件详情")
    @ObserverRequired
    public String saveEventDetail(@ApiParam(name="data",value="就诊事件json数据",defaultValue = "")
                                 @RequestParam(value="data",required = true) String data)
    {
        try {
            patientEventService.saveEventDetail(data, getUID());

            return write(200, "保存就诊事件成功！");
        } catch (Exception e) {
            return invalidUserException(e, -1, "保存就诊事件失败！");
        }
    }
}
