package com.saye.hospitalgd.scheduler.jobMethod;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.saye.hospitalgd.commons.date.DateDUtil;
import com.saye.hospitalgd.commons.getBean.GetBeanUtil;
import com.saye.hospitalgd.commons.log.LogUtil;
import com.saye.hospitalgd.commons.string.StringDUtil;
import com.saye.hospitalgd.entity.HisbillsHistory;
import com.saye.hospitalgd.service.HisInterFaceConfigService;
import com.saye.hospitalgd.service.HisbillsHistoryService;
import com.saye.hospitalgd.service.historyLog.HisBillLogService;
import com.saye.hospitalgd.service.historyLog.impl.HisBillLogServiceImpl;
import com.saye.hospitalgd.service.impl.HisInterFaceConfigServiceImpl;
import com.saye.hospitalgd.service.impl.HisbillsHistoryServiceImpl;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

/**
 * @author thuang
 * @version 1.0
 * @description: His获取数据方法（中联）
 * @date 2021/9/30 13:41
 */
@Slf4j
public class HISGetDataMethodByZL {

    public HashMap<Object, Object> getDate(String id, String name, String trade_date, String his_wsdl_id) {

        HashMap<Object, Object> responseMap = new HashMap<>();
        String errCode = "0";
        String errMsg = "";

        HisBillLogService hisBillLogService = GetBeanUtil.getBean(HisBillLogServiceImpl.class);

//        HisDetailService hisDetailService = GetBeanUtil.getBean(HisDetailServiceImpl.class);
        HisbillsHistoryService hisbillsHistoryService = GetBeanUtil.getBean(HisbillsHistoryServiceImpl.class);

        HisInterFaceConfigService hisInterFaceConfigService = GetBeanUtil.getBean(HisInterFaceConfigServiceImpl.class);

        try {
            //查询需要用到的参数

            //his接口地址
            List<HashMap<Object, Object>> hisConfigList = hisInterFaceConfigService.findConfigById(his_wsdl_id);
            String his_wsdl = StringDUtil.changeNullToEmpty(hisConfigList.get(0).get("HIS_INTERFACE_WSDL"));
            //开始结束时间

            String startTime = "";
            String endTime = "";
            if ("".equals(trade_date)) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                calendar.add(Calendar.DATE, -1);
                Date time = calendar.getTime();

                startTime = DateDUtil.DateToStr(DateDUtil.yyyy_MM_dd, time) + " 00:00:00";
                endTime = DateDUtil.DateToStr(DateDUtil.yyyy_MM_dd, new Date()) + " 00:00:00";
                trade_date = DateDUtil.DateToStr(DateDUtil.yyyy_MM_dd, time);
            } else {
                startTime = trade_date + " 00:00:00";
                Date date = DateDUtil.strToDate(DateDUtil.yyyy_MM_dd, trade_date);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                calendar.add(Calendar.DATE, 1);
                endTime = DateDUtil.DateToStr(DateDUtil.yyyy_MM_dd, calendar.getTime()) + " 00:00:00";
            }
            log.info("开始时间：" + startTime);
            log.info("结束时间：" + endTime);

            List<HisbillsHistory> hisbillsHistories = new ArrayList<>();

            //组织request
            JSONObject requestBody = new JSONObject();
            requestBody.putOnce("hospitalNo", "45776913-5").putOnce("source", "ZZJ").putOnce("HisOperCode", "ZZJ001,ZZJ002,ZZJ003,ZZJ004,ZZJ005").putOnce("StartTime", startTime).putOnce("EndTime", endTime);

            String body = HttpUtil.createPost(his_wsdl).body(JSONUtil.toJsonStr(requestBody)).execute().body();

            JSONObject entries = JSONUtil.parseObj(body);

            if (entries.get("ret_code").equals("0")) {//响应成功
                Object o = entries.get("data");
                log.info("o is :" + o);
                JSONArray objects = JSONUtil.parseArray(o);
                log.info("objects is :" + objects);
                hisbillsHistories = JSONUtil.toList(objects, HisbillsHistory.class);
            } else {
                errCode = "999";
                errMsg = "his 接口调用失败";
            }
            if (CollUtil.isNotEmpty(hisbillsHistories)) {
                //存一份his原始的
                hisbillsHistoryService.saveHisbillsOriginalData(hisbillsHistories, trade_date);
                hisbillsHistoryService.saveHisbillsHistoryData(hisbillsHistories, trade_date);
            }

        } catch (Exception e) {
            e.printStackTrace();
            errCode = "999";
            errMsg = "执行定时任务【" + name + "】出错，原因：" + e.getMessage();
        }

        //存储记录
        try {
            HashMap<Object, Object> addMap = new HashMap<>();
            addMap.put("trade_date", trade_date);
            addMap.put("quartz_id", id);
            addMap.put("quartz_name", name);
            addMap.put("his_wsdl_id", his_wsdl_id);

            if ("0".equals(errCode)) {
                addMap.put("is_ok", "1");
            } else {
                addMap.put("is_ok", "0");
            }

            addMap.put("modify_time", DateDUtil.getCurrentDate(DateDUtil.yyyy_MM_dd_HH_mm_ss));
            hisBillLogService.insertHisbillGetinfo(addMap);

        } catch (Exception e) {
            e.printStackTrace();
            errCode = "999";
            errMsg = "保存His获取的记录失败，原因" + e.getMessage();
        }

        if (!"0".equals(errCode)) {
            try {
                //记录日志
                LogUtil.error(this.getClass(), errMsg);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        responseMap.put("errCode", errCode);
        responseMap.put("errMsg", errMsg);
        return responseMap;
    }
}
