GoodsService.php 113 KB

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