TravelService.php 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  1. <?php
  2. namespace app\admin\service\goods;
  3. use app\model\Coupon;
  4. use app\model\Goods;
  5. use app\model\GoodsDetail;
  6. use app\model\GoodsLabel;
  7. use app\model\GoodsRunning;
  8. use app\model\GoodsSku;
  9. use app\model\SysCategory;
  10. use support\Db;
  11. use support\exception\BusinessException;
  12. use support\Redis;
  13. use support\Response;
  14. use Tinywan\Jwt\JwtToken;
  15. class TravelService
  16. {
  17. /**
  18. * @Desc 添加商品
  19. * @Author Gorden
  20. * @Date 2024/3/11 10:20
  21. *
  22. * @param $params
  23. * @return Response
  24. */
  25. public static function insert($params): Response
  26. {
  27. Db::beginTransaction();
  28. try {
  29. $params['goods_id'] = "GD" . date('ymdHi') . random_string(4, 'up');
  30. // 主表
  31. self::mainInsert($params);
  32. // 商品详情表
  33. self::detailInsert($params);
  34. // 商品标签表
  35. self::labelInsert($params);
  36. // 产品运行控制信息表
  37. self::goodsRunningInsert($params);
  38. // sku表
  39. self::goodsSkuSet($params, 'insert');
  40. // 待上架状态,上架时间大于当前时间
  41. if ($params['goods_status'] == 'PENDING' && strtotime($params['goods_on_addtimes']) > time()) {
  42. $redis = Redis::connection();
  43. $key = date('YmdHi', strtotime($params['goods_on_addtimes']));
  44. $redis->sAdd(Goods::LISTING_KEY_PREFIX . $key, $params['goods_id']);
  45. }
  46. // 自动下架
  47. if (!empty($params['goods_running_off_type']) && $params['goods_running_off_type'] == 'T' && !empty($params['goods_off_addtimes'])) {
  48. $redis = Redis::connection();
  49. $key = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi', strtotime($params['goods_off_addtimes']));
  50. $redis->sAdd($key, $params['goods_id']);
  51. }
  52. Db::commit();
  53. } catch (\PDOException $e) {
  54. Db::rollBack();
  55. dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
  56. return json_fail('数据写入失败~');
  57. } catch (BusinessException $e) {
  58. Db::rollBack();
  59. dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
  60. return json_fail($e->getMessage());
  61. } catch (\Exception $e) {
  62. Db::rollBack();
  63. dump($e->getTrace());
  64. return json_fail('数据写入失败~');
  65. }
  66. _syslog("添加商品", "商品名【" . $params['goods_name'] . "】");
  67. return json_success('success');
  68. }
  69. /**
  70. * @Desc 商品主表
  71. * @Author Gorden
  72. * @Date 2024/3/11 11:20
  73. *
  74. * @param $params
  75. * @return mixed|string
  76. * @throws BusinessException
  77. */
  78. public static function mainInsert($params)
  79. {
  80. if (!empty($params['goods_cover'])) {
  81. $params['goods_cover'] = str_replace(getenv('STORAGE_DOMAIN'), '', $params['goods_cover']);
  82. }
  83. // 如果产品是待处理状态
  84. if ($params['goods_status'] == 'PENDING') {
  85. if (strtotime($params['goods_on_addtimes']) <= time()) {
  86. $params['goods_status'] = 'ON';
  87. }
  88. }
  89. $category = SysCategory::where('category_id', $params['join_goods_category_id'])->first();
  90. if (!$category) {
  91. throw new BusinessException("产品分类不存在~");
  92. }
  93. if (empty($params['goods_category'])) {
  94. $params['goods_category'] = $category->category_classify;
  95. }
  96. try {
  97. $model = new Goods();
  98. $model->goods_id = $params['goods_id'];
  99. $model->join_goods_category_id = $params['join_goods_category_id'] ?? 0;
  100. $model->join_goods_supplier_id = $params['join_goods_supplier_id'] ?? 0;
  101. $model->goods_classify = $params['goods_classify'] ?? '';
  102. $model->goods_status = $params['goods_status'] ?? '';
  103. $model->goods_category = $params['goods_category'] ?? '';
  104. // $model->goods_prefix = $params['goods_prefix'] ?? 】($category->category_name ? '【' . $category->category_name . '' : '');
  105. $model->goods_prefix = $params['goods_prefix'] ?? '';
  106. $model->goods_name = $params['goods_name'];
  107. $model->goods_market_price = $params['goods_market_price'] ?? 0;
  108. $model->goods_sales_price = $params['goods_sales_price'] ?? 0;
  109. $model->goods_sku_json = !empty($params['goods_sku_json_label']) ? json_encode($params['goods_sku_json_label']) : json_encode(['规格' => ['标准']]);
  110. $model->goods_attribute_json = !empty($params['goods_attribute_json']) ? $params['goods_attribute_json'] : '[]';
  111. $model->goods_title = $params['goods_title'] ?? '';
  112. $model->goods_cover = $params['goods_cover'] ?? '';
  113. $model->goods_on_addtimes = isset($params['goods_on_addtimes']) ? strtotime($params['goods_on_addtimes']) : null;
  114. $model->goods_sort = $params['goods_sort'] ?? null;
  115. $model->goods_groupby = $params['goods_groupby'] ?? '';
  116. $model->goods_remark = $params['goods_remark'] ?? '';
  117. $model->goods_extend_json = $params['goods_extend_json'] ?? '{}';
  118. $model->is_support_appointment = $params['is_support_appointment'] ?? 'N';
  119. $model->creator_user_id = JwtToken::getCurrentId();
  120. $model->goods_addtimes = time();
  121. $model->goods_updatetimes = time();
  122. // {"express":"Y","self":"Y","arrival":"Y"}
  123. $expressJson = [];
  124. if (!empty($params['express_json'])) {
  125. if (in_array('express', $params['express_json'])) {
  126. $expressJson['express'] = 'Y';
  127. } else {
  128. $expressJson['express'] = 'N';
  129. }
  130. if (in_array('self', $params['express_json'])) {
  131. $expressJson['self'] = 'Y';
  132. } else {
  133. $expressJson['self'] = 'N';
  134. }
  135. if (in_array('arrival', $params['express_json'])) {
  136. $expressJson['arrival'] = 'Y';
  137. } else {
  138. $expressJson['arrival'] = 'N';
  139. }
  140. $model->goods_express_json = json_encode($expressJson);
  141. }
  142. if (!empty($params['is_support_appointment']) && $params['is_support_appointment'] == 'Y' && !empty($params['appointment_times'])) {
  143. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  144. $attributeJson = json_decode($model->goods_attribute_json, true);
  145. } elseif (empty($model->goods_attribute_json)) {
  146. $attributeJson = [];
  147. }
  148. $attributeJson['travel-day'] = $params['travel_day'];
  149. $attributeJson['travel-begin'] = $params['travel_begin'];
  150. $attributeJson['travel-night'] = $params['travel_night'];
  151. $attributeJson['travel-trans'] = $params['travel_trans'];
  152. $unixs = [];
  153. foreach ($params['appointment_times'] as $times) {
  154. $unix = strtotime($times['days']);
  155. $unixs[$unix] = $times['person'];
  156. }
  157. ksort($unixs);
  158. $months = [];
  159. foreach ($unixs as $key => $unix) {
  160. $month = date('Ym', $key);
  161. $day = date('d', $key);
  162. $months[$month][$day] = $unix;
  163. }
  164. $attributeJson['month'] = $months;
  165. $model->goods_attribute_json = json_encode($attributeJson, JSON_UNESCAPED_UNICODE);
  166. }
  167. if (!empty($params['goods_json']) && $params['join_goods_category_id'] == 65) {
  168. $goodsJson = json_decode($params['goods_json'], true);
  169. // foreach ($goodsJson as $key => $item) {
  170. // $goodsJson[$key]['color'] = hexToRgb($item['color']);
  171. // }
  172. //
  173. $model->goods_json = json_encode($goodsJson);
  174. } elseif (!empty($params['goods_json']) && $params['join_goods_category_id'] == 43) {
  175. $goodsJson = json_decode($params['goods_json'], true);
  176. $newGoodsJson = [];
  177. foreach ($goodsJson as $item1) {
  178. if (empty($item1['title'])) {
  179. continue;
  180. }
  181. $newItem1 = [];
  182. foreach ($item1['items'] as $item2) {
  183. $newParams = [];
  184. foreach ($item2['params'] as $param) {
  185. if (!empty($param[0]) || !empty($param[1])) {
  186. $newParams[] = $param;
  187. }
  188. }
  189. $newItem1['items'][$item2['key']] = $newParams;
  190. }
  191. $newItem1['service'] = $item1['service'] ?? '';
  192. $newGoodsJson[$item1['title']] = $newItem1;
  193. }
  194. $model->goods_json = json_encode($newGoodsJson);
  195. } else {
  196. $model->goods_json = '[]';
  197. }
  198. if (!empty($params['goods_premisses'])) {
  199. $attributeJson = [];
  200. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  201. $attributeJson = json_decode($model->goods_attribute_json, true);
  202. } elseif (empty($model->goods_attribute_json)) {
  203. $attributeJson = [];
  204. }
  205. $attributeJson['premisses'] = $params['goods_premisses'];
  206. $model->goods_attribute_json = json_encode($attributeJson);
  207. }
  208. if (!empty($params['goods_theme_color']) && !empty($params['goods_theme_icon'])) {
  209. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  210. $attributeJson = json_decode($model->goods_attribute_json, true);
  211. } elseif (empty($model->goods_attribute_json)) {
  212. $attributeJson = [];
  213. }
  214. $attributeJson['bg'] = $params['goods_theme_color'];
  215. $attributeJson['icon'] = str_replace(getenv('STORAGE_DOMAIN'), '', $params['goods_theme_icon']);
  216. $model->goods_attribute_json = json_encode($attributeJson);
  217. }
  218. if (!empty($params['address'])) {
  219. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  220. $attributeJson = json_decode($model->goods_attribute_json, true);
  221. } elseif (empty($model->goods_attribute_json)) {
  222. $attributeJson = [];
  223. }
  224. $attributeJson['address'] = $params['address'];
  225. $model->goods_attribute_json = json_encode($attributeJson);
  226. }
  227. if (!empty($params['position'])) {
  228. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  229. $attributeJson = json_decode($model->goods_attribute_json, true);
  230. } elseif (empty($model->goods_attribute_json)) {
  231. $attributeJson = [];
  232. }
  233. $attributeJson['position'] = $params['position'];
  234. $model->goods_attribute_json = json_encode($attributeJson);
  235. }
  236. if (isset($params['goods_service_premises'])) {
  237. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  238. $attributeJson = json_decode($model->goods_attribute_json, true);
  239. } elseif (empty($model->goods_attribute_json)) {
  240. $attributeJson = [];
  241. }
  242. $attributeJson['service_premises_id'] = $params['goods_service_premises'];
  243. $model->goods_attribute_json = json_encode($attributeJson);
  244. }
  245. if (isset($params['min_count'])) {
  246. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  247. $attributeJson = json_decode($model->goods_attribute_json, true);
  248. } elseif (empty($model->goods_attribute_json)) {
  249. $attributeJson = [];
  250. }
  251. $attributeJson['min-count'] = $params['min_count'];
  252. $model->goods_attribute_json = json_encode($attributeJson);
  253. }
  254. if (isset($params['teachers'])) {
  255. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  256. $attributeJson = json_decode($model->goods_attribute_json, true);
  257. } elseif (empty($model->goods_attribute_json)) {
  258. $attributeJson = [];
  259. }
  260. $attributeJson['teachers'] = $params['teachers'];
  261. $model->goods_attribute_json = json_encode($attributeJson);
  262. }
  263. if (isset($params['work_time'])) {
  264. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  265. $attributeJson = json_decode($model->goods_attribute_json, true);
  266. } elseif (empty($model->goods_attribute_json)) {
  267. $attributeJson = [];
  268. }
  269. $workTimeStart = date('H:i', strtotime($params['work_time'][0]));
  270. $workTimeEnd = date('H:i', strtotime($params['work_time'][1]));
  271. $attributeJsonTime = $workTimeStart . '至' . $workTimeEnd;
  272. $attributeJson['time'] = $attributeJsonTime;
  273. $model->goods_attribute_json = json_encode($attributeJson);
  274. }
  275. if (!empty($params['coupon_id']) && !empty($params['coupon_nbr'])) {
  276. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  277. $attributeJson = json_decode($model->goods_attribute_json, true);
  278. } elseif (empty($model->goods_attribute_json)) {
  279. $attributeJson = [];
  280. }
  281. $coupons = Coupon::whereIn('coupon_id', $params['coupon_id'])
  282. ->select('coupon_id', 'coupon_name')
  283. ->get()
  284. ->toArray();
  285. $couponList = [];
  286. foreach ($coupons as $coupon) {
  287. if (isset($params['coupon_nbr'][$coupon['coupon_id']])) {
  288. $couponList[$coupon['coupon_id']] = [
  289. 'num' => $params['coupon_nbr'][$coupon['coupon_id']],
  290. 'name' => $coupon['coupon_name']
  291. ];
  292. }
  293. }
  294. $attributeJson['coupon'] = $couponList;
  295. $model->goods_attribute_json = json_encode($attributeJson, JSON_UNESCAPED_UNICODE);
  296. }
  297. if ($model->save()) {
  298. return $model->goods_id;
  299. }
  300. // 异常
  301. throw new BusinessException("数据写入失败~");
  302. } catch (\Exception $e) {
  303. dump($e->getMessage());
  304. throw new BusinessException("数据写入失败~");
  305. }
  306. }
  307. /**
  308. * @Desc 详情表
  309. * @Author Gorden
  310. * @Date 2024/3/11 11:19
  311. *
  312. * @param $params
  313. * @return void
  314. * @throws BusinessException
  315. */
  316. public static function detailInsert($params)
  317. {
  318. if (!empty($params['goods_detail_slider_json'])) {
  319. $params['goods_detail_slider_json'] = str_replace(getenv('STORAGE_DOMAIN'), '', $params['goods_detail_slider_json']);
  320. $params['goods_detail_slider_json'] = json_encode(['slider' => $params['goods_detail_slider_json']]);
  321. }
  322. if (isset($params['curriculum'])) {
  323. $params['goods_detail_specs_json'] = json_encode([
  324. [
  325. 'key' => '课时',
  326. 'val' => $params['curriculum']['period'] ?? '',
  327. ],
  328. [
  329. 'key' => '群体',
  330. 'val' => $params['curriculum']['group'] ?? '',
  331. ],
  332. [
  333. 'key' => '课程',
  334. 'val' => $params['curriculum']['course'] ?? '',
  335. ],
  336. ]);
  337. }
  338. try {
  339. $model = new GoodsDetail();
  340. $model->join_detail_goods_id = $params['goods_id'];
  341. $model->goods_detail_code_json = $params['goods_detail_code_json'] ?? '{}';
  342. $model->goods_detail_slider_json = $params['goods_detail_slider_json'] ?? '{}';
  343. $model->goods_detail_specs_json = $params['goods_detail_specs_json'] ?? '{}';
  344. $model->goods_detail_content = $params['goods_detail_content'] ?? '';
  345. if (!$model->save()) {
  346. // 异常
  347. throw new BusinessException("轮播图/详情数据写入失败~");
  348. }
  349. } catch (\Exception $e) {
  350. dump($e->getMessage());
  351. throw new BusinessException("轮播图/详情数据写入失败~");
  352. }
  353. }
  354. /**
  355. * @Desc 标签表
  356. * @Author Gorden
  357. * @Date 2024/3/11 11:32
  358. *
  359. * @param $params
  360. * @return void
  361. * @throws BusinessException
  362. */
  363. public static function labelInsert($params)
  364. {
  365. $model = new GoodsLabel();
  366. $model->join_label_goods_id = $params['goods_id'];
  367. $model->goods_label = $params['goods_label'] ? implode(',', $params['goods_label']) : '';
  368. $model->goods_label_extend_json = !empty($params['goods_label_extend_json']) ? $params['goods_label_extend_json'] : '{}';
  369. if (!$model->save()) {
  370. // 异常
  371. throw new BusinessException('数据写入失败~');
  372. }
  373. }
  374. /**
  375. * @Desc 产品运行控制信息表
  376. * @Author Gorden
  377. * @Date 2024/3/11 11:38
  378. *
  379. * @param $params
  380. * @return void
  381. * @throws BusinessException
  382. */
  383. public static function goodsRunningInsert($params)
  384. {
  385. try {
  386. $model = new GoodsRunning();
  387. $model->join_running_goods_id = $params['goods_id'];
  388. $model->goods_running_storage = $params['goods_running_storage'] ?? 0;
  389. $model->goods_running_sale = $params['goods_running_sale'] ?? 0;
  390. $model->goods_running_off_type = !empty($params['goods_running_off_type']) ? $params['goods_running_off_type'] : '';
  391. $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'])]) : '[]';
  392. if (!$model->save()) {
  393. throw new BusinessException('数据写入失败');
  394. }
  395. } catch (\Exception $e) {
  396. dump($e->getMessage());
  397. throw new BusinessException('数据写入失败');
  398. }
  399. }
  400. /**
  401. * 更新商品
  402. * @param $params
  403. * @return \support\Response
  404. */
  405. public static function update($params)
  406. {
  407. Db::beginTransaction();
  408. try {
  409. // 主表
  410. self::mainUpdate($params);
  411. // 商品详情表
  412. self::detailUpdate($params);
  413. // 商品标签表
  414. self::labelUpdate($params);
  415. // 产品运行控制信息表
  416. self::goodsRunningUpdate($params);
  417. // sku表
  418. self::goodsSkuSet($params, 'update');
  419. Db::commit();
  420. } catch (BusinessException $e) {
  421. Db::rollBack();
  422. return json_fail($e->getMessage());
  423. } catch (\Exception $e) {
  424. Db::rollBack();
  425. return json_fail('数据更新失败~');
  426. }
  427. _syslog("编辑商品", "商品名【" . $params['goods_name'] . "】" ?? "商品ID:【" . $params['goods_id'] . "】");
  428. return json_success('success');
  429. }
  430. /**
  431. * @Desc
  432. * @Author Gorden
  433. * @Date 2024/3/12 8:44
  434. *
  435. * @param $params
  436. * @return void
  437. * @throws BusinessException
  438. */
  439. public static function mainUpdate($params)
  440. {
  441. try {
  442. $data = self::inputFilter($params, new Goods());
  443. if (!empty($data['goods_cover'])) {
  444. $data['goods_cover'] = str_replace(getenv('STORAGE_DOMAIN'), '', $data['goods_cover']);
  445. }
  446. $data['goods_on_addtimes'] = isset($data['goods_on_addtimes']) ? strtotime($data['goods_on_addtimes']) : 0;
  447. $data['goods_sku_json'] = !empty($params['goods_sku_json_label']) ? json_encode($params['goods_sku_json_label']) : json_encode(['规格' => ['标准']]);
  448. $row = Goods::find($data['goods_id']);
  449. if ($row->join_goods_category_id != $data['join_goods_category_id']) {
  450. $category = SysCategory::where('category_id', $params['join_goods_category_id'])->first();
  451. if (!$category) {
  452. throw new BusinessException("产品分类不存在~");
  453. }
  454. $data['goods_category'] = $category->category_classify ?? '';
  455. $data['goods_prefix'] = $data['goods_prefix'] ?? ($category->category_name ? '【' . $category->category_name . '】' : '');
  456. }
  457. if (!empty($params['goods_recommend_index'])) {
  458. $data['goods_category'] = $params['goods_recommend_index'];
  459. }
  460. // 上架时间有变动
  461. if ($data['goods_status'] == 'PENDING' && $row->goods_on_addtimes != $data['goods_on_addtimes']) {
  462. $redis = Redis::connection();
  463. // 删掉原来的
  464. $oldKey = Goods::LISTING_KEY_PREFIX . date('YmdHi', $row->goods_on_addtimes);
  465. $redis->srem($oldKey, $data['goods_id']);
  466. // 加入新的
  467. $newKey = Goods::LISTING_KEY_PREFIX . date('YmdHi', $data['goods_on_addtimes']);
  468. $redis->sadd($newKey, $data['goods_id']);
  469. }
  470. $expressJson = [];
  471. if (!empty($params['express_json'])) {
  472. if (in_array('express', $params['express_json'])) {
  473. $expressJson['express'] = 'Y';
  474. } else {
  475. $expressJson['express'] = 'N';
  476. }
  477. if (in_array('self', $params['express_json'])) {
  478. $expressJson['self'] = 'Y';
  479. } else {
  480. $expressJson['self'] = 'N';
  481. }
  482. if (in_array('arrival', $params['express_json'])) {
  483. $expressJson['arrival'] = 'Y';
  484. } else {
  485. $expressJson['arrival'] = 'N';
  486. }
  487. $data['goods_express_json'] = json_encode($expressJson);
  488. }
  489. if (!empty($params['is_support_appointment']) && $params['is_support_appointment'] == 'Y' && !empty($params['appointment_times'])) {
  490. $attributeJson = [];
  491. if (!empty($row->goods_attribute_json)) {
  492. $attributeJson = json_decode($row->goods_attribute_json, true);
  493. }
  494. $attributeJson['travel-day'] = $params['travel_day'];
  495. $attributeJson['travel-begin'] = $params['travel_begin'];
  496. $attributeJson['travel-night'] = $params['travel_night'];
  497. $attributeJson['travel-trans'] = $params['travel_trans'];
  498. $unixs = [];
  499. foreach ($params['appointment_times'] as $times) {
  500. $unix = strtotime($times['days']);
  501. $unixs[$unix] = $times['person'];
  502. }
  503. ksort($unixs);
  504. $months = [];
  505. foreach ($unixs as $key => $unix) {
  506. $month = date('Ym', $key);
  507. $day = date('d', $key);
  508. $months[$month][$day] = $unix;
  509. }
  510. $attributeJson['month'] = $months;
  511. $data['goods_attribute_json'] = json_encode($attributeJson, JSON_UNESCAPED_UNICODE);
  512. }
  513. if (!empty($params['coupon_id']) && !empty($params['coupon_nbr'])) {
  514. $attributeJson = [];
  515. if (!empty($row->goods_attribute_json)) {
  516. $attributeJson = json_decode($row->goods_attribute_json, true);
  517. }
  518. $coupons = Coupon::whereIn('coupon_id', $params['coupon_id'])
  519. ->select('coupon_id', 'coupon_name')
  520. ->get()
  521. ->toArray();
  522. $couponList = [];
  523. foreach ($coupons as $coupon) {
  524. if (isset($params['coupon_nbr'][$coupon['coupon_id']])) {
  525. $couponList[$coupon['coupon_id']] = [
  526. 'num' => $params['coupon_nbr'][$coupon['coupon_id']],
  527. 'name' => $coupon['coupon_name']
  528. ];
  529. }
  530. }
  531. $attributeJson['coupon'] = $couponList;
  532. $data['goods_attribute_json'] = json_encode($attributeJson, JSON_UNESCAPED_UNICODE);
  533. }
  534. if (!empty($params['goods_premisses'])) {
  535. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  536. if (is_json($data['goods_attribute_json'])) {
  537. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  538. } else {
  539. $data['goods_attribute_json'] = [];
  540. }
  541. } elseif (empty($data['goods_attribute_json'])) {
  542. $data['goods_attribute_json'] = [];
  543. }
  544. $data['goods_attribute_json']['premisses'] = $params['goods_premisses'];
  545. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  546. }
  547. if (!empty($data['goods_json']) && $data['join_goods_category_id'] == 65) {
  548. // $goodsJson = json_decode($data['goods_json'], true);
  549. // foreach ($goodsJson as $key => $item) {
  550. // $goodsJson[$key]['color'] = hexToRgb($item['color']);
  551. // }
  552. //
  553. // $data['goods_json'] = json_encode($goodsJson);
  554. } elseif (!empty($data['goods_json']) && $data['join_goods_category_id'] == 43) {
  555. $goodsJson = json_decode($data['goods_json'], true);
  556. $newGoodsJson = [];
  557. foreach ($goodsJson as $item1) {
  558. if (empty($item1['title'])) {
  559. continue;
  560. }
  561. $newItem1 = [];
  562. foreach ($item1['items'] as $item2) {
  563. $newParams = [];
  564. foreach ($item2['params'] as $param) {
  565. if (!empty($param[0]) || !empty($param[1])) {
  566. $newParams[] = $param;
  567. }
  568. }
  569. $newItem1['items'][$item2['key']] = $newParams;
  570. }
  571. $newItem1['service'] = $item1['service'];
  572. $newGoodsJson[$item1['title']] = $newItem1;
  573. }
  574. $data['goods_json'] = json_encode($newGoodsJson);
  575. } else {
  576. $data['goods_json'] = '[]';
  577. }
  578. if (!empty($params['goods_theme_color']) && !empty($params['goods_theme_icon'])) {
  579. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  580. if (is_json($data['goods_attribute_json'])) {
  581. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  582. } else {
  583. $data['goods_attribute_json'] = [];
  584. }
  585. } elseif (empty($data['goods_attribute_json'])) {
  586. $data['goods_attribute_json'] = [];
  587. }
  588. $data['goods_attribute_json']['bg'] = $params['goods_theme_color'];
  589. $data['goods_attribute_json']['icon'] = str_replace(getenv('STORAGE_DOMAIN'), '', $params['goods_theme_icon']);
  590. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  591. }
  592. if (!empty($params['address'])) {
  593. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  594. if (is_json($data['goods_attribute_json'])) {
  595. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  596. } else {
  597. $data['goods_attribute_json'] = [];
  598. }
  599. } elseif (empty($data['goods_attribute_json'])) {
  600. $data['goods_attribute_json'] = [];
  601. }
  602. $data['goods_attribute_json']['address'] = $params['address'];
  603. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  604. }
  605. if (!empty($params['position'])) {
  606. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  607. if (is_json($data['goods_attribute_json'])) {
  608. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  609. } else {
  610. $data['goods_attribute_json'] = [];
  611. }
  612. } elseif (empty($data['goods_attribute_json'])) {
  613. $data['goods_attribute_json'] = [];
  614. }
  615. $data['goods_attribute_json']['position'] = $params['position'];
  616. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  617. }
  618. if (isset($params['goods_service_premises'])) {
  619. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  620. if (is_json($data['goods_attribute_json'])) {
  621. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  622. } else {
  623. $data['goods_attribute_json'] = [];
  624. }
  625. } elseif (empty($data['goods_attribute_json'])) {
  626. $data['goods_attribute_json'] = [];
  627. }
  628. $data['goods_attribute_json']['service_premises_id'] = $params['goods_service_premises'];
  629. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  630. }
  631. if (isset($params['work_time'])) {
  632. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  633. if (is_json($data['goods_attribute_json'])) {
  634. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  635. } else {
  636. $data['goods_attribute_json'] = [];
  637. }
  638. } elseif (empty($data['goods_attribute_json'])) {
  639. $data['goods_attribute_json'] = [];
  640. }
  641. $workTimeStart = date('H:i', strtotime($params['work_time'][0]));
  642. $workTimeEnd = date('H:i', strtotime($params['work_time'][1]));
  643. $data['goods_attribute_json']['time'] = $workTimeStart . '至' . $workTimeEnd;
  644. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  645. }
  646. if (isset($params['min_count'])) {
  647. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  648. if (is_json($data['goods_attribute_json'])) {
  649. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  650. } else {
  651. $data['goods_attribute_json'] = [];
  652. }
  653. } elseif (empty($data['goods_attribute_json'])) {
  654. $data['goods_attribute_json'] = [];
  655. }
  656. $data['goods_attribute_json']['min-count'] = $params['min_count'];
  657. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  658. }
  659. if (isset($params['teachers'])) {
  660. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  661. if (is_json($data['goods_attribute_json'])) {
  662. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  663. } else {
  664. $data['goods_attribute_json'] = [];
  665. }
  666. } elseif (empty($data['goods_attribute_json'])) {
  667. $data['goods_attribute_json'] = [];
  668. }
  669. $data['goods_attribute_json']['teachers'] = $params['teachers'];
  670. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  671. }
  672. $data['goods_json'] = '[]';
  673. foreach ($data as $key => $val) {
  674. $row->{$key} = $val;
  675. }
  676. $row->goods_updatetimes = time();
  677. $row->updator_user_id = JwtToken::getCurrentId();
  678. $row->save();
  679. } catch (BusinessException $e) {
  680. throw new BusinessException($e->getMessage());
  681. } catch (\Exception $e) {
  682. dump($e->getMessage());
  683. throw new BusinessException('数据更新异常~1');
  684. }
  685. }
  686. /**
  687. * @Desc
  688. * @Author Gorden
  689. * @Date 2024/3/12 9:57
  690. *
  691. * @param $params
  692. * @return void
  693. * @throws BusinessException
  694. */
  695. public static function detailUpdate($params)
  696. {
  697. try {
  698. $data = self::inputFilter($params, new GoodsDetail());
  699. if (!empty($data['goods_detail_slider_json'])) {
  700. $data['goods_detail_slider_json'] = str_replace(getenv('STORAGE_DOMAIN'), '', $data['goods_detail_slider_json']);
  701. $data['goods_detail_slider_json'] = json_encode(['slider' => $data['goods_detail_slider_json']]);
  702. }
  703. if (isset($params['curriculum'])) {
  704. $data['goods_detail_specs_json'] = json_encode([
  705. [
  706. 'key' => '课时',
  707. 'val' => $params['curriculum']['period'] ?? '',
  708. ],
  709. [
  710. 'key' => '群体',
  711. 'val' => $params['curriculum']['group'] ?? '',
  712. ],
  713. [
  714. 'key' => '课程',
  715. 'val' => $params['curriculum']['course'] ?? '',
  716. ],
  717. ]);
  718. }
  719. // 根据goods_id 查详情ID
  720. $detail = GoodsDetail::where('join_detail_goods_id', $params['goods_id'])->first();
  721. if ($detail) {
  722. self::doUpdate($detail->join_detail_goods_id, $data, new GoodsDetail());
  723. } else {
  724. $data['join_detail_goods_id'] = $params['goods_id'];
  725. GoodsDetail::insert($data);
  726. }
  727. } catch (BusinessException $e) {
  728. throw new BusinessException($e->getMessage());
  729. } catch (\Exception $e) {
  730. dump($e->getMessage());
  731. throw new BusinessException('轮播图/详情数据更新异常~');
  732. }
  733. }
  734. /**
  735. * @Desc
  736. * @Author Gorden
  737. * @Date 2024/3/12 9:58
  738. *
  739. * @param $params
  740. * @return void
  741. * @throws BusinessException
  742. */
  743. public static function labelUpdate($params)
  744. {
  745. try {
  746. $data = self::inputFilter($params, new GoodsLabel());
  747. // 根据goods_id 查详情ID
  748. $detail = GoodsLabel::where('join_label_goods_id', $params['goods_id'])->first();
  749. if ($detail) {
  750. self::doUpdate($detail->goods_label_id, $data, new GoodsLabel());
  751. } else {
  752. $data['join_label_goods_id'] = $params['goods_id'];
  753. GoodsLabel::insert($data);
  754. }
  755. } catch (BusinessException $e) {
  756. throw new BusinessException($e->getMessage());
  757. } catch (\Exception $e) {
  758. dump($e->getMessage());
  759. throw new BusinessException('数据更新异常~3');
  760. }
  761. }
  762. /**
  763. * @Desc
  764. * @Author Gorden
  765. * @Date 2024/3/12 9:59
  766. *
  767. * @param $params
  768. * @return void
  769. * @throws BusinessException
  770. */
  771. public static function goodsRunningUpdate($params)
  772. {
  773. try {
  774. $data = self::inputFilter($params, new GoodsRunning());
  775. // 根据goods_id 查详情ID
  776. $detail = GoodsRunning::where('join_running_goods_id', $params['goods_id'])->first();
  777. if (!empty($params['goods_running_off_type']) && $params['goods_running_off_type'] == 'T') {
  778. $redis = Redis::connection();
  779. if (!empty($detail->goods_running_off_json)) {
  780. $goodsRunningOffJson = json_decode($detail->goods_running_off_json, true);
  781. if (isset($goodsRunningOffJson['time'])) {
  782. $oldKey = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi', $goodsRunningOffJson['time']);
  783. $goodsRunningOffJson['time'] = strtotime($params['goods_off_addtimes']);
  784. $data['goods_running_off_json'] = json_encode($goodsRunningOffJson);
  785. // 有老的下架时间,删除老的
  786. $redis->srem($oldKey, $params['goods_id']);
  787. } else {
  788. $goodsRunningOffJson['time'] = strtotime($params['goods_off_addtimes']);
  789. $data['goods_running_off_json'] = json_encode($goodsRunningOffJson);
  790. }
  791. } else {
  792. $data['goods_running_off_json'] = json_encode(['time' => strtotime($params['goods_off_addtimes'])]);
  793. }
  794. // 加入自动下架
  795. $newKey = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi', strtotime($params['goods_off_addtimes']));
  796. $redis->sAdd($newKey, $params['goods_id']);
  797. } 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') {
  798. $goodsRunningOffJson = json_decode($detail->goods_running_off_json, true);
  799. if (isset($goodsRunningOffJson['time'])) {
  800. $oldKey = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi', $goodsRunningOffJson['time']);
  801. $redis = Redis::connection();
  802. $redis->srem($oldKey, $params['goods_id']);
  803. }
  804. }
  805. if ($detail) {
  806. self::doUpdate($detail->join_running_goods_id, $data, new GoodsRunning());
  807. } else {
  808. // 兼容老数据……
  809. $data['join_running_goods_id'] = $params['goods_id'];
  810. GoodsRunning::insert($data);
  811. }
  812. } catch (BusinessException $e) {
  813. throw new BusinessException($e->getMessage());
  814. } catch (\Exception $e) {
  815. dump($e->getMessage());
  816. throw new BusinessException('数据更新异常~');
  817. }
  818. }
  819. /**
  820. * @Desc sku 设置
  821. * @Author Gorden
  822. * @Date 2024/4/10 10:43
  823. *
  824. * @param $params
  825. * @return void
  826. * @throws BusinessException
  827. */
  828. public static function goodsSkuSet($params, $operation = 'insert')
  829. {
  830. try {
  831. Db::beginTransaction();
  832. $skusOldIds = [];
  833. if ($operation == 'update') {
  834. // 查出所有的
  835. $skusOldIds = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->pluck('goods_sku_id', 'goods_sku_id');
  836. // 删掉原有的
  837. // GoodsSku::where('join_sku_goods_id', $params['goods_id'])->delete();
  838. }
  839. if (empty($skusOldIds) && empty($params['goods_sku_json_value'])) {
  840. $skuData = [
  841. 'join_sku_goods_id' => $params['goods_id'],
  842. 'goods_sku_status' => 'ON',
  843. 'goods_sku_specs_json' => '{"规格": "标准"}',
  844. 'goods_sku_title' => "标准" . $params['goods_name'],
  845. 'goods_sku_market_price' => $params['goods_market_price'] ?? 0,
  846. 'goods_sku_sales_price' => $params['goods_sales_price'] ?? 0,
  847. ];
  848. GoodsSku::insert($skuData);
  849. }
  850. // 入新的
  851. if (!empty($params['goods_sku_json_value'])) {
  852. foreach ($params['goods_sku_json_value'] as $item) {
  853. $skus = explode(',', $item['sku']);
  854. $skuArr = [];
  855. for ($i = 1; $i <= count($skus); $i++) {
  856. $skuName = "skuName" . $i;
  857. $key = $item[$skuName];
  858. $skuArr[$key] = $skus[$i - 1];
  859. }
  860. $specsJson = json_encode($skuArr);
  861. $skuTitle = str_replace('-', ',', $item['sku']) . $params['goods_name'];
  862. if ($operation == 'update' && !empty($item['sku_id'])) {
  863. $model = GoodsSku::where('goods_sku_id', $item['sku_id'])->where('goods_sku_status','ON')->first();
  864. if (!$model) {
  865. $model = new GoodsSku();
  866. } else {
  867. unset($skusOldIds[$model->goods_sku_id]);
  868. }
  869. } else {
  870. $model = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->where('goods_sku_status','ON')->where('goods_sku_title', $skuTitle)->first();
  871. if (!$model) {
  872. $model = new GoodsSku();
  873. } else {
  874. unset($skusOldIds[$model->goods_sku_id]);
  875. }
  876. }
  877. $model->join_sku_goods_id = $params['goods_id'];
  878. $model->goods_sku_status = $params['goods_status'];
  879. $model->goods_sku_specs_json = $specsJson;
  880. $model->goods_sku_title = $skuTitle;
  881. $model->goods_sku_market_price = $params['goods_market_price'] ?? 0;
  882. $model->goods_sku_sales_price = $item['price'];
  883. $model->goods_sku_storage_json = json_encode(['storage' => $item['stock']]);
  884. $model->save();
  885. }
  886. }
  887. if ($operation == 'update' && !empty($skusOldIds)) {
  888. // GoodsSku::whereIn('goods_sku_id', $skusOldIds)->delete();
  889. GoodsSku::whereIn('goods_sku_id', $skusOldIds)->update(['goods_sku_status' => 'DISABLED']);
  890. }
  891. Db::commit();
  892. } catch (\Exception $e) {
  893. dump($e->getTrace());
  894. Db::rollBack();
  895. throw new BusinessException('规格数据更新异常~');
  896. }
  897. }
  898. /**
  899. * @Desc
  900. * @Author Gorden
  901. * @Date 2024/3/12 8:45
  902. *
  903. * @param array $data
  904. * @param $model
  905. * @return array
  906. * @throws BusinessException
  907. */
  908. private static function inputFilter(array $data, $model): array
  909. {
  910. $table = config('database.connections.mysql.prefix') . $model->getTable();
  911. $allow_column = $model->getConnection()->select("desc `$table`");
  912. if (!$allow_column) {
  913. throw new BusinessException('表不存在', 2);
  914. }
  915. $columns = array_column($allow_column, 'Type', 'Field');
  916. foreach ($data as $col => $item) {
  917. if (!isset($columns[$col])) {
  918. unset($data[$col]);
  919. continue;
  920. }
  921. // 非字符串类型传空则为null
  922. if ($item === '' && strpos(strtolower($columns[$col]), 'varchar') === false && strpos(strtolower($columns[$col]), 'text') === false) {
  923. $data[$col] = null;
  924. }
  925. if (is_array($item)) {
  926. $data[$col] = implode(',', $item);
  927. }
  928. if ($item != '' && (strpos(strtolower($columns[$col]), 'varchar') || strpos(strtolower($columns[$col]), 'text'))) {
  929. // $data[$col] = htmlspecialchars($item);
  930. }
  931. }
  932. if (empty($data['created_at'])) {
  933. unset($data['created_at']);
  934. }
  935. if (empty($data['updated_at'])) {
  936. unset($data['updated_at']);
  937. }
  938. return $data;
  939. }
  940. /**
  941. * @Desc 执行更新
  942. * @Author Gorden
  943. * @Date 2024/3/12 8:43
  944. *
  945. * @param $id
  946. * @param $data
  947. * @param $model
  948. * @return void
  949. */
  950. private static function doUpdate($id, $data, $model)
  951. {
  952. $row = $model->find($id);
  953. foreach ($data as $key => $val) {
  954. $row->{$key} = $val;
  955. }
  956. $row->save();
  957. }
  958. }