GoodsService.php 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818
  1. <?php
  2. namespace app\admin\service\goods;
  3. use app\model\Goods;
  4. use app\model\GoodsComponent;
  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 app\model\SysDept;
  11. use app\model\SysSerial;
  12. use support\Db;
  13. use support\exception\BusinessException;
  14. use support\Redis;
  15. use support\Request;
  16. use support\Response;
  17. class GoodsService
  18. {
  19. public static function selectAll($goodsIds)
  20. {
  21. $goods = Goods::where('goods_status', 'ON')
  22. ->when($goodsIds != '', function ($query) use ($goodsIds) {
  23. $query->whereIn('goods_id', $goodsIds);
  24. })
  25. ->select('goods_id', 'goods_name')
  26. ->get();
  27. return json_success('', $goods);
  28. }
  29. public static function selectAllByGoodsName($goodsName)
  30. {
  31. $goods = Goods::with('sku')
  32. ->where('goods_status', 'ON')
  33. ->when($goodsName != '', function ($query) use ($goodsName) {
  34. $query->where('goods_name', 'like', "%" . $goodsName . '%');
  35. })
  36. ->select('goods_id', 'goods_name')
  37. ->get()
  38. ->toArray();
  39. foreach ($goods as &$item) {
  40. if (!empty($item['sku'])) {
  41. foreach ($item['sku'] as $key => $sku) {
  42. $specsJson = json_decode($sku['goods_sku_specs_json'], true);
  43. $skuTitle = '';
  44. foreach ($specsJson as $item2) {
  45. if (is_array($item2)) {
  46. $item2 = implode(',', $item2);
  47. }
  48. $skuTitle .= $item2 . '-';
  49. }
  50. $item['sku'][$key]['goods_sku_id'] = strval($item['sku'][$key]['goods_sku_id']);
  51. $item['sku'][$key]['goods_sku_title'] = rtrim($skuTitle, '-');
  52. }
  53. }
  54. }
  55. return json_success('', $goods);
  56. }
  57. public static function selectAllByCategoryForRuleAddComponent($category = "GOODS")
  58. {
  59. $categoryIds = [];
  60. if ($category == 'GOODS') {
  61. $categoryIds = [6, 7, 8, 10, 11, 12];
  62. } elseif ($category == 'SERVICE') {
  63. $categorySuperIds = [31, 43, 65, 30, 66, 72];
  64. $categorys = SysCategory::whereIn('category_id', $categorySuperIds)->get()->toArray();
  65. foreach ($categorys as $item) {
  66. if (empty($item['category_super_path'])) {
  67. $item['category_super_path'] = '#' . $item['category_id'] . '#';
  68. } else {
  69. $item['category_super_path'] = $item['category_super_path'] . '#' . $item['category_id'] . '#';
  70. }
  71. $categoryIds = SysCategory::where('category_super_path', 'like', '%' . $item['category_super_path'])->pluck('category_id');
  72. $categoryIds = $categoryIds ? $categoryIds->toArray() : [];
  73. $categorySuperIds = array_merge($categorySuperIds, $categoryIds);
  74. }
  75. $categoryIds = array_unique($categorySuperIds);
  76. }
  77. $goods = Goods::with('sku')
  78. // ->where('goods_classify', $category)
  79. ->when(!empty($categoryIds), function ($query) use ($categoryIds) {
  80. $query->whereIn('join_goods_category_id', $categoryIds);
  81. })->select('goods_id', 'goods_name', 'join_goods_category_id')
  82. ->get()
  83. ->toArray();
  84. foreach ($goods as &$good) {
  85. if (!empty($good['sku'])) {
  86. foreach ($good['sku'] as $key => $sku) {
  87. if (!empty($sku['goods_sku_specs_json'])) {
  88. $specsJson = json_decode($sku['goods_sku_specs_json'], true);
  89. $skuTitle = '';
  90. foreach ($specsJson as $item) {
  91. if (is_array($item)) {
  92. $item = implode(',', $item);
  93. }
  94. $skuTitle .= $item . '-';
  95. }
  96. $good['sku'][$key]['goods_sku_id'] = strval($good['sku'][$key]['goods_sku_id']);
  97. $good['sku'][$key]['goods_sku_title'] = rtrim($skuTitle, '-');
  98. }
  99. unset($good['sku'][$key]['goods_sku_specs_json'], $good['sku'][$key]['join_sku_goods_id']);
  100. }
  101. } else {
  102. $good['sku'] = [];
  103. }
  104. }
  105. return json_success('', $goods);
  106. }
  107. public static function select(Request $request, $classify = "GOODS")
  108. {
  109. $page = $request->get('page', 1);
  110. $pageSize = $request->get('pageSize', 20);
  111. $goodsName = $request->get('goods_name', '');
  112. $categoryId = $request->get('join_goods_category_id', null);
  113. $goodsCategory = $request->get('goods_category', null);
  114. if ($categoryId != null) {
  115. $categoryPath = SysCategory::where('category_id', $categoryId)->value('category_super_path');
  116. $categoryPath .= '#' . $categoryId . '#';
  117. $categoryIds = SysCategory::where('category_super_path', 'like', $categoryPath . '%')->pluck('category_id')->toArray();
  118. if (!empty($categoryIds)) {
  119. $categoryId = $categoryIds;
  120. } else {
  121. $categoryId = [$categoryId];
  122. }
  123. }
  124. $rows = Goods::with([
  125. 'category' => function ($query) {
  126. $query->select('category_id', 'category_name');
  127. },
  128. 'running' => function ($query) {
  129. $query->select('join_running_goods_id', 'goods_running_storage');
  130. },
  131. 'supplier' => function ($query) {
  132. $query->select('supplier_id', 'supplier_name');
  133. }
  134. ])->select('goods_id', 'join_goods_category_id', 'join_goods_supplier_id', 'goods_status', 'goods_sales_price', 'goods_category', 'goods_name', 'goods_title', 'goods_cover', 'goods_sort', 'goods_attribute_json', 'goods_addtimes')
  135. ->when($goodsName != '', function ($query) use ($goodsName) {
  136. $query->where(function ($q) use ($goodsName) {
  137. $q->where('goods_name', 'like', '%' . $goodsName . '%')
  138. ->OrWhere('goods_title', 'like', '%' . $goodsName . '%');
  139. });
  140. })->when($categoryId != null, function ($query) use ($categoryId) {
  141. $query->whereIn('join_goods_category_id', $categoryId);
  142. })->when($goodsCategory != null, function ($query) use ($goodsCategory) {
  143. $query->where('goods_category', $goodsCategory);
  144. })
  145. ->when($classify != '', function ($query) use ($classify) {
  146. if ($classify == 'GOODS') {
  147. $query->whereIn('join_goods_category_id', ['6', '7', '8', '9', '10', '11', '12', '30']);
  148. } else {
  149. $query->where('goods_classify', $classify);
  150. }
  151. })
  152. ->orderBy('goods_addtimes', 'DESC')
  153. ->forPage($page, $pageSize)
  154. ->get()
  155. ->toArray();
  156. $total = Goods::when($goodsName != '', function ($query) use ($goodsName) {
  157. $query->where(function ($q) use ($goodsName) {
  158. $q->where('goods_name', 'like', '%' . $goodsName . '%')
  159. ->OrWhere('goods_title', 'like', '%' . $goodsName . '%');
  160. });
  161. })->when($categoryId != null, function ($query) use ($categoryId) {
  162. $query->whereIn('join_goods_category_id', $categoryId);
  163. })->when($goodsCategory != null, function ($query) use ($goodsCategory) {
  164. $query->where('goods_category', $goodsCategory);
  165. })->when($classify != '', function ($query) use ($classify) {
  166. if ($classify == 'GOODS') {
  167. $query->whereIn('join_goods_category_id', ['6', '7', '8', '9', '10', '11', '12', '30']);
  168. } else {
  169. $query->where('goods_classify', $classify);
  170. }
  171. })
  172. ->count();
  173. foreach ($rows as &$row) {
  174. $row['goods_cover'] = getenv('STORAGE_DOMAIN') . $row['goods_cover'];
  175. if (isset($row['running'])) {
  176. $row['running']['goods_running_storage'] = intval($row['running']['goods_running_storage']);
  177. }
  178. if (!empty($row['goods_attribute_json'])) {
  179. $row['goods_attribute_json'] = json_decode($row['goods_attribute_json']);
  180. }
  181. }
  182. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  183. }
  184. public static function selectSpecial(Request $request)
  185. {
  186. $page = $request->get('page');
  187. $pageSize = $request->get('pageSize');
  188. $goodsName = $request->get('goods_name', '');
  189. $categoryId = $request->get('join_goods_category_id', null);
  190. if ($categoryId == null) {
  191. $categoryId = [65, 43];
  192. } elseif (is_string($categoryId)) {
  193. $categoryId = [$categoryId];
  194. }
  195. $rows = Goods::with([
  196. 'category' => function ($query) {
  197. $query->select('category_id', 'category_name');
  198. },
  199. 'running' => function ($query) {
  200. $query->select('join_running_goods_id', 'goods_running_storage');
  201. },
  202. 'supplier' => function ($query) {
  203. $query->select('supplier_id', 'supplier_name');
  204. }
  205. ])->select('goods_id', 'join_goods_category_id', 'join_goods_supplier_id', 'goods_status', 'goods_sales_price', 'goods_category', 'goods_name', 'goods_title', 'goods_cover', 'goods_sort', 'goods_addtimes')
  206. ->when($goodsName != '', function ($query) use ($goodsName) {
  207. $query->where(function ($q) use ($goodsName) {
  208. $q->where('goods_name', 'like', '%' . $goodsName . '%')
  209. ->OrWhere('goods_title', 'like', '%' . $goodsName . '%');
  210. });
  211. })->whereIn('join_goods_category_id', $categoryId)
  212. ->orderBy('goods_addtimes', 'DESC')
  213. ->forPage($page, $pageSize)
  214. ->get()
  215. ->toArray();
  216. $total = Goods::when($goodsName != '', function ($query) use ($goodsName) {
  217. $query->where(function ($q) use ($goodsName) {
  218. $q->where('goods_name', 'like', '%' . $goodsName . '%')
  219. ->OrWhere('goods_title', 'like', '%' . $goodsName . '%');
  220. });
  221. })->whereIn('join_goods_category_id', $categoryId)->count();
  222. foreach ($rows as &$row) {
  223. $row['goods_cover'] = getenv('STORAGE_DOMAIN') . $row['goods_cover'];
  224. if (isset($row['running'])) {
  225. $row['running']['goods_running_storage'] = intval($row['running']['goods_running_storage']);
  226. }
  227. }
  228. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  229. }
  230. public static function selectPicking(Request $request)
  231. {
  232. $page = $request->get('page');
  233. $pageSize = $request->get('pageSize');
  234. $goodsName = $request->get('goods_name', '');
  235. $categorySuperId = $request->get('category_super_id', '');
  236. $categoryIds = $request->get('join_goods_category_id', []);
  237. if (!empty($categorySuperId) && is_array($categoryIds)) {
  238. $category = SysCategory::where('category_id', $categorySuperId)->first();
  239. if (empty($category->category_super_path)) {
  240. $category->category_super_path = '#' . $categorySuperId . '#';
  241. } else {
  242. $category->category_super_path = $category->category_super_path . '#' . $categorySuperId . '#';
  243. }
  244. $categoryIds = SysCategory::where('category_super_path', 'like', '%' . $category->category_super_path)->pluck('category_id');
  245. $categoryIds = $categoryIds ? $categoryIds->toArray() : [];
  246. $categoryIds = array_merge($categoryIds, [$categorySuperId]);
  247. } elseif (!is_array($categoryIds)) {
  248. $categoryIds = [$categoryIds];
  249. }
  250. $rows = Goods::with([
  251. 'category' => function ($query) {
  252. $query->select('category_id', 'category_name');
  253. },
  254. 'running' => function ($query) {
  255. $query->select('join_running_goods_id', 'goods_running_storage');
  256. },
  257. 'supplier' => function ($query) {
  258. $query->select('supplier_id', 'supplier_name');
  259. }
  260. ])->select('goods_id', 'join_goods_category_id', 'join_goods_supplier_id', 'goods_status', 'goods_sales_price', 'goods_category', 'goods_name', 'goods_title', 'goods_cover', 'goods_sort', 'goods_addtimes')
  261. ->when($goodsName != '', function ($query) use ($goodsName) {
  262. $query->where(function ($q) use ($goodsName) {
  263. $q->where('goods_name', 'like', '%' . $goodsName . '%')
  264. ->OrWhere('goods_title', 'like', '%' . $goodsName . '%');
  265. });
  266. })->whereIn('join_goods_category_id', $categoryIds)
  267. ->orderBy('goods_addtimes', 'DESC')
  268. ->forPage($page, $pageSize)
  269. ->get()
  270. ->toArray();
  271. $total = Goods::when($goodsName != '', function ($query) use ($goodsName) {
  272. $query->where(function ($q) use ($goodsName) {
  273. $q->where('goods_name', 'like', '%' . $goodsName . '%')
  274. ->OrWhere('goods_title', 'like', '%' . $goodsName . '%');
  275. });
  276. })->whereIn('join_goods_category_id', $categoryIds)
  277. ->count();
  278. foreach ($rows as &$row) {
  279. $row['goods_cover'] = getenv('STORAGE_DOMAIN') . $row['goods_cover'];
  280. if (isset($row['running'])) {
  281. $row['running']['goods_running_storage'] = intval($row['running']['goods_running_storage']);
  282. }
  283. }
  284. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  285. }
  286. public static function selectPackage(Request $request)
  287. {
  288. $page = $request->get('page');
  289. $pageSize = $request->get('pageSize');
  290. $goodsName = $request->get('goods_name', '');
  291. $categoryId = $request->get('join_goods_category_id', null);
  292. $rows = Goods::with([
  293. 'category' => function ($query) {
  294. $query->select('category_id', 'category_name');
  295. },
  296. 'running' => function ($query) {
  297. $query->select('join_running_goods_id', 'goods_running_storage');
  298. },
  299. 'supplier' => function ($query) {
  300. $query->select('supplier_id', 'supplier_name');
  301. },
  302. ])->select('goods_id', 'join_goods_category_id', 'join_goods_supplier_id', 'goods_status', 'goods_sales_price', 'goods_category', 'goods_name', 'goods_title', 'goods_cover', 'goods_sort', 'goods_addtimes')
  303. ->when($goodsName != '', function ($query) use ($goodsName) {
  304. $query->where(function ($q) use ($goodsName) {
  305. $q->where('goods_name', 'like', '%' . $goodsName . '%')
  306. ->OrWhere('goods_title', 'like', '%' . $goodsName . '%');
  307. });
  308. })->when($categoryId != null, function ($query) use ($categoryId) {
  309. $query->where('join_goods_category_id', $categoryId);
  310. })
  311. ->where('goods_classify', 'PACKAGE')
  312. ->orderBy('goods_addtimes', 'DESC')
  313. ->forPage($page, $pageSize)
  314. ->get()
  315. ->toArray();
  316. $total = Goods::when($goodsName != '', function ($query) use ($goodsName) {
  317. $query->where(function ($q) use ($goodsName) {
  318. $q->where('goods_name', 'like', '%' . $goodsName . '%')
  319. ->OrWhere('goods_title', 'like', '%' . $goodsName . '%');
  320. });
  321. })->when($categoryId != null, function ($query) use ($categoryId) {
  322. $query->where('join_goods_category_id', $categoryId);
  323. })->where('goods_classify', 'PACKAGE')->count();
  324. foreach ($rows as &$row) {
  325. $row['goods_cover'] = getenv('STORAGE_DOMAIN') . $row['goods_cover'];
  326. if (isset($row['running'])) {
  327. $row['running']['goods_running_storage'] = intval($row['running']['goods_running_storage']);
  328. }
  329. // if (!empty($row['component'])) {
  330. // $ids = [];
  331. // $contentList = [];
  332. // foreach ($row['component'] as $component) {
  333. // $ids[] = $component['join_component_goods_id'];
  334. // $configJson = json_decode($component['goods_component_config_json'], true);
  335. // $contentList[] = [
  336. // 'goods_name' => $configJson['goods_name'] ?? '',
  337. // 'goods_sales_price' => $component['goods_component_price'],
  338. // 'nbr' => $configJson['nbr'] ?? 0
  339. // ];
  340. // }
  341. //
  342. // $row['join_component_goods_id'] = $ids;
  343. // $row['goodsContentList'] = $contentList;
  344. // }
  345. }
  346. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  347. }
  348. /**
  349. * @Desc 下拉选择服务商品
  350. * @Author Gorden
  351. * @Date 2024/4/24 13:32
  352. *
  353. * @param Request $request
  354. * @return Response
  355. */
  356. public static function selectList(Request $request, $goodsClassify = "SERVICE")
  357. {
  358. $keywords = $request->get('keywords', '');
  359. // if (!$keywords){
  360. // return json_success('暂无数据');
  361. // }
  362. // $categoryIds = SysCategory::whereIn('category_super_id', [5, 31, 32, 42, 66, 70, 72])->pluck('category_id');
  363. $goods = Goods::with('sku')
  364. // ->whereIn('join_goods_category_id', $categoryIds)
  365. ->when($keywords != '', function ($query) use ($keywords) {
  366. $query->where('goods_name', 'like', "%" . $keywords . "%");
  367. })
  368. ->when($goodsClassify != '', function ($query) use ($goodsClassify) {
  369. $query->where('goods_classify', $goodsClassify);
  370. })
  371. ->select('goods_id', 'goods_name', 'goods_sales_price', 'join_goods_category_id','goods_attribute_json')
  372. ->get()
  373. ->toArray();
  374. foreach ($goods as &$good) {
  375. if (!empty($good['sku'])) {
  376. foreach ($good['sku'] as $key => $sku) {
  377. if (!empty($sku['goods_sku_specs_json'])) {
  378. $good['sku'][$key]['goods_sku_specs_json'] = json_decode($sku['goods_sku_specs_json']);
  379. $skuName = '';
  380. foreach ($good['sku'][$key]['goods_sku_specs_json'] as $specsKey => $skuSpecs) {
  381. if (is_array($skuSpecs)) {
  382. $skuName = $skuName . ' ' . $specsKey . ':' . implode(' ', $skuSpecs) . ';';
  383. } else {
  384. $skuName = $skuName . ' ' . $specsKey . ':' . $skuSpecs . ';';
  385. }
  386. }
  387. $good['sku'][$key]['sku_name'] = $skuName;
  388. }
  389. }
  390. } else {
  391. $good['sku'] = [];
  392. }
  393. $good['premisses'] = [];
  394. if(!empty($good['goods_attribute_json'])){
  395. $attributeJson = json_decode($good['goods_attribute_json'],true);
  396. if (isset($attributeJson['premisses'])){
  397. $premisses = SysDept::whereIn('dept_id',$attributeJson['premisses'])
  398. ->select('dept_id','dept_name')
  399. ->get();
  400. $good['premisses'] = $premisses;
  401. }
  402. }
  403. }
  404. return json_success('', $goods);
  405. }
  406. /**
  407. * @Desc 商品详情
  408. * @Author Gorden
  409. * @Date 2024/3/28 10:25
  410. *
  411. * @param $goodsId
  412. * @return Response
  413. */
  414. public static function info($goodsId)
  415. {
  416. try {
  417. // 商品主表
  418. $main = Goods::with('category')->where('goods_id', $goodsId)->first();
  419. if (!empty($main)) {
  420. $main = $main->toArray();
  421. $main['goods_sku_json'] = json_decode($main['goods_sku_json'], true);
  422. $main['specList'] = [];
  423. foreach ($main['goods_sku_json'] as $key => $sku) {
  424. $main['specList'][] = [
  425. 'label' => $key,
  426. 'tags' => $sku
  427. ];
  428. }
  429. } else {
  430. $main = [];
  431. }
  432. // 详情表
  433. $detail = GoodsDetail::where('join_detail_goods_id', $goodsId)->first();
  434. if (!empty($detail)) {
  435. $detail = $detail->toArray();
  436. } else {
  437. $detail = [];
  438. }
  439. // 标签表
  440. $label = GoodsLabel::where('join_label_goods_id', $goodsId)->first();
  441. if (!empty($label)) {
  442. $label = $label->toArray();
  443. } else {
  444. $label = [];
  445. }
  446. // Running表
  447. $running = GoodsRunning::where('join_running_goods_id', $goodsId)->first();
  448. if (!empty($running)) {
  449. $running = $running->toArray();
  450. if (!empty($running['goods_running_off_json']) && is_json($running['goods_running_off_json'])) {
  451. $goodsRunningOffJson = json_decode($running['goods_running_off_json'], true);
  452. $running['goods_off_addtimes'] = isset($goodsRunningOffJson['time']) ? date("Y-m-d H:i", $goodsRunningOffJson['time']) : '';
  453. }
  454. $running['goods_running_storage'] = !empty($running['goods_running_storage']) ? intval($running['goods_running_storage']) : '';
  455. $running['goods_running_sale'] = !empty($running['goods_running_sale']) ? intval($running['goods_running_sale']) : '';
  456. } else {
  457. $running = [];
  458. }
  459. // Sku表
  460. $skus = GoodsSku::where('join_sku_goods_id', $goodsId)->get();
  461. if (!empty($skus)) {
  462. $skus = $skus->toArray();
  463. $submitList = [];
  464. foreach ($skus as $key => $sku) {
  465. $skuSpecsJson = json_decode($sku['goods_sku_specs_json'], true);
  466. $skuSpecs = '';
  467. $skuNameValue = [];
  468. $i = 1;
  469. foreach ($skuSpecsJson as $k => $item) {
  470. if (is_array($item)) {
  471. $item = implode(',', $item);
  472. }
  473. $skuSpecs = $skuSpecs . $item . ',';
  474. $skuNameKey = 'skuName' . $i;
  475. $skuValueKey = 'skuValue' . $i;
  476. $skuNameValue[$skuNameKey] = $k;
  477. $skuNameValue[$skuValueKey] = $item;
  478. $skuNameValue[$k] = $item;
  479. $i++;
  480. }
  481. $storage = json_decode($sku['goods_sku_storage_json'], true);
  482. $priceStorage = [
  483. 'sku_id' => $sku['goods_sku_id'],
  484. 'sku' => rtrim($skuSpecs, ',') ?? '',
  485. 'stock' => $storage['storage'] ?? '',
  486. 'price' => $sku['goods_sku_sales_price'] ?? '',
  487. ];
  488. $submitList[] = array_merge($skuNameValue, $priceStorage);
  489. }
  490. $skus['submitList'] = $submitList;
  491. } else {
  492. $skus = [];
  493. }
  494. // 合并数据
  495. $data = array_merge($main, $detail, $label, $running, ['sku' => $skus]);
  496. $data['goods_sku_json_label'] = [];
  497. $data['goods_label'] = !empty($data['goods_label']) ? explode(',', $data['goods_label']) : [];
  498. $data['goods_json'] = $data['goods_json'] ? json_decode($data['goods_json'], true) : [];
  499. $data['goods_cover'] = getenv('STORAGE_DOMAIN') . $data['goods_cover'];
  500. if (!empty($data['goods_detail_slider_json'])) {
  501. $data['goods_detail_slider_json'] = json_decode($data['goods_detail_slider_json'], true);
  502. // ……
  503. if (isset($data['goods_detail_slider_json']['slider'])) {
  504. $data['goods_detail_slider_json'] = explode(',', $data['goods_detail_slider_json']['slider']);
  505. }
  506. $slider = '';
  507. foreach ($data['goods_detail_slider_json'] as $item) {
  508. $slider .= getenv('STORAGE_DOMAIN') . $item . ',';
  509. }
  510. $data['goods_detail_slider_json'] = rtrim($slider, ',');
  511. }
  512. $extendJson = [];
  513. if (!empty($data['goods_attribute_json'])) {
  514. $extendJson = json_decode($data['goods_attribute_json'], true);
  515. $data['goods_attribute_json'] = $extendJson;
  516. if (isset($extendJson['premisses'])) {
  517. $data['goods_premisses'] = $extendJson['premisses'];
  518. $data['goods_premisses_str'] = SysDept::whereIn('dept_id', $extendJson['premisses'])->pluck('dept_name');
  519. }
  520. }
  521. $data['appointment_times'] = [];
  522. if ($data['is_support_appointment'] == 'Y' && isset($extendJson['dates'])) {
  523. $data['dates'] = $extendJson['dates'] ?? [];
  524. if (isset($extendJson['times'])) {
  525. $times = [];
  526. foreach ($extendJson['times'] as $time) {
  527. if (!empty($time['duration'])) {
  528. $startEndTime = explode('-', $time['duration']);
  529. $times[] = [
  530. 'person' => $time['person'],
  531. 'appointmentTimeStart' => $startEndTime[0],
  532. 'appointmentTimeEnd' => $startEndTime[1],
  533. ];
  534. }
  535. }
  536. $data['appointment_times'] = $times;
  537. }
  538. if (isset($extendJson['address'])) {
  539. $data['address'] = $extendJson['address'];
  540. }
  541. if (isset($extendJson['position'])) {
  542. $data['position'] = $extendJson['position'];
  543. }
  544. if (isset($extendJson['label'])) {
  545. $data['appointment_label'] = $extendJson['label'];
  546. }
  547. }
  548. if (!empty($data['goods_json']) && $data['join_goods_category_id'] == 65) {
  549. foreach ($data['goods_json'] as $key => $datum) {
  550. $data['goods_json'][$key]['color'] = rgbToHex($datum['color']);
  551. }
  552. } elseif (!empty($data['goods_json']) && $data['join_goods_category_id'] == 43) {
  553. $goodsJsonNew = [];
  554. foreach ($data['goods_json'] as $key1 => $item1) {
  555. $itemsNew = [];
  556. if (isset($item1['items'])) {
  557. foreach ($item1['items'] as $key2 => $item2) {
  558. $itemsNew[] = [
  559. 'key' => $key2,
  560. 'params' => $item2
  561. ];
  562. }
  563. }
  564. $goodsJsonNew[] = [
  565. 'title' => $key1,
  566. 'service' => $item1['service'] ?? '',
  567. 'items' => $itemsNew
  568. ];
  569. }
  570. $data['goods_json'] = $goodsJsonNew;
  571. }
  572. $data['goods_on_addtimes'] = date('Y-m-d\TH:i:s.u\Z', $data['goods_on_addtimes'] - 60 * 60 * 8);
  573. return json_success('', $data);
  574. } catch (\Exception $e) {
  575. dump($e->getMessage());
  576. return json_fail("查询错误~");
  577. }
  578. }
  579. public static function infoPackage($goodsId)
  580. {
  581. try {
  582. // 商品主表
  583. $main = Goods::where('goods_id', $goodsId)->first();
  584. if (!empty($main)) {
  585. $main = $main->toArray();
  586. $main['goods_sku_json'] = json_decode($main['goods_sku_json'], true);
  587. $main['specList'] = [];
  588. foreach ($main['goods_sku_json'] as $key => $sku) {
  589. $main['specList'][] = [
  590. 'label' => $key,
  591. 'tags' => $sku
  592. ];
  593. }
  594. } else {
  595. $main = [];
  596. }
  597. // 详情表
  598. $detail = GoodsDetail::where('join_detail_goods_id', $goodsId)->first();
  599. if (!empty($detail)) {
  600. $detail = $detail->toArray();
  601. } else {
  602. $detail = [];
  603. }
  604. // 标签表
  605. $label = GoodsLabel::where('join_label_goods_id', $goodsId)->first();
  606. if (!empty($label)) {
  607. $label = $label->toArray();
  608. } else {
  609. $label = [];
  610. }
  611. // Running表
  612. $running = GoodsRunning::where('join_running_goods_id', $goodsId)->first();
  613. if (!empty($running)) {
  614. $running = $running->toArray();
  615. if (!empty($running['goods_running_off_json']) && is_json($running['goods_running_off_json'])) {
  616. $goodsRunningOffJson = json_decode($running['goods_running_off_json'], true);
  617. $running['goods_off_addtimes'] = isset($goodsRunningOffJson['time']) ? date("Y-m-d H:i", $goodsRunningOffJson['time']) : '';
  618. }
  619. } else {
  620. $running = [];
  621. }
  622. // 组件表
  623. $component = GoodsComponent::where('join_component_master_goods_id', $goodsId)->get()->toArray();
  624. $componentArr['join_component_goods_id'] = [];
  625. $componentArr['goodsContentList'] = [];
  626. if ($component) {
  627. $ids = [];
  628. $contentList = [];
  629. foreach ($component as $item) {
  630. if (empty($item['join_component_goods_id'])) {
  631. continue;
  632. }
  633. $skus = GoodsSku::where('join_sku_goods_id', $item['join_component_goods_id'])
  634. ->select('goods_sku_id', 'join_sku_goods_id', 'goods_sku_specs_json', 'goods_sku_sales_price')
  635. ->get()
  636. ->toArray();
  637. foreach ($skus as $key => $sku2) {
  638. if (!empty($sku2['goods_sku_specs_json'])) {
  639. $skus[$key]['goods_sku_specs_json'] = json_decode($sku2['goods_sku_specs_json'], true);
  640. $skuName = '';
  641. foreach ($skus[$key]['goods_sku_specs_json'] as $specsKey => $skuSpecs) {
  642. if (is_array($skuSpecs)) {
  643. $skuName = $skuName . ' ' . $specsKey . ' ' . implode(' ', $skuSpecs);
  644. } else {
  645. $skuName = $skuName . ' ' . $specsKey . ' ' . $skuSpecs;
  646. }
  647. }
  648. $skus[$key]['sku_name'] = $skuName;
  649. }
  650. }
  651. $ids[] = $item['join_component_goods_id'];
  652. $configJson = json_decode($item['goods_component_json'], true);
  653. $contentList[] = [
  654. 'goods_id' => $item['join_component_goods_id'],
  655. 'goods_name' => $configJson['goods_name'] ?? '',
  656. 'goods_sales_price' => $item['goods_component_price'],
  657. 'nbr' => $configJson['nbr'] ?? 0,
  658. 'sku_id' => $configJson['sku_id'] ?? '',
  659. 'skus' => $skus
  660. ];
  661. }
  662. $componentArr['join_component_goods_id'] = $ids;
  663. $componentArr['goodsContentList'] = $contentList;
  664. }
  665. // 合并数据
  666. $data = array_merge($main, $detail, $label, $running, $componentArr);
  667. $data['goods_sku_json_label'] = [];
  668. $data['goods_label'] = !empty($data['goods_label']) ? explode(',', $data['goods_label']) : [];
  669. $data['goods_cover'] = getenv('STORAGE_DOMAIN') . $data['goods_cover'];
  670. if (!empty($data['goods_detail_slider_json'])) {
  671. $data['goods_detail_slider_json'] = json_decode($data['goods_detail_slider_json'], true);
  672. $slider = '';
  673. foreach ($data['goods_detail_slider_json'] as $item) {
  674. $slider .= getenv('STORAGE_DOMAIN') . $item . ',';
  675. }
  676. $data['goods_detail_slider_json'] = rtrim($slider, ',');
  677. }
  678. $data['goods_on_addtimes'] = date('Y-m-d\TH:i:s.u\Z', $data['goods_on_addtimes'] - 60 * 60 * 8);
  679. return json_success('', $data);
  680. } catch (\Exception $e) {
  681. dump($e->getTrace());
  682. return json_fail("查询错误~");
  683. }
  684. }
  685. /**
  686. * @Desc 添加商品
  687. * @Author Gorden
  688. * @Date 2024/3/11 10:20
  689. *
  690. * @param $params
  691. * @return Response
  692. */
  693. public static function insert($params): Response
  694. {
  695. Db::beginTransaction();
  696. try {
  697. $params['goods_id'] = "GD" . sprintf('%016d', SysSerial::getSerial()) . random_string(6, 'up');
  698. // 主表
  699. self::mainInsert($params);
  700. // 商品详情表
  701. self::detailInsert($params);
  702. // 商品标签表
  703. self::labelInsert($params);
  704. // 产品运行控制信息表
  705. self::goodsRunningInsert($params);
  706. // sku表
  707. self::goodsSkuSet($params, 'insert');
  708. // 待上架状态,上架时间大于当前时间
  709. if ($params['goods_status'] == 'PENDING' && strtotime($params['goods_on_addtimes']) > time()) {
  710. $redis = Redis::connection();
  711. $key = date('YmdHi', strtotime($params['goods_on_addtimes']));
  712. $redis->sAdd(Goods::LISTING_KEY_PREFIX . $key, $params['goods_id']);
  713. }
  714. // 自动下架
  715. if (!empty($params['goods_running_off_type']) && $params['goods_running_off_type'] == 'T' && !empty($params['goods_off_addtimes'])) {
  716. $redis = Redis::connection();
  717. $key = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi', strtotime($params['goods_off_addtimes']));
  718. $redis->sAdd($key, $params['goods_id']);
  719. }
  720. Db::commit();
  721. } catch (\PDOException $e) {
  722. Db::rollBack();
  723. dump($e->getMessage());
  724. return json_fail('数据写入失败~');
  725. } catch (BusinessException $e) {
  726. Db::rollBack();
  727. dump($e->getMessage());
  728. return json_fail($e->getMessage());
  729. } catch (\Exception $e) {
  730. Db::rollBack();
  731. dump($e->getTrace());
  732. return json_fail('数据写入失败~');
  733. }
  734. _syslog("添加商品", "商品名【" . $params['goods_name'] . "】");
  735. return json_success('success');
  736. }
  737. public static function insertRecharge($params)
  738. {
  739. try {
  740. Db::beginTransaction();
  741. $goods = new Goods();
  742. $goods->goods_id = "GD" . date('YmdHis') . random_string(6, 'up');
  743. $goods->join_goods_category_id = 59;
  744. $goods->goods_classify = 'RECHARGE';
  745. $goods->goods_status = $params['goods_status'];
  746. $goods->goods_sort = $params['goods_sort'];
  747. $goods->goods_category = $params['goods_category'];
  748. $goods->goods_name = $params['goods_name'];
  749. $goods->goods_market_price = $params['goods_sales_price'];
  750. $goods->goods_sales_price = $params['goods_sales_price'];
  751. $goods->goods_sku_json = json_encode(['规格' => [$params['goods_sales_price'] . '元']]);
  752. $goods->goods_attribute_json = json_encode(['added' => ['nbr' => $params['goods_rate'] / 100, 'mode' => 'rate'], 'min-count' => 1]);
  753. $goods->goods_cover = '/images/app/common/null-service.png';
  754. $goods->goods_process_json = json_encode(['mode' => 'do_shopping']);
  755. $goods->goods_addtimes = time();
  756. $goods->save();
  757. $sku = new GoodsSku();
  758. $sku->join_sku_goods_id = $goods->goods_id;
  759. $sku->goods_sku_status = $params['goods_status'];
  760. $sku->goods_sku_specs_json = json_encode(['规格' => $params['goods_sales_price'] . '元']);
  761. $sku->goods_sku_market_price = $params['goods_sales_price'];
  762. $sku->goods_sku_sales_price = $params['goods_sales_price'];
  763. $sku->goods_sku_storage_json = json_encode(['storage' => 9999]);
  764. $sku->save();
  765. Db::commit();
  766. return json_success('success');
  767. } catch (\Exception $e) {
  768. Db::rollBack();
  769. return json_fail('添加充值产品失败');
  770. }
  771. }
  772. public static function updateRecharge($params)
  773. {
  774. try {
  775. Db::beginTransaction();
  776. $goods = Goods::where('goods_id', $params['goods_id'])->first();
  777. if (!$goods) {
  778. return json_fail("数据异常");
  779. }
  780. $goods->goods_market_price = $params['goods_sales_price'];
  781. $goods->goods_sales_price = $params['goods_sales_price'];
  782. $goods->goods_status = $params['goods_status'];
  783. $goods->goods_sku_json = json_encode(['规格' => [$params['goods_sales_price'] . '元']]);
  784. $goods->goods_sort = $params['goods_sort'];
  785. if (!empty($goods->goods_attribute_json)) {
  786. $attributeJson = json_decode($goods->goods_attribute_json, true);
  787. $attributeJson['added']['nbr'] = $params['goods_rate'] / 100;
  788. $goods->goods_attribute_json = json_encode($attributeJson);
  789. }
  790. $goods->save();
  791. $sku = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->first();
  792. $sku->goods_sku_status = $params['goods_status'];
  793. $sku->goods_sku_specs_json = json_encode(['规格' => $params['goods_sales_price'] . '元']);
  794. $sku->goods_sku_market_price = $params['goods_sales_price'];
  795. $sku->goods_sku_sales_price = $params['goods_sales_price'];
  796. $sku->save();
  797. Db::commit();
  798. return json_success("success");
  799. } catch (\Exception $e) {
  800. Db::rollBack();
  801. dump($e->getMessage());
  802. return json_fail('编辑充值产品失败');
  803. }
  804. }
  805. public static function insertPackage($params): Response
  806. {
  807. Db::beginTransaction();
  808. try {
  809. $params['goods_id'] = "GD" . sprintf('%016d', SysSerial::getSerial()) . random_string(6, 'up');
  810. // 主表
  811. self::mainInsert($params);
  812. // 商品详情表
  813. self::detailInsert($params);
  814. // 套包组件表
  815. self::componentUpdate($params, 'insert');
  816. // 商品标签表
  817. self::labelInsert($params);
  818. // 产品运行控制信息表
  819. self::goodsRunningInsert($params);
  820. // sku表
  821. self::goodsSkuSet($params, 'insert');
  822. // 待上架状态,上架时间大于当前时间
  823. if ($params['goods_status'] == 'PENDING' && strtotime($params['goods_on_addtimes']) > time()) {
  824. $redis = Redis::connection();
  825. $key = date('YmdHi', strtotime($params['goods_on_addtimes']));
  826. $redis->sAdd(Goods::LISTING_KEY_PREFIX . $key, $params['goods_id']);
  827. }
  828. Db::commit();
  829. } catch (\PDOException $e) {
  830. Db::rollBack();
  831. dump($e->getMessage());
  832. return json_fail('数据写入失败~');
  833. } catch (BusinessException $e) {
  834. Db::rollBack();
  835. dump($e->getMessage());
  836. return json_fail($e->getMessage());
  837. } catch (\Exception $e) {
  838. Db::rollBack();
  839. dump($e->getTrace());
  840. return json_fail('数据写入失败~');
  841. }
  842. _syslog("添加套餐", "商品名【" . $params['goods_name'] . "】");
  843. return json_success('success');
  844. }
  845. public static function update($params)
  846. {
  847. Db::beginTransaction();
  848. try {
  849. // 主表
  850. self::mainUpdate($params);
  851. // 商品详情表
  852. self::detailUpdate($params);
  853. // 商品标签表
  854. self::labelUpdate($params);
  855. // 产品运行控制信息表
  856. self::goodsRunningUpdate($params);
  857. // sku表
  858. self::goodsSkuSet($params, 'update');
  859. Db::commit();
  860. } catch (BusinessException $e) {
  861. Db::rollBack();
  862. return json_fail($e->getMessage());
  863. } catch (\Exception $e) {
  864. Db::rollBack();
  865. return json_fail('数据更新失败~');
  866. }
  867. _syslog("编辑商品", "商品名【" . $params['goods_name'] . "】" ?? "商品ID:【" . $params['goods_id'] . "】");
  868. return json_success('success');
  869. }
  870. public static function changeStatus($params)
  871. {
  872. try {
  873. Goods::where('goods_id', $params['goods_id'])->update(['goods_status' => $params['goods_status']]);
  874. return json_success('修改成功');
  875. } catch (\Exception $e) {
  876. return json_fail('修改状态失败');
  877. }
  878. }
  879. public static function updatePackage($params)
  880. {
  881. Db::beginTransaction();
  882. try {
  883. // 主表
  884. self::mainUpdate($params);
  885. // 商品详情表
  886. self::detailUpdate($params);
  887. // 套包组件表
  888. self::componentUpdate($params, 'update');
  889. // 商品标签表
  890. self::labelUpdate($params);
  891. // 产品运行控制信息表
  892. self::goodsRunningUpdate($params);
  893. Db::commit();
  894. } catch (BusinessException $e) {
  895. Db::rollBack();
  896. return json_fail($e->getMessage());
  897. } catch (\Exception $e) {
  898. Db::rollBack();
  899. dump($e->getTrace());
  900. return json_fail('数据更新失败~');
  901. }
  902. _syslog("编辑套餐", "商品名【" . $params['goods_name'] . "】" ?? "商品ID:【" . $params['goods_id'] . "】");
  903. return json_success('success');
  904. }
  905. /**
  906. * @Desc 删除商品
  907. * @Author Gorden
  908. * @Date 2024/3/28 13:20
  909. *
  910. * @param $ids
  911. * @return Response
  912. */
  913. public static function delete($ids)
  914. {
  915. if (!$ids) {
  916. return json_fail("数据错误~");
  917. }
  918. if (!is_array($ids)) {
  919. $ids = [$ids];
  920. }
  921. $goods = Goods::whereIn('goods_id', $ids)->get()->toArray();
  922. if (!$goods) {
  923. return json_fail("数据错误~");
  924. }
  925. Db::beginTransaction();
  926. try {
  927. Goods::whereIn('goods_id', $ids)->delete();
  928. GoodsDetail::whereIn('join_detail_goods_id', $ids)->delete();
  929. GoodsLabel::whereIn('join_label_goods_id', $ids)->delete();
  930. GoodsRunning::whereIn('join_running_goods_id', $ids)->delete();
  931. GoodsSku::whereIn('join_sku_goods_id', $ids)->delete();
  932. Db::commit();
  933. _syslog("删除商品 / 套餐", "ID:【" . implode(',', $ids) . "】", $goods);
  934. return json_success("商品删除成功");
  935. } catch (\Exception $e) {
  936. Db::rollBack();
  937. return json_fail("商品删除失败~");
  938. }
  939. }
  940. /**
  941. * @Desc 商品主表
  942. * @Author Gorden
  943. * @Date 2024/3/11 11:20
  944. *
  945. * @param $params
  946. * @return mixed|string
  947. * @throws BusinessException
  948. */
  949. public static function mainInsert($params)
  950. {
  951. if (!empty($params['goods_cover'])) {
  952. $params['goods_cover'] = str_replace(getenv('STORAGE_DOMAIN'), '', $params['goods_cover']);
  953. }
  954. // 如果产品是待处理状态
  955. if ($params['goods_status'] == 'PENDING') {
  956. if (strtotime($params['goods_on_addtimes']) <= time()) {
  957. $params['goods_status'] = 'ON';
  958. }
  959. }
  960. $category = SysCategory::where('category_id', $params['join_goods_category_id'])->first();
  961. if (!$category) {
  962. throw new BusinessException("产品分类不存在~");
  963. }
  964. if (empty($params['goods_category'])) {
  965. $params['goods_category'] = $category->category_classify;
  966. }
  967. try {
  968. $model = new Goods();
  969. $model->goods_id = $params['goods_id'];
  970. $model->join_goods_category_id = $params['join_goods_category_id'] ?? 0;
  971. $model->join_goods_supplier_id = $params['join_goods_supplier_id'] ?? 0;
  972. $model->goods_classify = $params['goods_classify'] ?? '';
  973. $model->goods_status = $params['goods_status'] ?? '';
  974. $model->goods_category = $params['goods_category'] ?? '';
  975. $model->goods_prefix = $params['goods_prefix'] ?? ($category->category_name ? '【' . $category->category_name . '】' : '');
  976. $model->goods_name = $params['goods_name'];
  977. $model->goods_market_price = $params['goods_market_price'] ?? 0;
  978. $model->goods_sales_price = $params['goods_sales_price'] ?? 0;
  979. $model->goods_sku_json = !empty($params['goods_sku_json_label']) ? json_encode($params['goods_sku_json_label']) : json_encode(['规格' => ['标准']]);
  980. $model->goods_attribute_json = !empty($params['goods_attribute_json']) ? $params['goods_attribute_json'] : '[]';
  981. $model->goods_title = $params['goods_title'] ?? '';
  982. $model->goods_cover = $params['goods_cover'] ?? '';
  983. $model->goods_on_addtimes = isset($params['goods_on_addtimes']) ? strtotime($params['goods_on_addtimes']) : null;
  984. $model->goods_sort = $params['goods_sort'] ?? null;
  985. $model->goods_groupby = $params['goods_groupby'] ?? '';
  986. $model->goods_remark = $params['goods_remark'] ?? '';
  987. $model->goods_extend_json = $params['goods_extend_json'] ?? '{}';
  988. $model->is_support_appointment = $params['is_support_appointment'] ?? 'N';
  989. $model->goods_addtimes = time();
  990. if (!empty($params['is_support_appointment']) && $params['is_support_appointment'] == 'Y' && !empty($params['appointment_times'])) {
  991. $times = [];
  992. $attributeJsonTimeArr = [];
  993. $personTotal = 0;
  994. foreach ($params['appointment_times'] as $time) {
  995. $attributeJsonTimeArr[] = strtotime(date('Y-m-d ') . $time['appointmentTimeStart']);
  996. $attributeJsonTimeArr[] = strtotime(date('Y-m-d ') . $time['appointmentTimeEnd']);
  997. $personTotal += $time['person'];
  998. $times[$time['appointmentTimeStart']] = [
  999. 'person' => $time['person'],
  1000. 'duration' => $time['appointmentTimeStart'] . '-' . $time['appointmentTimeEnd']
  1001. ];
  1002. }
  1003. $attributeJsonTime = date('H:i', min($attributeJsonTimeArr)) . '至' . date('H:i', max($attributeJsonTimeArr));
  1004. $newDates = [];
  1005. foreach ($params['dates'] as $date) {
  1006. $key = self::$week[$date];
  1007. $newDates[$key] = $date;
  1008. }
  1009. ksort($newDates);
  1010. $currentDate = current($newDates);
  1011. $lastDate = end($newDates);
  1012. $attributeJsonDate = '';
  1013. if (self::$week[$lastDate] - self::$week[$currentDate] + 1 > count($newDates)) {
  1014. $attributeJsonDate = implode(',', $newDates);
  1015. } else if (self::$week[$lastDate] - self::$week[$currentDate] + 1 == count($newDates)) {
  1016. $attributeJsonDate = $currentDate . '至' . $lastDate;
  1017. }
  1018. $attributeJson = [
  1019. 'icon' => '',
  1020. 'date' => $attributeJsonDate,
  1021. 'time' => $attributeJsonTime,
  1022. 'dates' => $newDates ? array_values($newDates) : [],
  1023. 'times' => $times,
  1024. 'person' => $personTotal
  1025. ];
  1026. if (!empty($params['appointment_label'])) {
  1027. $attributeJson['label'] = $params['appointment_label'];
  1028. }
  1029. if (!empty($params['address'])) {
  1030. $attributeJson['address'] = $params['address'];
  1031. $attributeJson['position'] = $params['position'];
  1032. }
  1033. $model->goods_attribute_json = json_encode($attributeJson, JSON_UNESCAPED_UNICODE);
  1034. // $times = [];
  1035. // foreach ($params['appointment_times'] as $time) {
  1036. // $times[$time['appointmentTimeStart']] = [
  1037. // 'person' => $time['person'],
  1038. // 'duration' => $time['appointmentTimeStart'] . '-' . $time['appointmentTimeEnd']
  1039. // ];
  1040. // }
  1041. // $model->goods_attribute_json = json_encode([
  1042. // 'icon' => '',
  1043. // 'dates' => $params['dates'] ?? [],
  1044. // 'times' => $times
  1045. // ]);
  1046. }
  1047. if (!empty($params['goods_json']) && $params['join_goods_category_id'] == 65) {
  1048. $goodsJson = json_decode($params['goods_json'], true);
  1049. foreach ($goodsJson as $key => $item) {
  1050. $goodsJson[$key]['color'] = hexToRgb($item['color']);
  1051. }
  1052. $model->goods_json = json_encode($goodsJson);
  1053. } elseif (!empty($params['goods_json']) && $params['join_goods_category_id'] == 43) {
  1054. $goodsJson = json_decode($params['goods_json'], true);
  1055. $newGoodsJson = [];
  1056. foreach ($goodsJson as $item1) {
  1057. if (empty($item1['title'])) {
  1058. continue;
  1059. }
  1060. $newItem1 = [];
  1061. foreach ($item1['items'] as $item2) {
  1062. $newParams = [];
  1063. foreach ($item2['params'] as $param) {
  1064. if (!empty($param[0]) || !empty($param[1])) {
  1065. $newParams[] = $param;
  1066. }
  1067. }
  1068. $newItem1['items'][$item2['key']] = $newParams;
  1069. }
  1070. $newItem1['service'] = $item1['service'] ?? '';
  1071. $newGoodsJson[$item1['title']] = $newItem1;
  1072. }
  1073. $model->goods_json = json_encode($newGoodsJson);
  1074. } else {
  1075. $model->goods_json = '[]';
  1076. }
  1077. if (!empty($params['goods_premisses'])) {
  1078. $attributeJson = [];
  1079. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  1080. $attributeJson = json_decode($model->goods_attribute_json, true);
  1081. } elseif (empty($model->goods_attribute_json)) {
  1082. $attributeJson = [];
  1083. }
  1084. $attributeJson['premisses'] = $params['goods_premisses'];
  1085. $model->goods_attribute_json = json_encode($attributeJson);
  1086. }
  1087. if ($model->save()) {
  1088. return $model->goods_id;
  1089. }
  1090. // 异常
  1091. throw new BusinessException("数据写入失败~");
  1092. } catch (\Exception $e) {
  1093. dump($e->getMessage());
  1094. throw new BusinessException("数据写入失败~");
  1095. }
  1096. }
  1097. /**
  1098. * @Desc 详情表
  1099. * @Author Gorden
  1100. * @Date 2024/3/11 11:19
  1101. *
  1102. * @param $params
  1103. * @return void
  1104. * @throws BusinessException
  1105. */
  1106. public static function detailInsert($params)
  1107. {
  1108. if (!empty($params['goods_detail_slider_json'])) {
  1109. $params['goods_detail_slider_json'] = str_replace(getenv('STORAGE_DOMAIN'), '', $params['goods_detail_slider_json']);
  1110. $params['goods_detail_slider_json'] = json_encode(['slider' => $params['goods_detail_slider_json']]);
  1111. }
  1112. try {
  1113. $model = new GoodsDetail();
  1114. $model->join_detail_goods_id = $params['goods_id'];
  1115. $model->goods_detail_code_json = $params['goods_detail_code_json'] ?? '{}';
  1116. $model->goods_detail_slider_json = $params['goods_detail_slider_json'] ?? '{}';
  1117. $model->goods_detail_specs_json = $params['goods_detail_specs_json'] ?? '{}';
  1118. $model->goods_detail_content = $params['goods_detail_content'] ?? '';
  1119. if (!$model->save()) {
  1120. // 异常
  1121. throw new BusinessException("数据写入失败~");
  1122. }
  1123. } catch (\Exception $e) {
  1124. dump($e->getMessage());
  1125. throw new BusinessException("数据写入失败~");
  1126. }
  1127. }
  1128. public static function componentUpdate($params, $type = 'insert')
  1129. {
  1130. Db::beginTransaction();
  1131. try {
  1132. // 有先删除
  1133. if ($type == 'update') {
  1134. GoodsComponent::where('join_component_master_goods_id', $params['goods_id'])->delete();
  1135. } else {
  1136. Goods::where('goods_id', $params['goods_id'])->update(['goods_sku_json' => '{"规格": ["标准"]}']);
  1137. $skuData = [
  1138. 'join_sku_goods_id' => $params['goods_id'],
  1139. 'goods_sku_status' => 'ON',
  1140. 'goods_sku_specs_json' => '{"规格": "标准"}',
  1141. 'goods_sku_title' => "标准" . $params['goods_name'],
  1142. 'goods_sku_market_price' => $params['goods_market_price'] ?? 0,
  1143. 'goods_sku_sales_price' => $params['goods_sales_price'] ?? 0,
  1144. ];
  1145. GoodsSku::insert($skuData);
  1146. }
  1147. $data = [];
  1148. foreach ($params['goods_content_list'] as $item) {
  1149. $goods = Goods::where('goods_id', $params['goods_id'])->first();
  1150. if (!$goods) {
  1151. continue;
  1152. }
  1153. $data[] = [
  1154. 'join_component_master_goods_id' => $params['goods_id'],
  1155. 'join_component_goods_id' => $item['goods_id'] ?? '',
  1156. 'goods_component_price' => $item['goods_sales_price'] ?? '',
  1157. 'goods_component_cover' => $goods->goods_cover,
  1158. 'goods_component_json' => json_encode(['goods_name' => $item['goods_name'], 'nbr' => $item['nbr'], 'sku_id' => $item['sku_id'] ?? '']),
  1159. 'goods_component_addtimes' => time()
  1160. ];
  1161. }
  1162. if ($data) {
  1163. GoodsComponent::insert($data);
  1164. }
  1165. Db::commit();
  1166. } catch (\Exception $e) {
  1167. Db::rollBack();
  1168. dump($e->getMessage());
  1169. throw new BusinessException("数据写入失败~");
  1170. }
  1171. }
  1172. /**
  1173. * @Desc 标签表
  1174. * @Author Gorden
  1175. * @Date 2024/3/11 11:32
  1176. *
  1177. * @param $params
  1178. * @return void
  1179. * @throws BusinessException
  1180. */
  1181. public static function labelInsert($params)
  1182. {
  1183. $model = new GoodsLabel();
  1184. $model->join_label_goods_id = $params['goods_id'];
  1185. $model->goods_label = $params['goods_label'] ? implode(',', $params['goods_label']) : '';
  1186. $model->goods_label_extend_json = !empty($params['goods_label_extend_json']) ? $params['goods_label_extend_json'] : '{}';
  1187. if (!$model->save()) {
  1188. // 异常
  1189. throw new BusinessException('数据写入失败~');
  1190. }
  1191. }
  1192. /**
  1193. * @Desc 产品运行控制信息表
  1194. * @Author Gorden
  1195. * @Date 2024/3/11 11:38
  1196. *
  1197. * @param $params
  1198. * @return void
  1199. * @throws BusinessException
  1200. */
  1201. public static function goodsRunningInsert($params)
  1202. {
  1203. try {
  1204. $model = new GoodsRunning();
  1205. $model->join_running_goods_id = $params['goods_id'];
  1206. $model->goods_running_storage = $params['goods_running_storage'] ?? 0;
  1207. $model->goods_running_sale = $params['goods_running_sale'] ?? 0;
  1208. $model->goods_running_off_type = !empty($params['goods_running_off_type']) ? $params['goods_running_off_type'] : '';
  1209. $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'])]) : '[]';
  1210. if (!$model->save()) {
  1211. throw new BusinessException('数据写入失败');
  1212. }
  1213. } catch (\Exception $e) {
  1214. dump($e->getMessage());
  1215. throw new BusinessException('数据写入失败');
  1216. }
  1217. }
  1218. /**
  1219. * @Desc SKU
  1220. * @Author Gorden
  1221. * @Date 2024/3/11 12:01
  1222. *
  1223. * @param $params
  1224. * @return void
  1225. * @throws BusinessException
  1226. */
  1227. public static function skuInsert($params)
  1228. {
  1229. $model = new GoodsSku();
  1230. $model->join_sku_goods_id = $params['goods_id'];
  1231. $model->goods_sku_status = $params['goods_sku_status'];
  1232. $model->goods_sku_specs_json = $params['goods_sku_specs_json'];
  1233. $model->goods_sku_title = $params['goods_sku_title'];
  1234. $model->goods_sku_images_json = $params['goods_sku_images_json'];
  1235. $model->goods_sku_content = $params['goods_sku_content'];
  1236. $model->goods_sku_market_price = $params['goods_sku_market_price'];
  1237. $model->goods_sku_sales_price = $params['goods_sku_sales_price'];
  1238. $model->goods_sku_storage_json = $params['goods_sku_storage_json'];
  1239. $model->goods_sku_service_json = $params['goods_sku_service_json'];
  1240. $model->goods_sku_extend_json = $params['goods_sku_extend_json'];
  1241. if (!$model->save()) {
  1242. throw new BusinessException('数据写入失败~');
  1243. }
  1244. }
  1245. /**
  1246. * @Desc
  1247. * @Author Gorden
  1248. * @Date 2024/3/12 8:44
  1249. *
  1250. * @param $params
  1251. * @return void
  1252. * @throws BusinessException
  1253. */
  1254. public static function mainUpdate($params)
  1255. {
  1256. try {
  1257. $data = self::inputFilter($params, new Goods());
  1258. if (!empty($data['goods_cover'])) {
  1259. $data['goods_cover'] = str_replace(getenv('STORAGE_DOMAIN'), '', $data['goods_cover']);
  1260. }
  1261. $data['goods_on_addtimes'] = isset($data['goods_on_addtimes']) ? strtotime($data['goods_on_addtimes']) : 0;
  1262. $data['goods_sku_json'] = !empty($params['goods_sku_json_label']) ? json_encode($params['goods_sku_json_label']) : json_encode(['规格' => []]);
  1263. $row = Goods::find($data['goods_id']);
  1264. if ($row->join_goods_category_id != $data['join_goods_category_id']) {
  1265. $category = SysCategory::where('category_id', $params['join_goods_category_id'])->first();
  1266. if (!$category) {
  1267. throw new BusinessException("产品分类不存在~");
  1268. }
  1269. $data['goods_category'] = $category->category_classify ?? '';
  1270. $data['goods_prefix'] = $data['goods_prefix'] ?? ($category->category_name ? '【' . $category->category_name . '】' : '');
  1271. }
  1272. // 上架时间有变动
  1273. if ($data['goods_status'] == 'PENDING' && $row->goods_on_addtimes != $data['goods_on_addtimes']) {
  1274. $redis = Redis::connection();
  1275. // 删掉原来的
  1276. $oldKey = Goods::LISTING_KEY_PREFIX . date('YmdHi', $row->goods_on_addtimes);
  1277. $redis->srem($oldKey, $data['goods_id']);
  1278. // 加入新的
  1279. $newKey = Goods::LISTING_KEY_PREFIX . date('YmdHi', $data['goods_on_addtimes']);
  1280. $redis->sadd($newKey, $data['goods_id']);
  1281. }
  1282. if (!empty($params['is_support_appointment']) && $params['is_support_appointment'] == 'Y' && !empty($params['appointment_times'])) {
  1283. $times = [];
  1284. $attributeJsonTimeArr = [];
  1285. $personTotal = 0;
  1286. foreach ($params['appointment_times'] as $time) {
  1287. $attributeJsonTimeArr[] = strtotime(date('Y-m-d ') . $time['appointmentTimeStart']);
  1288. $attributeJsonTimeArr[] = strtotime(date('Y-m-d ') . $time['appointmentTimeEnd']);
  1289. $personTotal += $time['person'];
  1290. $times[$time['appointmentTimeStart']] = [
  1291. 'person' => $time['person'],
  1292. 'duration' => $time['appointmentTimeStart'] . '-' . $time['appointmentTimeEnd']
  1293. ];
  1294. }
  1295. $attributeJsonTime = date('H:i', min($attributeJsonTimeArr)) . '至' . date('H:i', max($attributeJsonTimeArr));
  1296. $newDates = [];
  1297. foreach ($params['dates'] as $date) {
  1298. $key = self::$week[$date];
  1299. $newDates[$key] = $date;
  1300. }
  1301. ksort($newDates);
  1302. $currentDate = current($newDates);
  1303. $lastDate = end($newDates);
  1304. $attributeJsonDate = '';
  1305. if (self::$week[$lastDate] - self::$week[$currentDate] + 1 > count($newDates)) {
  1306. $attributeJsonDate = implode(',', $newDates);
  1307. } else if (self::$week[$lastDate] - self::$week[$currentDate] + 1 == count($newDates)) {
  1308. $attributeJsonDate = $currentDate . '至' . $lastDate;
  1309. }
  1310. $attributeJson = [
  1311. 'icon' => '',
  1312. 'date' => $attributeJsonDate,
  1313. 'time' => $attributeJsonTime,
  1314. 'dates' => $newDates ? array_values($newDates) : [],
  1315. 'times' => $times,
  1316. 'person' => $personTotal
  1317. ];
  1318. if (!empty($params['appointment_label'])) {
  1319. $attributeJson['label'] = $params['appointment_label'];
  1320. }
  1321. if (!empty($params['address'])) {
  1322. $attributeJson['address'] = $params['address'];
  1323. $attributeJson['position'] = $params['position'];
  1324. }
  1325. $data['goods_attribute_json'] = json_encode($attributeJson, JSON_UNESCAPED_UNICODE);
  1326. }
  1327. if (!empty($params['goods_premisses'])) {
  1328. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  1329. if (is_json($data['goods_attribute_json'])) {
  1330. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  1331. } else {
  1332. $data['goods_attribute_json'] = [];
  1333. }
  1334. } elseif (empty($data['goods_attribute_json'])) {
  1335. $data['goods_attribute_json'] = [];
  1336. }
  1337. $data['goods_attribute_json']['premisses'] = $params['goods_premisses'];
  1338. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  1339. }
  1340. if (!empty($data['goods_json']) && $data['join_goods_category_id'] == 65) {
  1341. $goodsJson = json_decode($data['goods_json'], true);
  1342. foreach ($goodsJson as $key => $item) {
  1343. $goodsJson[$key]['color'] = hexToRgb($item['color']);
  1344. }
  1345. $data['goods_json'] = json_encode($goodsJson);
  1346. } elseif (!empty($data['goods_json']) && $data['join_goods_category_id'] == 43) {
  1347. $goodsJson = json_decode($data['goods_json'], true);
  1348. $newGoodsJson = [];
  1349. foreach ($goodsJson as $item1) {
  1350. if (empty($item1['title'])) {
  1351. continue;
  1352. }
  1353. $newItem1 = [];
  1354. foreach ($item1['items'] as $item2) {
  1355. $newParams = [];
  1356. foreach ($item2['params'] as $param) {
  1357. if (!empty($param[0]) || !empty($param[1])) {
  1358. $newParams[] = $param;
  1359. }
  1360. }
  1361. $newItem1['items'][$item2['key']] = $newParams;
  1362. }
  1363. $newItem1['service'] = $item1['service'];
  1364. $newGoodsJson[$item1['title']] = $newItem1;
  1365. }
  1366. $data['goods_json'] = json_encode($newGoodsJson);
  1367. } else {
  1368. $data['goods_json'] = '[]';
  1369. }
  1370. foreach ($data as $key => $val) {
  1371. $row->{$key} = $val;
  1372. }
  1373. $row->save();
  1374. } catch (BusinessException $e) {
  1375. throw new BusinessException($e->getMessage());
  1376. } catch (\Exception $e) {
  1377. dump($e->getTrace());
  1378. throw new BusinessException('数据更新异常~1');
  1379. }
  1380. }
  1381. /**
  1382. * @Desc
  1383. * @Author Gorden
  1384. * @Date 2024/3/12 9:57
  1385. *
  1386. * @param $params
  1387. * @return void
  1388. * @throws BusinessException
  1389. */
  1390. public static function detailUpdate($params)
  1391. {
  1392. try {
  1393. $data = self::inputFilter($params, new GoodsDetail());
  1394. if (!empty($data['goods_detail_slider_json'])) {
  1395. $data['goods_detail_slider_json'] = str_replace(getenv('STORAGE_DOMAIN'), '', $data['goods_detail_slider_json']);
  1396. $data['goods_detail_slider_json'] = json_encode(['slider' => $data['goods_detail_slider_json']]);
  1397. }
  1398. // 根据goods_id 查详情ID
  1399. $detail = GoodsDetail::where('join_detail_goods_id', $params['goods_id'])->first();
  1400. if ($detail) {
  1401. self::doUpdate($detail->join_detail_goods_id, $data, new GoodsDetail());
  1402. } else {
  1403. $data['join_detail_goods_id'] = $params['goods_id'];
  1404. GoodsDetail::insert($data);
  1405. }
  1406. } catch (BusinessException $e) {
  1407. throw new BusinessException($e->getMessage());
  1408. } catch (\Exception $e) {
  1409. dump($e->getMessage());
  1410. throw new BusinessException('数据更新异常~2');
  1411. }
  1412. }
  1413. /**
  1414. * @Desc
  1415. * @Author Gorden
  1416. * @Date 2024/3/12 9:58
  1417. *
  1418. * @param $params
  1419. * @return void
  1420. * @throws BusinessException
  1421. */
  1422. public static function labelUpdate($params)
  1423. {
  1424. try {
  1425. $data = self::inputFilter($params, new GoodsLabel());
  1426. // 根据goods_id 查详情ID
  1427. $detail = GoodsLabel::where('join_label_goods_id', $params['goods_id'])->first();
  1428. if ($detail) {
  1429. self::doUpdate($detail->goods_label_id, $data, new GoodsLabel());
  1430. } else {
  1431. $data['join_label_goods_id'] = $params['goods_id'];
  1432. GoodsLabel::insert($data);
  1433. }
  1434. } catch (BusinessException $e) {
  1435. throw new BusinessException($e->getMessage());
  1436. } catch (\Exception $e) {
  1437. dump($e->getMessage());
  1438. throw new BusinessException('数据更新异常~3');
  1439. }
  1440. }
  1441. /**
  1442. * @Desc
  1443. * @Author Gorden
  1444. * @Date 2024/3/12 9:59
  1445. *
  1446. * @param $params
  1447. * @return void
  1448. * @throws BusinessException
  1449. */
  1450. public static function goodsRunningUpdate($params)
  1451. {
  1452. try {
  1453. $data = self::inputFilter($params, new GoodsRunning());
  1454. // 根据goods_id 查详情ID
  1455. $detail = GoodsRunning::where('join_running_goods_id', $params['goods_id'])->first();
  1456. if (!empty($params['goods_running_off_type']) && $params['goods_running_off_type'] == 'T') {
  1457. $redis = Redis::connection();
  1458. if (!empty($detail->goods_running_off_json)) {
  1459. $goodsRunningOffJson = json_decode($detail->goods_running_off_json, true);
  1460. if (isset($goodsRunningOffJson['time'])) {
  1461. $oldKey = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi', $goodsRunningOffJson['time']);
  1462. $goodsRunningOffJson['time'] = strtotime($params['goods_off_addtimes']);
  1463. $data['goods_running_off_json'] = json_encode($goodsRunningOffJson);
  1464. // 有老的下架时间,删除老的
  1465. $redis->srem($oldKey, $params['goods_id']);
  1466. } else {
  1467. $goodsRunningOffJson['time'] = strtotime($params['goods_off_addtimes']);
  1468. $data['goods_running_off_json'] = json_encode($goodsRunningOffJson);
  1469. }
  1470. } else {
  1471. $data['goods_running_off_json'] = json_encode(['time' => strtotime($params['goods_off_addtimes'])]);
  1472. }
  1473. // 加入自动下架
  1474. $newKey = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi', strtotime($params['goods_off_addtimes']));
  1475. $redis->sAdd($newKey, $params['goods_id']);
  1476. } 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') {
  1477. $goodsRunningOffJson = json_decode($detail->goods_running_off_json, true);
  1478. if (isset($goodsRunningOffJson['time'])) {
  1479. $oldKey = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi', $goodsRunningOffJson['time']);
  1480. $redis = Redis::connection();
  1481. $redis->srem($oldKey, $params['goods_id']);
  1482. }
  1483. }
  1484. if ($detail) {
  1485. self::doUpdate($detail->join_running_goods_id, $data, new GoodsRunning());
  1486. } else {
  1487. // 兼容老数据……
  1488. $data['join_running_goods_id'] = $params['goods_id'];
  1489. GoodsRunning::insert($data);
  1490. }
  1491. } catch (BusinessException $e) {
  1492. throw new BusinessException($e->getMessage());
  1493. } catch (\Exception $e) {
  1494. dump($e->getMessage());
  1495. throw new BusinessException('数据更新异常~4');
  1496. }
  1497. }
  1498. /**
  1499. * @Desc sku 设置
  1500. * @Author Gorden
  1501. * @Date 2024/4/10 10:43
  1502. *
  1503. * @param $params
  1504. * @return void
  1505. * @throws BusinessException
  1506. */
  1507. public static function goodsSkuSet($params, $operation = 'insert')
  1508. {
  1509. try {
  1510. Db::beginTransaction();
  1511. $skusOldIds = [];
  1512. if ($operation == 'update') {
  1513. // 查出所有的
  1514. $skusOldIds = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->pluck('goods_sku_id', 'goods_sku_id');
  1515. // 删掉原有的
  1516. // GoodsSku::where('join_sku_goods_id', $params['goods_id'])->delete();
  1517. }
  1518. if (empty($skusOldIds) && empty($params['goods_sku_json_value'])) {
  1519. $skuData = [
  1520. 'join_sku_goods_id' => $params['goods_id'],
  1521. 'goods_sku_status' => 'ON',
  1522. 'goods_sku_specs_json' => '{"规格": "标准"}',
  1523. 'goods_sku_title' => "标准" . $params['goods_name'],
  1524. 'goods_sku_market_price' => $params['goods_market_price'] ?? 0,
  1525. 'goods_sku_sales_price' => $params['goods_sales_price'] ?? 0,
  1526. ];
  1527. GoodsSku::insert($skuData);
  1528. }
  1529. // 入新的
  1530. if (!empty($params['goods_sku_json_value'])) {
  1531. foreach ($params['goods_sku_json_value'] as $item) {
  1532. $skus = explode(',', $item['sku']);
  1533. $skuArr = [];
  1534. for ($i = 1; $i <= count($skus); $i++) {
  1535. $skuName = "skuName" . $i;
  1536. $key = $item[$skuName];
  1537. $skuArr[$key] = $skus[$i - 1];
  1538. }
  1539. $specsJson = json_encode($skuArr);
  1540. $skuTitle = str_replace('-', ',', $item['sku']) . $params['goods_name'];
  1541. if ($operation == 'update' && !empty($item['sku_id'])) {
  1542. $model = GoodsSku::where('goods_sku_id', $item['sku_id'])->first();
  1543. if (!$model) {
  1544. $model = new GoodsSku();
  1545. } else {
  1546. unset($skusOldIds[$model->goods_sku_id]);
  1547. }
  1548. } else {
  1549. $model = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->where('goods_sku_title', $skuTitle)->first();
  1550. if (!$model) {
  1551. $model = new GoodsSku();
  1552. } else {
  1553. unset($skusOldIds[$model->goods_sku_id]);
  1554. }
  1555. }
  1556. $model->join_sku_goods_id = $params['goods_id'];
  1557. $model->goods_sku_status = $params['goods_status'];
  1558. $model->goods_sku_specs_json = $specsJson;
  1559. $model->goods_sku_title = $skuTitle;
  1560. $model->goods_sku_market_price = $params['goods_market_price'] ?? 0;
  1561. $model->goods_sku_sales_price = $item['price'];
  1562. $model->goods_sku_storage_json = json_encode(['storage' => $item['stock']]);
  1563. $model->save();
  1564. }
  1565. }
  1566. if ($operation == 'update' && !empty($skusOldIds)) {
  1567. GoodsSku::whereIn('goods_sku_id', $skusOldIds)->delete();
  1568. }
  1569. Db::commit();
  1570. } catch (\Exception $e) {
  1571. dump($e->getTrace());
  1572. Db::rollBack();
  1573. throw new BusinessException('数据更新异常~5');
  1574. }
  1575. }
  1576. /**
  1577. * @Desc
  1578. * @Author Gorden
  1579. * @Date 2024/3/12 8:45
  1580. *
  1581. * @param array $data
  1582. * @param $model
  1583. * @return array
  1584. * @throws BusinessException
  1585. */
  1586. private static function inputFilter(array $data, $model): array
  1587. {
  1588. $table = config('database.connections.mysql.prefix') . $model->getTable();
  1589. $allow_column = $model->getConnection()->select("desc `$table`");
  1590. if (!$allow_column) {
  1591. throw new BusinessException('表不存在', 2);
  1592. }
  1593. $columns = array_column($allow_column, 'Type', 'Field');
  1594. foreach ($data as $col => $item) {
  1595. if (!isset($columns[$col])) {
  1596. unset($data[$col]);
  1597. continue;
  1598. }
  1599. // 非字符串类型传空则为null
  1600. if ($item === '' && strpos(strtolower($columns[$col]), 'varchar') === false && strpos(strtolower($columns[$col]), 'text') === false) {
  1601. $data[$col] = null;
  1602. }
  1603. if (is_array($item)) {
  1604. $data[$col] = implode(',', $item);
  1605. }
  1606. if ($item != '' && (strpos(strtolower($columns[$col]), 'varchar') || strpos(strtolower($columns[$col]), 'text'))) {
  1607. $data[$col] = htmlspecialchars($item);
  1608. }
  1609. }
  1610. if (empty($data['created_at'])) {
  1611. unset($data['created_at']);
  1612. }
  1613. if (empty($data['updated_at'])) {
  1614. unset($data['updated_at']);
  1615. }
  1616. return $data;
  1617. }
  1618. /**
  1619. * @Desc 执行更新
  1620. * @Author Gorden
  1621. * @Date 2024/3/12 8:43
  1622. *
  1623. * @param $id
  1624. * @param $data
  1625. * @param $model
  1626. * @return void
  1627. */
  1628. private static function doUpdate($id, $data, $model)
  1629. {
  1630. $row = $model->find($id);
  1631. foreach ($data as $key => $val) {
  1632. $row->{$key} = $val;
  1633. }
  1634. $row->save();
  1635. }
  1636. /**
  1637. * @Desc 上架定时
  1638. * @Author Gorden
  1639. * @Date 2024/4/11 15:13
  1640. *
  1641. * @return void
  1642. */
  1643. public static function checkListing()
  1644. {
  1645. $key = Goods::LISTING_KEY_PREFIX . date('YmdHi');
  1646. $redis = Redis::connection();
  1647. if (!$redis->exists($key)) {
  1648. return;
  1649. }
  1650. $goodsIds = $redis->sMembers($key);
  1651. if (Goods::whereIn('goods_id', $goodsIds)->update(['goods_status' => 'ON'])) {
  1652. $redis->del($key);
  1653. }
  1654. }
  1655. /**
  1656. * @Desc 自动下架
  1657. * @Author Gorden
  1658. * @Date 2024/4/26 15:26
  1659. *
  1660. * @return void
  1661. */
  1662. public static function checkOffListing()
  1663. {
  1664. $key = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi');
  1665. $redis = Redis::connection();
  1666. if (!$redis->exists($key)) {
  1667. return;
  1668. }
  1669. $goodsIds = $redis->sMembers($key);
  1670. if (Goods::whereIn('goods_id', $goodsIds)->update(['goods_status' => 'OFF'])) {
  1671. $redis->del($key);
  1672. }
  1673. }
  1674. public static $week = [
  1675. '周一' => 1,
  1676. '周二' => 2,
  1677. '周三' => 3,
  1678. '周四' => 4,
  1679. '周五' => 5,
  1680. '周六' => 6,
  1681. '周日' => 7,
  1682. ];
  1683. }