123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005 |
- <?php
- namespace app\admin\service\goods;
- use app\model\Coupon;
- use app\model\Goods;
- use app\model\GoodsDetail;
- use app\model\GoodsLabel;
- use app\model\GoodsRunning;
- use app\model\GoodsSku;
- use app\model\SysCategory;
- use support\Db;
- use support\exception\BusinessException;
- use support\Redis;
- use support\Response;
- use Tinywan\Jwt\JwtToken;
- class TravelService
- {
- /**
- * @Desc 添加商品
- * @Author Gorden
- * @Date 2024/3/11 10:20
- *
- * @param $params
- * @return Response
- */
- public static function insert($params): Response
- {
- Db::beginTransaction();
- try {
- $params['goods_id'] = "GD" . date('ymdHi') . random_string(4, 'up');
- // 主表
- self::mainInsert($params);
- // 商品详情表
- self::detailInsert($params);
- // 商品标签表
- self::labelInsert($params);
- // 产品运行控制信息表
- self::goodsRunningInsert($params);
- // sku表
- self::goodsSkuSet($params, 'insert');
- // 待上架状态,上架时间大于当前时间
- if ($params['goods_status'] == 'PENDING' && strtotime($params['goods_on_addtimes']) > 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();
- }
- }
|