GoodsService.php 116 KB

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