WorkOrderMaterial.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <template>
  2. <Dialog v-model="dialogVisible" title="选择物料" style="width: 1100px; min-height: 800px">
  3. <ContentWrap>
  4. <el-form
  5. class="-mb-15px"
  6. :model="queryParams"
  7. ref="queryFormRef"
  8. :inline="true"
  9. label-width="68px"
  10. >
  11. <el-form-item label="物料编码" prop="code">
  12. <el-input
  13. v-model="queryParams.code"
  14. placeholder="请输入物料编码"
  15. clearable
  16. @keyup.enter="handleQuery"
  17. />
  18. </el-form-item>
  19. <el-form-item label="物料名称" prop="name">
  20. <el-input
  21. v-model="queryParams.name"
  22. placeholder="请输入物料名称"
  23. clearable
  24. @keyup.enter="handleQuery"
  25. />
  26. </el-form-item>
  27. <el-form-item>
  28. <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
  29. <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
  30. <el-button @click="handleConfirm" class="custom-green-button"
  31. ><Icon icon="ep:check" class="mr-5px" /> 确认选择</el-button
  32. >
  33. <el-button @click="handleView" type="warning"
  34. ><Icon icon="ep:plus" class="mr-5px" /> 新增物料</el-button
  35. >
  36. </el-form-item>
  37. </el-form>
  38. </ContentWrap>
  39. <ContentWrap>
  40. <el-table
  41. v-loading="loading"
  42. :data="list"
  43. :stripe="true"
  44. ref="tableRef"
  45. :show-overflow-tooltip="true"
  46. @row-click="handleRowClick"
  47. :row-class-name="rowClassName"
  48. >
  49. <el-table-column width="60" label="选择">
  50. <template #default="{ row }">
  51. <el-checkbox
  52. :model-value="selectedRows.some((item) => item.materialCode === row.materialCode)"
  53. @click.stop="toggleRow(row)"
  54. class="no-label-radio"
  55. />
  56. </template>
  57. </el-table-column>
  58. <el-table-column
  59. label="物料编码"
  60. align="center"
  61. prop="materialCode"
  62. :show-overflow-tooltip="true"
  63. class="!w-100px"
  64. />
  65. <el-table-column
  66. label="物料名称"
  67. align="center"
  68. prop="materialName"
  69. :show-overflow-tooltip="true"
  70. />
  71. <el-table-column label="单位" align="center" prop="unit" />
  72. <el-table-column label="总库存数量" align="center" prop="totalInventoryQuantity" />
  73. <el-table-column label="来源" align="center" prop="materialSource" />
  74. <el-table-column label="消耗数量" align="center" prop="quantity">
  75. <template #default="scope">
  76. <el-input
  77. v-model="scope.row.quantity"
  78. @click.stop=""
  79. @focus="scope.$el.querySelector('input').focus()"
  80. />
  81. </template>
  82. </el-table-column>
  83. </el-table>
  84. <!-- 分页 -->
  85. <Pagination
  86. :total="total"
  87. v-model:page="queryParams.pageNo"
  88. v-model:limit="queryParams.pageSize"
  89. @pagination="getList"
  90. />
  91. </ContentWrap>
  92. </Dialog>
  93. <MaintainMaterialDrawer
  94. ref="showDrawer"
  95. :model-value="drawerVisible"
  96. @update:model-value="(val) => (drawerVisible = val)"
  97. @add="handleChildSubmit"
  98. />
  99. </template>
  100. <script setup lang="ts">
  101. import { IotMaintainMaterialVO } from '@/api/pms/maintain/material'
  102. import * as WorkOrderBomMaterialApi from '@/api/pms/iotmainworkorderbommaterial'
  103. import { defineExpose, ref } from 'vue'
  104. import MaintainMaterialDrawer from '@/views/pms/maintain/material/MaintainMaterialDrawer.vue'
  105. const message = useMessage() // 消息弹窗
  106. // const emit = defineEmits(['choose']) // 定义 success 事件,用于操作成功后的回调
  107. // 调整 emit 类型
  108. const emit = defineEmits<{
  109. (e: 'choose', value: WorkOrderBomMaterialApi.IotMainWorkOrderBomMaterialVO[]): void
  110. (e: 'close'): void
  111. }>()
  112. const dialogVisible = ref(false) // 弹窗的是否展示
  113. const loading = ref(true) // 列表的加载中
  114. const queryFormRef = ref() // 搜索的表单
  115. const list = ref<WorkOrderBomMaterialApi.IotMainWorkOrderBomMaterialVO[]>([]) // 列表的数据
  116. const total = ref(0) // 列表的总页数
  117. const tableRef = ref()
  118. const selectedRows = ref<WorkOrderBomMaterialApi.IotMainWorkOrderBomMaterialVO[]>([]) // 多选数据(存储所有选中行的数组)
  119. const queryParams = reactive({
  120. pageNo: 1,
  121. pageSize: 10,
  122. deptId: 0,
  123. bomNodeId: 0,
  124. name: '',
  125. code: ''
  126. })
  127. const drawerVisible = ref<boolean>(false)
  128. const showDrawer = ref()
  129. const handleView = () => {
  130. drawerVisible.value = true
  131. showDrawer.value.openDrawer()
  132. }
  133. const addMateriall = ref(null)
  134. const handleChildSubmit = (formData) => {
  135. const modified = removeOnesFromKeys(formData)
  136. modified.materialName = modified.name;
  137. modified.materialCode = modified.code;
  138. modified.materialSource = '手动添加';
  139. addMateriall.value = modified;
  140. list.value.unshift(modified)
  141. total.value = total.value+1
  142. }
  143. const removeOnesFromKeys = (obj: Record<string, any>) => {
  144. return Object.keys(obj).reduce(
  145. (acc, key) => {
  146. const newKey = key.replace(/1/g, '') // 替换所有 1
  147. acc[newKey] = obj[key]
  148. return acc
  149. },
  150. {} as Record<string, any>
  151. )
  152. }
  153. // 点击整行选中
  154. const handleRowClick = (row) => {
  155. toggleRow(row)
  156. }
  157. const open = async (deptId: number, bomNodeId: number) => {
  158. console.log('传递过来的数据:', deptId)
  159. selectedRows.value = []
  160. dialogVisible.value = true
  161. queryParams.deptId = deptId
  162. queryParams.bomNodeId = bomNodeId
  163. await getList()
  164. }
  165. defineExpose({ open }) // 提供 open 方法,用于打开弹窗
  166. const getList = async () => {
  167. loading.value = true
  168. try {
  169. // queryParams.deptId = props.deptId
  170. const data =
  171. await WorkOrderBomMaterialApi.IotMainWorkOrderBomMaterialApi.workOrderMaterials(queryParams)
  172. list.value = data.list
  173. total.value = data.total
  174. } finally {
  175. loading.value = false
  176. }
  177. }
  178. // 确认选择
  179. const handleConfirm = () => {
  180. if (selectedRows.value.length === 0) {
  181. ElMessage.warning('请至少选择一个物料')
  182. return
  183. }
  184. const filters = selectedRows.value.filter((item) => item.quantity === null||item.quantity === undefined)
  185. if (filters.length > 0) {
  186. message.error('消耗数量必填')
  187. return
  188. }
  189. emit(
  190. 'choose',
  191. selectedRows.value.map((row) => ({
  192. ...row,
  193. // 确保返回必要字段
  194. id: row.id,
  195. materialCode: row.materialCode,
  196. materialName: row.materialName,
  197. unitPrice: row.unitPrice,
  198. unit: row.unit,
  199. quantity: row.quantity
  200. }))
  201. )
  202. dialogVisible.value = false
  203. handleClose()
  204. }
  205. // 关闭时清空选择
  206. const handleClose = () => {
  207. tableRef.value?.clearSelection()
  208. selectedRows.value = []
  209. emit('close')
  210. }
  211. const rowClassName = ({ row }: { row: any }) => {
  212. let className = '';
  213. if(row.bomMaterialFlag === 'Y'){
  214. className = 'row-green-bg'
  215. }
  216. if (row.materialSource === '本地库存' && row.bomMaterialFlag != 'Y') {
  217. className = ''
  218. }
  219. if (row.materialSource === 'sap库存') {
  220. className = 'row-red-bg'
  221. }
  222. return className
  223. }
  224. // 多选 切换行选中状态
  225. const toggleRow = (row) => {
  226. const index = selectedRows.value.findIndex((item) => item.materialCode === row.materialCode)
  227. if (index > -1) {
  228. selectedRows.value.splice(index, 1) // 取消选中
  229. } else {
  230. selectedRows.value.push(row) // 选中
  231. }
  232. }
  233. /** 搜索按钮操作 */
  234. const handleQuery = () => {
  235. queryParams.pageNo = 1
  236. getList()
  237. }
  238. const choose = (row: IotMaintainMaterialVO) => {
  239. emit('choose', row)
  240. dialogVisible.value = false
  241. }
  242. /** 重置按钮操作 */
  243. const resetQuery = () => {
  244. queryFormRef.value.resetFields()
  245. handleQuery()
  246. }
  247. </script>
  248. <style lang="scss" scoped>
  249. .no-label-radio .el-radio__label {
  250. display: none;
  251. }
  252. .no-label-radio .el-radio__inner {
  253. margin-right: 0;
  254. }
  255. /* 自定义淡绿色按钮 */
  256. :deep(.custom-green-button) {
  257. background-color: #e1f3d8;
  258. border-color: #e1f3d8;
  259. color: #67c23a;
  260. }
  261. /* 悬停效果 */
  262. :deep(.custom-green-button:hover) {
  263. background-color: #d1e8c0;
  264. border-color: #d1e8c0;
  265. color: #5daf34;
  266. }
  267. /* 点击效果 */
  268. :deep(.custom-green-button:active) {
  269. background-color: #c2dca8;
  270. border-color: #c2dca8;
  271. }
  272. :deep(.el-table .row-green-bg) {
  273. background-color: #f0f9eb !important; /* Element Plus 绿色背景 */
  274. }
  275. :deep(.el-table .bg-blue-5) {
  276. background-color: #3fa5de !important; /* Element Plus 绿色背景 */
  277. }
  278. </style>