package com.yihu.figure.controller.jw;

import com.yihu.figure.controller.BaseController;
import com.yihu.figure.service.jw.JwSmjkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.json.JSONArray;
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;

/**
 * Created by zhenglingfeng on 2016/10/17.
 */
@Controller
@RequestMapping(value = "/wlyy_service/record", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(description = "医生端-健康档案服务（旧版本接口）")
public class RecordController extends BaseController {

    @Autowired
    private JwSmjkService jwSmjkService;


    @RequestMapping(value = "/hospitalization", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("获取住院记录")
    public String getHospitalizationRecord(@ApiParam(name="strSSID",value="健康卡号",defaultValue = "A20140513123")
                                           @RequestParam(value="strSSID",required = true) String strSSID,
                                           @ApiParam(name="startNum",value="需要获取的起始行数",defaultValue = "0")
                                           @RequestParam(value="startNum",required = true) String startNum,
                                           @ApiParam(name="endNum",value="需要获取的结束的行数",defaultValue = "10")
                                           @RequestParam(value="endNum",required = true) String endNum) {
        try {
            String result = jwSmjkService.getHospitalizationRecord(strSSID, startNum, endNum);
            if (result.startsWith("error")) {
                return write(200, result, "data", new JSONArray().toString());
            }
            return write(200, "获取住院记录成功！", "data", result);
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "获取住院记录失败！");
        }
    }

    /**
     * 获取门/急诊数据
     *
     * @return
     */
    @ApiOperation("获取门/急诊数据")
    @RequestMapping(value = "/outpatient", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    @ResponseBody
    public String getOutpatientRecord(@ApiParam(name="strSSID",value="健康卡号",defaultValue = "B09036263193")
                                      @RequestParam(value="strSSID",required = true) String strSSID,
                                      @ApiParam(name="startNum",value="需要获取的起始行数",defaultValue = "0")
                                      @RequestParam(value="startNum",required = true) String startNum,
                                      @ApiParam(name="endNum",value="需要获取的结束的行数",defaultValue = "10")
                                      @RequestParam(value="endNum",required = true) String endNum) {
        try {
            String result = jwSmjkService.getOutpatientRecord(strSSID, startNum, endNum);
            if (result.startsWith("error")) {
                return write(200, result, "data", new JSONArray().toString());
            }
            return write(200, "获取门/急诊数据成功！", "data", result);
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "获取门/急诊数据失败！");
        }
    }

    @RequestMapping(value = "/healthData", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("获取健康档案信息")
    public String getHealthData(@ApiParam(name="strSSID",value="健康卡号",defaultValue = "A20140513123")
                                @RequestParam(value="strSSID",required = true) String strSSID,
                                @ApiParam(name="strEvent",value="事件ID",defaultValue = "")
                                @RequestParam(value="strEvent",required = true) String strEvent,
                                @ApiParam(name="strCatalog",value="档案类型",defaultValue = "")
                                @RequestParam(value="strCatalog",required = true) String strCatalog,
                                @ApiParam(name="strSerial",value="该类别顺序号，默认填1",defaultValue = "1")
                                @RequestParam(value="strSerial",required = true) String strSerial) {
        try {
            String result = jwSmjkService.getHealthData(strSSID, strEvent, strCatalog, strSerial);
            if (result.startsWith("error")) {
                return write(200, result, "data", new JSONArray().toString());
            }
            return write(200, "获取健康档案信息成功！", "data", result);
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "获取健康档案信息失败！");
        }
    }

    @RequestMapping(value = "/residentEventList", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("获取健康档案信息列表")
    public String getResidentEventListJson(@ApiParam(name="strSSID",value="健康卡号",defaultValue = "A20140513123")
                                @RequestParam(value="strSSID",required = true) String strSSID,
                                @ApiParam(name="type",value="类型",defaultValue = "")
                                @RequestParam(value="type",required = true) String type,
                                @ApiParam(name="page",value="第几页",defaultValue = "")
                                @RequestParam(value="page",required = true) String page,
                                @ApiParam(name="pageSize",value="",defaultValue = "10")
                                @RequestParam(value="pageSize",required = true) String pageSize) {
        try {
            String result = jwSmjkService.getResidentEventListJson(strSSID, type, page, pageSize);

            return write(200, "获取健康档案信息成功！", "data", result);
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, e.getMessage());
        }
    }


    @RequestMapping(value = "/drug", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("获取用药记录")
    public String getDrugsList(@ApiParam(name="strSSID",value="健康卡号",defaultValue = "A20140513123")
                               @RequestParam(value="strSSID",required = true) String strSSID,
                               @RequestParam(value="startNum",required = true) String startNum,
                               @ApiParam(name="endNum",value="需要获取的结束的行数",defaultValue = "10")
                               @RequestParam(value="endNum",required = true) String endNum) {
        try {
            String result = jwSmjkService.getDrugsList(strSSID, startNum, endNum);
            if (result.startsWith("error")) {
                return write(200, result, "data", new JSONArray().toString());
            }
            return write(200, "获取用药记录成功！", "data", result);
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "获取用药记录失败！");
        }
    }
}
