GoodsService.php 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435
  1. <?php
  2. namespace app\admin\service\goods;
  3. use app\common\Tree;
  4. use app\model\Appointment;
  5. use app\model\Goods;
  6. use app\model\GoodsComponent;
  7. use app\model\GoodsDetail;
  8. use app\model\GoodsLabel;
  9. use app\model\GoodsRunning;
  10. use app\model\GoodsSku;
  11. use app\model\OrderSheet;
  12. use app\model\SysCategory;
  13. use app\model\SysDept;
  14. use app\model\SysSerial;
  15. use app\model\SysUser;
  16. use support\Db;
  17. use support\exception\BusinessException;
  18. use support\Redis;
  19. use support\Request;
  20. use support\Response;
  21. use Tinywan\Jwt\JwtToken;
  22. class GoodsService
  23. {
  24. public static function selectAll($goodsIds)
  25. {
  26. $goods = Goods::where('goods_status', 'ON')
  27. ->when($goodsIds != '', function ($query) use ($goodsIds) {
  28. $query->whereIn('goods_id', $goodsIds);
  29. })
  30. ->select('goods_id', 'goods_name')
  31. ->get();
  32. return json_success('', $goods);
  33. }
  34. public static function selectAllByGoodsName($goodsName)
  35. {
  36. $goods = Goods::with('sku')
  37. ->where('goods_status', 'ON')
  38. ->when($goodsName != '', function ($query) use ($goodsName) {
  39. $query->where('goods_name', 'like', "%" . $goodsName . '%');
  40. })
  41. ->select('goods_id', 'goods_name', 'goods_classify')
  42. ->get()
  43. ->toArray();
  44. foreach ($goods as &$item) {
  45. if (!empty($item['sku'])) {
  46. foreach ($item['sku'] as $key => $sku) {
  47. $specsJson = json_decode($sku['goods_sku_specs_json'], true);
  48. $skuTitle = '';
  49. foreach ($specsJson as $item2) {
  50. if (is_array($item2)) {
  51. $item2 = implode(',', $item2);
  52. }
  53. $skuTitle .= $item2 . '-';
  54. }
  55. $item['sku'][$key]['goods_sku_id'] = strval($item['sku'][$key]['goods_sku_id']);
  56. $item['sku'][$key]['goods_sku_title'] = rtrim($skuTitle, '-');
  57. }
  58. }
  59. }
  60. return json_success('', $goods);
  61. }
  62. public static function selectAllByCategoryForRuleAddComponent($category = "GOODS")
  63. {
  64. $categoryIds = [];
  65. $categorySuperIds = [];
  66. if ($category == 'GOODS') {
  67. $categorySuperIds = [5];
  68. } elseif ($category == 'SERVICE') {
  69. $categorySuperIds = [31, 154, 42, 65, 30, 66, 72, 70];
  70. }
  71. $categorys = SysCategory::whereIn('category_id', $categorySuperIds)->get()->toArray();
  72. foreach ($categorys as $item) {
  73. if (empty($item['category_super_path'])) {
  74. $item['category_super_path'] = '#' . $item['category_id'] . '#';
  75. } else {
  76. $item['category_super_path'] = $item['category_super_path'] . '#' . $item['category_id'] . '#';
  77. }
  78. $categoryIds = SysCategory::where('category_super_path', 'like', '%' . $item['category_super_path'] . '%')->pluck('category_id');
  79. $categoryIds = $categoryIds ? $categoryIds->toArray() : [];
  80. $categorySuperIds = array_merge($categorySuperIds, $categoryIds);
  81. }
  82. $categoryIds = array_unique($categorySuperIds);
  83. $goods = Goods::with('sku')
  84. // ->where('goods_classify', $category)
  85. ->when(!empty($categoryIds), function ($query) use ($categoryIds) {
  86. $query->whereIn('join_goods_category_id', $categoryIds);
  87. })->when(empty($categoryIds), function ($query) {
  88. $query->where('goods_classify', '<>', 'RECHARGE');
  89. })->where('goods_status', 'ON')
  90. ->select('goods_id', 'goods_name', 'join_goods_category_id')
  91. ->get()
  92. ->toArray();
  93. foreach ($goods as &$good) {
  94. if (!empty($good['sku'])) {
  95. foreach ($good['sku'] as $key => $sku) {
  96. if (!empty($sku['goods_sku_specs_json'])) {
  97. $specsJson = json_decode($sku['goods_sku_specs_json'], true);
  98. $skuTitle = '';
  99. foreach ($specsJson as $item) {
  100. if (is_array($item)) {
  101. $item = implode(',', $item);
  102. }
  103. $skuTitle .= $item . '-';
  104. }
  105. $good['sku'][$key]['goods_sku_id'] = strval($good['sku'][$key]['goods_sku_id']);
  106. $good['sku'][$key]['goods_sku_title'] = rtrim($skuTitle, '-');
  107. }
  108. unset($good['sku'][$key]['goods_sku_specs_json'], $good['sku'][$key]['join_sku_goods_id']);
  109. }
  110. } else {
  111. $good['sku'] = [];
  112. }
  113. }
  114. return json_success('', $goods);
  115. }
  116. public static function selectPremisesByGoodsId($goodsId)
  117. {
  118. $goods = Goods::where('goods_id', $goodsId)
  119. ->select('goods_id', 'goods_attribute_json')
  120. ->first();
  121. $premisses = [];
  122. if (!empty($goods->goods_attribute_json)) {
  123. $attributeJson = json_decode($goods->goods_attribute_json, true);
  124. if (isset($attributeJson['premisses'])) {
  125. $premisses = SysDept::whereIn('dept_id', $attributeJson['premisses'])
  126. ->select('dept_id', 'dept_name')
  127. ->get()
  128. ->toArray();
  129. }
  130. }
  131. return json_success('', $premisses);
  132. }
  133. public static function select(Request $request, $classify = "GOODS")
  134. {
  135. $page = $request->get('page', 1);
  136. $pageSize = $request->get('pageSize', 20);
  137. $goodsName = $request->get('goods_name', '');
  138. $categoryId = $request->get('join_goods_category_id', null);
  139. $goodsCategory = $request->get('goods_category', null);
  140. if ($categoryId != null) {
  141. $categoryPath = SysCategory::where('category_id', $categoryId)->value('category_super_path');
  142. $categoryPath .= '#' . $categoryId . '#';
  143. $categoryIds = SysCategory::where('category_super_path', 'like', $categoryPath . '%')->pluck('category_id')->toArray();
  144. $categoryIds[] = intval($categoryId);
  145. if (!empty($categoryIds)) {
  146. $categoryId = $categoryIds;
  147. } else {
  148. $categoryId = [intval($categoryId)];
  149. }
  150. }
  151. $rows = Goods::with([
  152. 'category' => function ($query) {
  153. $query->select('category_id', 'category_name');
  154. },
  155. 'running' => function ($query) {
  156. $query->select('join_running_goods_id', 'goods_running_storage');
  157. },
  158. 'supplier' => function ($query) {
  159. $query->select('supplier_id', 'supplier_name');
  160. },
  161. 'user' => function ($query) {
  162. $query->select('user_id', 'user_name');
  163. }
  164. ])->select('goods_id', 'join_goods_category_id', 'join_goods_supplier_id', 'creator_user_id', 'goods_status', 'goods_sales_price', 'goods_category', 'goods_name', 'goods_title', 'goods_cover', 'goods_sort', 'goods_attribute_json', 'goods_addtimes', 'goods_updatetimes')
  165. ->when($goodsName != '', function ($query) use ($goodsName) {
  166. $query->where(function ($q) use ($goodsName) {
  167. $q->where('goods_name', 'like', '%' . $goodsName . '%')
  168. ->OrWhere('goods_title', 'like', '%' . $goodsName . '%');
  169. });
  170. })->when($categoryId != null, function ($query) use ($categoryId) {
  171. $query->whereIn('join_goods_category_id', $categoryId);
  172. })->when($goodsCategory != null, function ($query) use ($goodsCategory) {
  173. $query->where('goods_category', $goodsCategory);
  174. })
  175. ->when($classify != '', function ($query) use ($classify, $categoryId) {
  176. if ($classify == 'GOODS' && empty($categoryId)) {
  177. $query->whereIn('join_goods_category_id', ['6', '7', '8', '9', '10', '11', '12', '30']);
  178. } else if ($classify != 'GOODS' && empty($categoryId)) {
  179. $query->where('goods_classify', $classify);
  180. }
  181. })
  182. ->orderBy('goods_sort', 'DESC')
  183. ->orderBy('goods_addtimes', 'DESC')
  184. ->forPage($page, $pageSize)
  185. ->get()
  186. ->toArray();
  187. $total = Goods::when($goodsName != '', function ($query) use ($goodsName) {
  188. $query->where(function ($q) use ($goodsName) {
  189. $q->where('goods_name', 'like', '%' . $goodsName . '%')
  190. ->OrWhere('goods_title', 'like', '%' . $goodsName . '%');
  191. });
  192. })->when($categoryId != null, function ($query) use ($categoryId) {
  193. $query->whereIn('join_goods_category_id', $categoryId);
  194. })->when($goodsCategory != null, function ($query) use ($goodsCategory) {
  195. $query->where('goods_category', $goodsCategory);
  196. })->when($classify != '', function ($query) use ($classify, $categoryId) {
  197. if ($classify == 'GOODS' && empty($categoryId)) {
  198. $query->whereIn('join_goods_category_id', ['6', '7', '8', '9', '10', '11', '12', '30']);
  199. } else if ($classify != 'GOODS' && empty($categoryId)) {
  200. $query->where('goods_classify', $classify);
  201. }
  202. })
  203. ->count();
  204. foreach ($rows as &$row) {
  205. $row['goods_cover'] = getenv('STORAGE_DOMAIN') . $row['goods_cover'];
  206. if (isset($row['running'])) {
  207. $row['running']['goods_running_storage'] = intval($row['running']['goods_running_storage']);
  208. }
  209. if (!empty($row['goods_attribute_json'])) {
  210. $row['goods_attribute_json'] = json_decode($row['goods_attribute_json']);
  211. }
  212. if (!empty($row['goods_category']) && $row['goods_category'] == 'INDEX') {
  213. $row['goods_recommend_index'] = 'INDEX';
  214. }
  215. }
  216. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  217. }
  218. public static function selectSpecial(Request $request)
  219. {
  220. $page = $request->get('page');
  221. $pageSize = $request->get('pageSize');
  222. $goodsName = $request->get('goods_name', '');
  223. $categoryId = $request->get('join_goods_category_id', null);
  224. if ($categoryId == null) {
  225. $categoryId = [65, 43];
  226. } elseif (is_string($categoryId)) {
  227. $categoryId = [$categoryId];
  228. }
  229. $rows = Goods::with([
  230. 'category' => function ($query) {
  231. $query->select('category_id', 'category_name');
  232. },
  233. 'running' => function ($query) {
  234. $query->select('join_running_goods_id', 'goods_running_storage');
  235. },
  236. 'supplier' => function ($query) {
  237. $query->select('supplier_id', 'supplier_name');
  238. },
  239. 'user' => function ($query) {
  240. $query->select('user_id', 'user_name');
  241. }
  242. ])->select('goods_id', 'join_goods_category_id', 'join_goods_supplier_id', 'creator_user_id', 'goods_status', 'goods_sales_price', 'goods_category', 'goods_name', 'goods_title', 'goods_cover', 'goods_sort', 'goods_addtimes', 'goods_updatetimes')
  243. ->when($goodsName != '', function ($query) use ($goodsName) {
  244. $query->where(function ($q) use ($goodsName) {
  245. $q->where('goods_name', 'like', '%' . $goodsName . '%')
  246. ->OrWhere('goods_title', 'like', '%' . $goodsName . '%');
  247. });
  248. })->whereIn('join_goods_category_id', $categoryId)
  249. ->orderBy('goods_sort', 'DESC')
  250. ->orderBy('goods_addtimes', 'DESC')
  251. ->forPage($page, $pageSize)
  252. ->get()
  253. ->toArray();
  254. $total = Goods::when($goodsName != '', function ($query) use ($goodsName) {
  255. $query->where(function ($q) use ($goodsName) {
  256. $q->where('goods_name', 'like', '%' . $goodsName . '%')
  257. ->OrWhere('goods_title', 'like', '%' . $goodsName . '%');
  258. });
  259. })->whereIn('join_goods_category_id', $categoryId)->count();
  260. foreach ($rows as &$row) {
  261. $row['goods_cover'] = getenv('STORAGE_DOMAIN') . $row['goods_cover'];
  262. if (isset($row['running'])) {
  263. $row['running']['goods_running_storage'] = intval($row['running']['goods_running_storage']);
  264. }
  265. }
  266. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  267. }
  268. public static function selectPicking(Request $request)
  269. {
  270. $page = $request->get('page');
  271. $pageSize = $request->get('pageSize');
  272. $goodsName = $request->get('goods_name', '');
  273. $categorySuperId = $request->get('category_super_id', '');
  274. $categoryIds = $request->get('join_goods_category_id', []);
  275. if (!empty($categorySuperId) && is_array($categoryIds)) {
  276. $category = SysCategory::where('category_id', $categorySuperId)->first();
  277. if (empty($category->category_super_path)) {
  278. $category->category_super_path = '#' . $categorySuperId . '#';
  279. } else {
  280. $category->category_super_path = $category->category_super_path . '#' . $categorySuperId . '#';
  281. }
  282. $categoryIds = SysCategory::where('category_super_path', 'like', '%' . $category->category_super_path)->pluck('category_id');
  283. $categoryIds = $categoryIds ? $categoryIds->toArray() : [];
  284. $categoryIds = array_merge($categoryIds, [$categorySuperId]);
  285. } elseif (!is_array($categoryIds)) {
  286. $categoryIds = [$categoryIds];
  287. }
  288. $rows = Goods::with([
  289. 'category' => function ($query) {
  290. $query->select('category_id', 'category_name');
  291. },
  292. 'running' => function ($query) {
  293. $query->select('join_running_goods_id', 'goods_running_storage');
  294. },
  295. 'supplier' => function ($query) {
  296. $query->select('supplier_id', 'supplier_name');
  297. },
  298. 'user' => function ($query) {
  299. $query->select('user_id', 'user_name');
  300. }
  301. ])->select('goods_id', 'join_goods_category_id', 'join_goods_supplier_id', 'creator_user_id', 'goods_status', 'goods_sales_price', 'goods_category', 'goods_name', 'goods_title', 'goods_cover', 'goods_sort', 'goods_addtimes', 'goods_updatetimes')
  302. ->when($goodsName != '', function ($query) use ($goodsName) {
  303. $query->where(function ($q) use ($goodsName) {
  304. $q->where('goods_name', 'like', '%' . $goodsName . '%')
  305. ->OrWhere('goods_title', 'like', '%' . $goodsName . '%');
  306. });
  307. })->whereIn('join_goods_category_id', $categoryIds)
  308. ->orderBy('goods_sort', 'DESC')
  309. ->orderBy('goods_addtimes', 'DESC')
  310. ->forPage($page, $pageSize)
  311. ->get()
  312. ->toArray();
  313. $total = Goods::when($goodsName != '', function ($query) use ($goodsName) {
  314. $query->where(function ($q) use ($goodsName) {
  315. $q->where('goods_name', 'like', '%' . $goodsName . '%')
  316. ->OrWhere('goods_title', 'like', '%' . $goodsName . '%');
  317. });
  318. })->whereIn('join_goods_category_id', $categoryIds)
  319. ->count();
  320. foreach ($rows as &$row) {
  321. $row['goods_cover'] = getenv('STORAGE_DOMAIN') . $row['goods_cover'];
  322. if (isset($row['running'])) {
  323. $row['running']['goods_running_storage'] = intval($row['running']['goods_running_storage']);
  324. }
  325. }
  326. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  327. }
  328. public static function selectPackage(Request $request)
  329. {
  330. $page = $request->get('page');
  331. $pageSize = $request->get('pageSize');
  332. $goodsName = $request->get('goods_name', '');
  333. $categoryId = $request->get('join_goods_category_id', null);
  334. $rows = Goods::with([
  335. 'category' => function ($query) {
  336. $query->select('category_id', 'category_name');
  337. },
  338. 'running' => function ($query) {
  339. $query->select('join_running_goods_id', 'goods_running_storage');
  340. },
  341. 'supplier' => function ($query) {
  342. $query->select('supplier_id', 'supplier_name');
  343. },
  344. 'user' => function ($query) {
  345. $query->select('user_id', 'user_name');
  346. }
  347. ])->select('goods_id', 'join_goods_category_id', 'join_goods_supplier_id', 'creator_user_id', 'goods_status', 'goods_sales_price', 'goods_category', 'goods_name', 'goods_title', 'goods_cover', 'goods_sort', 'goods_addtimes', 'goods_updatetimes')
  348. ->when($goodsName != '', function ($query) use ($goodsName) {
  349. $query->where(function ($q) use ($goodsName) {
  350. $q->where('goods_name', 'like', '%' . $goodsName . '%')
  351. ->OrWhere('goods_title', 'like', '%' . $goodsName . '%');
  352. });
  353. })->when($categoryId != null, function ($query) use ($categoryId) {
  354. $query->where('join_goods_category_id', $categoryId);
  355. })
  356. ->where('goods_classify', 'PACKAGE')
  357. ->orderBy('goods_sort', 'DESC')
  358. ->orderBy('goods_addtimes', 'DESC')
  359. ->forPage($page, $pageSize)
  360. ->get()
  361. ->toArray();
  362. $total = Goods::when($goodsName != '', function ($query) use ($goodsName) {
  363. $query->where(function ($q) use ($goodsName) {
  364. $q->where('goods_name', 'like', '%' . $goodsName . '%')
  365. ->OrWhere('goods_title', 'like', '%' . $goodsName . '%');
  366. });
  367. })->when($categoryId != null, function ($query) use ($categoryId) {
  368. $query->where('join_goods_category_id', $categoryId);
  369. })->where('goods_classify', 'PACKAGE')->count();
  370. foreach ($rows as &$row) {
  371. $row['goods_cover'] = getenv('STORAGE_DOMAIN') . $row['goods_cover'];
  372. if (isset($row['running'])) {
  373. $row['running']['goods_running_storage'] = intval($row['running']['goods_running_storage']);
  374. }
  375. // if (!empty($row['component'])) {
  376. // $ids = [];
  377. // $contentList = [];
  378. // foreach ($row['component'] as $component) {
  379. // $ids[] = $component['join_component_goods_id'];
  380. // $configJson = json_decode($component['goods_component_config_json'], true);
  381. // $contentList[] = [
  382. // 'goods_name' => $configJson['goods_name'] ?? '',
  383. // 'goods_sales_price' => $component['goods_component_price'],
  384. // 'nbr' => $configJson['nbr'] ?? 0
  385. // ];
  386. // }
  387. //
  388. // $row['join_component_goods_id'] = $ids;
  389. // $row['goodsContentList'] = $contentList;
  390. // }
  391. }
  392. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  393. }
  394. /**
  395. * @Desc 下拉选择服务商品
  396. * @Author Gorden
  397. * @Date 2024/4/24 13:32
  398. *
  399. * @param Request $request
  400. * @return Response
  401. */
  402. public static function selectList(Request $request, $goodsClassify = "SERVICE")
  403. {
  404. $keywords = $request->get('keywords', '');
  405. $isSupportAppointment = $request->get('is_support_appointment', '');
  406. // if (!$keywords){
  407. // return json_success('暂无数据');
  408. // }
  409. // $categoryIds = SysCategory::whereIn('category_super_id', [5, 31, 32, 42, 66, 70, 72])->pluck('category_id');
  410. $goods = Goods::with('sku')
  411. // ->whereIn('join_goods_category_id', $categoryIds)
  412. ->when($keywords != '', function ($query) use ($keywords) {
  413. $query->where('goods_name', 'like', "%" . $keywords . "%");
  414. })
  415. ->when($goodsClassify != '', function ($query) use ($goodsClassify) {
  416. if ($goodsClassify == 'NOPACKAGE') {
  417. $query->where('goods_classify', '<>', 'PACKAGE');
  418. } else if ($goodsClassify == 'SERVICE') {
  419. $query->whereIn('goods_classify', ['SERVICE', 'CHNMED', 'CHNNCD']);
  420. } else {
  421. $query->where('goods_classify', $goodsClassify);
  422. }
  423. })
  424. ->when($isSupportAppointment != '', function ($query) use ($isSupportAppointment) {
  425. $query->where('is_support_appointment', $isSupportAppointment);
  426. })
  427. ->select('goods_id', 'goods_name', 'goods_sales_price', 'join_goods_category_id', 'goods_attribute_json')
  428. ->get()
  429. ->toArray();
  430. foreach ($goods as &$good) {
  431. if (!empty($good['sku'])) {
  432. foreach ($good['sku'] as $key => $sku) {
  433. if (!empty($sku['goods_sku_specs_json'])) {
  434. $good['sku'][$key]['goods_sku_specs_json'] = json_decode($sku['goods_sku_specs_json']);
  435. $skuName = '';
  436. foreach ($good['sku'][$key]['goods_sku_specs_json'] as $specsKey => $skuSpecs) {
  437. if (is_array($skuSpecs)) {
  438. $skuName = $skuName . ' ' . $specsKey . ':' . implode(' ', $skuSpecs) . ';';
  439. } else {
  440. $skuName = $skuName . ' ' . $specsKey . ':' . $skuSpecs . ';';
  441. }
  442. }
  443. $good['sku'][$key]['sku_name'] = $skuName;
  444. }
  445. }
  446. } else {
  447. $good['sku'] = [];
  448. }
  449. $good['premisses'] = [];
  450. if (!empty($good['goods_attribute_json'])) {
  451. $attributeJson = json_decode($good['goods_attribute_json'], true);
  452. if (isset($attributeJson['premisses'])) {
  453. $premisses = SysDept::when(!empty($attributeJson['premisses']), function ($query) use ($attributeJson) {
  454. $query->whereIn('dept_id', $attributeJson['premisses']);
  455. })->where('dept_category', '营业场所')
  456. ->select('dept_id', 'dept_name')
  457. ->get();
  458. $good['premisses'] = $premisses;
  459. }
  460. }
  461. }
  462. return json_success('', $goods);
  463. }
  464. public static function selectCascaderList(Request $request)
  465. {
  466. $categoryIds = $request->get('category_id','');
  467. $categorySuperId = $request->get('category_super_id','');
  468. if (!$categoryIds && !$categorySuperId){
  469. return json_fail('参数异常');
  470. }
  471. $data = [];
  472. if (!empty($categoryIds)){
  473. $categorys = SysCategory::whereIn('category_id',$categoryIds);
  474. }else if(!empty($categorySuperId)){
  475. $categorys = SysCategory::where('category_super_id',$categorySuperId);
  476. }
  477. $categorys = $categorys->where('category_status','ACTIVED')
  478. ->select('category_id as id','category_name as name','category_super_id as pid','category_super_path')
  479. ->orderBy('category_sort','DESC')
  480. ->get()
  481. ->toArray();
  482. $data = array_merge($data,$categorys);
  483. foreach ($categorys as $category){
  484. // if(empty($category['category_super_path'])){
  485. $category['category_super_path'] = '#'.$category['id'].'#';
  486. // }
  487. $subCategory = SysCategory::where('category_super_path','like','%'.$category['category_super_path'].'%')
  488. ->where('category_status','ACTIVED')
  489. ->select('category_id as id','category_name as name','category_super_id as pid','category_super_path')
  490. ->orderBy('category_sort','DESC')
  491. ->get()
  492. ->toArray();
  493. $data = array_merge($data,$subCategory);
  494. }
  495. $goodsCategoryIds = array_column($data,'id');
  496. $goods = Goods::with('sku')
  497. ->whereIn('join_goods_category_id',$goodsCategoryIds)
  498. ->where('goods_status','ON')
  499. ->select('goods_id','goods_id as id','goods_name as name','join_goods_category_id as pid','goods_attribute_json','goods_classify')
  500. ->orderBy('goods_sort','DESC')
  501. ->orderBy('goods_addtimes','DESC')
  502. ->get()
  503. ->toArray();
  504. foreach ($goods as &$good) {
  505. if (!empty($good['sku'])) {
  506. foreach ($good['sku'] as $key => $sku) {
  507. if (!empty($sku['goods_sku_specs_json'])) {
  508. $good['sku'][$key]['goods_sku_specs_json'] = json_decode($sku['goods_sku_specs_json']);
  509. $skuName = '';
  510. foreach ($good['sku'][$key]['goods_sku_specs_json'] as $specsKey => $skuSpecs) {
  511. $keyStr = ($specsKey == '规格') ? '' : ($specsKey.':');
  512. if (is_array($skuSpecs)) {
  513. $skuName = $skuName .$keyStr . ' ' . implode(' ', $skuSpecs) . '; ';
  514. } else {
  515. $skuName = $skuName .$keyStr . ' ' . $skuSpecs . '; ';
  516. }
  517. }
  518. $good['sku'][$key]['sku_name'] = rtrim($skuName,'; ');
  519. }
  520. }
  521. } else {
  522. $good['sku'] = [];
  523. }
  524. $good['premisses'] = [];
  525. if (!empty($good['goods_attribute_json'])) {
  526. $attributeJson = json_decode($good['goods_attribute_json'], true);
  527. if (isset($attributeJson['premisses'])) {
  528. $premisses = SysDept::when(!empty($attributeJson['premisses']), function ($query) use ($attributeJson) {
  529. $query->whereIn('dept_id', $attributeJson['premisses']);
  530. })->where('dept_category', '营业场所')
  531. ->select('dept_id', 'dept_name')
  532. ->get();
  533. $good['premisses'] = $premisses;
  534. }
  535. }
  536. }
  537. $data = array_merge($data,$goods);
  538. $tree = new Tree($data);
  539. return json_success('success', $tree->getTree());
  540. }
  541. /**
  542. * @Desc 商品详情
  543. * @Author Gorden
  544. * @Date 2024/3/28 10:25
  545. *
  546. * @param $goodsId
  547. * @return Response
  548. */
  549. public static function info($goodsId)
  550. {
  551. try {
  552. // 商品主表
  553. $main = Goods::with('category')->where('goods_id', $goodsId)->first();
  554. if (!empty($main)) {
  555. $main = $main->toArray();
  556. $main['goods_sku_json'] = json_decode($main['goods_sku_json'], true);
  557. $main['specList'] = [];
  558. foreach ($main['goods_sku_json'] as $key => $sku) {
  559. $main['specList'][] = [
  560. 'label' => $key,
  561. 'tags' => $sku
  562. ];
  563. }
  564. } else {
  565. $main = [];
  566. }
  567. // 详情表
  568. $detail = GoodsDetail::where('join_detail_goods_id', $goodsId)->first();
  569. if (!empty($detail)) {
  570. $detail = $detail->toArray();
  571. } else {
  572. $detail = [];
  573. }
  574. // 标签表
  575. $label = GoodsLabel::where('join_label_goods_id', $goodsId)->first();
  576. if (!empty($label)) {
  577. $label = $label->toArray();
  578. } else {
  579. $label = [];
  580. }
  581. // Running表
  582. $running = GoodsRunning::where('join_running_goods_id', $goodsId)->first();
  583. if (!empty($running)) {
  584. $running = $running->toArray();
  585. if (!empty($running['goods_running_off_json']) && is_json($running['goods_running_off_json'])) {
  586. $goodsRunningOffJson = json_decode($running['goods_running_off_json'], true);
  587. $running['goods_off_addtimes'] = isset($goodsRunningOffJson['time']) ? date("Y-m-d H:i", $goodsRunningOffJson['time']) : '';
  588. }
  589. $running['goods_running_storage'] = !empty($running['goods_running_storage']) ? intval($running['goods_running_storage']) : '';
  590. $running['goods_running_sale'] = !empty($running['goods_running_sale']) ? intval($running['goods_running_sale']) : '';
  591. } else {
  592. $running = [];
  593. }
  594. // Sku表
  595. $skus = GoodsSku::where('join_sku_goods_id', $goodsId)->get();
  596. if (!empty($skus)) {
  597. $skus = $skus->toArray();
  598. $submitList = [];
  599. foreach ($skus as $key => $sku) {
  600. $skuSpecsJson = json_decode($sku['goods_sku_specs_json'], true);
  601. $skuSpecs = '';
  602. $skuNameValue = [];
  603. $i = 1;
  604. foreach ($skuSpecsJson as $k => $item) {
  605. if (is_array($item)) {
  606. $item = implode(',', $item);
  607. }
  608. $skuSpecs = $skuSpecs . $item . ',';
  609. $skuNameKey = 'skuName' . $i;
  610. $skuValueKey = 'skuValue' . $i;
  611. $skuNameValue[$skuNameKey] = $k;
  612. $skuNameValue[$skuValueKey] = $item;
  613. $skuNameValue[$k] = $item;
  614. $i++;
  615. }
  616. $storage = json_decode($sku['goods_sku_storage_json'], true);
  617. $priceStorage = [
  618. 'sku_id' => $sku['goods_sku_id'],
  619. 'sku' => rtrim($skuSpecs, ',') ?? '',
  620. 'stock' => $storage['storage'] ?? '',
  621. 'price' => $sku['goods_sku_sales_price'] ?? '',
  622. ];
  623. $submitList[] = array_merge($skuNameValue, $priceStorage);
  624. }
  625. $skus['submitList'] = $submitList;
  626. } else {
  627. $skus = [];
  628. }
  629. // 合并数据
  630. $data = array_merge($main, $detail, $label, $running, ['sku' => $skus]);
  631. $data['goods_sku_json_label'] = [];
  632. $data['goods_label'] = !empty($data['goods_label']) ? explode(',', $data['goods_label']) : [];
  633. $data['goods_json'] = $data['goods_json'] ? json_decode($data['goods_json'], true) : [];
  634. $data['goods_cover'] = getenv('STORAGE_DOMAIN') . $data['goods_cover'];
  635. if (!empty($data['goods_category']) && $data['goods_category'] == 'INDEX') {
  636. $data['goods_recommend_index'] = 'INDEX';
  637. }
  638. // 创建者
  639. $data['creator_username'] = '';
  640. if (!empty($data['creator_user_id'])) {
  641. $data['creator_username'] = SysUser::where('user_id', $data['creator_user_id'])->value('user_name');
  642. }
  643. if (!empty($data['goods_detail_slider_json'])) {
  644. $data['goods_detail_slider_json'] = json_decode($data['goods_detail_slider_json'], true);
  645. // ……
  646. if (isset($data['goods_detail_slider_json']['slider'])) {
  647. $data['goods_detail_slider_json'] = explode(',', $data['goods_detail_slider_json']['slider']);
  648. }
  649. $slider = '';
  650. foreach ($data['goods_detail_slider_json'] as $item) {
  651. $slider .= getenv('STORAGE_DOMAIN') . $item . ',';
  652. }
  653. $data['goods_detail_slider_json'] = rtrim($slider, ',');
  654. }
  655. $extendJson = [];
  656. if (!empty($data['goods_attribute_json'])) {
  657. $extendJson = json_decode($data['goods_attribute_json'], true);
  658. $data['goods_attribute_json'] = $extendJson;
  659. if (isset($extendJson['premisses'])) {
  660. $data['goods_premisses'] = $extendJson['premisses'];
  661. $data['goods_premisses_str'] = SysDept::whereIn('dept_id', $extendJson['premisses'])->pluck('dept_name');
  662. }
  663. if (isset($extendJson['bg'])) {
  664. $data['goods_theme_color'] = $extendJson['bg'];
  665. }
  666. if (isset($extendJson['icon'])) {
  667. $data['goods_theme_icon'] = getenv('STORAGE_DOMAIN') . $extendJson['icon'];
  668. }
  669. if (isset($extendJson['service_premises_id'])) {
  670. $data['goods_service_premises'] = $extendJson['service_premises_id'];
  671. }
  672. }
  673. $data['express_json'] = [];
  674. if (!empty($data['goods_express_json'])) {
  675. $goodsExpressJson = json_decode($data['goods_express_json'], true);
  676. if (isset($goodsExpressJson['express']) && $goodsExpressJson['express'] == 'Y') {
  677. $data['express_json'][] = 'express';
  678. }
  679. if (isset($goodsExpressJson['self']) && $goodsExpressJson['self'] == 'Y') {
  680. $data['express_json'][] = 'self';
  681. }
  682. if (isset($goodsExpressJson['arrival']) && $goodsExpressJson['arrival'] == 'Y') {
  683. $data['express_json'][] = 'arrival';
  684. }
  685. }
  686. $data['appointment_times'] = [];
  687. if ($data['is_support_appointment'] == 'Y' && isset($extendJson['dates'])) {
  688. $data['dates'] = $extendJson['dates'] ?? [];
  689. if (isset($extendJson['times'])) {
  690. $times = [];
  691. foreach ($extendJson['times'] as $time) {
  692. if (!empty($time['duration'])) {
  693. $startEndTime = explode('-', $time['duration']);
  694. $times[] = [
  695. 'person' => $time['person'],
  696. 'appointmentTimeStart' => $startEndTime[0],
  697. 'appointmentTimeEnd' => $startEndTime[1],
  698. ];
  699. }
  700. }
  701. $data['appointment_times'] = $times;
  702. }
  703. if (isset($extendJson['time'])) {
  704. $extendJsonTime = explode('至', $extendJson['time']);
  705. if (isset($extendJsonTime[0]) && isset($extendJsonTime[1])) {
  706. $data['work_time'] = [
  707. date('Y-m-d H:i:s', strtotime(date('Y-m-d ') . $extendJsonTime[0])),
  708. date('Y-m-d H:i:s', strtotime(date('Y-m-d ') . $extendJsonTime[1])),
  709. ];
  710. }
  711. }
  712. // if (isset($extendJson['teachers'])){
  713. // $data['teachers'] = $extendJson['teachers'];
  714. // }
  715. // if (isset($extendJson['address'])) {
  716. // $data['address'] = $extendJson['address'];
  717. // }
  718. // if (isset($extendJson['min-count'])){
  719. // $data['min_count'] = $extendJson['min-count'];
  720. // }
  721. // if (isset($extendJson['position'])) {
  722. // $data['position'] = $extendJson['position'];
  723. // }
  724. if (isset($extendJson['label'])) {
  725. $data['appointment_label'] = $extendJson['label'];
  726. }
  727. }
  728. if (isset($extendJson['month']) && $data['is_support_appointment'] == 'Y') {
  729. $times = [];
  730. foreach ($extendJson['month'] as $monKey => $months) {
  731. foreach ($months as $day => $person) {
  732. $times[] = [
  733. 'days' => date('Y-m-d', strtotime($monKey . $day)),
  734. 'person' => $person
  735. ];
  736. }
  737. }
  738. $data['appointment_times'] = $times;
  739. $data['travel_begin'] = $extendJson['travel-begin'];
  740. $data['travel_day'] = $extendJson['travel-day'];
  741. $data['travel_night'] = $extendJson['travel-night'];
  742. $data['travel_trans'] = $extendJson['travel-trans'];
  743. }
  744. if (!empty($data['goods_json']) && $data['join_goods_category_id'] == 65) {
  745. // foreach ($data['goods_json'] as $key => $datum) {
  746. // $data['goods_json'][$key]['color'] = rgbToHex($datum['color']);
  747. // }
  748. } elseif (!empty($data['goods_json']) && $data['join_goods_category_id'] == 43) {
  749. $goodsJsonNew = [];
  750. foreach ($data['goods_json'] as $key1 => $item1) {
  751. $itemsNew = [];
  752. if (isset($item1['items'])) {
  753. foreach ($item1['items'] as $key2 => $item2) {
  754. $itemsNew[] = [
  755. 'key' => $key2,
  756. 'params' => $item2
  757. ];
  758. }
  759. }
  760. $goodsJsonNew[] = [
  761. 'title' => $key1,
  762. 'service' => $item1['service'] ?? '',
  763. 'items' => $itemsNew
  764. ];
  765. }
  766. $data['goods_json'] = $goodsJsonNew;
  767. }
  768. if (isset($extendJson['teachers'])) {
  769. $data['teachers'] = $extendJson['teachers'];
  770. }
  771. if (isset($extendJson['address'])) {
  772. $data['address'] = $extendJson['address'];
  773. }
  774. if (isset($extendJson['min-count'])) {
  775. $data['min_count'] = $extendJson['min-count'];
  776. }
  777. if (isset($extendJson['position'])) {
  778. $data['position'] = $extendJson['position'];
  779. }
  780. $data['goods_on_addtimes'] = date('Y-m-d\TH:i:s.u\Z', $data['goods_on_addtimes'] - 60 * 60 * 8);
  781. // 详情表数据
  782. if (!empty($data['goods_detail_specs_json'])) {
  783. $specsJson = json_decode($data['goods_detail_specs_json'], true);
  784. foreach ($specsJson as $itemSpecsJson) {
  785. if (isset($itemSpecsJson['key']) && $itemSpecsJson['key'] == '课时') {
  786. $data['curriculum']['period'] = $itemSpecsJson['val'];
  787. } elseif (isset($itemSpecsJson['key']) && $itemSpecsJson['key'] == '群体') {
  788. $data['curriculum']['group'] = $itemSpecsJson['val'];
  789. } elseif (isset($itemSpecsJson['key']) && $itemSpecsJson['key'] == '课程') {
  790. $data['curriculum']['course'] = $itemSpecsJson['val'];
  791. }
  792. }
  793. }
  794. return json_success('', $data);
  795. } catch (\Exception $e) {
  796. dump($e->getMessage());
  797. return json_fail("查询错误~");
  798. }
  799. }
  800. public static function infoPackage($goodsId)
  801. {
  802. try {
  803. // 商品主表
  804. $main = Goods::where('goods_id', $goodsId)->first();
  805. if (!empty($main)) {
  806. $main = $main->toArray();
  807. $main['goods_sku_json'] = json_decode($main['goods_sku_json'], true);
  808. $main['specList'] = [];
  809. foreach ($main['goods_sku_json'] as $key => $sku) {
  810. $main['specList'][] = [
  811. 'label' => $key,
  812. 'tags' => $sku
  813. ];
  814. }
  815. } else {
  816. $main = [];
  817. }
  818. // 详情表
  819. $detail = GoodsDetail::where('join_detail_goods_id', $goodsId)->first();
  820. if (!empty($detail)) {
  821. $detail = $detail->toArray();
  822. } else {
  823. $detail = [];
  824. }
  825. // 标签表
  826. $label = GoodsLabel::where('join_label_goods_id', $goodsId)->first();
  827. if (!empty($label)) {
  828. $label = $label->toArray();
  829. } else {
  830. $label = [];
  831. }
  832. // Running表
  833. $running = GoodsRunning::where('join_running_goods_id', $goodsId)->first();
  834. if (!empty($running)) {
  835. $running = $running->toArray();
  836. if (!empty($running['goods_running_off_json']) && is_json($running['goods_running_off_json'])) {
  837. $goodsRunningOffJson = json_decode($running['goods_running_off_json'], true);
  838. $running['goods_off_addtimes'] = isset($goodsRunningOffJson['time']) ? date("Y-m-d H:i", $goodsRunningOffJson['time']) : '';
  839. }
  840. } else {
  841. $running = [];
  842. }
  843. // 组件表
  844. $component = GoodsComponent::where('join_component_master_goods_id', $goodsId)->get()->toArray();
  845. $componentArr['join_component_goods_id'] = [];
  846. $componentArr['goodsContentList'] = [];
  847. if ($component) {
  848. $ids = [];
  849. $contentList = [];
  850. foreach ($component as $item) {
  851. if (empty($item['join_component_goods_id'])) {
  852. continue;
  853. }
  854. $skus = GoodsSku::where('join_sku_goods_id', $item['join_component_goods_id'])
  855. ->select('goods_sku_id', 'join_sku_goods_id', 'goods_sku_specs_json', 'goods_sku_sales_price')
  856. ->get()
  857. ->toArray();
  858. foreach ($skus as $key => $sku2) {
  859. if (!empty($sku2['goods_sku_specs_json'])) {
  860. $skus[$key]['goods_sku_specs_json'] = json_decode($sku2['goods_sku_specs_json'], true);
  861. $skuName = '';
  862. foreach ($skus[$key]['goods_sku_specs_json'] as $specsKey => $skuSpecs) {
  863. if (is_array($skuSpecs)) {
  864. $skuName = $skuName . ' ' . $specsKey . ' ' . implode(' ', $skuSpecs);
  865. } else {
  866. $skuName = $skuName . ' ' . $specsKey . ' ' . $skuSpecs;
  867. }
  868. }
  869. $skus[$key]['sku_name'] = $skuName;
  870. }
  871. }
  872. $ids[] = $item['join_component_goods_id'];
  873. $configJson = json_decode($item['goods_component_json'], true);
  874. $contentList[] = [
  875. 'goods_id' => $item['join_component_goods_id'],
  876. 'goods_name' => $configJson['goods_name'] ?? '',
  877. 'goods_sales_price' => $item['goods_component_price'],
  878. 'nbr' => $configJson['nbr'] ?? 0,
  879. 'sku_id' => $configJson['sku_id'] ?? '',
  880. 'skus' => $skus
  881. ];
  882. }
  883. $componentArr['join_component_goods_id'] = $ids;
  884. $componentArr['goodsContentList'] = $contentList;
  885. }
  886. // 合并数据
  887. $data = array_merge($main, $detail, $label, $running, $componentArr);
  888. $data['goods_sku_json_label'] = [];
  889. $data['goods_label'] = !empty($data['goods_label']) ? explode(',', $data['goods_label']) : [];
  890. $data['goods_cover'] = getenv('STORAGE_DOMAIN') . $data['goods_cover'];
  891. if (!empty($data['goods_detail_slider_json'])) {
  892. $data['goods_detail_slider_json'] = json_decode($data['goods_detail_slider_json'], true);
  893. $slider = '';
  894. foreach ($data['goods_detail_slider_json'] as $item) {
  895. $slider .= getenv('STORAGE_DOMAIN') . $item . ',';
  896. }
  897. $data['goods_detail_slider_json'] = rtrim($slider, ',');
  898. }
  899. $extendJson = [];
  900. if (!empty($data['goods_attribute_json'])) {
  901. $extendJson = json_decode($data['goods_attribute_json'], true);
  902. $data['goods_attribute_json'] = $extendJson;
  903. if (isset($extendJson['premisses'])) {
  904. $data['goods_premisses'] = $extendJson['premisses'];
  905. $data['goods_premisses_str'] = SysDept::whereIn('dept_id', $extendJson['premisses'])->pluck('dept_name');
  906. }
  907. }
  908. $data['goods_on_addtimes'] = date('Y-m-d\TH:i:s.u\Z', $data['goods_on_addtimes'] - 60 * 60 * 8);
  909. return json_success('', $data);
  910. } catch (\Exception $e) {
  911. dump($e->getTrace());
  912. return json_fail("查询错误~");
  913. }
  914. }
  915. /**
  916. * @Desc 添加商品
  917. * @Author Gorden
  918. * @Date 2024/3/11 10:20
  919. *
  920. * @param $params
  921. * @return Response
  922. */
  923. public static function insert($params): Response
  924. {
  925. Db::beginTransaction();
  926. try {
  927. $params['goods_id'] = "GD" . sprintf('%016d', SysSerial::getSerial()) . random_string(6, 'up');
  928. // 主表
  929. self::mainInsert($params);
  930. // 商品详情表
  931. self::detailInsert($params);
  932. // 商品标签表
  933. self::labelInsert($params);
  934. // 产品运行控制信息表
  935. self::goodsRunningInsert($params);
  936. // sku表
  937. self::goodsSkuSet($params, 'insert');
  938. // 待上架状态,上架时间大于当前时间
  939. if ($params['goods_status'] == 'PENDING' && strtotime($params['goods_on_addtimes']) > time()) {
  940. $redis = Redis::connection();
  941. $key = date('YmdHi', strtotime($params['goods_on_addtimes']));
  942. $redis->sAdd(Goods::LISTING_KEY_PREFIX . $key, $params['goods_id']);
  943. }
  944. // 自动下架
  945. if (!empty($params['goods_running_off_type']) && $params['goods_running_off_type'] == 'T' && !empty($params['goods_off_addtimes'])) {
  946. $redis = Redis::connection();
  947. $key = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi', strtotime($params['goods_off_addtimes']));
  948. $redis->sAdd($key, $params['goods_id']);
  949. }
  950. Db::commit();
  951. } catch (\PDOException $e) {
  952. Db::rollBack();
  953. dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
  954. return json_fail('数据写入失败~');
  955. } catch (BusinessException $e) {
  956. Db::rollBack();
  957. dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
  958. return json_fail($e->getMessage());
  959. } catch (\Exception $e) {
  960. Db::rollBack();
  961. dump($e->getTrace());
  962. return json_fail('数据写入失败~');
  963. }
  964. _syslog("添加商品", "商品名【" . $params['goods_name'] . "】");
  965. return json_success('success');
  966. }
  967. public static function insertRecharge($params)
  968. {
  969. try {
  970. Db::beginTransaction();
  971. $goods = new Goods();
  972. $goods->goods_id = "GD" . date('YmdHis') . random_string(6, 'up');
  973. $goods->join_goods_category_id = 59;
  974. $goods->goods_classify = 'RECHARGE';
  975. $goods->goods_status = $params['goods_status'];
  976. $goods->goods_sort = $params['goods_sort'];
  977. $goods->goods_category = $params['goods_category'];
  978. $goods->goods_name = $params['goods_name'];
  979. $goods->goods_market_price = $params['goods_sales_price'];
  980. $goods->goods_sales_price = $params['goods_sales_price'];
  981. $goods->goods_sku_json = json_encode(['规格' => [$params['goods_sales_price'] . '元']]);
  982. $goods->goods_attribute_json = json_encode(['added' => ['nbr' => $params['goods_rate'] / 100, 'mode' => 'rate'], 'min-count' => 1]);
  983. $goods->goods_cover = '/images/app/common/null-service.png';
  984. $goods->goods_process_json = json_encode(['mode' => 'do_shopping']);
  985. $goods->goods_addtimes = time();
  986. $goods->save();
  987. $sku = new GoodsSku();
  988. $sku->join_sku_goods_id = $goods->goods_id;
  989. $sku->goods_sku_status = $params['goods_status'];
  990. $sku->goods_sku_specs_json = json_encode(['规格' => $params['goods_sales_price'] . '元']);
  991. $sku->goods_sku_market_price = $params['goods_sales_price'];
  992. $sku->goods_sku_sales_price = $params['goods_sales_price'];
  993. $sku->goods_sku_storage_json = json_encode(['storage' => 9999]);
  994. $sku->save();
  995. Db::commit();
  996. return json_success('success');
  997. } catch (\Exception $e) {
  998. Db::rollBack();
  999. return json_fail('添加充值产品失败');
  1000. }
  1001. }
  1002. public static function updateRecharge($params)
  1003. {
  1004. try {
  1005. Db::beginTransaction();
  1006. $goods = Goods::where('goods_id', $params['goods_id'])->first();
  1007. if (!$goods) {
  1008. return json_fail("数据异常");
  1009. }
  1010. $goods->goods_market_price = $params['goods_sales_price'];
  1011. $goods->goods_sales_price = $params['goods_sales_price'];
  1012. $goods->goods_status = $params['goods_status'];
  1013. $goods->goods_sku_json = json_encode(['规格' => [$params['goods_sales_price'] . '元']]);
  1014. $goods->goods_sort = $params['goods_sort'];
  1015. if (!empty($goods->goods_attribute_json)) {
  1016. $attributeJson = json_decode($goods->goods_attribute_json, true);
  1017. $attributeJson['added']['nbr'] = $params['goods_rate'] / 100;
  1018. $goods->goods_attribute_json = json_encode($attributeJson);
  1019. }
  1020. $goods->save();
  1021. $sku = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->first();
  1022. $sku->goods_sku_status = $params['goods_status'];
  1023. $sku->goods_sku_specs_json = json_encode(['规格' => $params['goods_sales_price'] . '元']);
  1024. $sku->goods_sku_market_price = $params['goods_sales_price'];
  1025. $sku->goods_sku_sales_price = $params['goods_sales_price'];
  1026. $sku->save();
  1027. Db::commit();
  1028. return json_success("success");
  1029. } catch (\Exception $e) {
  1030. Db::rollBack();
  1031. dump($e->getMessage());
  1032. return json_fail('编辑充值产品失败');
  1033. }
  1034. }
  1035. public static function insertPackage($params): Response
  1036. {
  1037. Db::beginTransaction();
  1038. try {
  1039. $params['goods_id'] = "GD" . sprintf('%016d', SysSerial::getSerial()) . random_string(6, 'up');
  1040. // 主表
  1041. self::mainInsert($params);
  1042. // 商品详情表
  1043. self::detailInsert($params);
  1044. // 套包组件表
  1045. self::componentUpdate($params, 'insert');
  1046. // 商品标签表
  1047. self::labelInsert($params);
  1048. // 产品运行控制信息表
  1049. self::goodsRunningInsert($params);
  1050. // sku表
  1051. // self::goodsSkuSet($params, 'insert');
  1052. // 待上架状态,上架时间大于当前时间
  1053. if ($params['goods_status'] == 'PENDING' && strtotime($params['goods_on_addtimes']) > time()) {
  1054. $redis = Redis::connection();
  1055. $key = date('YmdHi', strtotime($params['goods_on_addtimes']));
  1056. $redis->sAdd(Goods::LISTING_KEY_PREFIX . $key, $params['goods_id']);
  1057. }
  1058. Db::commit();
  1059. } catch (\PDOException $e) {
  1060. Db::rollBack();
  1061. dump($e->getMessage());
  1062. return json_fail('数据写入失败~');
  1063. } catch (BusinessException $e) {
  1064. Db::rollBack();
  1065. dump($e->getMessage());
  1066. return json_fail($e->getMessage());
  1067. } catch (\Exception $e) {
  1068. Db::rollBack();
  1069. dump($e->getTrace());
  1070. return json_fail('数据写入失败~');
  1071. }
  1072. _syslog("添加套餐", "商品名【" . $params['goods_name'] . "】");
  1073. return json_success('success');
  1074. }
  1075. public static function update($params)
  1076. {
  1077. Db::beginTransaction();
  1078. try {
  1079. // 主表
  1080. self::mainUpdate($params);
  1081. // 商品详情表
  1082. self::detailUpdate($params);
  1083. // 商品标签表
  1084. self::labelUpdate($params);
  1085. // 产品运行控制信息表
  1086. self::goodsRunningUpdate($params);
  1087. // sku表
  1088. self::goodsSkuSet($params, 'update');
  1089. Db::commit();
  1090. } catch (BusinessException $e) {
  1091. Db::rollBack();
  1092. return json_fail($e->getMessage());
  1093. } catch (\Exception $e) {
  1094. Db::rollBack();
  1095. return json_fail('数据更新失败~');
  1096. }
  1097. _syslog("编辑商品", "商品名【" . $params['goods_name'] . "】" ?? "商品ID:【" . $params['goods_id'] . "】");
  1098. return json_success('success');
  1099. }
  1100. public static function changeStatus($params)
  1101. {
  1102. try {
  1103. Goods::where('goods_id', $params['goods_id'])->update(['goods_status' => $params['goods_status']]);
  1104. return json_success('修改成功');
  1105. } catch (\Exception $e) {
  1106. return json_fail('修改状态失败');
  1107. }
  1108. }
  1109. public static function updatePackage($params)
  1110. {
  1111. Db::beginTransaction();
  1112. try {
  1113. // 主表
  1114. self::mainUpdate($params);
  1115. // 商品详情表
  1116. self::detailUpdate($params);
  1117. // 套包组件表
  1118. self::componentUpdate($params, 'update');
  1119. // 商品标签表
  1120. self::labelUpdate($params);
  1121. // 产品运行控制信息表
  1122. self::goodsRunningUpdate($params);
  1123. Db::commit();
  1124. } catch (BusinessException $e) {
  1125. Db::rollBack();
  1126. return json_fail($e->getMessage());
  1127. } catch (\Exception $e) {
  1128. Db::rollBack();
  1129. dump($e->getTrace());
  1130. return json_fail('数据更新失败~');
  1131. }
  1132. _syslog("编辑套餐", "商品名【" . $params['goods_name'] . "】" ?? "商品ID:【" . $params['goods_id'] . "】");
  1133. return json_success('success');
  1134. }
  1135. /**
  1136. * @Desc 删除商品
  1137. * @Author Gorden
  1138. * @Date 2024/3/28 13:20
  1139. *
  1140. * @param $ids
  1141. * @return Response
  1142. */
  1143. public static function delete($ids)
  1144. {
  1145. if (!$ids) {
  1146. return json_fail("数据错误~");
  1147. }
  1148. if (!is_array($ids)) {
  1149. $ids = [$ids];
  1150. }
  1151. $goods = Goods::whereIn('goods_id', $ids)->get()->toArray();
  1152. if (!$goods) {
  1153. return json_fail("数据错误~");
  1154. }
  1155. // 是否在套包里
  1156. if (GoodsComponent::whereIn('join_component_goods_id', $ids)->exists()) {
  1157. return json_fail("当前商品存在于套包中,请先在套包中删除");
  1158. }
  1159. // 是否已被购买过
  1160. if (OrderSheet::whereIn('join_sheet_goods_id', $ids)->exists()) {
  1161. return json_fail("当前商品已有购买历史,如不在APP显示,请选择【下架】操作");
  1162. }
  1163. // 是否预约过
  1164. if (Appointment::whereIn('join_appointment_goods_id', $ids)->exists()) {
  1165. return json_fail("当前商品已有预约历史,如不在APP显示,请选择【下架】操作");
  1166. }
  1167. Db::beginTransaction();
  1168. try {
  1169. Goods::whereIn('goods_id', $ids)->delete();
  1170. GoodsDetail::whereIn('join_detail_goods_id', $ids)->delete();
  1171. GoodsLabel::whereIn('join_label_goods_id', $ids)->delete();
  1172. GoodsRunning::whereIn('join_running_goods_id', $ids)->delete();
  1173. GoodsSku::whereIn('join_sku_goods_id', $ids)->delete();
  1174. Db::commit();
  1175. _syslog("删除商品 / 套餐", "ID:【" . implode(',', $ids) . "】", $goods);
  1176. return json_success("商品删除成功");
  1177. } catch (\Exception $e) {
  1178. Db::rollBack();
  1179. return json_fail("商品删除失败~");
  1180. }
  1181. }
  1182. /**
  1183. * @Desc 商品主表
  1184. * @Author Gorden
  1185. * @Date 2024/3/11 11:20
  1186. *
  1187. * @param $params
  1188. * @return mixed|string
  1189. * @throws BusinessException
  1190. */
  1191. public static function mainInsert($params)
  1192. {
  1193. if (!empty($params['goods_cover'])) {
  1194. $params['goods_cover'] = str_replace(getenv('STORAGE_DOMAIN'), '', $params['goods_cover']);
  1195. }
  1196. // 如果产品是待处理状态
  1197. if ($params['goods_status'] == 'PENDING') {
  1198. if (strtotime($params['goods_on_addtimes']) <= time()) {
  1199. $params['goods_status'] = 'ON';
  1200. }
  1201. }
  1202. $category = SysCategory::where('category_id', $params['join_goods_category_id'])->first();
  1203. if (!$category) {
  1204. throw new BusinessException("产品分类不存在~");
  1205. }
  1206. if (empty($params['goods_category'])) {
  1207. $params['goods_category'] = $category->category_classify;
  1208. }
  1209. try {
  1210. $model = new Goods();
  1211. $model->goods_id = $params['goods_id'];
  1212. $model->join_goods_category_id = $params['join_goods_category_id'] ?? 0;
  1213. $model->join_goods_supplier_id = $params['join_goods_supplier_id'] ?? 0;
  1214. $model->goods_classify = $params['goods_classify'] ?? '';
  1215. $model->goods_status = $params['goods_status'] ?? '';
  1216. $model->goods_category = $params['goods_category'] ?? '';
  1217. // $model->goods_prefix = $params['goods_prefix'] ?? 】($category->category_name ? '【' . $category->category_name . '' : '');
  1218. $model->goods_prefix = $params['goods_prefix'] ?? '';
  1219. $model->goods_name = $params['goods_name'];
  1220. $model->goods_market_price = $params['goods_market_price'] ?? 0;
  1221. $model->goods_sales_price = $params['goods_sales_price'] ?? 0;
  1222. $model->goods_sku_json = !empty($params['goods_sku_json_label']) ? json_encode($params['goods_sku_json_label']) : json_encode(['规格' => ['标准']]);
  1223. $model->goods_attribute_json = !empty($params['goods_attribute_json']) ? $params['goods_attribute_json'] : '[]';
  1224. $model->goods_title = $params['goods_title'] ?? '';
  1225. $model->goods_cover = $params['goods_cover'] ?? '';
  1226. $model->goods_on_addtimes = isset($params['goods_on_addtimes']) ? strtotime($params['goods_on_addtimes']) : null;
  1227. $model->goods_sort = $params['goods_sort'] ?? null;
  1228. $model->goods_groupby = $params['goods_groupby'] ?? '';
  1229. $model->goods_remark = $params['goods_remark'] ?? '';
  1230. $model->goods_extend_json = $params['goods_extend_json'] ?? '{}';
  1231. $model->is_support_appointment = $params['is_support_appointment'] ?? 'N';
  1232. $model->creator_user_id = JwtToken::getCurrentId();
  1233. $model->goods_addtimes = time();
  1234. $model->goods_updatetimes = time();
  1235. // {"express":"Y","self":"Y","arrival":"Y"}
  1236. $expressJson = [];
  1237. if (!empty($params['express_json'])) {
  1238. if (in_array('express', $params['express_json'])) {
  1239. $expressJson['express'] = 'Y';
  1240. } else {
  1241. $expressJson['express'] = 'N';
  1242. }
  1243. if (in_array('self', $params['express_json'])) {
  1244. $expressJson['self'] = 'Y';
  1245. } else {
  1246. $expressJson['self'] = 'N';
  1247. }
  1248. if (in_array('arrival', $params['express_json'])) {
  1249. $expressJson['arrival'] = 'Y';
  1250. } else {
  1251. $expressJson['arrival'] = 'N';
  1252. }
  1253. $model->goods_express_json = json_encode($expressJson);
  1254. }
  1255. if (!empty($params['is_support_appointment']) && $params['is_support_appointment'] == 'Y' && !empty($params['appointment_times']) && $params['goods_category'] != 'TRAVEL') {
  1256. $times = [];
  1257. $attributeJsonTimeArr = [];
  1258. $personTotal = 0;
  1259. foreach ($params['appointment_times'] as $time) {
  1260. $attributeJsonTimeArr[] = strtotime(date('Y-m-d ') . $time['appointmentTimeStart']);
  1261. $attributeJsonTimeArr[] = strtotime(date('Y-m-d ') . $time['appointmentTimeEnd']);
  1262. $personTotal += $time['person'];
  1263. $times[$time['appointmentTimeStart']] = [
  1264. 'person' => $time['person'],
  1265. 'duration' => $time['appointmentTimeStart'] . '-' . $time['appointmentTimeEnd']
  1266. ];
  1267. }
  1268. $attributeJsonTime = date('H:i', min($attributeJsonTimeArr)) . '至' . date('H:i', max($attributeJsonTimeArr));
  1269. $newDates = [];
  1270. foreach ($params['dates'] as $date) {
  1271. $key = self::$week[$date];
  1272. $newDates[$key] = $date;
  1273. }
  1274. ksort($newDates);
  1275. $currentDate = current($newDates);
  1276. $lastDate = end($newDates);
  1277. $attributeJsonDate = '';
  1278. if (self::$week[$lastDate] - self::$week[$currentDate] + 1 > count($newDates)) {
  1279. $attributeJsonDate = implode(',', $newDates);
  1280. } else if (self::$week[$lastDate] - self::$week[$currentDate] + 1 == count($newDates)) {
  1281. $attributeJsonDate = $currentDate . '至' . $lastDate;
  1282. }
  1283. // if (isset($params['work_time'])){
  1284. // $workTimeStart = date('H:i',strtotime($params['work_time'][0]));
  1285. // $workTimeEnd = date('H:i',strtotime($params['work_time'][1]));
  1286. // $attributeJsonTime = $workTimeStart.'至'.$workTimeEnd;
  1287. // }
  1288. $attributeJson = [
  1289. 'icon' => $model->goods_cover,
  1290. 'date' => $attributeJsonDate,
  1291. // 'time' => $attributeJsonTime,
  1292. 'dates' => $newDates ? array_values($newDates) : [],
  1293. 'times' => $times,
  1294. 'person' => $personTotal
  1295. ];
  1296. // if(isset($params['address'])){
  1297. // $attributeJson['address'] = $params['address'];
  1298. // }
  1299. // if (isset($params['min_count'])){
  1300. // $attributeJson['min-count'] = $params['min_count'];
  1301. // }else{
  1302. // $attributeJson['min-count'] = 1;
  1303. // }
  1304. // if (isset($params['teachers'])){
  1305. // $attributeJson['teachers'] = $params['teachers'];
  1306. // }
  1307. if (!empty($params['appointment_label'])) {
  1308. $attributeJson['label'] = $params['appointment_label'];
  1309. }
  1310. // if (!empty($params['address'])) {
  1311. // $attributeJson['address'] = $params['address'];
  1312. // }
  1313. // if (!empty($params['position'])){
  1314. // $attributeJson['position'] = $params['position'];
  1315. // }
  1316. // if (isset($params['goods_service_premises'])){
  1317. // $attributeJson['service_premises_id'] = $params['goods_service_premises'];
  1318. // }
  1319. $model->goods_attribute_json = json_encode($attributeJson, JSON_UNESCAPED_UNICODE);
  1320. // $times = [];
  1321. // foreach ($params['appointment_times'] as $time) {
  1322. // $times[$time['appointmentTimeStart']] = [
  1323. // 'person' => $time['person'],
  1324. // 'duration' => $time['appointmentTimeStart'] . '-' . $time['appointmentTimeEnd']
  1325. // ];
  1326. // }
  1327. // $model->goods_attribute_json = json_encode([
  1328. // 'icon' => '',
  1329. // 'dates' => $params['dates'] ?? [],
  1330. // 'times' => $times
  1331. // ]);
  1332. }
  1333. if (!empty($params['is_support_appointment']) && $params['is_support_appointment'] == 'Y' && !empty($params['appointment_times']) && $params['goods_category'] == 'TRAVEL') {
  1334. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  1335. $attributeJson = json_decode($model->goods_attribute_json, true);
  1336. } elseif (empty($model->goods_attribute_json)) {
  1337. $attributeJson = [];
  1338. }
  1339. $attributeJson['travel-day'] = $params['travel_day'];
  1340. $attributeJson['travel-begin'] = $params['travel_begin'];
  1341. $attributeJson['travel-night'] = $params['travel_night'];
  1342. $attributeJson['travel-trans'] = $params['travel_trans'];
  1343. $unixs = [];
  1344. foreach ($params['appointment_times'] as $times) {
  1345. $unix = strtotime($times['days']);
  1346. $unixs[$unix] = $times['person'];
  1347. }
  1348. ksort($unixs);
  1349. $months = [];
  1350. foreach ($unixs as $key => $unix) {
  1351. $month = date('Ym', $key);
  1352. $day = date('d', $key);
  1353. $months[$month][$day] = $unix;
  1354. }
  1355. $attributeJson['month'] = $months;
  1356. $model->goods_attribute_json = json_encode($attributeJson, JSON_UNESCAPED_UNICODE);
  1357. }
  1358. if (!empty($params['goods_json']) && $params['join_goods_category_id'] == 65) {
  1359. $goodsJson = json_decode($params['goods_json'], true);
  1360. // foreach ($goodsJson as $key => $item) {
  1361. // $goodsJson[$key]['color'] = hexToRgb($item['color']);
  1362. // }
  1363. //
  1364. $model->goods_json = json_encode($goodsJson);
  1365. } elseif (!empty($params['goods_json']) && $params['join_goods_category_id'] == 43) {
  1366. $goodsJson = json_decode($params['goods_json'], true);
  1367. $newGoodsJson = [];
  1368. foreach ($goodsJson as $item1) {
  1369. if (empty($item1['title'])) {
  1370. continue;
  1371. }
  1372. $newItem1 = [];
  1373. foreach ($item1['items'] as $item2) {
  1374. $newParams = [];
  1375. foreach ($item2['params'] as $param) {
  1376. if (!empty($param[0]) || !empty($param[1])) {
  1377. $newParams[] = $param;
  1378. }
  1379. }
  1380. $newItem1['items'][$item2['key']] = $newParams;
  1381. }
  1382. $newItem1['service'] = $item1['service'] ?? '';
  1383. $newGoodsJson[$item1['title']] = $newItem1;
  1384. }
  1385. $model->goods_json = json_encode($newGoodsJson);
  1386. } else {
  1387. $model->goods_json = '[]';
  1388. }
  1389. if (!empty($params['goods_premisses'])) {
  1390. $attributeJson = [];
  1391. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  1392. $attributeJson = json_decode($model->goods_attribute_json, true);
  1393. } elseif (empty($model->goods_attribute_json)) {
  1394. $attributeJson = [];
  1395. }
  1396. $attributeJson['premisses'] = $params['goods_premisses'];
  1397. $model->goods_attribute_json = json_encode($attributeJson);
  1398. }
  1399. if (!empty($params['goods_theme_color']) && !empty($params['goods_theme_icon'])) {
  1400. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  1401. $attributeJson = json_decode($model->goods_attribute_json, true);
  1402. } elseif (empty($model->goods_attribute_json)) {
  1403. $attributeJson = [];
  1404. }
  1405. $attributeJson['bg'] = $params['goods_theme_color'];
  1406. $attributeJson['icon'] = str_replace(getenv('STORAGE_DOMAIN'), '', $params['goods_theme_icon']);
  1407. $model->goods_attribute_json = json_encode($attributeJson);
  1408. }
  1409. if (!empty($params['address'])) {
  1410. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  1411. $attributeJson = json_decode($model->goods_attribute_json, true);
  1412. } elseif (empty($model->goods_attribute_json)) {
  1413. $attributeJson = [];
  1414. }
  1415. $attributeJson['address'] = $params['address'];
  1416. $model->goods_attribute_json = json_encode($attributeJson);
  1417. }
  1418. if (!empty($params['position'])) {
  1419. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  1420. $attributeJson = json_decode($model->goods_attribute_json, true);
  1421. } elseif (empty($model->goods_attribute_json)) {
  1422. $attributeJson = [];
  1423. }
  1424. $attributeJson['position'] = $params['position'];
  1425. $model->goods_attribute_json = json_encode($attributeJson);
  1426. }
  1427. if (isset($params['goods_service_premises'])) {
  1428. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  1429. $attributeJson = json_decode($model->goods_attribute_json, true);
  1430. } elseif (empty($model->goods_attribute_json)) {
  1431. $attributeJson = [];
  1432. }
  1433. $attributeJson['service_premises_id'] = $params['goods_service_premises'];
  1434. $model->goods_attribute_json = json_encode($attributeJson);
  1435. }
  1436. if (isset($params['min_count'])) {
  1437. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  1438. $attributeJson = json_decode($model->goods_attribute_json, true);
  1439. } elseif (empty($model->goods_attribute_json)) {
  1440. $attributeJson = [];
  1441. }
  1442. $attributeJson['min-count'] = $params['min_count'];
  1443. $model->goods_attribute_json = json_encode($attributeJson);
  1444. }
  1445. if (isset($params['teachers'])) {
  1446. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  1447. $attributeJson = json_decode($model->goods_attribute_json, true);
  1448. } elseif (empty($model->goods_attribute_json)) {
  1449. $attributeJson = [];
  1450. }
  1451. $attributeJson['teachers'] = $params['teachers'];
  1452. $model->goods_attribute_json = json_encode($attributeJson);
  1453. }
  1454. if (isset($params['work_time'])) {
  1455. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  1456. $attributeJson = json_decode($model->goods_attribute_json, true);
  1457. } elseif (empty($model->goods_attribute_json)) {
  1458. $attributeJson = [];
  1459. }
  1460. $workTimeStart = date('H:i', strtotime($params['work_time'][0]));
  1461. $workTimeEnd = date('H:i', strtotime($params['work_time'][1]));
  1462. $attributeJsonTime = $workTimeStart . '至' . $workTimeEnd;
  1463. $attributeJson['time'] = $attributeJsonTime;
  1464. $model->goods_attribute_json = json_encode($attributeJson);
  1465. }
  1466. if ($model->save()) {
  1467. return $model->goods_id;
  1468. }
  1469. // 异常
  1470. throw new BusinessException("数据写入失败~");
  1471. } catch (\Exception $e) {
  1472. dump($e->getMessage());
  1473. throw new BusinessException("数据写入失败~");
  1474. }
  1475. }
  1476. /**
  1477. * @Desc 详情表
  1478. * @Author Gorden
  1479. * @Date 2024/3/11 11:19
  1480. *
  1481. * @param $params
  1482. * @return void
  1483. * @throws BusinessException
  1484. */
  1485. public static function detailInsert($params)
  1486. {
  1487. if (!empty($params['goods_detail_slider_json'])) {
  1488. $params['goods_detail_slider_json'] = str_replace(getenv('STORAGE_DOMAIN'), '', $params['goods_detail_slider_json']);
  1489. $params['goods_detail_slider_json'] = json_encode(['slider' => $params['goods_detail_slider_json']]);
  1490. }
  1491. if (isset($params['curriculum'])) {
  1492. $params['goods_detail_specs_json'] = json_encode([
  1493. [
  1494. 'key' => '课时',
  1495. 'val' => $params['curriculum']['period'] ?? '',
  1496. ],
  1497. [
  1498. 'key' => '群体',
  1499. 'val' => $params['curriculum']['group'] ?? '',
  1500. ],
  1501. [
  1502. 'key' => '课程',
  1503. 'val' => $params['curriculum']['course'] ?? '',
  1504. ],
  1505. ]);
  1506. }
  1507. try {
  1508. $model = new GoodsDetail();
  1509. $model->join_detail_goods_id = $params['goods_id'];
  1510. $model->goods_detail_code_json = $params['goods_detail_code_json'] ?? '{}';
  1511. $model->goods_detail_slider_json = $params['goods_detail_slider_json'] ?? '{}';
  1512. $model->goods_detail_specs_json = $params['goods_detail_specs_json'] ?? '{}';
  1513. $model->goods_detail_content = $params['goods_detail_content'] ?? '';
  1514. if (!$model->save()) {
  1515. // 异常
  1516. throw new BusinessException("轮播图/详情数据写入失败~");
  1517. }
  1518. } catch (\Exception $e) {
  1519. dump($e->getMessage());
  1520. throw new BusinessException("轮播图/详情数据写入失败~");
  1521. }
  1522. }
  1523. public static function componentUpdate($params, $type = 'insert')
  1524. {
  1525. Db::beginTransaction();
  1526. try {
  1527. $goodsSku = '';
  1528. // 有先删除
  1529. if ($type == 'update') {
  1530. GoodsComponent::where('join_component_master_goods_id', $params['goods_id'])->delete();
  1531. $goodsSku = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->first();
  1532. if ($goodsSku) {
  1533. $goodsSku->goods_sku_market_price = $params['goods_market_price'] ?? 0;
  1534. $goodsSku->goods_sku_sales_price = $params['goods_sales_price'] ?? 0;
  1535. $goodsSku->save();
  1536. }
  1537. }
  1538. if ($type == 'insert' || !$goodsSku) {
  1539. Goods::where('goods_id', $params['goods_id'])->update(['goods_sku_json' => '{"规格": ["标准"]}']);
  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. $data = [];
  1551. foreach ($params['goods_content_list'] as $item) {
  1552. if (!in_array($item['goods_id'], $params['join_component_goods_id'])) {
  1553. continue;
  1554. }
  1555. $goods = Goods::where('goods_id', $params['goods_id'])->first();
  1556. if (!$goods) {
  1557. continue;
  1558. }
  1559. $data[] = [
  1560. 'join_component_master_goods_id' => $params['goods_id'],
  1561. 'join_component_goods_id' => $item['goods_id'] ?? '',
  1562. 'join_component_goods_sku_id' => $item['sku_id'] ?? '',
  1563. 'goods_component_price' => $item['goods_sales_price'] ?? '',
  1564. 'goods_component_cover' => $goods->goods_cover,
  1565. 'goods_component_json' => json_encode(['goods_name' => $item['goods_name'], 'nbr' => $item['nbr'], 'sku_id' => $item['sku_id'] ?? '']),
  1566. 'goods_component_addtimes' => time()
  1567. ];
  1568. }
  1569. if ($data) {
  1570. GoodsComponent::insert($data);
  1571. }
  1572. Db::commit();
  1573. } catch (\Exception $e) {
  1574. Db::rollBack();
  1575. dump($e->getMessage());
  1576. throw new BusinessException("数据写入失败~");
  1577. }
  1578. }
  1579. /**
  1580. * @Desc 标签表
  1581. * @Author Gorden
  1582. * @Date 2024/3/11 11:32
  1583. *
  1584. * @param $params
  1585. * @return void
  1586. * @throws BusinessException
  1587. */
  1588. public static function labelInsert($params)
  1589. {
  1590. $model = new GoodsLabel();
  1591. $model->join_label_goods_id = $params['goods_id'];
  1592. $model->goods_label = $params['goods_label'] ? implode(',', $params['goods_label']) : '';
  1593. $model->goods_label_extend_json = !empty($params['goods_label_extend_json']) ? $params['goods_label_extend_json'] : '{}';
  1594. if (!$model->save()) {
  1595. // 异常
  1596. throw new BusinessException('数据写入失败~');
  1597. }
  1598. }
  1599. /**
  1600. * @Desc 产品运行控制信息表
  1601. * @Author Gorden
  1602. * @Date 2024/3/11 11:38
  1603. *
  1604. * @param $params
  1605. * @return void
  1606. * @throws BusinessException
  1607. */
  1608. public static function goodsRunningInsert($params)
  1609. {
  1610. try {
  1611. $model = new GoodsRunning();
  1612. $model->join_running_goods_id = $params['goods_id'];
  1613. $model->goods_running_storage = $params['goods_running_storage'] ?? 0;
  1614. $model->goods_running_sale = $params['goods_running_sale'] ?? 0;
  1615. $model->goods_running_off_type = !empty($params['goods_running_off_type']) ? $params['goods_running_off_type'] : '';
  1616. $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'])]) : '[]';
  1617. if (!$model->save()) {
  1618. throw new BusinessException('数据写入失败');
  1619. }
  1620. } catch (\Exception $e) {
  1621. dump($e->getMessage());
  1622. throw new BusinessException('数据写入失败');
  1623. }
  1624. }
  1625. /**
  1626. * @Desc SKU
  1627. * @Author Gorden
  1628. * @Date 2024/3/11 12:01
  1629. *
  1630. * @param $params
  1631. * @return void
  1632. * @throws BusinessException
  1633. */
  1634. public static function skuInsert($params)
  1635. {
  1636. $model = new GoodsSku();
  1637. $model->join_sku_goods_id = $params['goods_id'];
  1638. $model->goods_sku_status = $params['goods_sku_status'];
  1639. $model->goods_sku_specs_json = $params['goods_sku_specs_json'];
  1640. $model->goods_sku_title = $params['goods_sku_title'];
  1641. $model->goods_sku_images_json = $params['goods_sku_images_json'];
  1642. $model->goods_sku_content = $params['goods_sku_content'];
  1643. $model->goods_sku_market_price = $params['goods_sku_market_price'];
  1644. $model->goods_sku_sales_price = $params['goods_sku_sales_price'];
  1645. $model->goods_sku_storage_json = $params['goods_sku_storage_json'];
  1646. $model->goods_sku_service_json = $params['goods_sku_service_json'];
  1647. $model->goods_sku_extend_json = $params['goods_sku_extend_json'];
  1648. if (!$model->save()) {
  1649. throw new BusinessException('规格数据写入失败~');
  1650. }
  1651. }
  1652. /**
  1653. * @Desc
  1654. * @Author Gorden
  1655. * @Date 2024/3/12 8:44
  1656. *
  1657. * @param $params
  1658. * @return void
  1659. * @throws BusinessException
  1660. */
  1661. public static function mainUpdate($params)
  1662. {
  1663. try {
  1664. $data = self::inputFilter($params, new Goods());
  1665. if (!empty($data['goods_cover'])) {
  1666. $data['goods_cover'] = str_replace(getenv('STORAGE_DOMAIN'), '', $data['goods_cover']);
  1667. }
  1668. $data['goods_on_addtimes'] = isset($data['goods_on_addtimes']) ? strtotime($data['goods_on_addtimes']) : 0;
  1669. $data['goods_sku_json'] = !empty($params['goods_sku_json_label']) ? json_encode($params['goods_sku_json_label']) : json_encode(['规格' => ['标准']]);
  1670. $row = Goods::find($data['goods_id']);
  1671. if ($row->join_goods_category_id != $data['join_goods_category_id']) {
  1672. $category = SysCategory::where('category_id', $params['join_goods_category_id'])->first();
  1673. if (!$category) {
  1674. throw new BusinessException("产品分类不存在~");
  1675. }
  1676. $data['goods_category'] = $category->category_classify ?? '';
  1677. $data['goods_prefix'] = $data['goods_prefix'] ?? ($category->category_name ? '【' . $category->category_name . '】' : '');
  1678. }
  1679. if (!empty($params['goods_recommend_index'])) {
  1680. $data['goods_category'] = $params['goods_recommend_index'];
  1681. }
  1682. // 上架时间有变动
  1683. if ($data['goods_status'] == 'PENDING' && $row->goods_on_addtimes != $data['goods_on_addtimes']) {
  1684. $redis = Redis::connection();
  1685. // 删掉原来的
  1686. $oldKey = Goods::LISTING_KEY_PREFIX . date('YmdHi', $row->goods_on_addtimes);
  1687. $redis->srem($oldKey, $data['goods_id']);
  1688. // 加入新的
  1689. $newKey = Goods::LISTING_KEY_PREFIX . date('YmdHi', $data['goods_on_addtimes']);
  1690. $redis->sadd($newKey, $data['goods_id']);
  1691. }
  1692. $expressJson = [];
  1693. if (!empty($params['express_json'])) {
  1694. if (in_array('express', $params['express_json'])) {
  1695. $expressJson['express'] = 'Y';
  1696. } else {
  1697. $expressJson['express'] = 'N';
  1698. }
  1699. if (in_array('self', $params['express_json'])) {
  1700. $expressJson['self'] = 'Y';
  1701. } else {
  1702. $expressJson['self'] = 'N';
  1703. }
  1704. if (in_array('arrival', $params['express_json'])) {
  1705. $expressJson['arrival'] = 'Y';
  1706. } else {
  1707. $expressJson['arrival'] = 'N';
  1708. }
  1709. $data['goods_express_json'] = json_encode($expressJson);
  1710. }
  1711. if (!empty($params['is_support_appointment']) && $params['is_support_appointment'] == 'Y' && !empty($params['appointment_times']) && $params['goods_category'] != 'TRAVEL') {
  1712. $attributeJson = [];
  1713. if (!empty($row->goods_attribute_json)) {
  1714. $attributeJson = json_decode($row->goods_attribute_json, true);
  1715. }
  1716. $times = [];
  1717. $attributeJsonTimeArr = [];
  1718. $personTotal = 0;
  1719. foreach ($params['appointment_times'] as $time) {
  1720. $attributeJsonTimeArr[] = strtotime(date('Y-m-d ') . $time['appointmentTimeStart']);
  1721. $attributeJsonTimeArr[] = strtotime(date('Y-m-d ') . $time['appointmentTimeEnd']);
  1722. $personTotal += $time['person'];
  1723. $times[$time['appointmentTimeStart']] = [
  1724. 'person' => $time['person'],
  1725. 'duration' => $time['appointmentTimeStart'] . '-' . $time['appointmentTimeEnd']
  1726. ];
  1727. }
  1728. $attributeJsonTime = date('H:i', min($attributeJsonTimeArr)) . '至' . date('H:i', max($attributeJsonTimeArr));
  1729. $newDates = [];
  1730. foreach ($params['dates'] as $date) {
  1731. $key = self::$week[$date];
  1732. $newDates[$key] = $date;
  1733. }
  1734. ksort($newDates);
  1735. $currentDate = current($newDates);
  1736. $lastDate = end($newDates);
  1737. $attributeJsonDate = '';
  1738. if (self::$week[$lastDate] - self::$week[$currentDate] + 1 > count($newDates)) {
  1739. $attributeJsonDate = implode(',', $newDates);
  1740. } else if (self::$week[$lastDate] - self::$week[$currentDate] + 1 == count($newDates)) {
  1741. $attributeJsonDate = $currentDate . '至' . $lastDate;
  1742. }
  1743. $attributeJson['date'] = $attributeJsonDate;
  1744. $attributeJson['icon'] = $data['goods_cover'] ?? '';
  1745. $attributeJson['time'] = $attributeJsonTime;
  1746. $attributeJson['dates'] = $newDates ? array_values($newDates) : [];
  1747. $attributeJson['times'] = $times;
  1748. $attributeJson['person'] = $personTotal;
  1749. if (!empty($params['appointment_label'])) {
  1750. $attributeJson['label'] = $params['appointment_label'];
  1751. }
  1752. // if (!empty($params['address'])) {
  1753. // $attributeJson['address'] = $params['address'];
  1754. // }
  1755. // if (!empty($params['position'])){
  1756. // $attributeJson['position'] = $params['position'];
  1757. // }
  1758. // if (isset($params['goods_service_premises'])){
  1759. // $attributeJson['service_premises_id'] = $params['goods_service_premises'];
  1760. // }
  1761. // if (isset($params['work_time'])){
  1762. // $workTimeStart = date('H:i',strtotime($params['work_time'][0]));
  1763. // $workTimeEnd = date('H:i',strtotime($params['work_time'][1]));
  1764. // $attributeJson['time'] = $workTimeStart.'至'.$workTimeEnd;
  1765. // }
  1766. // if (isset($params['min_count'])){
  1767. // $attributeJson['min-count'] = $params['min_count'];
  1768. // }else{
  1769. // $attributeJson['min-count'] = 1;
  1770. // }
  1771. // if (isset($params['teachers'])){
  1772. // $attributeJson['teachers'] = $params['teachers'];
  1773. // }
  1774. $data['goods_attribute_json'] = json_encode($attributeJson, JSON_UNESCAPED_UNICODE);
  1775. }
  1776. if (!empty($params['is_support_appointment']) && $params['is_support_appointment'] == 'Y' && !empty($params['appointment_times']) && $params['goods_category'] == 'TRAVEL') {
  1777. $attributeJson = [];
  1778. if (!empty($row->goods_attribute_json)) {
  1779. $attributeJson = json_decode($row->goods_attribute_json, true);
  1780. }
  1781. $attributeJson['travel-day'] = $params['travel_day'];
  1782. $attributeJson['travel-begin'] = $params['travel_begin'];
  1783. $attributeJson['travel-night'] = $params['travel_night'];
  1784. $attributeJson['travel-trans'] = $params['travel_trans'];
  1785. $unixs = [];
  1786. foreach ($params['appointment_times'] as $times) {
  1787. $unix = strtotime($times['days']);
  1788. $unixs[$unix] = $times['person'];
  1789. }
  1790. ksort($unixs);
  1791. $months = [];
  1792. foreach ($unixs as $key => $unix) {
  1793. $month = date('Ym', $key);
  1794. $day = date('d', $key);
  1795. $months[$month][$day] = $unix;
  1796. }
  1797. $attributeJson['month'] = $months;
  1798. $data['goods_attribute_json'] = json_encode($attributeJson, JSON_UNESCAPED_UNICODE);
  1799. }
  1800. if (!empty($params['goods_premisses'])) {
  1801. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  1802. if (is_json($data['goods_attribute_json'])) {
  1803. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  1804. } else {
  1805. $data['goods_attribute_json'] = [];
  1806. }
  1807. } elseif (empty($data['goods_attribute_json'])) {
  1808. $data['goods_attribute_json'] = [];
  1809. }
  1810. $data['goods_attribute_json']['premisses'] = $params['goods_premisses'];
  1811. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  1812. }
  1813. if (!empty($data['goods_json']) && $data['join_goods_category_id'] == 65) {
  1814. // $goodsJson = json_decode($data['goods_json'], true);
  1815. // foreach ($goodsJson as $key => $item) {
  1816. // $goodsJson[$key]['color'] = hexToRgb($item['color']);
  1817. // }
  1818. //
  1819. // $data['goods_json'] = json_encode($goodsJson);
  1820. } elseif (!empty($data['goods_json']) && $data['join_goods_category_id'] == 43) {
  1821. $goodsJson = json_decode($data['goods_json'], true);
  1822. $newGoodsJson = [];
  1823. foreach ($goodsJson as $item1) {
  1824. if (empty($item1['title'])) {
  1825. continue;
  1826. }
  1827. $newItem1 = [];
  1828. foreach ($item1['items'] as $item2) {
  1829. $newParams = [];
  1830. foreach ($item2['params'] as $param) {
  1831. if (!empty($param[0]) || !empty($param[1])) {
  1832. $newParams[] = $param;
  1833. }
  1834. }
  1835. $newItem1['items'][$item2['key']] = $newParams;
  1836. }
  1837. $newItem1['service'] = $item1['service'];
  1838. $newGoodsJson[$item1['title']] = $newItem1;
  1839. }
  1840. $data['goods_json'] = json_encode($newGoodsJson);
  1841. } else {
  1842. $data['goods_json'] = '[]';
  1843. }
  1844. if (!empty($params['goods_theme_color']) && !empty($params['goods_theme_icon'])) {
  1845. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  1846. if (is_json($data['goods_attribute_json'])) {
  1847. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  1848. } else {
  1849. $data['goods_attribute_json'] = [];
  1850. }
  1851. } elseif (empty($data['goods_attribute_json'])) {
  1852. $data['goods_attribute_json'] = [];
  1853. }
  1854. $data['goods_attribute_json']['bg'] = $params['goods_theme_color'];
  1855. $data['goods_attribute_json']['icon'] = str_replace(getenv('STORAGE_DOMAIN'), '', $params['goods_theme_icon']);
  1856. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  1857. }
  1858. if (!empty($params['address'])) {
  1859. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  1860. if (is_json($data['goods_attribute_json'])) {
  1861. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  1862. } else {
  1863. $data['goods_attribute_json'] = [];
  1864. }
  1865. } elseif (empty($data['goods_attribute_json'])) {
  1866. $data['goods_attribute_json'] = [];
  1867. }
  1868. $data['goods_attribute_json']['address'] = $params['address'];
  1869. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  1870. }
  1871. if (!empty($params['position'])) {
  1872. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  1873. if (is_json($data['goods_attribute_json'])) {
  1874. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  1875. } else {
  1876. $data['goods_attribute_json'] = [];
  1877. }
  1878. } elseif (empty($data['goods_attribute_json'])) {
  1879. $data['goods_attribute_json'] = [];
  1880. }
  1881. $data['goods_attribute_json']['position'] = $params['position'];
  1882. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  1883. }
  1884. if (isset($params['goods_service_premises'])) {
  1885. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  1886. if (is_json($data['goods_attribute_json'])) {
  1887. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  1888. } else {
  1889. $data['goods_attribute_json'] = [];
  1890. }
  1891. } elseif (empty($data['goods_attribute_json'])) {
  1892. $data['goods_attribute_json'] = [];
  1893. }
  1894. $data['goods_attribute_json']['service_premises_id'] = $params['goods_service_premises'];
  1895. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  1896. }
  1897. if (isset($params['work_time'])) {
  1898. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  1899. if (is_json($data['goods_attribute_json'])) {
  1900. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  1901. } else {
  1902. $data['goods_attribute_json'] = [];
  1903. }
  1904. } elseif (empty($data['goods_attribute_json'])) {
  1905. $data['goods_attribute_json'] = [];
  1906. }
  1907. $workTimeStart = date('H:i', strtotime($params['work_time'][0]));
  1908. $workTimeEnd = date('H:i', strtotime($params['work_time'][1]));
  1909. $data['goods_attribute_json']['time'] = $workTimeStart . '至' . $workTimeEnd;
  1910. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  1911. }
  1912. if (isset($params['min_count'])) {
  1913. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  1914. if (is_json($data['goods_attribute_json'])) {
  1915. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  1916. } else {
  1917. $data['goods_attribute_json'] = [];
  1918. }
  1919. } elseif (empty($data['goods_attribute_json'])) {
  1920. $data['goods_attribute_json'] = [];
  1921. }
  1922. $data['goods_attribute_json']['min-count'] = $params['min_count'];
  1923. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  1924. }
  1925. if (isset($params['teachers'])) {
  1926. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  1927. if (is_json($data['goods_attribute_json'])) {
  1928. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  1929. } else {
  1930. $data['goods_attribute_json'] = [];
  1931. }
  1932. } elseif (empty($data['goods_attribute_json'])) {
  1933. $data['goods_attribute_json'] = [];
  1934. }
  1935. $data['goods_attribute_json']['teachers'] = $params['teachers'];
  1936. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  1937. }
  1938. foreach ($data as $key => $val) {
  1939. $row->{$key} = $val;
  1940. }
  1941. $row->goods_updatetimes = time();
  1942. $row->save();
  1943. } catch (BusinessException $e) {
  1944. throw new BusinessException($e->getMessage());
  1945. } catch (\Exception $e) {
  1946. dump($e->getTrace());
  1947. throw new BusinessException('数据更新异常~1');
  1948. }
  1949. }
  1950. /**
  1951. * @Desc
  1952. * @Author Gorden
  1953. * @Date 2024/3/12 9:57
  1954. *
  1955. * @param $params
  1956. * @return void
  1957. * @throws BusinessException
  1958. */
  1959. public static function detailUpdate($params)
  1960. {
  1961. try {
  1962. $data = self::inputFilter($params, new GoodsDetail());
  1963. if (!empty($data['goods_detail_slider_json'])) {
  1964. $data['goods_detail_slider_json'] = str_replace(getenv('STORAGE_DOMAIN'), '', $data['goods_detail_slider_json']);
  1965. $data['goods_detail_slider_json'] = json_encode(['slider' => $data['goods_detail_slider_json']]);
  1966. }
  1967. if (isset($params['curriculum'])) {
  1968. $data['goods_detail_specs_json'] = json_encode([
  1969. [
  1970. 'key' => '课时',
  1971. 'val' => $params['curriculum']['period'] ?? '',
  1972. ],
  1973. [
  1974. 'key' => '群体',
  1975. 'val' => $params['curriculum']['group'] ?? '',
  1976. ],
  1977. [
  1978. 'key' => '课程',
  1979. 'val' => $params['curriculum']['course'] ?? '',
  1980. ],
  1981. ]);
  1982. }
  1983. // 根据goods_id 查详情ID
  1984. $detail = GoodsDetail::where('join_detail_goods_id', $params['goods_id'])->first();
  1985. if ($detail) {
  1986. self::doUpdate($detail->join_detail_goods_id, $data, new GoodsDetail());
  1987. } else {
  1988. $data['join_detail_goods_id'] = $params['goods_id'];
  1989. GoodsDetail::insert($data);
  1990. }
  1991. } catch (BusinessException $e) {
  1992. throw new BusinessException($e->getMessage());
  1993. } catch (\Exception $e) {
  1994. dump($e->getMessage());
  1995. throw new BusinessException('轮播图/详情数据更新异常~');
  1996. }
  1997. }
  1998. /**
  1999. * @Desc
  2000. * @Author Gorden
  2001. * @Date 2024/3/12 9:58
  2002. *
  2003. * @param $params
  2004. * @return void
  2005. * @throws BusinessException
  2006. */
  2007. public static function labelUpdate($params)
  2008. {
  2009. try {
  2010. $data = self::inputFilter($params, new GoodsLabel());
  2011. // 根据goods_id 查详情ID
  2012. $detail = GoodsLabel::where('join_label_goods_id', $params['goods_id'])->first();
  2013. if ($detail) {
  2014. self::doUpdate($detail->goods_label_id, $data, new GoodsLabel());
  2015. } else {
  2016. $data['join_label_goods_id'] = $params['goods_id'];
  2017. GoodsLabel::insert($data);
  2018. }
  2019. } catch (BusinessException $e) {
  2020. throw new BusinessException($e->getMessage());
  2021. } catch (\Exception $e) {
  2022. dump($e->getMessage());
  2023. throw new BusinessException('数据更新异常~3');
  2024. }
  2025. }
  2026. /**
  2027. * @Desc
  2028. * @Author Gorden
  2029. * @Date 2024/3/12 9:59
  2030. *
  2031. * @param $params
  2032. * @return void
  2033. * @throws BusinessException
  2034. */
  2035. public static function goodsRunningUpdate($params)
  2036. {
  2037. try {
  2038. $data = self::inputFilter($params, new GoodsRunning());
  2039. // 根据goods_id 查详情ID
  2040. $detail = GoodsRunning::where('join_running_goods_id', $params['goods_id'])->first();
  2041. if (!empty($params['goods_running_off_type']) && $params['goods_running_off_type'] == 'T') {
  2042. $redis = Redis::connection();
  2043. if (!empty($detail->goods_running_off_json)) {
  2044. $goodsRunningOffJson = json_decode($detail->goods_running_off_json, true);
  2045. if (isset($goodsRunningOffJson['time'])) {
  2046. $oldKey = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi', $goodsRunningOffJson['time']);
  2047. $goodsRunningOffJson['time'] = strtotime($params['goods_off_addtimes']);
  2048. $data['goods_running_off_json'] = json_encode($goodsRunningOffJson);
  2049. // 有老的下架时间,删除老的
  2050. $redis->srem($oldKey, $params['goods_id']);
  2051. } else {
  2052. $goodsRunningOffJson['time'] = strtotime($params['goods_off_addtimes']);
  2053. $data['goods_running_off_json'] = json_encode($goodsRunningOffJson);
  2054. }
  2055. } else {
  2056. $data['goods_running_off_json'] = json_encode(['time' => strtotime($params['goods_off_addtimes'])]);
  2057. }
  2058. // 加入自动下架
  2059. $newKey = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi', strtotime($params['goods_off_addtimes']));
  2060. $redis->sAdd($newKey, $params['goods_id']);
  2061. } 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') {
  2062. $goodsRunningOffJson = json_decode($detail->goods_running_off_json, true);
  2063. if (isset($goodsRunningOffJson['time'])) {
  2064. $oldKey = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi', $goodsRunningOffJson['time']);
  2065. $redis = Redis::connection();
  2066. $redis->srem($oldKey, $params['goods_id']);
  2067. }
  2068. }
  2069. if ($detail) {
  2070. self::doUpdate($detail->join_running_goods_id, $data, new GoodsRunning());
  2071. } else {
  2072. // 兼容老数据……
  2073. $data['join_running_goods_id'] = $params['goods_id'];
  2074. GoodsRunning::insert($data);
  2075. }
  2076. } catch (BusinessException $e) {
  2077. throw new BusinessException($e->getMessage());
  2078. } catch (\Exception $e) {
  2079. dump($e->getMessage());
  2080. throw new BusinessException('数据更新异常~');
  2081. }
  2082. }
  2083. /**
  2084. * @Desc sku 设置
  2085. * @Author Gorden
  2086. * @Date 2024/4/10 10:43
  2087. *
  2088. * @param $params
  2089. * @return void
  2090. * @throws BusinessException
  2091. */
  2092. public static function goodsSkuSet($params, $operation = 'insert')
  2093. {
  2094. try {
  2095. Db::beginTransaction();
  2096. $skusOldIds = [];
  2097. if ($operation == 'update') {
  2098. // 查出所有的
  2099. $skusOldIds = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->pluck('goods_sku_id', 'goods_sku_id');
  2100. // 删掉原有的
  2101. // GoodsSku::where('join_sku_goods_id', $params['goods_id'])->delete();
  2102. }
  2103. if (empty($skusOldIds) && empty($params['goods_sku_json_value'])) {
  2104. $skuData = [
  2105. 'join_sku_goods_id' => $params['goods_id'],
  2106. 'goods_sku_status' => 'ON',
  2107. 'goods_sku_specs_json' => '{"规格": "标准"}',
  2108. 'goods_sku_title' => "标准" . $params['goods_name'],
  2109. 'goods_sku_market_price' => $params['goods_market_price'] ?? 0,
  2110. 'goods_sku_sales_price' => $params['goods_sales_price'] ?? 0,
  2111. ];
  2112. GoodsSku::insert($skuData);
  2113. }
  2114. // 入新的
  2115. if (!empty($params['goods_sku_json_value'])) {
  2116. foreach ($params['goods_sku_json_value'] as $item) {
  2117. $skus = explode(',', $item['sku']);
  2118. $skuArr = [];
  2119. for ($i = 1; $i <= count($skus); $i++) {
  2120. $skuName = "skuName" . $i;
  2121. $key = $item[$skuName];
  2122. $skuArr[$key] = $skus[$i - 1];
  2123. }
  2124. $specsJson = json_encode($skuArr);
  2125. $skuTitle = str_replace('-', ',', $item['sku']) . $params['goods_name'];
  2126. if ($operation == 'update' && !empty($item['sku_id'])) {
  2127. $model = GoodsSku::where('goods_sku_id', $item['sku_id'])->first();
  2128. if (!$model) {
  2129. $model = new GoodsSku();
  2130. } else {
  2131. unset($skusOldIds[$model->goods_sku_id]);
  2132. }
  2133. } else {
  2134. $model = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->where('goods_sku_title', $skuTitle)->first();
  2135. if (!$model) {
  2136. $model = new GoodsSku();
  2137. } else {
  2138. unset($skusOldIds[$model->goods_sku_id]);
  2139. }
  2140. }
  2141. $model->join_sku_goods_id = $params['goods_id'];
  2142. $model->goods_sku_status = $params['goods_status'];
  2143. $model->goods_sku_specs_json = $specsJson;
  2144. $model->goods_sku_title = $skuTitle;
  2145. $model->goods_sku_market_price = $params['goods_market_price'] ?? 0;
  2146. $model->goods_sku_sales_price = $item['price'];
  2147. $model->goods_sku_storage_json = json_encode(['storage' => $item['stock']]);
  2148. $model->save();
  2149. }
  2150. }
  2151. if ($operation == 'update' && !empty($skusOldIds)) {
  2152. GoodsSku::whereIn('goods_sku_id', $skusOldIds)->delete();
  2153. }
  2154. Db::commit();
  2155. } catch (\Exception $e) {
  2156. dump($e->getTrace());
  2157. Db::rollBack();
  2158. throw new BusinessException('规格数据更新异常~');
  2159. }
  2160. }
  2161. /**
  2162. * @Desc
  2163. * @Author Gorden
  2164. * @Date 2024/3/12 8:45
  2165. *
  2166. * @param array $data
  2167. * @param $model
  2168. * @return array
  2169. * @throws BusinessException
  2170. */
  2171. private static function inputFilter(array $data, $model): array
  2172. {
  2173. $table = config('database.connections.mysql.prefix') . $model->getTable();
  2174. $allow_column = $model->getConnection()->select("desc `$table`");
  2175. if (!$allow_column) {
  2176. throw new BusinessException('表不存在', 2);
  2177. }
  2178. $columns = array_column($allow_column, 'Type', 'Field');
  2179. foreach ($data as $col => $item) {
  2180. if (!isset($columns[$col])) {
  2181. unset($data[$col]);
  2182. continue;
  2183. }
  2184. // 非字符串类型传空则为null
  2185. if ($item === '' && strpos(strtolower($columns[$col]), 'varchar') === false && strpos(strtolower($columns[$col]), 'text') === false) {
  2186. $data[$col] = null;
  2187. }
  2188. if (is_array($item)) {
  2189. $data[$col] = implode(',', $item);
  2190. }
  2191. if ($item != '' && (strpos(strtolower($columns[$col]), 'varchar') || strpos(strtolower($columns[$col]), 'text'))) {
  2192. // $data[$col] = htmlspecialchars($item);
  2193. }
  2194. }
  2195. if (empty($data['created_at'])) {
  2196. unset($data['created_at']);
  2197. }
  2198. if (empty($data['updated_at'])) {
  2199. unset($data['updated_at']);
  2200. }
  2201. return $data;
  2202. }
  2203. /**
  2204. * @Desc 执行更新
  2205. * @Author Gorden
  2206. * @Date 2024/3/12 8:43
  2207. *
  2208. * @param $id
  2209. * @param $data
  2210. * @param $model
  2211. * @return void
  2212. */
  2213. private static function doUpdate($id, $data, $model)
  2214. {
  2215. $row = $model->find($id);
  2216. foreach ($data as $key => $val) {
  2217. $row->{$key} = $val;
  2218. }
  2219. $row->save();
  2220. }
  2221. /**
  2222. * @Desc 上架定时
  2223. * @Author Gorden
  2224. * @Date 2024/4/11 15:13
  2225. *
  2226. * @return void
  2227. */
  2228. public static function checkListing()
  2229. {
  2230. $key = Goods::LISTING_KEY_PREFIX . date('YmdHi');
  2231. $redis = Redis::connection();
  2232. if (!$redis->exists($key)) {
  2233. return;
  2234. }
  2235. $goodsIds = $redis->sMembers($key);
  2236. if (Goods::whereIn('goods_id', $goodsIds)->update(['goods_status' => 'ON'])) {
  2237. $redis->del($key);
  2238. }
  2239. }
  2240. /**
  2241. * @Desc 自动下架
  2242. * @Author Gorden
  2243. * @Date 2024/4/26 15:26
  2244. *
  2245. * @return void
  2246. */
  2247. public static function checkOffListing()
  2248. {
  2249. $key = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi');
  2250. $redis = Redis::connection();
  2251. if (!$redis->exists($key)) {
  2252. return;
  2253. }
  2254. $goodsIds = $redis->sMembers($key);
  2255. if (Goods::whereIn('goods_id', $goodsIds)->update(['goods_status' => 'OFF'])) {
  2256. $redis->del($key);
  2257. }
  2258. }
  2259. public static $week = [
  2260. '周一' => 1,
  2261. '周二' => 2,
  2262. '周三' => 3,
  2263. '周四' => 4,
  2264. '周五' => 5,
  2265. '周六' => 6,
  2266. '周日' => 7,
  2267. ];
  2268. }