GoodsService.php 79 KB

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