GoodsService.php 76 KB

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