GoodsService.php 80 KB

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