package com.saye.hospitalgd.controller.historyLog;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.saye.hospitalgd.commons.date.DateDUtil;
import com.saye.hospitalgd.commons.string.StringDUtil;
import com.saye.hospitalgd.scheduler.jobMethod.HISGetDataMethodByHY;
import com.saye.hospitalgd.scheduler.jobMethod.HISGetDataMethodByJH;
import com.saye.hospitalgd.scheduler.jobMethod.HISGetDataMethodByWN;
import com.saye.hospitalgd.scheduler.jobMethod.HISGetDataMethodByZL;
import com.saye.hospitalgd.service.HisInterFaceConfigService;
import com.saye.hospitalgd.service.historyLog.HisBillLogService;
import com.saye.hospitalgd.service.system.ServiceParamsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @author thuang
 * @version 1.0
 * @description: his对账数据结果查询信息类
 * @date 2021/8/30 10:54
 */
@Controller
@RequestMapping("/hisBillLog")
@Api(tags = "his对账数据结果查询信息类")
public class HisBillLogController {

    @Autowired
    private HisBillLogService hisBillLogService;

    @Autowired
    private ServiceParamsService serviceParamsService;

    @Autowired
    private HisInterFaceConfigService hisInterFaceConfigService;

    @RequestMapping("/toHisBillLog")
    public String toHisBillLog(ModelMap modelMap){

        Calendar calendar=Calendar.getInstance();
        calendar.setTime(new Date());

        calendar.add(Calendar.DATE,-1);
        Date startDate = calendar.getTime();

        String startTime= DateDUtil.DateToStr(DateDUtil.yyyy_MM_dd,startDate);
        String endTime= DateDUtil.getCurrentDate(DateDUtil.yyyy_MM_dd);

        modelMap.addAttribute("startTime",startTime);
        modelMap.addAttribute("endTime",endTime);

        try {
            //厂商
            List<HashMap<Object,Object>> firmlist = hisInterFaceConfigService.findAllconfig();
            HashMap<String,String> firmMap=new HashMap<>();
            for (HashMap<Object, Object> hashMap : firmlist) {
                String id = StringDUtil.changeNullToEmpty(hashMap.get("ID"));
                String his_interface_name = StringDUtil.changeNullToEmpty(hashMap.get("HIS_INTERFACE_NAME"));
                firmMap.put(id,his_interface_name);
            }

            modelMap.addAttribute("firmlist",firmlist);
        }catch (Exception e){
            System.out.println("返回厂商信息出错，原因"+e.getMessage());
        }
        return "historyLog/hisBillLog";
    }

    /**
     * @description: 查询HIS调用的日志
     * @author thuang
     * @date 2021/8/30 14:30
     * @version 1.0
     */
    @RequestMapping("/findHisBillLogPageList")
    @ResponseBody
    @ApiOperation(value = "查询HIS调用的日志")
    public HashMap<Object,Object> findHisBillLogPageList(String business,String status,String startTime,String endTime,int page,int limit) {
        HashMap<Object,Object> responseMap=new HashMap<Object,Object>();

        try {

            HashMap<Object,Object> map=new HashMap<Object,Object>();
            map.put("is_ok",status);
            map.put("startTime",startTime);
            map.put("endTime",endTime);
            map.put("his_wsdl_id",business);

            PageHelper.startPage(page, limit);
            PageInfo<HashMap<Object, Object>> pageInfo = new PageInfo<HashMap<Object, Object>>(hisBillLogService.findHisBillLogPageList(map));

            responseMap.put("code", 0);
            responseMap.put("msg", "OK");
            responseMap.put("count", pageInfo.getTotal());
            responseMap.put("data", pageInfo.getList());
        } catch (Exception e) {
            e.printStackTrace();
            String msg=e.getMessage();
            responseMap.put("code", 1);
            responseMap.put("msg", "查询失败,原因："+msg);
        }

        return responseMap;
    }

    /**
     * @description: his重新获取数
     * @author thuang
     * @date 2021/10/9 15:30
     * @version 1.0
     */
    @RequestMapping("/restartGetBill")
    @ResponseBody
    @ApiOperation(value = "his重新获取数")
    public HashMap<Object,Object> restartGetBill(@RequestBody HashMap<Object,Object> map){
        HashMap<Object,Object> responseMap=new HashMap<>();
        String errCode="0";
        String errMsg="";

        try {
            //交易时间
            String trade_date = StringDUtil.changeNullToEmpty(map.get("trade_date"));

            //根据交易时间获取交易生成的定时任务
            HashMap<Object,Object> searchMap=new HashMap<>();
            searchMap.put("trade_date",trade_date);
            List<HashMap<Object, Object>> hisBillLogByParam = hisBillLogService.findHisBillLogByParam(searchMap);
            HashMap<Object, Object> hisBillLog = hisBillLogByParam.get(0);
            String quartz_id = StringDUtil.changeNullToEmpty(hisBillLog.get("QUARTZ_ID"));
            String quartz_name = StringDUtil.changeNullToEmpty(hisBillLog.get("QUARTZ_NAME"));

            HashMap<Object, Object> resultMap = null;

            List<HashMap<Object, Object>> serviceParams = serviceParamsService.findParamValByParamCode("his_wsdl_id");
            String his_wsdl_id = StringDUtil.changeNullToEmpty(serviceParams.get(0).get("PARAM_VAL"));

            if ("1".equals(his_wsdl_id)){
                resultMap = new HISGetDataMethodByZL().getDate(quartz_id,quartz_name,trade_date,his_wsdl_id);
            }else if ("2".equals(his_wsdl_id)){
                resultMap = new HISGetDataMethodByWN().getDate(quartz_id,quartz_name,trade_date,his_wsdl_id);
            }else if("3".equals(his_wsdl_id)){
                resultMap = new HISGetDataMethodByHY().getDate(quartz_id,quartz_name,trade_date,his_wsdl_id);
            }else if ("4".equals(his_wsdl_id)){
                resultMap = new HISGetDataMethodByJH().getDate(quartz_id,quartz_name,trade_date,his_wsdl_id);
            }

            errCode=StringDUtil.changeNullToEmpty(resultMap.get("errCode"));
            errMsg=StringDUtil.changeNullToEmpty(resultMap.get("errMsg"));
        }catch (Exception e){
            e.printStackTrace();
            errCode="999";
            errMsg="重新执行定时任务失败，原因："+e.getMessage();
        }
        responseMap.put("errCode",errCode);
        responseMap.put("errMsg",errMsg);

        return  responseMap;
    }
}
