GoodsService.php 128 KB

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