GoodsService.php 111 KB

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