GoodsService.php 107 KB

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