From 3d8cb6c1787dec23cbc85d4b262fd1f00654f891 Mon Sep 17 00:00:00 2001
From: king <18310653075@163.com>
Date: 星期二, 18 二月 2020 01:33:07 +0800
Subject: [PATCH] 2020-02-18

---
 src/tabviews/tableshare/actionList/index.jsx |  278 +++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 267 insertions(+), 11 deletions(-)

diff --git a/src/tabviews/tableshare/actionList/index.jsx b/src/tabviews/tableshare/actionList/index.jsx
index 4b1444c..f8ae9a3 100644
--- a/src/tabviews/tableshare/actionList/index.jsx
+++ b/src/tabviews/tableshare/actionList/index.jsx
@@ -2,6 +2,7 @@
 import PropTypes from 'prop-types'
 import moment from 'moment'
 import { Button, Affix, Modal, notification, Spin, message } from 'antd'
+import * as XLSX from 'xlsx'
 import MutilForm from '@/tabviews/tableshare/mutilform'
 import ExcelIn from '../excelin'
 import Utils from '@/utils/utils.js'
@@ -18,12 +19,13 @@
     type: PropTypes.string,           // 鍒ゆ柇褰撳墠涓轰富琛紙main锛夈�佸瓙琛紙sub锛夈�佸瓙琛ㄦ爣绛撅紙subtab锛�
     MenuID: PropTypes.string,         // 鑿滃崟ID
     actions: PropTypes.array,         // 鎸夐挳缁�
-    logcolumns: PropTypes.array,      // 鏃ュ織涓樉绀哄垪
+    logcolumns: PropTypes.array,      // 鏄剧ず鍒�
     dict: PropTypes.object,           // 瀛楀吀椤�
     setting: PropTypes.any,           // 椤甸潰閫氱敤璁剧疆
     ContainerId: PropTypes.any,       // tab椤甸潰ID锛岀敤浜庡脊绐楁帶鍒�
     refreshdata: PropTypes.func,      // 鎵ц瀹屾垚鍚庢暟鎹埛鏂�
     triggerPopview: PropTypes.func,   // 寮圭獥鏍囩椤佃Е鍙�
+    getexceloutparam: PropTypes.func,  // 鑾峰彇excel瀵煎嚭鏁版嵁
     gettableselected: PropTypes.func  // 鑾峰彇琛ㄦ牸涓暟鎹�
   }
 
@@ -114,8 +116,20 @@
         this.improveAction(item)
       })
     } else if (item.OpenType === 'excelOut') {
-      this.setState({loadingUuid: item.uuid})
-      this.refreshdata(item, 'excelOut')
+      if (
+        item.intertype === 'inner' && !item.innerFunc &&
+        ((setting.interType === 'inner' && setting.innerFunc) || setting.interType === 'outer')
+      ) {
+        notification.warning({
+          top: 92,
+          message: '瀵煎嚭鎸夐挳閰嶇疆閿欒锛�',
+          duration: 10
+        })
+        return
+      }
+
+      this.triggerExcelout(item)
+
     } else if (item.OpenType === 'excelIn') {
       if (item.verify && item.verify.sheet && item.verify.columns && item.verify.columns.length > 0) {
         let primaryId = '' // 瀵煎叆鏃惰Id
@@ -543,15 +557,11 @@
    * 5銆侀�氱煡涓诲垪琛ㄥ埛鏂�
    */
   execSuccess = (btn, res) => {
-    if (btn.OpenType === 'excelOut' || btn.OpenType === 'excelIn') { // 瀵煎嚭excel
-      this.setState({
-        loadingUuid: ''
-      })
-    } else if (res && res.ErrCode === 'S') { // 鎵ц鎴愬姛
+    if (res && res.ErrCode === 'S') { // 鎵ц鎴愬姛
       notification.success({
         top: 92,
         message: res.ErrMesg || this.props.dict['main.action.confirm.success'],
-        duration: 2
+        duration: btn.verify && btn.verify.stime ? btn.verify.stime : 2
       })
     } else if (res && res.ErrCode === '-1') { // 瀹屾垚鍚庝笉鎻愮ず
 
@@ -570,7 +580,12 @@
       this.setState({
         visible: false
       })
+    } else if (btn.OpenType === 'excelOut' || btn.OpenType === 'excelIn') { // 瀵煎嚭excel
+      this.setState({
+        loadingUuid: ''
+      })
     }
+
     this.refreshdata(btn, 'success')
   }
 
@@ -589,14 +604,14 @@
       notification.error({
         top: 92,
         message: res.message || res.ErrMesg,
-        duration: btn.errorTime || 15
+        duration: btn.verify && btn.verify.ntime ? btn.verify.ntime : 15
       })
     } else if (res.ErrCode === 'F') {
       notification.error({
         className: 'notification-custom-error',
         top: 92,
         message: res.message || res.ErrMesg,
-        duration: btn.errorTime || 15
+        duration: btn.verify && btn.verify.ftime ? btn.verify.ftime : 15
       })
     } else if (res.ErrCode === 'NM') {
       message.error(res.message || res.ErrMesg)
@@ -796,6 +811,9 @@
     })
   }
 
+  /**
+   * @description Excel 瀵煎叆
+   */
   getexceldata = (data, btn, errors, primaryId) => {
     if (errors) {
       if (errors === 'notexit') {
@@ -946,6 +964,244 @@
   }
 
   /**
+   * @description Excel 瀵煎嚭
+   */
+  triggerExcelout = (btn) => {
+    let viewParam = this.props.getexceloutparam()
+    let name = `${viewParam.menuName}${moment().format('YYYYMMDDHHmmss')}.xlsx`
+    // let pageSize = 100
+    
+    this.setState({loadingUuid: btn.uuid})
+
+    if (btn.pagination !== 'true') {
+      if (btn.intertype === 'inner' && !btn.innerFunc) { // 浣跨敤绯荤粺鍑芥暟
+        if (!viewParam.arr_field) {
+          this.execError({ErrCode: 'N', message: '鏈缃樉绀哄垪锛�'}, btn)
+          return
+        }
+
+        let param = this.getExcelDefaultParam(viewParam.arr_field, viewParam.orderBy, viewParam.search)
+
+        Api.genericInterface(param).then(result => {
+          if (result.status) {
+            this.exportExcel(result.data, btn, name)
+          } else {
+            this.execError(result, btn)
+          }
+        })
+      } else if (btn.intertype === 'inner' && btn.innerFunc) { // 浣跨敤鍐呴儴鍑芥暟
+        let param = this.getExcelCustomParam(viewParam.orderBy, viewParam.search)
+        param.func = btn.innerFunc
+
+        Api.genericInterface(param).then(result => {
+          if (result.status) {
+            this.exportExcel(result.data, btn, name)
+          } else {
+            this.execError(result, btn)
+          }
+        })
+      } else if (btn.intertype === 'outer' && !btn.innerFunc) { // 浣跨敤澶栭儴鍑芥暟
+        let param = this.getExcelCustomParam(viewParam.orderBy, viewParam.search)
+        if (btn.sysInterface === 'true') {
+          param.rduri = window.GLOB.mainSystemApi || window.GLOB.subSystemApi
+        } else {
+          param.rduri = btn.interface
+        }
+  
+        param.appkey = window.GLOB.appkey || ''
+  
+        if (btn.outerFunc) {
+          param.func = btn.outerFunc
+        }
+
+        Api.genericInterface(param).then(result => {
+          if (result.status) {
+            this.exportExcel(result.data, btn, name)
+          } else {
+            this.execError(result, btn)
+          }
+        })
+      } else if (btn.intertype === 'outer' && btn.innerFunc) {
+        let param = this.getExcelCustomParam(viewParam.orderBy, viewParam.search)
+        param.func = btn.innerFunc
+
+        Api.genericInterface(param).then(res => {
+          if (res.status) {
+            delete res.ErrCode
+            delete res.ErrMesg
+            delete res.message
+            delete res.status
+
+            if (btn.sysInterface === 'true') {
+              res.rduri = window.GLOB.mainSystemApi || window.GLOB.subSystemApi
+            } else {
+              res.rduri = btn.interface
+            }
+  
+            if (btn.outerFunc) {
+              res.func = btn.outerFunc
+            }
+      
+            res.appkey = window.GLOB.appkey || '' // 澶栭儴璇锋眰鏃讹紝缁熶竴娣诲姞appkey
+      
+            Api.genericInterface(res).then(result => {
+              if (result.status) {
+                this.exportExcel(result.data, btn, name)
+              } else {
+                this.execError(result, btn)
+              }
+            })
+          } else {
+            this.execError(res, btn)
+          }
+        })
+      } else {
+        this.execError({ErrCode: 'N', message: '瀵煎嚭鎸夐挳璁剧疆閿欒锛�'}, btn)
+      }
+    }
+  }
+
+  exportExcel = (data, btn, name) => {
+    const { logcolumns } = this.props
+    if (data && data.length > 0) {
+      try {
+        let _header = []
+        let _topRow = {}
+        let colwidth = []
+        logcolumns.forEach(col => {
+          if (col.Hide === 'true') return
+
+          if (!data[0].hasOwnProperty(col.field)) return
+
+          _header.push(col.field)
+          _topRow[col.field] = col.label
+
+          let _colwidth = Math.floor(col.Width / 6)
+
+          if (!_colwidth || _colwidth < 10) {
+            _colwidth = 10
+          }
+
+          colwidth.push({width: _colwidth})
+        })
+
+        let table = []
+
+        table.push(_topRow)
+
+        data.forEach(item => {
+          let _row = {}
+          _header.forEach(field => {
+            _row[field] = item[field]
+          })
+
+          table.push(_row)
+        })
+
+        // const ws = XLSX.utils.aoa_to_sheet(data)
+        const ws = XLSX.utils.json_to_sheet(table, {header: _header, skipHeader: true})
+
+        ws['!cols'] = colwidth
+
+        const wb = XLSX.utils.book_new()
+        XLSX.utils.book_append_sheet(wb, ws, 'Sheet1')
+
+        XLSX.writeFile(wb, name)
+
+        this.execSuccess(btn, {ErrCode: 'S', ErrMesg: '瀵煎嚭鎴愬姛锛�'})
+      } catch {
+        this.execError({ErrCode: 'N', message: 'Excel鐢熸垚澶辫触锛�'}, btn)
+      }
+      
+    } else {
+      this.execError({ErrCode: 'N', message: '鏈煡璇㈠埌瀵煎嚭鏁版嵁锛�'}, btn)
+    }
+  }
+
+  /**
+   * @description 鑾峰彇鐢ㄦ埛鑷畾涔夊瓨鍌ㄨ繃绋嬩紶鍙�
+   */
+  getExcelCustomParam = (orderBy, search, pagination = false, pageIndex = 1, pageSize = 100) => {
+    let _search = Utils.formatCustomMainSearch(search)
+
+    let param = {
+      OrderCol: orderBy,
+      ..._search
+    }
+
+    if (pagination) {
+      param.PageIndex = pageIndex
+      param.PageSize = pageSize
+    }
+
+    return param
+  }
+
+  /**
+   * @description 鑾峰彇榛樿瀛樺偍杩囩▼璇锋眰鍙傛暟
+   */
+  getExcelDefaultParam = (arr_field, orderBy, search, pagination = false, pageIndex = 1, pageSize = 100) => {
+    const { setting } = this.props
+
+    let _search = Utils.joinMainSearchkey(search)
+
+    _search = _search ? 'where ' + _search : ''
+    
+    let param = {
+      func: 'sPC_Get_TableData',
+      obj_name: 'data',
+      arr_field: arr_field,
+      appkey: window.GLOB.appkey || ''
+    }
+    
+    let _dataresource = setting.dataresource
+
+    if (/\s/.test(_dataresource)) {
+      _dataresource = '(' + _dataresource + ') tb'
+    }
+
+    if (setting.queryType === 'statistics') { // 缁熻鏁版嵁婧愶紝鍐呭鏇挎崲
+      let fieldmap = new Map()
+      let options = search.map(item => {
+        let _field = item.key
+
+        if (fieldmap.has(_field)) {
+          _field = _field + '1'
+        }
+
+        fieldmap.set(item.key, true)
+
+        return {
+          reg: new RegExp('@' + _field, 'ig'),
+          value: item.value
+        }
+      })
+
+      options.reverse()
+
+      options.forEach(item => {
+        _dataresource = _dataresource.replace(item.reg, `'${item.value}'`)
+      })
+
+      _search = ''
+    }
+
+    let LText = ''
+    if (pagination) {
+      LText = `select top ${pageSize} ${arr_field} from (select ${arr_field} ,ROW_NUMBER() over(order by ${orderBy}) as rows from ${_dataresource} ${_search}) tmptable where rows > ${pageSize * (pageIndex - 1)} order by tmptable.rows`
+    } else {
+      LText = `select ${arr_field} from (select ${arr_field} ,ROW_NUMBER() over(order by ${orderBy}) as rows from ${_dataresource} ${_search}) tmptable order by tmptable.rows`
+    }
+
+    param.LText = Utils.formatOptions(LText)
+    param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss') + '.000'
+    param.secretkey = Utils.encrypt(param.LText, param.timestamp)
+    param.DateCount = ''
+
+    return param
+  }
+
+  /**
    * @description 妯℃�佹锛堣〃鍗曪級锛岀‘璁�
    */
   handleOk = () => {

--
Gitblit v1.8.0