king
2024-10-18 df2a8398142b7a5176552e254422300b5a014ece
src/tabviews/custom/components/calendar/index.jsx
@@ -1,32 +1,31 @@
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { Spin, notification, Modal } from 'antd'
import { Spin } from 'antd'
import Api from '@/api'
import Utils from '@/utils/utils.js'
import asyncComponent from '@/utils/asyncComponent'
import UtilsDM from '@/utils/utils-datamanage.js'
import MKEmitter from '@/utils/events.js'
import CalendarBoard from './board'
import './index.scss'
const NormalHeader = asyncComponent(() => import('@/tabviews/custom/components/share/normalheader'))
const MainSearch = asyncComponent(() => import('@/tabviews/zshare/topSearch'))
const PopupButton = asyncComponent(() => import('@/tabviews/zshare/actionList/popupbutton'))
const CalendarBoard = asyncComponent(() => import('./board'))
class NormalCalendar extends Component {
  static propTpyes = {
    config: PropTypes.object,        // 组件配置信息
    mainSearch: PropTypes.any,       // 外层搜索条件
  }
  state = {
    BID: '',                   // 上级ID
    config: null,              // 图表配置信息
    loading: false,            // 数据加载状态
    data: null,                // 数据
    data: [],                // 数据
    search: '',
    year: new Date().getFullYear(),
    BData: ''
  }
@@ -36,8 +35,8 @@
   * @description 初始化处理
   */
  UNSAFE_componentWillMount () {
    let _config = fromJS(this.props.config).toJS()
    const { config } = this.props
    let _config = fromJS(config).toJS()
    let BData = ''
    if (_config.setting.supModule) {
@@ -46,18 +45,35 @@
      BData = window.GLOB.CacheData.get(_config.$pageId)
    }
    let BID = BData ? (BData.$BID || '') : ''
    if (_config.setting.interType === 'system') {
      if (/@mk_year@/ig.test(_config.setting.dataresource) || /@mk_year@/ig.test(_config.setting.customScript)) {
        _config.setting.$re_year = true
      }
    } else {
      _config.setting.$re_year = true
    }
    if (_config.wrap.colorField && (!_config.wrap.signs || _config.wrap.signs.length === 0)) {
      _config.wrap.colorField = ''
    }
    if (_config.action[0] && _config.action[0].config && _config.action[0].config.enabled) {
      _config.setting.linkbtn = _config.action[0].uuid
    } else {
      _config.action = []
    }
    if (_config.setting.supModule && !BID) {
      _config.setting.onload = 'false'
    }
    this.setState({
      BID: BData ? (BData.$BID || '') : '',
      BID: BID,
      BData: BData,
      config: _config,
      arr_field: _config.columns.map(col => col.field).join(','),
      search: Utils.initMainSearch(_config.search) // 搜索条件初始化(含有时间格式,需要转化)
    }, () => {
      if (_config.setting.onload === 'true') {
        setTimeout(() => {
          this.loadData()
        }, _config.setting.delay || 0)
      }
      search: _config.$searches
    })
  }
@@ -68,19 +84,11 @@
    MKEmitter.addListener('resetSelectLine', this.resetParentParam)
    MKEmitter.addListener('refreshByButtonResult', this.refreshByButtonResult)
    if (config.$cache && !this.loaded) {
      Api.getLCacheConfig(config.uuid).then(res => {
        if (!res || this.loaded) return
        this.setState({data: res.map((item, index) => {
          item.key = index
          item.$$uuid = item[config.setting.primaryKey] || ''
          item.$Index = index + 1 + ''
          return item
        })})
      })
    if (config.setting.useMSearch) {
      MKEmitter.addListener('searchRefresh', this.searchRefresh)
    }
    this.initExec()
  }
  shouldComponentUpdate (nextProps, nextState) {
@@ -92,21 +100,79 @@
      return
    }
    MKEmitter.removeListener('reloadData', this.reloadData)
    MKEmitter.removeListener('searchRefresh', this.searchRefresh)
    MKEmitter.removeListener('resetSelectLine', this.resetParentParam)
    MKEmitter.removeListener('refreshByButtonResult', this.refreshByButtonResult)
  }
  /**
   * @description 图表数据更新,刷新内容
   */
  UNSAFE_componentWillReceiveProps (nextProps) {
  initExec = () => {
    const { config, BID } = this.state
    if (config.$cache) {
      if (config.$time) {
        Api.getLCacheConfig(config.uuid, config.$time, BID).then(res => {
          if (!res.valid && config.setting.onload === 'true') {
            setTimeout(() => {
              this.loadData('init')
            }, config.setting.delay || 0)
          }
          if (!res.data) return
          let _data = []
          res.data.forEach((item, index) => {
            item.key = index
            item.$$uuid = item[config.setting.primaryKey] || ''
            let pass = this.resetLine(item)
            if (pass) {
              _data.push(item)
            }
          })
          this.setState({data: _data})
        })
      } else {
        Api.getLCacheConfig(config.uuid, 0, BID).then(res => {
          if (!res.data || this.loaded) return
          let _data = []
          res.data.forEach((item, index) => {
            item.key = index
            item.$$uuid = item[config.setting.primaryKey] || ''
            let pass = this.resetLine(item)
            if (pass) {
              _data.push(item)
            }
          })
          this.setState({data: _data})
        })
        if (config.setting.onload === 'true') {
          setTimeout(() => {
            this.loadData('init')
          }, config.setting.delay || 0)
        }
      }
    } else if (config.setting.onload === 'true') {
      setTimeout(() => {
        this.loadData()
      }, config.setting.delay || 0)
    }
  }
  searchRefresh = (searchId) => {
    const { config } = this.state
    if (config.setting.useMSearch && nextProps.mainSearch && !is(fromJS(this.props.mainSearch), fromJS(nextProps.mainSearch))) {
      this.setState({}, () => {
        this.loadData()
      })
    }
    if (config.$searchId !== searchId) return
    this.setState({}, () => {
      this.loadData()
    })
  }
  /**
@@ -120,8 +186,8 @@
    if (config.uuid !== menuId) return
    if (position === 'mainline' && config.setting.supModule && BID) { // 刷新源组件时,附带刷新上级行与当前组件
      MKEmitter.emit('reloadData', config.setting.supModule, BID)
    if (['mainline', 'maingrid', 'popclose'].includes(position) && config.setting.supModule) {
      MKEmitter.emit('reloadData', config.setting.supModule, position === 'maingrid' ? '' : BID)
    } else {
      this.loadData()
    }
@@ -146,9 +212,8 @@
    this.loadData()
  }
  async loadData () {
    const { mainSearch } = this.props
    const { config, arr_field, BID, search } = this.state
  async loadData (type) {
    const { config, BID, search, year } = this.state
    
    if (config.setting.supModule && !BID) { // BID 不存在时,不做查询
      this.setState({
@@ -159,17 +224,17 @@
    }
    let searches = fromJS(search).toJS()
    if (config.setting.useMSearch && mainSearch && mainSearch.length > 0) { // 主表搜索条件
      let keys = searches.map(item => item.key.toLowerCase())
    if (config.setting.useMSearch) { // 主表搜索条件
      let mainSearch = window.GLOB.SearchBox.get(config.$searchId) || []
      let keys = config.$s_keys || []
      mainSearch.forEach(item => {
        if (!keys.includes(item.key.toLowerCase())) {
          searches.push(item)
        }
        if (keys.includes(item.key.toLowerCase())) return
        searches.push(item)
      })
    }
    let requireFields = searches.filter(item => item.required && item.value === '')
    if (requireFields.length > 0) {
    if (config.$s_req && searches.filter(item => item.required && item.value === '').length > 0) {
      return
    }
@@ -178,64 +243,94 @@
    })
    let _orderBy = config.setting.order || ''
    let param = UtilsDM.getQueryDataParams(config.setting, arr_field, searches, _orderBy, 1, 99999, BID)
    let param = UtilsDM.getQueryDataParams(config.setting, searches, _orderBy, 1, 99999, BID, '', year)
    let result = await Api.genericInterface(param)
    if (result.status) {
      this.loaded = true
      if (config.$cache && config.setting.onload !== 'false') {
        Api.writeCacheConfig(config.uuid, result.data || '')
      if (config.$cache && type === 'init') {
        Api.writeCacheConfig(config.uuid, result.data || [], BID)
      }
      let data = result.data.map((item, index) => {
      let data = []
      result.data.forEach((item, index) => {
        item.key = index
        item.$$uuid = item[config.setting.primaryKey] || ''
        item.$$BID = BID || ''
        return item
        let pass = this.resetLine(item)
        if (pass) {
          data.push(item)
        }
      })
      this.setState({
        data: data,
        loading: false
      })
      if (result.message) {
        if (result.ErrCode === 'Y') {
          Modal.success({
            title: result.message
          })
        } else if (result.ErrCode === 'S') {
          notification.success({
            top: 92,
            message: result.message,
            duration: 2
          })
        }
      }
      UtilsDM.querySuccess(result)
    } else {
      this.setState({
        loading: false
      })
      
      if (!result.message) return
      if (result.ErrCode === 'N') {
        Modal.error({
          title: result.message,
        })
      } else if (result.ErrCode !== '-2') {
        notification.error({
          top: 92,
          message: result.message,
          duration: 10
        })
      }
      UtilsDM.queryFail(result)
    }
  }
  resetLine = (item) => {
    const { config: { wrap } } = this.state
    let startTime = item[wrap.timeField] || ''
    let endTime = item[wrap.endField || wrap.timeField] || ''
    item.$message = item[wrap.remarkField]
    if (!/^(1|2)\d{3}(-|\/)\d{2}(-|\/)\d{2}/.test(startTime)) return false
    if (!/^(1|2)\d{3}(-|\/)\d{2}(-|\/)\d{2}/.test(endTime)) return false
    if (!item.$message) return false
    if (wrap.colorField) {
      let sign = item[wrap.colorField] || ''
      wrap.signs.forEach(cell => {
        if (cell.sign !== sign) return
        item.$style = cell.style
        item.$color = cell.background
        item.$level = cell.$index
      })
    }
    let equal = endTime.substr(0, 4) === startTime.substr(0, 4)
    item.$startM = +(startTime.substr(0, 4) + startTime.substr(5, 2))
    item.$endM = +(endTime.substr(0, 4) + endTime.substr(5, 2))
    item.$start = +(startTime.substr(0, 4) + startTime.substr(5, 2) + startTime.substr(8, 2))
    item.$end = +(endTime.substr(0, 4) + endTime.substr(5, 2) + endTime.substr(8, 2))
    item.$startTime = equal ? `${startTime.substr(5, 2)}-${startTime.substr(8, 2)}` : `${startTime.substr(0, 4)}-${startTime.substr(5, 2)}-${startTime.substr(8, 2)}`
    item.$endTime = equal ?  `${endTime.substr(5, 2)}-${endTime.substr(8, 2)}` : `${endTime.substr(0, 4)}-${endTime.substr(5, 2)}-${endTime.substr(8, 2)}`
    return true
  }
  refreshbysearch = (searches) => {
    this.setState({
      search: searches
    }, () => {
      this.loadData()
    })
  }
  yearChange = (value) => {
    const { config } = this.state
    if (!config.setting.$re_year) return
    this.setState({
      year: value
    }, () => {
      this.loadData()
    })
@@ -248,7 +343,7 @@
      <div className="normal-calendar-box" id={'anchor' + config.uuid} style={{...config.style}}>
        {loading ?
          <div className="loading-mask">
            {data ? <div className="ant-spin-blur"></div> : null}
            <div className="ant-spin-blur"></div>
            <Spin />
          </div> : null
        }
@@ -259,7 +354,7 @@
        {config.action[0] ? 
          <PopupButton disabled={false} BID={BID} btn={config.action[0]} BData={BData} setting={config.setting} selectedData={[]}/> : null
        }
        <CalendarBoard config={config} />
        <CalendarBoard config={config} data={data} yearChange={this.yearChange}/>
      </div>
    )
  }