GoodsService.php 77 KB

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