GoodsService.php 73 KB

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