GoodsService.php 77 KB

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