GoodsService.php 74 KB

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