CommonSystemSettingEvent.js 13.2 KB
/* eslint-disable array-callback-return,no-undef,prefer-destructuring */
import moment from 'moment';
import React, { Component } from 'react';
import { Modal, message } from 'antd';
import * as commonFunc from './commonFunc';
import * as commonBusiness from './commonBusiness'; /* 单据业务功能 */
import * as commonUtils from '../../utils/utils'; /* 通用方法 */
import * as commonConfig from '../../utils/config';
import * as commonServices from '../../services/services';

const { confirm } = Modal;

export default (ChildComponent) => {
  return class extends Component {
    constructor(props) {
      super(props);
      this.state = {
      };
      this.form = {};
      /*   表单对象   */
    }
    componentWillReceiveProps(nextProps) {
      const { formData, currentId } = nextProps;
      let { masterConfig } = nextProps;
      if (commonUtils.isEmptyArr(masterConfig) && formData.length > 0) {
        const sId = currentId !== undefined ? currentId : '';
        masterConfig = formData.filter(item => !item.bGrd)[0];
        const sysaccountperiodConfig = formData.filter(item => item.bGrd && item.sTbName === 'sysaccountperiod')[0];
        const sysaccountperiodColumn = commonFunc.getHeaderConfig(sysaccountperiodConfig);
        const sysbillnosettingsConfig = formData.filter(item => item.bGrd && item.sTbName === 'sysbillnosettings')[0];
        const sysbillnosettingsColumn = commonFunc.getHeaderConfig(sysbillnosettingsConfig);

        const syspushMsgConfig = commonUtils.isNotEmptyArr(formData.filter(item => item.bGrd && item.sTbName === 'sysafteroperationmsg')) ?
          formData.filter(item => item.bGrd && item.sTbName === 'sysafteroperationmsg')[0] : {};
        let syspushMsgColumn = [];
        if (commonUtils.isNotEmptyObject(syspushMsgConfig)) {
          syspushMsgColumn = commonFunc.getHeaderConfig(syspushMsgConfig);
        }

        this.handleGetData(masterConfig, sysaccountperiodConfig, sysbillnosettingsConfig, syspushMsgConfig);
        this.props.onSaveState({
          masterConfig,
          sysaccountperiodConfig,
          sysaccountperiodColumn,
          sysbillnosettingsConfig,
          sysbillnosettingsColumn,
          syspushMsgConfig,
          syspushMsgColumn,
          sId,
          pageLoading: false,
        });
      }
    }

    shouldComponentUpdate(nextProps) {
      const { masterConfig, masterData } = nextProps;
      return commonUtils.isNotEmptyObject(masterConfig) && commonUtils.isNotEmptyObject(masterData);
    }
    handleGetData = (masterConfig, sysaccountperiodConfig, sysbillnosettingsConfig, syspushMsgConfig) => {
      const { currentId, year } = this.props;
      const sId = currentId !== undefined ? currentId : '';
      const condition = {
        sSqlCondition: {
          sParentId: masterConfig.sId,
        },
        bFilter: [{
          bFilterName: 'sPeriodId',
          bFilterValue: commonUtils.isEmpty(year) ? new Date().getFullYear().toString() : year,
          bFilterCondition: 'like',
        }],
      };
      this.handleGetMasterDataSet({
        configData: masterConfig, condition: { sId, pageSize: '', pageNum: '' },
      });
      this.props.handleGetDataSet({
        name: 'sysaccountperiod', configData: sysaccountperiodConfig, condition,
      });
      this.props.handleGetDataSet({
        name: 'sysbillnosettings', configData: sysbillnosettingsConfig, condition: { sId, pageSize: '', pageNum: '' },
      });

      if (commonUtils.isNotEmptyObject(syspushMsgConfig)) {
        this.props.handleGetDataSet({
          name: 'syspushMsg', configData: syspushMsgConfig, condition: { sId, pageSize: '', pageNum: '' },
        });
      }
    };

    /* 获取后台数据(数据集) */
    handleGetMasterDataSet = async (params) => {
      const { token, sModelsId } = this.props;
      const {
        configData, condition,
      } = params;
      const configDataId = configData.sId;
      const dataUrl = `${commonConfig.server_host}syssystem/getSyssystemDataByFormcustomId/${configDataId}?sModelsId=${sModelsId}`;
      const dataReturn = (await commonServices.postValueService(token, condition, dataUrl)).data;
      if (dataReturn.code === 1) {
        const masterData = {};
        const returnData = dataReturn.dataset.rows[0].dataSet;
        await configData.gdsconfigformslave.forEach(async (itemConfig) => {
          const iIndex = returnData.findIndex(itemData => itemData.sName === itemConfig.sControlName.substring(1));
          if (iIndex > -1) {
            masterData[itemConfig.sControlName] = returnData[iIndex].sValue;
            if (itemConfig.sControlName.substring(0, 1) === 'b') {
              if (returnData[iIndex].sValue === '0') {
                masterData[itemConfig.sControlName] = false;
              } else if (returnData[iIndex].sValue === '1') {
                masterData[itemConfig.sControlName] = true;
              }
            }
          }
        });
        this.props.onSaveState({
          masterData,
          syssystemsettingsData: returnData,
        });
      } else {
        this.props.getServiceError(dataReturn.msg);
      }
    };
    /*   取消操作  */
    handleCancel = () => {
      /*   待用数据声明   */
      const {
        masterConfig, sysaccountperiodConfig, sysbillnosettingsConfig, syspushMsgConfig,
      } = this.props;
      const onGetDataOk = this.handleGetData;
      const onSaveStateOk = this.props.onSaveState;
      confirm({
        title: '确定要取消',
        onOk() {
          onGetDataOk(masterConfig, sysaccountperiodConfig, sysbillnosettingsConfig, syspushMsgConfig);
          onSaveStateOk({ enabled: false });
        },
        onCancel() {
        },
      });
    };
    /**   toolbar校验保存   */
    handleValidateSave = async () => {
      this.form.validateFields((err) => {
        /*   验证通过与不通过走不同的流程   */
        if (err) { /*   验证失败   */
          /*   直接渲染显示错误提示   */
          for (const key of Object.keys(err)) {
            message.warning(err[key].errors[0].message);
          }
          this.props.onSaveState({
            loading: false,
          });
        } else { /*   验证成功   */
          const {
            masterConfig, masterData, sysaccountperiodConfig, sysaccountperiodData, sysbillnosettingsConfig, sysbillnosettingsData, syssystemsettingsData, sysbillnosettingsDelData, syspushMsgConfig, syspushMsgData, syspushMsgDelData,
          } = this.props;
          const data = [];
          for (const key of Object.keys(masterData)) {
            if (key !== 'handleType') {
              const systemRow = {};
              systemRow.sValue = masterData[key];
              if (key.substring(0, 1) === 'b') {
                if (masterData[key] === true) {
                  systemRow.sValue = '1';
                } else if (masterData[key] === false) {
                  systemRow.sValue = '0';
                }
              } else if (key.substring(0, 1) === 's') {
                if (commonUtils.isNotEmptyStr(masterData[key])) {
                  systemRow.sValue = masterData[key].toString();
                }
              } else if (key.substring(0, 1) === 'd' || key.substring(0, 1) === 'i') {
                if (commonUtils.isNotEmptyNumber(masterData[key])) {
                  systemRow.sValue = masterData[key].toString();
                }
              }
              const iIndex = syssystemsettingsData.findIndex(item => item.sName === key.substring(1));
              if (iIndex > -1) {
                systemRow.handleType = 'update';
                syssystemsettingsData[iIndex] = { ...syssystemsettingsData[iIndex], ...systemRow };
              } else {
                systemRow.sId = commonUtils.createSid();
                systemRow.sName = key.substring(1);
                systemRow.handleType = 'add';
                syssystemsettingsData.push(systemRow);
              }
            }
          }
          data.push(commonBusiness.mergeData('syssystemsettings', masterConfig.sTbName, syssystemsettingsData));
          data.push(commonBusiness.mergeData('sysaccountperiod', sysaccountperiodConfig.sTbName, sysaccountperiodData));
          data.push(commonBusiness.mergeData('sysbillnosettings', sysbillnosettingsConfig.sTbName, sysbillnosettingsData, sysbillnosettingsDelData));
          if (commonUtils.isNotEmptyObject(syspushMsgConfig)) {
            data.push(commonBusiness.mergeData('syspushMsg', syspushMsgConfig.sTbName, syspushMsgData, syspushMsgDelData));
          }


          this.handleSaveData({ data, sClientType: '1', sSysLogSrcId: masterData.sId });
        }
      });
    };
    /**   toolbar保存   */
    handleSaveData = async (params) => {
      const {
        token, sModelsId, masterConfig, sysaccountperiodConfig, sysbillnosettingsConfig, syspushMsgConfig, masterData,
      } = this.props;
      const returnData = await commonBusiness.saveData({ token, value: params, sModelsId });
      if (commonUtils.isNotEmptyObject(returnData)) {
        this.props.onSaveState({
          enabled: false, currentId: masterData.sId, loading: false,
        });
        this.handleGetData(masterConfig, sysaccountperiodConfig, sysbillnosettingsConfig, syspushMsgConfig);
        return true;
      } else {
        this.props.onSaveState({
          loading: false,
        });
        return false;
      }
    };
    /**   表单回带   */
    handleForm = (form) => {
      this.form = form;
    };
    /** 表格数据更改 */
    // name 不写完整的state名称作用为了要用到total // (name, changeValue, sId, dropDownData)
    handleTableChange = (name, sFieldName, changeValue, sId, dropDownData) => {
      /*   从CommonBase获取默认参数   */
      const {
        [`${name}Data`]: tableData, masterData, sModelsType, app,
      } = this.props;
      const iIndex = tableData.findIndex(item => item.sId === sId);
      if (name === 'sysaccountperiod' && (sFieldName === 'tStartDate' || sFieldName === 'tEndDate')) {
        if (sFieldName === 'tEndDate' || iIndex !== 0) {
          message.warning('请选择第一条数据的开始日期');
          return;
        }
        tableData[iIndex].tEndDate = moment(changeValue.tStartDate, 'YYYY-MM-DD HH:mm:ss').add(1, 'months').subtract(1, 'days').format('YYYY-MM-DD HH:mm:ss');
        tableData.forEach((itemData) => {
          if (itemData.sId !== sId) {
            const subscript = tableData.indexOf(itemData);
            itemData.tStartDate = moment(changeValue.tStartDate, 'YYYY-MM-DD HH:mm:ss').add(subscript, 'months').format('YYYY-MM-DD HH:mm:ss');
            itemData.tEndDate = moment(changeValue.tStartDate, 'YYYY-MM-DD HH:mm:ss').add(subscript + 1, 'months').subtract(1, 'days').format('YYYY-MM-DD HH:mm:ss');
            itemData.handleType = 'update';
          }
        });
      }
      const tableDataRow = this.props.onDataChange(name, sFieldName, changeValue, sId, dropDownData, true);
      const models = sModelsType.includes('sales/') || sModelsType.includes('manufacture/') ? 'Product' : 'Materials';
      tableData[iIndex] = commonBusiness.getCalculateAllMoney(app, models, sFieldName, masterData, tableDataRow);
      if (commonUtils.isEmptyStr(tableData[iIndex].handleType)) {
        tableData[iIndex].handleType = 'update';
      }
      this.props.onSaveState({ [`${name}Data`]: tableData, masterData });
    };
    /**   点击修改按钮操作   */
    handleEdit = () => {
      const {
        masterConfig, sysaccountperiodConfig, sysbillnosettingsConfig, syspushMsgConfig,
      } = this.props;
      this.handleGetData(masterConfig, sysaccountperiodConfig, sysbillnosettingsConfig, syspushMsgConfig);
      this.props.onSaveState({ enabled: true, loading: false });
    };
    /**   添加表格空行   */
    handleTableAdd = (name, isWait) => {
      /*   从CommonBase获取默认参数   */
      let { [`${name}Data`]: tableData } = this.props;
      tableData = commonUtils.isEmptyObject(tableData) ? [] : tableData;
      const tableDataRow = this.props.onDataRowAdd(name, true);
      if (name === 'syspushMsg') {
        if (commonUtils.isEmptyArr(this.props.sysbillnosettingsSelectedRowKeys)) {
          message.error('请选择数据行!');
          return;
        } else {
          tableDataRow.sParentId = this.props.sysbillnosettingsSelectedRowKeys[0];
        }
      }
      if (isWait) {
        return tableDataRow;
      } else if (commonUtils.isNotEmptyObject(tableDataRow)) {
        tableData.push(tableDataRow);
        this.props.onSaveState({ [`${name}Data`]: tableData, [`${name}Pagination`]: { total: tableData.length, current: 9999 }, [`${name}SelectedRowKeys`]: [tableDataRow.sId] });
      }
    };
    handleTreeSelect = (name, selectedKeys, e) => {
      const { treeNode } = e.node.props;
      this.props.onTreeSelect(name, selectedKeys, e);
      this.props.onSaveState({ selectedId: treeNode.sId, selectedName: treeNode.sName });
    };
    render() {
      return (
        <ChildComponent
          {...this.props}
          {...this.state}
          onEdit={this.handleEdit}
          onCancel={this.handleCancel}
          onSubmit={this.handleValidateSave}
          onReturnForm={this.handleForm}
          onDataChange={this.handleTableChange}
          onDataRowAdd={this.handleTableAdd}
          onTreeSelect={this.handleTreeSelect}
        />
      );
    }
  };
};