<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
  <title>医德帮-医保上传平台</title>
  <link th:href="@{css/bootstrap.min.css}" rel="stylesheet" media="all">
  <link th:href="@{css/materialdesignicons.min.css}" rel="stylesheet" media="all">
  <link th:href="@{css/style.min.css}" rel="stylesheet" media="all">
</head>

<body>
<div class="lyear-layout-web" id="batchUpload">
  <input type="hidden" id="userId"/>
  <div class="lyear-layout-container">
    <!--左侧导航-->
    <aside class="lyear-layout-sidebar">

      <!-- logo -->
      <div class="lyear-layout-sidebar-scroll">

        <nav class="sidebar-main">
          <ul class="nav nav-drawer">
            <li class="nav-item active"> <a href="javascript:void(0)" @click="jumpPage('index')"><i class="mdi mdi-home"></i>医德帮-医保上传平台</a> </li>
            <li class="nav-item nav-item-has-subnav">
              <a href="javascript:void(0)"><i class="mdi mdi-palette"></i>数据查询</a>
              <ul class="nav nav-subnav">
                <li> <a href="javascript:void(0)" @click="jumpPage('uploadInfo')">报文查询</a> </li>
                <li> <a href="javascript:void(0)" @click="jumpPage('uploadHistory')">历史数据查询</a> </li>
              </ul>
            </li>
            <li class="nav-item nav-item-has-subnav active open">
              <a href="javascript:void(0)"><i class="mdi mdi-format-align-justify"></i>数据上传</a>
              <ul class="nav nav-subnav">
                <li class="active"> <a href="javascript:void(0)" @click="jumpPage('batchUpload')">批量上传</a> </li>
                <li> <a href="javascript:void(0)" @click="jumpPage('export')">数据导出</a> </li>
              </ul>
            </li>
            <li> <a href="javascript:void(0)" @click="jumpPage('uploadInfo')"><i class="mdi mdi-file-outline"></i>定时设置</a> </li>
          </ul>
        </nav>
      </div>

    </aside>
    <!--End 左侧导航-->

    <!--头部信息-->
    <header class="lyear-layout-header">

      <nav class="navbar navbar-default">
        <div class="topbar">

          <div class="topbar-left">
            <div class="lyear-aside-toggler">
              <span class="lyear-toggler-bar"></span>
              <span class="lyear-toggler-bar"></span>
              <span class="lyear-toggler-bar"></span>
            </div>
            <span class="navbar-page-title"></span>
          </div>

          <ul class="topbar-right">
            <li class="dropdown dropdown-profile">
              <a href="javascript:void(0)" data-toggle="dropdown">
                <span th:text="${name}"><span class="caret"></span></span>
              </a>
              <ul class="dropdown-menu dropdown-menu-right">
                <li> <a href="lyear_pages_edit_pwd.html"><i class="mdi mdi-lock-outline"></i> 修改密码</a> </li>
                <li class="divider"></li>
                <li> <a href="lyear_pages_login.html"><i class="mdi mdi-logout-letiant"></i> 退出登录</a> </li>
              </ul>
            </li>
            <!--切换主题配色-->
            <li class="dropdown dropdown-skin">
              <span data-toggle="dropdown" class="icon-palette"><i class="mdi mdi-palette"></i></span>
              <ul class="dropdown-menu dropdown-menu-right" data-stopPropagation="true">
                <li class="drop-title"><p>主题</p></li>
                <li class="drop-skin-li clearfix">
                  <span class="inverse">
                    <input type="radio" name="site_theme" value="default" id="site_theme_1" checked>
                    <label for="site_theme_1"></label>
                  </span>
                  <span>
                    <input type="radio" name="site_theme" value="dark" id="site_theme_2">
                    <label for="site_theme_2"></label>
                  </span>
                  <span>
                    <input type="radio" name="site_theme" value="translucent" id="site_theme_3">
                    <label for="site_theme_3"></label>
                  </span>
                </li>
                <li class="drop-title"><p>LOGO</p></li>
                <li class="drop-skin-li clearfix">
                  <span class="inverse">
                    <input type="radio" name="logo_bg" value="default" id="logo_bg_1" checked>
                    <label for="logo_bg_1"></label>
                  </span>
                  <span>
                    <input type="radio" name="logo_bg" value="color_2" id="logo_bg_2">
                    <label for="logo_bg_2"></label>
                  </span>
                  <span>
                    <input type="radio" name="logo_bg" value="color_3" id="logo_bg_3">
                    <label for="logo_bg_3"></label>
                  </span>
                  <span>
                    <input type="radio" name="logo_bg" value="color_4" id="logo_bg_4">
                    <label for="logo_bg_4"></label>
                  </span>
                  <span>
                    <input type="radio" name="logo_bg" value="color_5" id="logo_bg_5">
                    <label for="logo_bg_5"></label>
                  </span>
                  <span>
                    <input type="radio" name="logo_bg" value="color_6" id="logo_bg_6">
                    <label for="logo_bg_6"></label>
                  </span>
                  <span>
                    <input type="radio" name="logo_bg" value="color_7" id="logo_bg_7">
                    <label for="logo_bg_7"></label>
                  </span>
                  <span>
                    <input type="radio" name="logo_bg" value="color_8" id="logo_bg_8">
                    <label for="logo_bg_8"></label>
                  </span>
                </li>
                <li class="drop-title"><p>头部</p></li>
                <li class="drop-skin-li clearfix">
                  <span class="inverse">
                    <input type="radio" name="header_bg" value="default" id="header_bg_1" checked>
                    <label for="header_bg_1"></label>
                  </span>
                  <span>
                    <input type="radio" name="header_bg" value="color_2" id="header_bg_2">
                    <label for="header_bg_2"></label>
                  </span>
                  <span>
                    <input type="radio" name="header_bg" value="color_3" id="header_bg_3">
                    <label for="header_bg_3"></label>
                  </span>
                  <span>
                    <input type="radio" name="header_bg" value="color_4" id="header_bg_4">
                    <label for="header_bg_4"></label>
                  </span>
                  <span>
                    <input type="radio" name="header_bg" value="color_5" id="header_bg_5">
                    <label for="header_bg_5"></label>
                  </span>
                  <span>
                    <input type="radio" name="header_bg" value="color_6" id="header_bg_6">
                    <label for="header_bg_6"></label>
                  </span>
                  <span>
                    <input type="radio" name="header_bg" value="color_7" id="header_bg_7">
                    <label for="header_bg_7"></label>
                  </span>
                  <span>
                    <input type="radio" name="header_bg" value="color_8" id="header_bg_8">
                    <label for="header_bg_8"></label>
                  </span>
                </li>
                <li class="drop-title"><p>侧边栏</p></li>
                <li class="drop-skin-li clearfix">
                  <span class="inverse">
                    <input type="radio" name="sidebar_bg" value="default" id="sidebar_bg_1" checked>
                    <label for="sidebar_bg_1"></label>
                  </span>
                  <span>
                    <input type="radio" name="sidebar_bg" value="color_2" id="sidebar_bg_2">
                    <label for="sidebar_bg_2"></label>
                  </span>
                  <span>
                    <input type="radio" name="sidebar_bg" value="color_3" id="sidebar_bg_3">
                    <label for="sidebar_bg_3"></label>
                  </span>
                  <span>
                    <input type="radio" name="sidebar_bg" value="color_4" id="sidebar_bg_4">
                    <label for="sidebar_bg_4"></label>
                  </span>
                  <span>
                    <input type="radio" name="sidebar_bg" value="color_5" id="sidebar_bg_5">
                    <label for="sidebar_bg_5"></label>
                  </span>
                  <span>
                    <input type="radio" name="sidebar_bg" value="color_6" id="sidebar_bg_6">
                    <label for="sidebar_bg_6"></label>
                  </span>
                  <span>
                    <input type="radio" name="sidebar_bg" value="color_7" id="sidebar_bg_7">
                    <label for="sidebar_bg_7"></label>
                  </span>
                  <span>
                    <input type="radio" name="sidebar_bg" value="color_8" id="sidebar_bg_8">
                    <label for="sidebar_bg_8"></label>
                  </span>
                </li>
              </ul>
            </li>
            <!--切换主题配色-->
          </ul>

        </div>
      </nav>

    </header>
    <!--End 头部信息-->

    <!--页面主要内容-->
    <main class="lyear-layout-content">
      <div class="container-fluid">
        <div class="row">
          <div class="col-lg-12">
            <div class="card">
              <div class="card-header">
                <row>
                  <h4 class="col-sm-1">异常记录查询</h4>
                  <div style="float: left">
                    <input type="text" id="test1" placeholder="请选择日期">
                  </div>
                  <div class="col-sm-2">
                    <button class="btn btn-primary btn-sm" @click="searchExceptionLogs" :disabled="!startDate">查询</button>
                    <button class="btn btn-primary btn-sm" @click="searchExceptionLogs" :disabled="!pageInfo.list.length > 0">上传</button>
                  </div>
                </row>
              </div>
              <div class="card-body">
                <div class="form-horizontal" role="form">
                  <div class="row">
                    <div class="col-lg-12">
                      <div class="table-responsive">
                        <table class="table table-hover">
                          <thead>
                          <tr  class="text-center">
                            <th class="text-center col-sm-1">诊所编号</th>
                            <th class="text-center col-sm-1">交易码</th>
                            <th class="text-center col-sm-1">流水号</th>
                            <th class="text-center col-sm-4">报错信息</th>
                            <th class="text-center col-sm-1">重试次数</th>
                            <th class="text-center col-sm-1">状态</th>
                            <th class="text-center col-sm-1">统计日期</th>
                            <th class="text-center col-sm-1">操作</th>
                          </tr>
                          </thead>
                          <tbody  class="text-center">
                          <tr v-for="item in pageInfo.list">
                            <td>{{item.clinicNo}}</td>
                            <td>{{item.tradeCode}}</td>
                            <td>{{item.serialNumber}}</td>
                            <td>{{item.errorMsg}}</td>
                            <td>{{item.retryCount}}</td>
                            <td v-if="item.status === 0"><span class="label label-warning">失败</span></td>
                            <td v-else-if="item.status === 1"><span class="label label-warning">成功</span></td>
                            <td v-else><span class="label label-warning">异常</span></td>
                            <td>{{item.createDate}}</td>
                            <td>上传</td>
                          </tr>
                          </tbody>
                        </table>
                        <nav v-if="pageInfo.list.length > 0">
                          <ul class="pagination">
                            <li :style="{pointerEvents: (pageInfo.isFirstPage?'none':'')}" >
                              <a href="javascript:void(0)" @click="goPage(pageInfo.prePage)">
                                <span><i class="mdi mdi-chevron-left"></i></span>
                              </a>
                            </li>
                            <li :class="pageInfo.pageNum == item ? 'active':''" v-for="item in pageInfo.pages"><a href="javascript:void(0)" @click="goPage(item)" style="cursor:hand">{{item}}</a></li>
                            <li :style="{pointerEvents: (pageInfo.isLastPage?'none':'')}" >
                              <a href="javascript:void(0)" @click="goPage(pageInfo.nextPage)">
                                <span><i class="mdi mdi-chevron-right"></i></span>
                              </a>
                            </li>
                          </ul>
                        </nav>
                      </div>
                    </div>

                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>

        <div class="row">
          <div class="col-lg-12">
            <div class="card">
              <div class="card-header">
                <h4>快捷上传</h4>
              </div>
              <div class="card-body">
                <div class="form-horizontal" role="form">
                  <fieldset>
                    <div class="form-group">
                      <label class="col-sm-2 control-label">上传当日数据</label>
                      <div class="col-sm-2">
                        <button type="button" class="btn btn-primary" @click="uploadToday">上传</button>
                      </div>
                    </div>
                    <div class="form-group">
                      <label class="col-sm-2 control-label">生成昨日订单数据</label>
                      <div class="col-sm-6">
                        <button type="button" class="btn btn-primary" @click="generateYesterdayDate">上传</button>
                      </div>
                    </div>
                    <div class="form-group">
                      <label class="col-sm-2 control-label">上传昨日订单数据</label>
                      <div class="col-sm-2">
                        <button type="button" class="btn btn-primary" @click="uploadYesterdayOrder">上传</button>
                      </div>
<!--                      <div class="col-sm-6">-->
<!--                        <div class="progress-bar" role="progressbar" style="width: 25%;" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100">-->
<!--                          25%-->
<!--                        </div>-->
<!--                      </div>-->
                    </div>
                    <div class="form-group">
                      <label class="col-sm-2 control-label">上传昨日订单状态</label>
                      <div class="col-sm-6">
                        <button type="button" class="btn btn-primary" @click="uploadYesterdayDocStatus">上传</button>
                      </div>
                    </div>
                  </fieldset>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </main>
  </div>
</div>

<script type="text/javascript" src="js/vue.js"></script>
<script type="text/javascript" src="js/laydate/laydate.js"></script>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/perfect-scrollbar.min.js"></script>
<script type="text/javascript" src="js/main.min.js"></script>
<script type="text/javascript" src="js/layer/layer.js"></script>
<script type="text/javascript" src="js/socket.js"></script>
<script>
  const app = new Vue({
    el: '#batchUpload',
    data: {
      pageInfo: {
        pageNum: 1,
        pageSize: 10,
        list: []
      },
      userId: null,
      startDate: "",
      endDate: "",
      uploadMsg: "开始上传..",
      generatePro: 0,
    },
    mounted() {
      this.userId = this.generateUserId(12)
      this.openSocket()
      this.initLaydate()
    },
    methods: {
      generateUserId(n) {
        let res = "";
        for(let i = 0; i < n ; i ++) {
          let id = Math.ceil(Math.random()*35);
          res += chars[id];
        }
        return res
      },
      openSocket() {
        let socket
        let that = this
        if(typeof(WebSocket) == "undefined") {
          console.log("您的浏览器不支持WebSocket");
        }else{
          //实现化WebSocket对象，指定要连接的服务器地址与端口  建立连接
          // let userId = document.getElementById('userId').value;
          // let socketUrl="ws://127.0.0.1:22599/webSocket/"+userId;
          let socketUrl="ws://localhost:18080/webSocket/" + this.userId;
          console.log(socketUrl);
          if(socket!=null){
            socket.close();
            socket=null;
          }
          socket = new WebSocket(socketUrl);
          //打开事件
          socket.onopen = function() {
            console.log("websocket已打开");
            //socket.send("这是来自客户端的消息" + location.href + new Date());
          };
          //获得消息事件
          socket.onmessage = function(msg) {
            this.generatePro = msg.data
            $("#progress-order").css("width", msg.data)

          };
          //关闭事件
          socket.onclose = function() {
            console.log("websocket已关闭");
          };
          //发生了错误事件
          socket.onerror = function() {
            console.log("websocket发生了错误");
          }
        }
      },
      jumpPage(path) {
        window.location.href = path
      },
      goPage(page) {
        this.pageInfo.pageNum = page
        this.searchExceptionLogs()
      },
      initLaydate() {
        let that = this
        laydate.render({
          elem: '#test1',
          type: 'date',
          trigger: 'click',
          min: -31,
          max: 31,
          range: true,
          done: function(value, date){
            let dateArr = value.split(" - ")
            that.startDate = dateArr[0]
            that.endDate = dateArr[1]
          }
        })
      },
      getTotalPageNum (totalRecord, pageSize = this.pageSize) {
        return (totalRecord + pageSize - 1) / pageSize;
      },
      dateFormat(fmt, date) {
        let ret;
        const opt = {
          "Y+": date.getFullYear().toString(),        // 年
          "m+": (date.getMonth() + 1).toString(),     // 月
          "d+": date.getDate().toString(),            // 日
          "H+": date.getHours().toString(),           // 时
          "M+": date.getMinutes().toString(),         // 分
          "S+": date.getSeconds().toString()          // 秒
          // 有其他格式化字符需求可以继续添加，必须转化成字符串
        };
        for (let k in opt) {
          ret = new RegExp("(" + k + ")").exec(fmt);
          if (ret) {
            fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
          };
        };
        return fmt;
      },
      searchExceptionLogs() {
        let that = this
        let load = layer.msg('加载中', {
          icon: 16,
          shade: 0.01
        });
        $.ajax({
          type : "GET",
          url : "http://localhost:18080/search/exceptionList?startDate=" + this.startDate + "&endDate=" + this.endDate + "&page=" + this.pageInfo.pageNum + "&pageSize=" + this.pageInfo.pageSize,
          success : function(res) {
            layer.close(load)
            if(res){
              that.pageInfo = res
              that.pageInfo.list = res.list.map(item => {
                if(item.createDate) {
                  item.createDate = that.dateFormat('YYYY-mm-dd HH:MM:SS', new Date(item.createDate))
                }
                return item
              })
              console.log(that.pageInfo)
            } else {

            }
          },
          error : function(e){
          }
        });
      },
      uploadToday() {
        let load = layer.msg(this.uploadMsg, {
          shade: [0.8,'#fff'] //0.1透明度的白色背景
        });
        $.ajax({
          type : "GET",
          url : "http://localhost:18080/export/updateToday?sid=" + $("#userId").val(),
          success : function(res) {
            if(res){
              $("#loadingModal").modal('hide');
              alert("上传成功,主单数据" + res.preDateCount + "条,状态数据" + res.confDateCount + "条")
            } else {
              $("#loadingModal").modal('hide');
            }
          },
          error : function(e){
            alert("服务异常")
            $("#loadingModal").modal('hide');
          }
        });
      },
      generateYesterdayDate() {
        let load = layer.msg('请稍后...', {
          icon: 16,
          shade: 0.01
        });
        $.ajax({
          type : "GET",
          url : "http://localhost:18080/export/generateYesterdayDate",
          success : function(res) {
            if(res){
              load.close()
              alert("操作成功,主单数据" + res.preDateCount + "条,状态数据" + res.confDateCount + "条")
            } else {
              load.close()
            }
          },
          error : function(e){
            load.close()
          }
        });
      },
      uploadYesterdayOrder() {
        $.ajax({
          type : "GET",
          url : "http://localhost:18080/export/uploadYesterdayOrder?sid="  + this.userId,
          success : function(res) {
            if(res){
              load.close()
              alert("上传成功" + res + "条,状态数据")
            } else {
              load.close()
            }
          },
          error : function(e){
            load.close()
          }
        });
      },
      uploadYesterdayDocStatus() {
        let load = layer.msg('请稍后...', {
          icon: 16,
          shade: 0.01
        });
        $.ajax({
          type : "GET",
          url : "http://localhost:18080/export/uploadYesterdayDocStatus",
          success : function(res) {
            if(res){
              load.close()
              alert("上传成功" + res + "条,状态数据")
            } else {
              load.close()
            }
          },
          error : function(e){
            load.close()
          }
        });
      },
    }
  })
</script>
</body>
</html>