| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- <?php
- namespace app\common\library\ps;
- use app\common\model\ps\ProductProcess;
- use app\common\model\ps\WorkorderProcess;
- /**
- * 产品/工单工序嵌套保存
- */
- class ProcessWriter
- {
- /**
- * @param int $ownerId
- * @param array $rows
- * @param string $type product|workorder
- */
- public static function save($ownerId, $rows, $type = 'product')
- {
- $ownerId = (int)$ownerId;
- if ($ownerId <= 0) {
- return;
- }
- $isProduct = $type === 'product';
- $fk = $isProduct ? 'product_id' : 'workorder_id';
- if ($isProduct) {
- ProductProcess::where($fk, $ownerId)->delete();
- } else {
- WorkorderProcess::where($fk, $ownerId)->delete();
- }
- if ($rows instanceof \think\Collection) {
- $rows = $rows->toArray();
- }
- if (!is_array($rows)) {
- return;
- }
- $seq = 1;
- foreach ($rows as $row) {
- if ($row instanceof \think\Model) {
- $row = $row->toArray();
- }
- if (!is_array($row) || trim((string)($row['name'] ?? '')) === '') {
- continue;
- }
- $data = [
- $fk => $ownerId,
- 'seq' => isset($row['seq']) && $row['seq'] !== '' ? (int)$row['seq'] : $seq,
- 'code' => isset($row['code']) ? trim((string)$row['code']) : '',
- 'name' => trim((string)$row['name']),
- 'workstation' => isset($row['workstation']) ? (string)$row['workstation'] : '',
- 'standard_time' => isset($row['standard_time']) ? (string)$row['standard_time'] : '',
- 'params' => isset($row['params']) ? (string)$row['params'] : '',
- 'qc_points' => isset($row['qc_points']) ? (string)$row['qc_points'] : '',
- 'remark' => isset($row['remark']) ? (string)$row['remark'] : '',
- ];
- if ($isProduct) {
- (new ProductProcess())->allowField(true)->save($data);
- } else {
- (new WorkorderProcess())->allowField(true)->save($data);
- }
- $seq++;
- }
- }
- }
|