save($params); $products = StoreProduct::field('id,two_cate_id,cate_id')->where('two_cate_id', 'in', $secondCateIds)->whereOr('cate_id', 'in', $thirdCateIds)->select()->toArray(); $productInfo = []; $time = time(); foreach ($products as $product) { $productInfo[] = [ 'product_id' => $product['id'], 'form_id' => $activityZoneForm->id, 'create_time' => $time, 'update_time' => $time, ]; } if (!empty($productInfo)) { ActivityZone::insertAll($productInfo); } Db::commit(); return true; } catch (\Throwable $e) { Db::rollback(); throw new BusinessException($e->getMessage()); } } /** * @notes 编辑 * @param array $params * @return bool * @author admin * @date 2024/12/20 10:52 */ public static function edit(array $params): bool { Db::startTrans(); try { ActivityZoneForm::where('id', $params['id'])->update([ 'type' => $params['type'], 'product_id' => $params['product_id'], ]); Db::commit(); return true; } catch (\Throwable $e) { Db::rollback(); throw new BusinessException($e->getMessage()); } } /** * @notes 删除 * @param array $params * @return bool * @author admin * @date 2024/12/20 10:52 */ public static function delete(array $params): bool { return ActivityZoneForm::destroy($params['id']); } /** * @notes 获取详情 * @param $params * @return array * @author admin * @date 2024/12/20 10:52 */ public static function detail($params): array { return ActivityZoneForm::findOrEmpty($params['id'])->toArray(); } public static function export($params) { $service = new ActivityZoneService(); $activityZoneForm = ActivityZoneForm::findOrEmpty($params['id'])->toArray(); $productIds = ActivityZone::where('form_id', $params['id'])->column('product_id'); $products = StoreProduct::field('id,unit,store_name,two_cate_id')->whereIn('id', $productIds)->order('two_cate_id')->select()->toArray(); $unitIds = array_unique(array_column($products, 'unit')); $cateIds = array_unique(array_column($products, 'two_cate_id')); $unit = StoreProductUnit::whereIn('id', $unitIds)->field('id,name')->withTrashed()->select()->toArray(); $categories = StoreCategory::whereIn('id', $cateIds)->field('id,name')->withTrashed()->select()->toArray(); $unit = reset_index($unit, 'id'); $categories = reset_index($categories, 'id'); $data = []; foreach ($products as $item) { $currentCate = $categories[$item['two_cate_id']]['name'] ?? ''; if (!empty($currentCate)) { $item['unit_name'] = $unit[$item['unit']]['name'] ?? ''; unset($item['unit'], $item['two_cate_id']); $data[$currentCate][] = $item; } } return $service->export($data, $activityZoneForm['title'], $activityZoneForm['remark']); } }