ProcessWriter.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace app\common\library\ps;
  3. use app\common\model\ps\ProductProcess;
  4. use app\common\model\ps\WorkorderProcess;
  5. /**
  6. * 产品/工单工序嵌套保存
  7. */
  8. class ProcessWriter
  9. {
  10. /**
  11. * @param int $ownerId
  12. * @param array $rows
  13. * @param string $type product|workorder
  14. */
  15. public static function save($ownerId, $rows, $type = 'product')
  16. {
  17. $ownerId = (int)$ownerId;
  18. if ($ownerId <= 0) {
  19. return;
  20. }
  21. $isProduct = $type === 'product';
  22. $fk = $isProduct ? 'product_id' : 'workorder_id';
  23. if ($isProduct) {
  24. ProductProcess::where($fk, $ownerId)->delete();
  25. } else {
  26. WorkorderProcess::where($fk, $ownerId)->delete();
  27. }
  28. if ($rows instanceof \think\Collection) {
  29. $rows = $rows->toArray();
  30. }
  31. if (!is_array($rows)) {
  32. return;
  33. }
  34. $seq = 1;
  35. foreach ($rows as $row) {
  36. if ($row instanceof \think\Model) {
  37. $row = $row->toArray();
  38. }
  39. if (!is_array($row) || trim((string)($row['name'] ?? '')) === '') {
  40. continue;
  41. }
  42. $data = [
  43. $fk => $ownerId,
  44. 'seq' => isset($row['seq']) && $row['seq'] !== '' ? (int)$row['seq'] : $seq,
  45. 'code' => isset($row['code']) ? trim((string)$row['code']) : '',
  46. 'name' => trim((string)$row['name']),
  47. 'workstation' => isset($row['workstation']) ? (string)$row['workstation'] : '',
  48. 'standard_time' => isset($row['standard_time']) ? (string)$row['standard_time'] : '',
  49. 'params' => isset($row['params']) ? (string)$row['params'] : '',
  50. 'qc_points' => isset($row['qc_points']) ? (string)$row['qc_points'] : '',
  51. 'remark' => isset($row['remark']) ? (string)$row['remark'] : '',
  52. ];
  53. if ($isProduct) {
  54. (new ProductProcess())->allowField(true)->save($data);
  55. } else {
  56. (new WorkorderProcess())->allowField(true)->save($data);
  57. }
  58. $seq++;
  59. }
  60. }
  61. }