TblProductionAddpgForm.vue 9.5 KB
<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <!-- <table style="width: 100%; text-align: center" border>
          <tr>
            <th style="width: 200px">工序</th>
            <th style="width: 200px">承接人</th>
            <th style="width: 200px">工时</th>
          </tr>
          <tr v-for="(item, index) in res" :key="index">
            <td>
              <a-form-model-item prop="gongxu" style="margin-bottom: 0px">
                <a-input
                  v-model="item.gongxu"
                  style="border: none; background-color: transparent; text-align: center"
                  :readOnly="true"
                ></a-input>
              </a-form-model-item>
            </td>
            <td>
              <a-form-model-item prop="undertakerjig" style="margin-bottom: 0px">
                <a-select size="small" v-model="model.undertaker" placeholder="请选择承接人" allowClear>
                  <a-select-option
                    v-for="(option, optionIndex) in options"
                    :key="optionIndex"
                    :value="option.userName"
                    >{{ option.userName }}</a-select-option
                  >
                </a-select>
              </a-form-model-item>
            </td>
            <td>
              <a-form-model-item prop="workHours" style="margin-bottom: 0px">
                <a-input v-model="model.workHoursjig" style="text-align: center"></a-input>
              </a-form-model-item>
            </td>
          </tr>
        </table> -->

        <a-table
          ref="table"
          size="middle"
          bordered
          rowKey="id"
          class="j-table-force-nowrap"
          :scroll="{ x: true }"
          :columns="columns"
          :dataSource="res"
          :pagination="false"
        >
        </a-table>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import Axios from 'axios'
import { addPg, gongxuList, queryBynumber, querygx } from '../../../api/manage'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { listSalary } from '@/api/TblSalaryBase'
import { mapGetters } from 'vuex'
export default {
  name: 'TblProductionAddpgForm',
  mixins: [JeecgListMixin],
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      orderNumber: '',
      productType: '',
      zong: 0,
      yp: 0,
      options: [],
      model: {},
      res: [],
      panduan: [],
      columns: [
        {
          title: '工序',
          align: 'center',
          width: 200,
          dataIndex: 'workingProcedure',
        },
        {
          title: '承接人',
          align: 'center',
          dataIndex: 'undertaker',
          width: 200,
          scopedSlots: { customRender: 'undertaker' },
          customRender: (text, record, index) => {
            return (
              <a-select size="small" v-model={record.undertaker} placeholder="请选择承接人" allowClear>
                {this.options.map((option, optionIndex) => (
                  <a-select-option key={optionIndex} value={option.userName}>
                    {option.userName}
                  </a-select-option>
                ))}
              </a-select>
            )
          },
        },
        {
          title: '工时',
          align: 'center',
          dataIndex: 'workHours',
          scopedSlots: { customRender: 'workHours' },
          customRender: (text, record, index) => {
            return <a-input v-model={record.workHours} style="text-align: center" />
          },
        },
      ],
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        undertaker: [{ required: true, message: '请选择承接人!' }],
      },
      url: {
        add: '/production/tblProductionGongxu/add',
        list: '/production/tblProductionPlan/list',
        queryByType: '/production/tblProductTypeGongxu/queryByType',
      },
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    // this.modelDefault = JSON.parse(JSON.stringify(this.model))
    this.loadUser()
  },
  methods: {
    loadUser() {
      var that = this
      listSalary().then((res) => {
        if (res.success) {
          // that.treeData = [];
          that.options = res.result
        }
      })
    },
    add(record) {
      console.log('产品类型:', record.productType)
      this.productType = record.productType
      this.panduan = record

      gongxuList(this.url.queryByType, { productTypeId: record.productType }).then((ress) => {
        if (ress) {
          //如果已派工时为0总工时*工时比例,否则(总工时-已派工时)*工时比例
          console.log(record.dispatchedWorkingHours)
          if (record.dispatchedWorkingHours <= 0 || record.dispatchedWorkingHours === '') {
            console.log('aa', ress.result)
            console.log(record.totalWorkingHours)
            // 将获取到的工序列表添加到数据源中
            this.res = ress.result.map((item) => ({
              workingProcedure: item.gongxu + ' (' + (item.bili * 100).toFixed() + '%)',
              undertaker: '', // 初始为空,等待用户输入
              workHours: parseFloat((record.totalWorkingHours * item.bili).toFixed(1))
                .toString()
                .replace(/(\.\d*?[1-9])0+$/, '$1'), // 初始为空,等待用户输入
              dispatchTime: new Date(),
              orderNumber: record.orderId,
              dispatchRen: this.userInfo().realname,
              productType: record.productType,
            }))
          } else if (record.dispatchedWorkingHours > 0) {
            console.log('bb', ress.result)
            console.log(record.totalWorkingHours)
            // 将获取到的工序列表添加到数据源中
            this.res = ress.result.map((item) => ({
              workingProcedure: item.gongxu + ' (' + (item.bili * 100).toFixed() + '%)',
              undertaker: '', // 初始为空,等待用户输入
              workHours: parseFloat(((record.totalWorkingHours - record.dispatchedWorkingHours) * item.bili).toFixed(1))
                .toString()
                .replace(/(\.\d*?[1-9])0+$/, '$1'), // 初始为空,等待用户输入
              dispatchTime: new Date(),
              orderNumber: record.orderId,
              dispatchRen: this.userInfo().realname,
              productType: record.productType,
            }))
          }
        }
      })
      console.log('派发人:', this.userInfo().realname)
    },
    ...mapGetters(['nickname', 'avatar', 'userInfo']),
    submitForm() {
      const that = this
      // 构造数据对象数组
      let data = []
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          // 过滤出承接人不为空的记录
            const nonEmptyUndertakerRecords = this.res.filter((record) => record.undertaker)
          if (nonEmptyUndertakerRecords.length>0) {
            
            // 检查是否所有输入框的值都是数字
            const allNumeric = this.res.every((record) => /^\d+(\.\d+)?$/.test(record.workHours))
            if (allNumeric) {
              that.confirmLoading = true
              data=nonEmptyUndertakerRecords;
              console.log('res:', this.res)
              // 使用 reduce 方法累加工时字段值
              const zong = this.res.reduce((acc, current) => {
                console.log('current.workhours:', current.workHours)
                return acc + parseFloat(current.workHours)
              }, 0)
              console.log(this.panduan.dispatchedWorkingHours)
              if (this.panduan.dispatchedWorkingHours === '' || this.panduan.dispatchedWorkingHours === null) {
                this.panduan.dispatchedWorkingHours = 0
              }
              console.log(this.panduan.dispatchedWorkingHours)
              const zongpaifa = parseFloat(this.panduan.dispatchedWorkingHours) + zong
              console.log('派发工时和:', zong)
              console.log('已派发工时:', zongpaifa)
              if (zongpaifa <= this.panduan.totalWorkingHours) {
                addPg(this.url.add, data)
                  .then((res) => {
                    if (res.success) {
                      that.$message.success(res.message)
                      that.$emit('ok')
                    } else {
                      that.$message.warning(res.message)
                    }
                  })
                  .finally(() => {
                    that.confirmLoading = false
                  })
              } else if (zongpaifa > this.panduan.totalWorkingHours) {
                that.$message.error('派发工时超过总工时')
                that.confirmLoading = false
              }
            } else {
              that.$message.error('工时格式错误')
              that.confirmLoading = false
            }
          } else {
            that.$message.error('没有派工给承接人')
            that.confirmLoading = false
          }
        }
      })
    },
  },
}
</script>

<style lang="less" scoped>
.input_dis {
  border-color: #91d5ff;
  background-color: #e6f7ff;
}
</style>