GoodsService.php 126 KB

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