GoodsService.php 112 KB

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