package com.joju.datamanager.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.joju.datamanager.mapper.CallNumbersMapper;
import com.joju.datamanager.model.callnumber.CallNumbers;
import com.joju.datamanager.service.CallNumbersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description: 排队叫号实现类
 * @author: Mr.zs
 * @create: 2024-03-27 19:02
 **/
@Service
@Slf4j
public class CallNumbersServiceImpl extends ServiceImpl<CallNumbersMapper, CallNumbers> implements CallNumbersService {

    private static final String DATA_GET_URL = "http://168.168.0.37:5053";

    @Override
    public List<CallNumbers> getCallNumberByIdentity(String identity) {

        List<CallNumbers> callNumbers = null;

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("CardNo", identity);
        String result = HttpUtil.get(DATA_GET_URL + "/HospitalCallApi/GetPatientWaitinfo", paramMap);

        JSONObject resultJson = JSONUtil.parseObj(result);
        if (Convert.toStr(resultJson.get("State")).equals("True")) {//响应成功
            Object resData = resultJson.get("Result");
            JSONObject ResultJson = JSONUtil.parseObj(resData);
            Object ds = ResultJson.get("ds");

            JSONArray resDataJsonArray = JSONUtil.parseArray(ds);
            log.info("resDataJsonArray is :" + resDataJsonArray);


            callNumbers = JSONUtil.toList(resDataJsonArray, CallNumbers.class);

            callNumbers = callNumbers.stream().filter(c -> c.getADiagnosticStatus().equals("候诊")).collect(Collectors.toList());

        }
        return callNumbers;
    }


}
