schangxiang@126.com
2025-04-29 27ba504441037666e787ded85b4af2f65be65c17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import { ref, onMounted, reactive, computed, Ref, watch } from 'vue'
import { injectModel } from '@/libs/Provider/Provider'
import { WmsContainerDrawer } from '../Models/WmsContainerDrawer'
import { ElMessage } from 'element-plus'
import isEqual from 'lodash/isEqual'
import { ConfirmBox } from '@/components/ConfirmBox/ConfirmBox'
import { cloneDeep } from 'lodash'
 
export const useWmsContainerDrawer = (props: any, ctx?: any) => {
  const wmsContainerDrawer =
    injectModel<WmsContainerDrawer>('wmsContainerDrawer')
  /**
   * 用来对比的初始化数据
   */
  const initiateData: Ref<Record<string, any>> = ref({})
  const formData = ref<Record<string, any>>({})
  // ref
  const formRef = ref()
 
  const current = computed(() => {
    return props.row || null
  })
  const visible = computed({
    get() {
      return props.modelValue
    },
    set(val) {
      ctx.emit('update:modelValue', val)
    },
  })
  /**
   * 添加的form字段
   */
  const formItems = reactive([
    {
      label: '托盘编号',
      prop: 'containerNo',
      el: 'input',
      placeholder: '请输入托盘编号',
      rules: [{ required: true, message: '托盘编号不能为空', trigger: 'blur' }],
    },
    {
      label: '托盘类型',
      prop: 'containerType',
      el: 'select',
      placeholder: '请选择托盘类型',
      options: [
        { label: '类型1', value: 0 },
        { label: '类型2', value: 1 },
        // 添加所有枚举值
      ],
      rules: [{ required: true, message: '请选择托盘类型', trigger: 'change' }],
    },
    {
      label: '托盘状态',
      prop: 'containerStatus',
      el: 'select',
      placeholder: '请选择托盘状态',
      options: [
        { label: '状态1', value: 0 },
        { label: '状态2', value: 1 },
        // 添加所有枚举值
      ],
      rules: [{ required: true, message: '请选择托盘状态', trigger: 'change' }],
    },
    {
      label: '长度(mm)',
      prop: 'specLength',
      el: 'input',
      placeholder: '请输入长度',
      step: 0.01,
      precision: 2,
    },
    {
      label: '宽度(mm)',
      prop: 'specWidth',
      el: 'input',
      placeholder: '请输入宽度',
      step: 0.01,
      precision: 2,
    },
    {
      label: '高度(mm)',
      prop: 'specHeight',
      el: 'input',
      placeholder: '请输入高度',
      step: 0.01,
      precision: 2,
    },
    {
      label: '载重上限(kg)',
      prop: 'maxWeight',
      el: 'input',
      placeholder: '请输入载重上限',
      step: 0.1,
      precision: 2,
    },
    {
      label: '备注',
      prop: 'remark',
      el: 'input',
      type: 'textarea',
      placeholder: '请输入备注',
      rows: 3,
    },
  ])
  /**
   * 校验是否有数据变化
   */
  const checkIsEqualObject = () => {
    const data = {
      formData: formData.value,
    }
    const check = isEqual(initiateData.value, data)
    return check
  }
 
  const onClose = (done: () => void) => {
    if (visible.value) {
      if (checkIsEqualObject()) {
        visible.value = false
        done && done()
      } else {
        ConfirmBox('是否保存设置?')
          .then(() => {
            onConfirm()
          })
          .catch(() => {
            visible.value = false
            done && done()
          })
      }
    }
  }
  /**
   * 保存
   */
  const onConfirm = async () => {
    await formRef.value?.validate()
    const data = {
      containerNo: formData.value.containerNo,
      containerType: formData.value.containerType,
      containerStatus: formData.value.containerStatus,
      specLength: formData.value.specLength,
      specWidth: formData.value.specWidth,
      specHeight: formData.value.specHeight,
      limitLength: formData.value.limitLength,
      limitWidth: formData.value.limitWidth,
      limitHeight: formData.value.limitHeight,
      maxWeight: formData.value.maxWeight,
      sort: formData.value.sort,
      remark: formData.value.remark,
    }
    if (!current.value) {
      await wmsContainerDrawer.addWmsContainer(data)
    } else {
      const id = current.value.id
      await wmsContainerDrawer.updateWmsContainer(id, data)
    }
    ElMessage.success('保存成功')
    ctx.emit('confirm')
  }
 
  const updateCheckData = () => {
    initiateData.value = {
      formData: {
        ...formData.value,
      },
    }
  }
  /**
   * 弹窗打开获取详情
   */
  const onOpen = async () => {
    if (current.value) {
      const res = await wmsContainerDrawer.getWmsContainerDetail(current.value)
 
      formData.value = {
        containerNo: res.containerNo,
        containerType: res.containerType,
        containerStatus: res.containerStatus,
        specLength: res.specLength,
        specWidth: res.specWidth,
        specHeight: res.specHeight,
        limitLength: res.limitLength,
        limitWidth: res.limitWidth,
        limitHeight: res.limitHeight,
        maxWeight: res.maxWeight,
        remark: res.remark,
        id: res.id,
      }
      updateCheckData()
    } else {
      formData.value = {}
      updateCheckData()
    }
  }
 
  watch(() => current.value, onOpen)
 
  return {
    formItems,
    formData,
    visible,
    formRef,
    onOpen,
    onClose,
    onConfirm,
  }
}