oeeLabelPrint.js 11.1 KB
/* eslint-disable array-callback-return,no-undef */
import React, { Component } from 'react';
import { routerRedux } from 'dva/router';
import { Layout, Spin, message } from 'antd';// Tabs, Form,
import { Form } from '@ant-design/compatible';
import '@ant-design/compatible/assets/index.css';
import StaticEditTable from '../../components/Common/CommonTable';
import * as commonFunc from '../../components/Common/commonFunc';
import CommonBase from '../../components/Common/CommonBase';
import CommonSales from '../../components/Common/CommonBillEvent';
import oeeStyle from './oee.less';
import * as commonUtils from '../../utils/utils';
import ShowType from '../../components/Common/CommonComponent';
import * as commonConfig from '../../utils/config';
import * as commonBusiness from '../../components/Common/commonBusiness';
import * as commonServices from '../../services/services';

const bShowPrint = false;
const { Content } = Layout;
class PrintOeeLabel extends Component {
  constructor(props) {
    super(props);
    this.state = {
    };
    this.form = {}; /*   表单对象   */
  }
  componentWillReceiveProps(nextProps) {
    const {
      slaveData, masterData, formData,
    } = nextProps;
    if (commonUtils.isEmptyObject(masterData) && formData.length > 0) {
      if (commonUtils.isNotEmptyArr(slaveData)) {
        this.props.onSaveState({ masterData: slaveData[0] });
      }
    }
  }
  handleBtnPrint = async (name) => {
    const {
      app, sModelsId, masterData, slaveData, slaveSelectedRowKeys, slaveConfig, formRoute,
    } = this.props;
    const { token } = app;
    let urlPrint = '';
    let printReportName = '';
    let printAssignField = '';
    const BtnPrintConfig = commonUtils.isNotEmptyObject(slaveConfig) ? slaveConfig.gdsconfigformslave.filter(item => item.sControlName.indexOf('BtnPrint') > -1) : [];
    let masterDataJson = '';
    if (commonUtils.isNotEmptyArr(BtnPrintConfig)) {
      printReportName = BtnPrintConfig[0].sRelation;
      printAssignField = BtnPrintConfig[0].sAssignField;/* 取赋值字段 */
    }
    if (commonUtils.isEmpty(printReportName)) {
      message.warn('没有报表模板!');
      return;
    }
    /* 单条数据打印 */
    if (commonUtils.isNotEmptyArr(slaveData)) {
      if (slaveData.length > 1) { /* 多条数据 */
        if (commonUtils.isEmpty(slaveSelectedRowKeys)) {
          message.warn('请选择要打印的数据!');
          return;
        }
        const slaveSelectedData = slaveData.filter(item => slaveSelectedRowKeys.includes(item.sId));
        let flag = false;
        slaveSelectedData.forEach((slaveItem) => {
          if (!slaveItem.dPackQty > 0 || !slaveItem.dTrunkQty > 0) {
            flag = true;
          }
        });
        if (flag) {
          message.warn('包数或每包数量必填!');
          return;
        }
        masterDataJson = JSON.stringify(slaveSelectedData);
      } else {
        if (!masterData.dPackQty > 0 || !masterData.dTrunkQty > 0) {
          message.warn('包数或每包数量必填!');
          return;
        }
        const listData = [];
        listData.push(masterData);
        masterDataJson = JSON.stringify(listData);
      }
    }
    /* 多条数据打印 */
    // printReportName = encodeURI(printReportName);
    const param = masterDataJson;
    const addState = {}; /* 打印按钮赋值配置 */
    addState.pitproductbarcode = printAssignField;
    const sAssignFieldMap = commonUtils.isNotEmptyObject(addState) ? JSON.stringify(addState) : {};
    if (name === 'view') { /* 打印预览 */
      urlPrint = `${commonConfig.file_host}printReport/printPdfOee?${false ? 'fileType=.xlsx&' : ''}printType=noquery&sModelsId=${sModelsId}&token=${encodeURIComponent(token)}`;
      /* Get提交地址栏参数过多,更改Get提交为Post提交 */
      this.handleOpenPost(urlPrint, param, sAssignFieldMap, printReportName);
    } else if (name === 'direct') { /* 直接打印 */
      const condition = { masterData: param, sAssignFieldMap, sReportParam: printReportName };
      const dataUrl = `${commonConfig.file_host_ebc}printReport/printPdfOeeOnline?sModelsId=${sModelsId}&sName=${formRoute}`;
      const dataReturn = (await commonServices.postValueService(token, condition, dataUrl)).data;
      if (dataReturn.code === 1) {
        message.success(dataReturn.msg);
        this.handleColse();
      } else {
        this.props.getServiceError(dataReturn);
      }
    }
  };
  handleOpenPost= (url, params, sAssignFieldMap, printReportName) => {
    const newWin = window.open();
    let formStr = '';
    formStr = `<form style="visibility:hidden;" method="POST" action="${url}">` +
      `<input type="hidden" name="masterData" value='${params}' />` +
      `<input type="hidden" name="sAssignFieldMap" value='${sAssignFieldMap}' />` +
      `<input type="hidden" name="sReportParam" value='${printReportName}' />` +
      '</form>';
    newWin.document.body.innerHTML = formStr;
    newWin.document.forms[0].submit();
    return newWin;
  }
  handleColse = () => {
    const {
      dispatch, onSaveCurrentState,
    } = this.props;
    dispatch(routerRedux.push('/indexOee/oeeCurrentState'));
    onSaveCurrentState(undefined);// 变为当前设备的未清页面
  }

  render() {
    return (
      <div>
        <Spin spinning={false}>
          <div>
            <PrintOeeLabelComponent
              {...this.props}
              {...this.state}
              onPrintLabel={this.handleBtnPrint}
              onClose={this.handleColse}
              onDataChange={this.props.onDataChange}
              onChange={this.props.onChange}
              onSelectRowChange={this.props.onTableSelectRowChange}
            />
          </div>
        </Spin>
      </div>
    );
  }
}
const PrintOeeLabelComponent = Form.create({
  mapPropsToFields(props) {
    const { masterData, masterConfig } = props;
    const obj = commonFunc.mapPropsToFields(masterData, Form, masterConfig);
    return obj;
  },
})((props) => {
  const {
    form, onReturnForm, slaveConfig, slaveData, masterData: masterDataNew,
  } = props;
  /*   回带表单   */
  onReturnForm(form);
  const tableProps = {
    ...commonBusiness.getTableTypes('slave', props),
    enabled: true,
    bRowClick: true,
    formRoute: props.formRoute,
  };
  let bView = true;/* 单条数据 */
  let configs = {};
  let ableConfigs = {};
  if (commonUtils.isNotEmptyArr(slaveData)) {
    if (slaveData.length > 1) {
      bView = false; /* 多条数据 */
    }
  }
  configs = commonUtils.isNotEmptyObject(slaveConfig) ? slaveConfig.gdsconfigformslave.filter(item => item.sName !== '' && item.bVisible && item.sControlName.indexOf('Btn') === -1 && item.iTag === 2) : [];
  ableConfigs = commonUtils.isNotEmptyObject(slaveConfig) ? slaveConfig.gdsconfigformslave.filter(item => item.sName !== '' && item.bVisible && item.sControlName.indexOf('Btn') === -1 && item.iTag === 3) : [];
  return (
    <div>
      <Form style={{ height: '78.8vh', background: bView ? '#d9d9d9' : '#ffffff' }}>
        { bView && commonUtils.isNotEmptyArr(slaveData) ?
          <div style={{ display: bView ? 'block' : 'none' }}>
            <Layout style={{ height: '100%' }} className="xly-list">
              <Layout className="table employeeStateLayout">
                <div className="employeeState">
                  <div className={oeeStyle.configsValue}>
                    {
                    commonUtils.isNotEmptyArr(configs) ?
                      configs.map((item, index) => {
                        return (
                          // eslint-disable-next-line react/no-array-index-key
                          <div key={index} style={{ height: '2.5rem' }} className={`${oeeStyle.configsItem} ${oeeStyle.settingBg}`}>
                            <div className={oeeStyle.configName}>{item.showName}</div><div className={oeeStyle.configValue}>{commonUtils.isNotEmptyObject(masterDataNew) ? masterDataNew[item.sName] : ''}</div>
                          </div>
                        );
                      }) : ''
                  }
                  </div>
                </div>
                {/* 右侧 */}
                <div className="xly-normal-list upEmployeeState">
                  <div style={{ border: '1px solid #d9d9d9', flex: 1 }}>
                    {/* 修改配置值 */}
                    <div style={{ height: '80%', overflowY: 'hidden', background: '#d9d9d9' }}>
                      { commonUtils.isNotEmptyArr(ableConfigs) ?
                      ableConfigs.map((child) => {
                        const sMemo = child.sName.toLowerCase().endsWith('memo');
                        let enabledNew = !child.bReadonly;
                        if (child.iTag === 1) {
                          enabledNew = false;
                        } else if (child.iTag === 3) {
                          enabledNew = true;
                        }
                        const showTypeProps = {
                          name: 'master',
                          form: props.form,
                          getSqlDropDownData: props.getSqlDropDownData,
                          getSqlCondition: props.getSqlCondition,
                          handleSqlDropDownNewRecord: props.handleSqlDropDownNewRecord,
                          getFloatNum: props.getFloatNum,
                          onChange: props.onChange,
                          showConfig: child,
                          formItemLayout: {},
                          textArea: sMemo,
                          enabled: enabledNew,
                          dataValue: commonUtils.isNotEmptyObject(masterDataNew) ? masterDataNew[child.sName] : '',
                          bTable: false,
                          formRoute: props.formRoute,
                        };
                        return <ShowType {...showTypeProps} />;
                      }) : ''
                    }
                    </div>
                  </div>
                </div>
              </Layout>
            </Layout>
          </div> :
          <Layout className="table printLayout">
            <div className="xly-tab-table" style={{ display: bView ? 'none' : 'block', marginTop: '5px', flex: 1 }}>
              <Content>
                <StaticEditTable {...tableProps} footer="hidden" />
              </Content>
            </div>
          </Layout>
          }
      </Form>
      {/* 底部按钮 */}
      <div className={oeeStyle.bottomBtns}>
        <div className={oeeStyle.btnBrints} style={{ justifyContent: 'flex-end' }}>
          {/* eslint-disable-next-line jsx-a11y/anchor-is-valid */}
          <a style={{ background: '#339967' }} onClick={props.onPrintLabel.bind(this, 'view')}>
            打印预览
          </a>
          {
            bShowPrint ?
              // eslint-disable-next-line jsx-a11y/anchor-is-valid
              <a style={{ background: '#177ddc' }} onClick={props.onPrintLabel.bind(this, 'direct')}>
                直接打印
              </a> : ''
          }
          {/* eslint-disable-next-line jsx-a11y/anchor-is-valid */}
          <a style={{ background: '#a61d24' }} onClick={props.onClose}>
            关闭
          </a>
        </div>
      </div>
    </div>
  );
});

export default CommonBase(CommonSales(PrintOeeLabel));