package thyyxxk.webserver.controller.powersi;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.service.externalhttp.PowersiSrvc;
import thyyxxk.webserver.utils.FilterUtil;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.TokenUtil;

/**
 * @description: 创智中台接口
 * @author: DingJie
 * @create: 2021-05-27 10:10:57
 **/
@Slf4j
@RestController
@RequestMapping("/powersi")
public class PowerSiApiController {
    private final PowersiSrvc srvc;

    @Autowired
    public PowerSiApiController(PowersiSrvc srvc) {
        this.srvc = srvc;
    }

    @GetMapping("/getEmpiViewUrl")
    public ResultVo<String> getEmpiViewUrl(@RequestParam("id") String id) {
        log.info("【操作员：{}】，请求患者360视图：{}", TokenUtil.getInstance().getTokenUserId(), id);
        JSONObject param = new JSONObject();
        param.put("de000000009", "");
        param.put("de000000010", id);
        JSONObject result = srvc.queryEmpiPatientList(param);
        if (null == result) {
            return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
        }
        if (result.getIntValue("code") != 0) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString("msg"));
        }
        JSONArray data = result.getJSONArray("data");
        if (null == data || data.size() == 0) {
            return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "没有找到此患者的主索引。");
        }
        JSONObject index = data.getJSONObject(0);
        String empiViewUrl = String.format("http://172.16.32.183:9002/mmg_business_thyy/business/#/360pat/patientView?" +
                        "mainIndex=%s&patName=%s&sexName=%s&visitId=%s&tag=patientTime",
                index.getString("de000000007"), index.getString("de020103900"),
                FilterUtil.filterSex(index.getString("de020104000")), id);
        return ResultVoUtil.success(empiViewUrl);
    }

}
