time()) { $redis = Redis::connection(); $key = date('YmdHi', strtotime($params['goods_on_addtimes'])); $redis->sAdd(Goods::LISTING_KEY_PREFIX . $key, $params['goods_id']); } // 自动下架 if (!empty($params['goods_running_off_type']) && $params['goods_running_off_type'] == 'T' && !empty($params['goods_off_addtimes'])) { $redis = Redis::connection(); $key = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi', strtotime($params['goods_off_addtimes'])); $redis->sAdd($key, $params['goods_id']); } Db::commit(); } catch (\PDOException $e) { Db::rollBack(); dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage()); return json_fail('数据写入失败~'); } catch (BusinessException $e) { Db::rollBack(); dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage()); return json_fail($e->getMessage()); } catch (\Exception $e) { Db::rollBack(); dump($e->getTrace()); return json_fail('数据写入失败~'); } _syslog("添加商品", "商品名【" . $params['goods_name'] . "】"); return json_success('success'); } /** * @Desc 商品主表 * @Author Gorden * @Date 2024/3/11 11:20 * * @param $params * @return mixed|string * @throws BusinessException */ public static function mainInsert($params) { if (!empty($params['goods_cover'])) { $params['goods_cover'] = str_replace(getenv('STORAGE_DOMAIN'), '', $params['goods_cover']); } // 如果产品是待处理状态 if ($params['goods_status'] == 'PENDING') { if (strtotime($params['goods_on_addtimes']) <= time()) { $params['goods_status'] = 'ON'; } } $category = SysCategory::where('category_id', $params['join_goods_category_id'])->first(); if (!$category) { throw new BusinessException("产品分类不存在~"); } if (empty($params['goods_category'])) { $params['goods_category'] = $category->category_classify; } try { $model = new Goods(); $model->goods_id = $params['goods_id']; $model->join_goods_category_id = $params['join_goods_category_id'] ?? 0; $model->join_goods_supplier_id = $params['join_goods_supplier_id'] ?? 0; $model->goods_classify = $params['goods_classify'] ?? ''; $model->goods_status = $params['goods_status'] ?? ''; $model->goods_category = $params['goods_category'] ?? ''; // $model->goods_prefix = $params['goods_prefix'] ?? 】($category->category_name ? '【' . $category->category_name . '' : ''); $model->goods_prefix = $params['goods_prefix'] ?? ''; $model->goods_name = $params['goods_name']; $model->goods_market_price = $params['goods_market_price'] ?? 0; $model->goods_sales_price = $params['goods_sales_price'] ?? 0; $model->goods_sku_json = !empty($params['goods_sku_json_label']) ? json_encode($params['goods_sku_json_label']) : json_encode(['规格' => ['标准']]); $model->goods_attribute_json = !empty($params['goods_attribute_json']) ? $params['goods_attribute_json'] : '[]'; $model->goods_title = $params['goods_title'] ?? ''; $model->goods_cover = $params['goods_cover'] ?? ''; $model->goods_on_addtimes = isset($params['goods_on_addtimes']) ? strtotime($params['goods_on_addtimes']) : null; $model->goods_sort = $params['goods_sort'] ?? null; $model->goods_groupby = $params['goods_groupby'] ?? ''; $model->goods_remark = $params['goods_remark'] ?? ''; $model->goods_extend_json = $params['goods_extend_json'] ?? '{}'; $model->is_support_appointment = $params['is_support_appointment'] ?? 'N'; $model->creator_user_id = JwtToken::getCurrentId(); $model->goods_addtimes = time(); $model->goods_updatetimes = time(); // {"express":"Y","self":"Y","arrival":"Y"} $expressJson = []; if (!empty($params['express_json'])) { if (in_array('express', $params['express_json'])) { $expressJson['express'] = 'Y'; } else { $expressJson['express'] = 'N'; } if (in_array('self', $params['express_json'])) { $expressJson['self'] = 'Y'; } else { $expressJson['self'] = 'N'; } if (in_array('arrival', $params['express_json'])) { $expressJson['arrival'] = 'Y'; } else { $expressJson['arrival'] = 'N'; } $model->goods_express_json = json_encode($expressJson); } if (!empty($params['is_support_appointment']) && $params['is_support_appointment'] == 'Y' && !empty($params['appointment_times'])) { if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) { $attributeJson = json_decode($model->goods_attribute_json, true); } elseif (empty($model->goods_attribute_json)) { $attributeJson = []; } $attributeJson['travel-day'] = $params['travel_day']; $attributeJson['travel-begin'] = $params['travel_begin']; $attributeJson['travel-night'] = $params['travel_night']; $attributeJson['travel-trans'] = $params['travel_trans']; $unixs = []; foreach ($params['appointment_times'] as $times) { $unix = strtotime($times['days']); $unixs[$unix] = $times['person']; } ksort($unixs); $months = []; foreach ($unixs as $key => $unix) { $month = date('Ym', $key); $day = date('d', $key); $months[$month][$day] = $unix; } $attributeJson['month'] = $months; $model->goods_attribute_json = json_encode($attributeJson, JSON_UNESCAPED_UNICODE); } if (!empty($params['goods_json']) && $params['join_goods_category_id'] == 65) { $goodsJson = json_decode($params['goods_json'], true); // foreach ($goodsJson as $key => $item) { // $goodsJson[$key]['color'] = hexToRgb($item['color']); // } // $model->goods_json = json_encode($goodsJson); } elseif (!empty($params['goods_json']) && $params['join_goods_category_id'] == 43) { $goodsJson = json_decode($params['goods_json'], true); $newGoodsJson = []; foreach ($goodsJson as $item1) { if (empty($item1['title'])) { continue; } $newItem1 = []; foreach ($item1['items'] as $item2) { $newParams = []; foreach ($item2['params'] as $param) { if (!empty($param[0]) || !empty($param[1])) { $newParams[] = $param; } } $newItem1['items'][$item2['key']] = $newParams; } $newItem1['service'] = $item1['service'] ?? ''; $newGoodsJson[$item1['title']] = $newItem1; } $model->goods_json = json_encode($newGoodsJson); } else { $model->goods_json = '[]'; } if (!empty($params['goods_premisses'])) { $attributeJson = []; if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) { $attributeJson = json_decode($model->goods_attribute_json, true); } elseif (empty($model->goods_attribute_json)) { $attributeJson = []; } $attributeJson['premisses'] = $params['goods_premisses']; $model->goods_attribute_json = json_encode($attributeJson); } if (!empty($params['goods_theme_color']) && !empty($params['goods_theme_icon'])) { if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) { $attributeJson = json_decode($model->goods_attribute_json, true); } elseif (empty($model->goods_attribute_json)) { $attributeJson = []; } $attributeJson['bg'] = $params['goods_theme_color']; $attributeJson['icon'] = str_replace(getenv('STORAGE_DOMAIN'), '', $params['goods_theme_icon']); $model->goods_attribute_json = json_encode($attributeJson); } if (!empty($params['address'])) { if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) { $attributeJson = json_decode($model->goods_attribute_json, true); } elseif (empty($model->goods_attribute_json)) { $attributeJson = []; } $attributeJson['address'] = $params['address']; $model->goods_attribute_json = json_encode($attributeJson); } if (!empty($params['position'])) { if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) { $attributeJson = json_decode($model->goods_attribute_json, true); } elseif (empty($model->goods_attribute_json)) { $attributeJson = []; } $attributeJson['position'] = $params['position']; $model->goods_attribute_json = json_encode($attributeJson); } if (isset($params['goods_service_premises'])) { if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) { $attributeJson = json_decode($model->goods_attribute_json, true); } elseif (empty($model->goods_attribute_json)) { $attributeJson = []; } $attributeJson['service_premises_id'] = $params['goods_service_premises']; $model->goods_attribute_json = json_encode($attributeJson); } if (isset($params['min_count'])) { if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) { $attributeJson = json_decode($model->goods_attribute_json, true); } elseif (empty($model->goods_attribute_json)) { $attributeJson = []; } $attributeJson['min-count'] = $params['min_count']; $model->goods_attribute_json = json_encode($attributeJson); } if (isset($params['teachers'])) { if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) { $attributeJson = json_decode($model->goods_attribute_json, true); } elseif (empty($model->goods_attribute_json)) { $attributeJson = []; } $attributeJson['teachers'] = $params['teachers']; $model->goods_attribute_json = json_encode($attributeJson); } if (isset($params['work_time'])) { if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) { $attributeJson = json_decode($model->goods_attribute_json, true); } elseif (empty($model->goods_attribute_json)) { $attributeJson = []; } $workTimeStart = date('H:i', strtotime($params['work_time'][0])); $workTimeEnd = date('H:i', strtotime($params['work_time'][1])); $attributeJsonTime = $workTimeStart . '至' . $workTimeEnd; $attributeJson['time'] = $attributeJsonTime; $model->goods_attribute_json = json_encode($attributeJson); } if (!empty($params['coupon_id']) && !empty($params['coupon_nbr'])) { if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) { $attributeJson = json_decode($model->goods_attribute_json, true); } elseif (empty($model->goods_attribute_json)) { $attributeJson = []; } $coupons = Coupon::whereIn('coupon_id', $params['coupon_id']) ->select('coupon_id', 'coupon_name') ->get() ->toArray(); $couponList = []; foreach ($coupons as $coupon) { if (isset($params['coupon_nbr'][$coupon['coupon_id']])) { $couponList[$coupon['coupon_id']] = [ 'num' => $params['coupon_nbr'][$coupon['coupon_id']], 'name' => $coupon['coupon_name'] ]; } } $attributeJson['coupon'] = $couponList; $model->goods_attribute_json = json_encode($attributeJson, JSON_UNESCAPED_UNICODE); } if ($model->save()) { return $model->goods_id; } // 异常 throw new BusinessException("数据写入失败~"); } catch (\Exception $e) { dump($e->getMessage()); throw new BusinessException("数据写入失败~"); } } /** * @Desc 详情表 * @Author Gorden * @Date 2024/3/11 11:19 * * @param $params * @return void * @throws BusinessException */ public static function detailInsert($params) { if (!empty($params['goods_detail_slider_json'])) { $params['goods_detail_slider_json'] = str_replace(getenv('STORAGE_DOMAIN'), '', $params['goods_detail_slider_json']); $params['goods_detail_slider_json'] = json_encode(['slider' => $params['goods_detail_slider_json']]); } if (isset($params['curriculum'])) { $params['goods_detail_specs_json'] = json_encode([ [ 'key' => '课时', 'val' => $params['curriculum']['period'] ?? '', ], [ 'key' => '群体', 'val' => $params['curriculum']['group'] ?? '', ], [ 'key' => '课程', 'val' => $params['curriculum']['course'] ?? '', ], ]); } try { $model = new GoodsDetail(); $model->join_detail_goods_id = $params['goods_id']; $model->goods_detail_code_json = $params['goods_detail_code_json'] ?? '{}'; $model->goods_detail_slider_json = $params['goods_detail_slider_json'] ?? '{}'; $model->goods_detail_specs_json = $params['goods_detail_specs_json'] ?? '{}'; $model->goods_detail_content = $params['goods_detail_content'] ?? ''; if (!$model->save()) { // 异常 throw new BusinessException("轮播图/详情数据写入失败~"); } } catch (\Exception $e) { dump($e->getMessage()); throw new BusinessException("轮播图/详情数据写入失败~"); } } /** * @Desc 标签表 * @Author Gorden * @Date 2024/3/11 11:32 * * @param $params * @return void * @throws BusinessException */ public static function labelInsert($params) { $model = new GoodsLabel(); $model->join_label_goods_id = $params['goods_id']; $model->goods_label = $params['goods_label'] ? implode(',', $params['goods_label']) : ''; $model->goods_label_extend_json = !empty($params['goods_label_extend_json']) ? $params['goods_label_extend_json'] : '{}'; if (!$model->save()) { // 异常 throw new BusinessException('数据写入失败~'); } } /** * @Desc 产品运行控制信息表 * @Author Gorden * @Date 2024/3/11 11:38 * * @param $params * @return void * @throws BusinessException */ public static function goodsRunningInsert($params) { try { $model = new GoodsRunning(); $model->join_running_goods_id = $params['goods_id']; $model->goods_running_storage = $params['goods_running_storage'] ?? 0; $model->goods_running_sale = $params['goods_running_sale'] ?? 0; $model->goods_running_off_type = !empty($params['goods_running_off_type']) ? $params['goods_running_off_type'] : ''; $model->goods_running_off_json = !empty($params['goods_running_off_type']) && $params['goods_running_off_type'] == 'T' && !empty($params['goods_off_addtimes']) ? json_encode(['time' => strtotime($params['goods_off_addtimes'])]) : '[]'; if (!$model->save()) { throw new BusinessException('数据写入失败'); } } catch (\Exception $e) { dump($e->getMessage()); throw new BusinessException('数据写入失败'); } } /** * 更新商品 * @param $params * @return \support\Response */ public static function update($params) { Db::beginTransaction(); try { // 主表 self::mainUpdate($params); // 商品详情表 self::detailUpdate($params); // 商品标签表 self::labelUpdate($params); // 产品运行控制信息表 self::goodsRunningUpdate($params); // sku表 self::goodsSkuSet($params, 'update'); Db::commit(); } catch (BusinessException $e) { Db::rollBack(); return json_fail($e->getMessage()); } catch (\Exception $e) { Db::rollBack(); return json_fail('数据更新失败~'); } _syslog("编辑商品", "商品名【" . $params['goods_name'] . "】" ?? "商品ID:【" . $params['goods_id'] . "】"); return json_success('success'); } /** * @Desc * @Author Gorden * @Date 2024/3/12 8:44 * * @param $params * @return void * @throws BusinessException */ public static function mainUpdate($params) { try { $data = self::inputFilter($params, new Goods()); if (!empty($data['goods_cover'])) { $data['goods_cover'] = str_replace(getenv('STORAGE_DOMAIN'), '', $data['goods_cover']); } $data['goods_on_addtimes'] = isset($data['goods_on_addtimes']) ? strtotime($data['goods_on_addtimes']) : 0; $data['goods_sku_json'] = !empty($params['goods_sku_json_label']) ? json_encode($params['goods_sku_json_label']) : json_encode(['规格' => ['标准']]); $row = Goods::find($data['goods_id']); if ($row->join_goods_category_id != $data['join_goods_category_id']) { $category = SysCategory::where('category_id', $params['join_goods_category_id'])->first(); if (!$category) { throw new BusinessException("产品分类不存在~"); } $data['goods_category'] = $category->category_classify ?? ''; $data['goods_prefix'] = $data['goods_prefix'] ?? ($category->category_name ? '【' . $category->category_name . '】' : ''); } if (!empty($params['goods_recommend_index'])) { $data['goods_category'] = $params['goods_recommend_index']; } // 上架时间有变动 if ($data['goods_status'] == 'PENDING' && $row->goods_on_addtimes != $data['goods_on_addtimes']) { $redis = Redis::connection(); // 删掉原来的 $oldKey = Goods::LISTING_KEY_PREFIX . date('YmdHi', $row->goods_on_addtimes); $redis->srem($oldKey, $data['goods_id']); // 加入新的 $newKey = Goods::LISTING_KEY_PREFIX . date('YmdHi', $data['goods_on_addtimes']); $redis->sadd($newKey, $data['goods_id']); } $expressJson = []; if (!empty($params['express_json'])) { if (in_array('express', $params['express_json'])) { $expressJson['express'] = 'Y'; } else { $expressJson['express'] = 'N'; } if (in_array('self', $params['express_json'])) { $expressJson['self'] = 'Y'; } else { $expressJson['self'] = 'N'; } if (in_array('arrival', $params['express_json'])) { $expressJson['arrival'] = 'Y'; } else { $expressJson['arrival'] = 'N'; } $data['goods_express_json'] = json_encode($expressJson); } if (!empty($params['is_support_appointment']) && $params['is_support_appointment'] == 'Y' && !empty($params['appointment_times'])) { $attributeJson = []; if (!empty($row->goods_attribute_json)) { $attributeJson = json_decode($row->goods_attribute_json, true); } $attributeJson['travel-day'] = $params['travel_day']; $attributeJson['travel-begin'] = $params['travel_begin']; $attributeJson['travel-night'] = $params['travel_night']; $attributeJson['travel-trans'] = $params['travel_trans']; $unixs = []; foreach ($params['appointment_times'] as $times) { $unix = strtotime($times['days']); $unixs[$unix] = $times['person']; } ksort($unixs); $months = []; foreach ($unixs as $key => $unix) { $month = date('Ym', $key); $day = date('d', $key); $months[$month][$day] = $unix; } $attributeJson['month'] = $months; $data['goods_attribute_json'] = json_encode($attributeJson, JSON_UNESCAPED_UNICODE); } if (!empty($params['coupon_id']) && !empty($params['coupon_nbr'])) { $attributeJson = []; if (!empty($row->goods_attribute_json)) { $attributeJson = json_decode($row->goods_attribute_json, true); } $coupons = Coupon::whereIn('coupon_id', $params['coupon_id']) ->select('coupon_id', 'coupon_name') ->get() ->toArray(); $couponList = []; foreach ($coupons as $coupon) { if (isset($params['coupon_nbr'][$coupon['coupon_id']])) { $couponList[$coupon['coupon_id']] = [ 'num' => $params['coupon_nbr'][$coupon['coupon_id']], 'name' => $coupon['coupon_name'] ]; } } $attributeJson['coupon'] = $couponList; $data['goods_attribute_json'] = json_encode($attributeJson, JSON_UNESCAPED_UNICODE); } if (!empty($params['goods_premisses'])) { if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) { if (is_json($data['goods_attribute_json'])) { $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true); } else { $data['goods_attribute_json'] = []; } } elseif (empty($data['goods_attribute_json'])) { $data['goods_attribute_json'] = []; } $data['goods_attribute_json']['premisses'] = $params['goods_premisses']; $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']); } if (!empty($data['goods_json']) && $data['join_goods_category_id'] == 65) { // $goodsJson = json_decode($data['goods_json'], true); // foreach ($goodsJson as $key => $item) { // $goodsJson[$key]['color'] = hexToRgb($item['color']); // } // // $data['goods_json'] = json_encode($goodsJson); } elseif (!empty($data['goods_json']) && $data['join_goods_category_id'] == 43) { $goodsJson = json_decode($data['goods_json'], true); $newGoodsJson = []; foreach ($goodsJson as $item1) { if (empty($item1['title'])) { continue; } $newItem1 = []; foreach ($item1['items'] as $item2) { $newParams = []; foreach ($item2['params'] as $param) { if (!empty($param[0]) || !empty($param[1])) { $newParams[] = $param; } } $newItem1['items'][$item2['key']] = $newParams; } $newItem1['service'] = $item1['service']; $newGoodsJson[$item1['title']] = $newItem1; } $data['goods_json'] = json_encode($newGoodsJson); } else { $data['goods_json'] = '[]'; } if (!empty($params['goods_theme_color']) && !empty($params['goods_theme_icon'])) { if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) { if (is_json($data['goods_attribute_json'])) { $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true); } else { $data['goods_attribute_json'] = []; } } elseif (empty($data['goods_attribute_json'])) { $data['goods_attribute_json'] = []; } $data['goods_attribute_json']['bg'] = $params['goods_theme_color']; $data['goods_attribute_json']['icon'] = str_replace(getenv('STORAGE_DOMAIN'), '', $params['goods_theme_icon']); $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']); } if (!empty($params['address'])) { if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) { if (is_json($data['goods_attribute_json'])) { $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true); } else { $data['goods_attribute_json'] = []; } } elseif (empty($data['goods_attribute_json'])) { $data['goods_attribute_json'] = []; } $data['goods_attribute_json']['address'] = $params['address']; $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']); } if (!empty($params['position'])) { if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) { if (is_json($data['goods_attribute_json'])) { $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true); } else { $data['goods_attribute_json'] = []; } } elseif (empty($data['goods_attribute_json'])) { $data['goods_attribute_json'] = []; } $data['goods_attribute_json']['position'] = $params['position']; $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']); } if (isset($params['goods_service_premises'])) { if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) { if (is_json($data['goods_attribute_json'])) { $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true); } else { $data['goods_attribute_json'] = []; } } elseif (empty($data['goods_attribute_json'])) { $data['goods_attribute_json'] = []; } $data['goods_attribute_json']['service_premises_id'] = $params['goods_service_premises']; $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']); } if (isset($params['work_time'])) { if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) { if (is_json($data['goods_attribute_json'])) { $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true); } else { $data['goods_attribute_json'] = []; } } elseif (empty($data['goods_attribute_json'])) { $data['goods_attribute_json'] = []; } $workTimeStart = date('H:i', strtotime($params['work_time'][0])); $workTimeEnd = date('H:i', strtotime($params['work_time'][1])); $data['goods_attribute_json']['time'] = $workTimeStart . '至' . $workTimeEnd; $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']); } if (isset($params['min_count'])) { if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) { if (is_json($data['goods_attribute_json'])) { $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true); } else { $data['goods_attribute_json'] = []; } } elseif (empty($data['goods_attribute_json'])) { $data['goods_attribute_json'] = []; } $data['goods_attribute_json']['min-count'] = $params['min_count']; $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']); } if (isset($params['teachers'])) { if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) { if (is_json($data['goods_attribute_json'])) { $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true); } else { $data['goods_attribute_json'] = []; } } elseif (empty($data['goods_attribute_json'])) { $data['goods_attribute_json'] = []; } $data['goods_attribute_json']['teachers'] = $params['teachers']; $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']); } $data['goods_json'] = '[]'; foreach ($data as $key => $val) { $row->{$key} = $val; } $row->goods_updatetimes = time(); $row->updator_user_id = JwtToken::getCurrentId(); $row->save(); } catch (BusinessException $e) { throw new BusinessException($e->getMessage()); } catch (\Exception $e) { dump($e->getMessage()); throw new BusinessException('数据更新异常~1'); } } /** * @Desc * @Author Gorden * @Date 2024/3/12 9:57 * * @param $params * @return void * @throws BusinessException */ public static function detailUpdate($params) { try { $data = self::inputFilter($params, new GoodsDetail()); if (!empty($data['goods_detail_slider_json'])) { $data['goods_detail_slider_json'] = str_replace(getenv('STORAGE_DOMAIN'), '', $data['goods_detail_slider_json']); $data['goods_detail_slider_json'] = json_encode(['slider' => $data['goods_detail_slider_json']]); } if (isset($params['curriculum'])) { $data['goods_detail_specs_json'] = json_encode([ [ 'key' => '课时', 'val' => $params['curriculum']['period'] ?? '', ], [ 'key' => '群体', 'val' => $params['curriculum']['group'] ?? '', ], [ 'key' => '课程', 'val' => $params['curriculum']['course'] ?? '', ], ]); } // 根据goods_id 查详情ID $detail = GoodsDetail::where('join_detail_goods_id', $params['goods_id'])->first(); if ($detail) { self::doUpdate($detail->join_detail_goods_id, $data, new GoodsDetail()); } else { $data['join_detail_goods_id'] = $params['goods_id']; GoodsDetail::insert($data); } } catch (BusinessException $e) { throw new BusinessException($e->getMessage()); } catch (\Exception $e) { dump($e->getMessage()); throw new BusinessException('轮播图/详情数据更新异常~'); } } /** * @Desc * @Author Gorden * @Date 2024/3/12 9:58 * * @param $params * @return void * @throws BusinessException */ public static function labelUpdate($params) { try { $data = self::inputFilter($params, new GoodsLabel()); // 根据goods_id 查详情ID $detail = GoodsLabel::where('join_label_goods_id', $params['goods_id'])->first(); if ($detail) { self::doUpdate($detail->goods_label_id, $data, new GoodsLabel()); } else { $data['join_label_goods_id'] = $params['goods_id']; GoodsLabel::insert($data); } } catch (BusinessException $e) { throw new BusinessException($e->getMessage()); } catch (\Exception $e) { dump($e->getMessage()); throw new BusinessException('数据更新异常~3'); } } /** * @Desc * @Author Gorden * @Date 2024/3/12 9:59 * * @param $params * @return void * @throws BusinessException */ public static function goodsRunningUpdate($params) { try { $data = self::inputFilter($params, new GoodsRunning()); // 根据goods_id 查详情ID $detail = GoodsRunning::where('join_running_goods_id', $params['goods_id'])->first(); if (!empty($params['goods_running_off_type']) && $params['goods_running_off_type'] == 'T') { $redis = Redis::connection(); if (!empty($detail->goods_running_off_json)) { $goodsRunningOffJson = json_decode($detail->goods_running_off_json, true); if (isset($goodsRunningOffJson['time'])) { $oldKey = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi', $goodsRunningOffJson['time']); $goodsRunningOffJson['time'] = strtotime($params['goods_off_addtimes']); $data['goods_running_off_json'] = json_encode($goodsRunningOffJson); // 有老的下架时间,删除老的 $redis->srem($oldKey, $params['goods_id']); } else { $goodsRunningOffJson['time'] = strtotime($params['goods_off_addtimes']); $data['goods_running_off_json'] = json_encode($goodsRunningOffJson); } } else { $data['goods_running_off_json'] = json_encode(['time' => strtotime($params['goods_off_addtimes'])]); } // 加入自动下架 $newKey = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi', strtotime($params['goods_off_addtimes'])); $redis->sAdd($newKey, $params['goods_id']); } else if (!empty($params['goods_running_off_type']) && !empty($detail->goods_running_off_type) && $params['goods_running_off_type'] == 'H' && $detail->goods_running_off_type == 'T') { $goodsRunningOffJson = json_decode($detail->goods_running_off_json, true); if (isset($goodsRunningOffJson['time'])) { $oldKey = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi', $goodsRunningOffJson['time']); $redis = Redis::connection(); $redis->srem($oldKey, $params['goods_id']); } } if ($detail) { self::doUpdate($detail->join_running_goods_id, $data, new GoodsRunning()); } else { // 兼容老数据…… $data['join_running_goods_id'] = $params['goods_id']; GoodsRunning::insert($data); } } catch (BusinessException $e) { throw new BusinessException($e->getMessage()); } catch (\Exception $e) { dump($e->getMessage()); throw new BusinessException('数据更新异常~'); } } /** * @Desc sku 设置 * @Author Gorden * @Date 2024/4/10 10:43 * * @param $params * @return void * @throws BusinessException */ public static function goodsSkuSet($params, $operation = 'insert') { try { Db::beginTransaction(); $skusOldIds = []; if ($operation == 'update') { // 查出所有的 $skusOldIds = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->pluck('goods_sku_id', 'goods_sku_id'); // 删掉原有的 // GoodsSku::where('join_sku_goods_id', $params['goods_id'])->delete(); } if (empty($skusOldIds) && empty($params['goods_sku_json_value'])) { $skuData = [ 'join_sku_goods_id' => $params['goods_id'], 'goods_sku_status' => 'ON', 'goods_sku_specs_json' => '{"规格": "标准"}', 'goods_sku_title' => "标准" . $params['goods_name'], 'goods_sku_market_price' => $params['goods_market_price'] ?? 0, 'goods_sku_sales_price' => $params['goods_sales_price'] ?? 0, ]; GoodsSku::insert($skuData); } // 入新的 if (!empty($params['goods_sku_json_value'])) { foreach ($params['goods_sku_json_value'] as $item) { $skus = explode(',', $item['sku']); $skuArr = []; for ($i = 1; $i <= count($skus); $i++) { $skuName = "skuName" . $i; $key = $item[$skuName]; $skuArr[$key] = $skus[$i - 1]; } $specsJson = json_encode($skuArr); $skuTitle = str_replace('-', ',', $item['sku']) . $params['goods_name']; if ($operation == 'update' && !empty($item['sku_id'])) { $model = GoodsSku::where('goods_sku_id', $item['sku_id'])->where('goods_sku_status','ON')->first(); if (!$model) { $model = new GoodsSku(); } else { unset($skusOldIds[$model->goods_sku_id]); } } else { $model = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->where('goods_sku_status','ON')->where('goods_sku_title', $skuTitle)->first(); if (!$model) { $model = new GoodsSku(); } else { unset($skusOldIds[$model->goods_sku_id]); } } $model->join_sku_goods_id = $params['goods_id']; $model->goods_sku_status = $params['goods_status']; $model->goods_sku_specs_json = $specsJson; $model->goods_sku_title = $skuTitle; $model->goods_sku_market_price = $params['goods_market_price'] ?? 0; $model->goods_sku_sales_price = $item['price']; $model->goods_sku_storage_json = json_encode(['storage' => $item['stock']]); $model->save(); } } if ($operation == 'update' && !empty($skusOldIds)) { // GoodsSku::whereIn('goods_sku_id', $skusOldIds)->delete(); GoodsSku::whereIn('goods_sku_id', $skusOldIds)->update(['goods_sku_status' => 'DISABLED']); } Db::commit(); } catch (\Exception $e) { dump($e->getTrace()); Db::rollBack(); throw new BusinessException('规格数据更新异常~'); } } /** * @Desc * @Author Gorden * @Date 2024/3/12 8:45 * * @param array $data * @param $model * @return array * @throws BusinessException */ private static function inputFilter(array $data, $model): array { $table = config('database.connections.mysql.prefix') . $model->getTable(); $allow_column = $model->getConnection()->select("desc `$table`"); if (!$allow_column) { throw new BusinessException('表不存在', 2); } $columns = array_column($allow_column, 'Type', 'Field'); foreach ($data as $col => $item) { if (!isset($columns[$col])) { unset($data[$col]); continue; } // 非字符串类型传空则为null if ($item === '' && strpos(strtolower($columns[$col]), 'varchar') === false && strpos(strtolower($columns[$col]), 'text') === false) { $data[$col] = null; } if (is_array($item)) { $data[$col] = implode(',', $item); } if ($item != '' && (strpos(strtolower($columns[$col]), 'varchar') || strpos(strtolower($columns[$col]), 'text'))) { // $data[$col] = htmlspecialchars($item); } } if (empty($data['created_at'])) { unset($data['created_at']); } if (empty($data['updated_at'])) { unset($data['updated_at']); } return $data; } /** * @Desc 执行更新 * @Author Gorden * @Date 2024/3/12 8:43 * * @param $id * @param $data * @param $model * @return void */ private static function doUpdate($id, $data, $model) { $row = $model->find($id); foreach ($data as $key => $val) { $row->{$key} = $val; } $row->save(); } }