GoodsService.php 129 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864
  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. ->whereIn('category_status', ['ACTIVED', 'UNSHOW'])
  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. ->whereIn('category_status', ['ACTIVED', 'UNSHOW'])
  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. $data['couponContentList'] = [];
  1234. $data['coupon_id'] = [];
  1235. if (!empty($goodsAttributeJson['coupon'])) {
  1236. foreach ($goodsAttributeJson['coupon'] as $key => $coupon) {
  1237. $data['coupon_id'][] = $key;
  1238. $data['couponContentList'][] = [
  1239. 'coupon_id' => $key,
  1240. 'coupon_name' => $coupon['name'],
  1241. 'nbr' => $coupon['num']
  1242. ];
  1243. }
  1244. }
  1245. }
  1246. $data['goods_on_addtimes'] = date('Y-m-d\TH:i:s.u\Z', $data['goods_on_addtimes'] - 60 * 60 * 8);
  1247. return json_success('', $data);
  1248. } catch (\Exception $e) {
  1249. dump($e->getTrace());
  1250. return json_fail("查询错误~");
  1251. }
  1252. }
  1253. /**
  1254. * @Desc 添加商品
  1255. * @Author Gorden
  1256. * @Date 2024/3/11 10:20
  1257. *
  1258. * @param $params
  1259. * @return Response
  1260. */
  1261. public static function insert($params): Response
  1262. {
  1263. Db::beginTransaction();
  1264. try {
  1265. $params['goods_id'] = "GD" . date('ymdHi') . random_string(4, 'up');
  1266. // 主表
  1267. self::mainInsert($params);
  1268. // 商品详情表
  1269. self::detailInsert($params);
  1270. // 商品标签表
  1271. self::labelInsert($params);
  1272. // 产品运行控制信息表
  1273. self::goodsRunningInsert($params);
  1274. // sku表
  1275. self::goodsSkuSet($params, 'insert');
  1276. // 待上架状态,上架时间大于当前时间
  1277. if ($params['goods_status'] == 'PENDING' && strtotime($params['goods_on_addtimes']) > time()) {
  1278. $redis = Redis::connection();
  1279. $key = date('YmdHi', strtotime($params['goods_on_addtimes']));
  1280. $redis->sAdd(Goods::LISTING_KEY_PREFIX . $key, $params['goods_id']);
  1281. }
  1282. // 自动下架
  1283. if (!empty($params['goods_running_off_type']) && $params['goods_running_off_type'] == 'T' && !empty($params['goods_off_addtimes'])) {
  1284. $redis = Redis::connection();
  1285. $key = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi', strtotime($params['goods_off_addtimes']));
  1286. $redis->sAdd($key, $params['goods_id']);
  1287. }
  1288. Db::commit();
  1289. } catch (\PDOException $e) {
  1290. Db::rollBack();
  1291. dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
  1292. return json_fail('数据写入失败~');
  1293. } catch (BusinessException $e) {
  1294. Db::rollBack();
  1295. dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
  1296. return json_fail($e->getMessage());
  1297. } catch (\Exception $e) {
  1298. Db::rollBack();
  1299. dump($e->getTrace());
  1300. return json_fail('数据写入失败~');
  1301. }
  1302. _syslog("添加商品", "商品名【" . $params['goods_name'] . "】");
  1303. return json_success('success');
  1304. }
  1305. public static function insertRecharge($params)
  1306. {
  1307. try {
  1308. Db::beginTransaction();
  1309. $goods = new Goods();
  1310. $goods->goods_id = "GD" . date('ymdHi') . random_string(4, 'up');
  1311. $goods->join_goods_category_id = 59;
  1312. $goods->goods_classify = 'RECHARGE';
  1313. $goods->goods_status = $params['goods_status'];
  1314. $goods->goods_sort = $params['goods_sort'];
  1315. $goods->goods_category = $params['goods_category'];
  1316. $goods->goods_name = $params['goods_name'];
  1317. $goods->goods_market_price = $params['goods_sales_price'];
  1318. $goods->goods_sales_price = $params['goods_sales_price'];
  1319. $goods->goods_sku_json = json_encode(['规格' => [$params['goods_sales_price'] . '元']]);
  1320. $goods->goods_attribute_json = json_encode(['added' => ['nbr' => $params['goods_rate'] / 100, 'mode' => 'rate'], 'min-count' => 1]);
  1321. $goods->goods_cover = '/images/app/common/null-service.png';
  1322. $goods->goods_process_json = json_encode(['mode' => 'do_shopping']);
  1323. $goods->goods_addtimes = time();
  1324. $goods->save();
  1325. $sku = new GoodsSku();
  1326. $sku->join_sku_goods_id = $goods->goods_id;
  1327. $sku->goods_sku_status = $params['goods_status'];
  1328. $sku->goods_sku_specs_json = json_encode(['规格' => $params['goods_sales_price'] . '元']);
  1329. $sku->goods_sku_market_price = $params['goods_sales_price'];
  1330. $sku->goods_sku_sales_price = $params['goods_sales_price'];
  1331. $sku->goods_sku_storage_json = json_encode(['storage' => 9999]);
  1332. $sku->save();
  1333. Db::commit();
  1334. return json_success('success');
  1335. } catch (\Exception $e) {
  1336. Db::rollBack();
  1337. return json_fail('添加充值产品失败');
  1338. }
  1339. }
  1340. public static function updateRecharge($params)
  1341. {
  1342. try {
  1343. Db::beginTransaction();
  1344. $goods = Goods::where('goods_id', $params['goods_id'])->first();
  1345. if (!$goods) {
  1346. return json_fail("数据异常");
  1347. }
  1348. $goods->goods_market_price = $params['goods_sales_price'];
  1349. $goods->goods_sales_price = $params['goods_sales_price'];
  1350. $goods->goods_status = $params['goods_status'];
  1351. $goods->goods_sku_json = json_encode(['规格' => [$params['goods_sales_price'] . '元']]);
  1352. $goods->goods_sort = $params['goods_sort'];
  1353. $goods->goods_updatetimes = time();
  1354. $goods->updator_user_id = JwtToken::getCurrentId();
  1355. if (!empty($goods->goods_attribute_json)) {
  1356. $attributeJson = json_decode($goods->goods_attribute_json, true);
  1357. $attributeJson['added']['nbr'] = $params['goods_rate'] / 100;
  1358. $goods->goods_attribute_json = json_encode($attributeJson);
  1359. }
  1360. $goods->save();
  1361. $sku = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->where('goods_sku_status', 'ON')->first();
  1362. $sku->goods_sku_status = $params['goods_status'];
  1363. $sku->goods_sku_specs_json = json_encode(['规格' => $params['goods_sales_price'] . '元']);
  1364. $sku->goods_sku_market_price = $params['goods_sales_price'];
  1365. $sku->goods_sku_sales_price = $params['goods_sales_price'];
  1366. $sku->save();
  1367. Db::commit();
  1368. return json_success("success");
  1369. } catch (\Exception $e) {
  1370. Db::rollBack();
  1371. dump($e->getMessage());
  1372. return json_fail('编辑充值产品失败');
  1373. }
  1374. }
  1375. public static function insertPackage($params): Response
  1376. {
  1377. Db::beginTransaction();
  1378. try {
  1379. $params['goods_id'] = "GD" . date('ymdHi') . random_string(4, 'up');
  1380. // 主表
  1381. self::mainInsert($params);
  1382. // 商品详情表
  1383. self::detailInsert($params);
  1384. // 套包组件表
  1385. self::componentUpdate($params, 'insert');
  1386. // 商品标签表
  1387. self::labelInsert($params);
  1388. // 产品运行控制信息表
  1389. self::goodsRunningInsert($params);
  1390. // sku表
  1391. // self::goodsSkuSet($params, 'insert');
  1392. // 待上架状态,上架时间大于当前时间
  1393. if ($params['goods_status'] == 'PENDING' && strtotime($params['goods_on_addtimes']) > time()) {
  1394. $redis = Redis::connection();
  1395. $key = date('YmdHi', strtotime($params['goods_on_addtimes']));
  1396. $redis->sAdd(Goods::LISTING_KEY_PREFIX . $key, $params['goods_id']);
  1397. }
  1398. Db::commit();
  1399. } catch (BusinessException $e) {
  1400. Db::rollBack();
  1401. dump($e->getMessage());
  1402. return json_fail($e->getMessage());
  1403. } catch (\Exception $e) {
  1404. Db::rollBack();
  1405. dump($e->getTrace());
  1406. return json_fail('数据写入失败~');
  1407. }
  1408. _syslog("添加套餐", "商品名【" . $params['goods_name'] . "】");
  1409. return json_success('success');
  1410. }
  1411. public static function update($params)
  1412. {
  1413. Db::beginTransaction();
  1414. try {
  1415. // 主表
  1416. self::mainUpdate($params);
  1417. // 商品详情表
  1418. self::detailUpdate($params);
  1419. // 商品标签表
  1420. self::labelUpdate($params);
  1421. // 产品运行控制信息表
  1422. self::goodsRunningUpdate($params);
  1423. // sku表
  1424. self::goodsSkuSet($params, 'update');
  1425. Db::commit();
  1426. } catch (BusinessException $e) {
  1427. Db::rollBack();
  1428. return json_fail($e->getMessage());
  1429. } catch (\Exception $e) {
  1430. Db::rollBack();
  1431. return json_fail('数据更新失败~');
  1432. }
  1433. _syslog("编辑商品", "商品名【" . $params['goods_name'] . "】" ?? "商品ID:【" . $params['goods_id'] . "】");
  1434. return json_success('success');
  1435. }
  1436. public static function changeStatus($params)
  1437. {
  1438. try {
  1439. Goods::where('goods_id', $params['goods_id'])->update(['goods_status' => $params['goods_status']]);
  1440. return json_success('修改成功');
  1441. } catch (\Exception $e) {
  1442. return json_fail('修改状态失败');
  1443. }
  1444. }
  1445. public static function updatePackage($params)
  1446. {
  1447. Db::beginTransaction();
  1448. try {
  1449. // 主表
  1450. self::mainUpdate($params);
  1451. // 商品详情表
  1452. self::detailUpdate($params);
  1453. // 套包组件表
  1454. self::componentUpdate($params, 'update');
  1455. // 商品标签表
  1456. self::labelUpdate($params);
  1457. // 产品运行控制信息表
  1458. self::goodsRunningUpdate($params);
  1459. // sku表
  1460. // self::goodsSkuSet($params, 'update');
  1461. Db::commit();
  1462. } catch (BusinessException $e) {
  1463. Db::rollBack();
  1464. return json_fail($e->getMessage());
  1465. } catch (\Exception $e) {
  1466. Db::rollBack();
  1467. dump($e->getTrace());
  1468. return json_fail('数据更新失败~');
  1469. }
  1470. _syslog("编辑套餐", "商品名【" . $params['goods_name'] . "】" ?? "商品ID:【" . $params['goods_id'] . "】");
  1471. return json_success('success');
  1472. }
  1473. /**
  1474. * @Desc 删除商品
  1475. * @Author Gorden
  1476. * @Date 2024/3/28 13:20
  1477. *
  1478. * @param $ids
  1479. * @return Response
  1480. */
  1481. public static function delete($ids)
  1482. {
  1483. if (!$ids) {
  1484. return json_fail("数据错误~");
  1485. }
  1486. if (!is_array($ids)) {
  1487. $ids = [$ids];
  1488. }
  1489. $goods = Goods::whereIn('goods_id', $ids)->get()->toArray();
  1490. if (!$goods) {
  1491. return json_fail("数据错误~");
  1492. }
  1493. // 是否在套包里
  1494. if (GoodsComponent::whereIn('join_component_goods_id', $ids)->exists()) {
  1495. return json_fail("当前商品存在于套包中,请先在套包中删除");
  1496. }
  1497. // 是否已被购买过
  1498. if (OrderSheet::whereIn('join_sheet_goods_id', $ids)->exists()) {
  1499. return json_fail("当前商品已有购买历史,如不在APP显示,请选择【下架】操作");
  1500. }
  1501. // 是否预约过
  1502. if (Appointment::whereIn('join_appointment_goods_id', $ids)->exists()) {
  1503. return json_fail("当前商品已有预约历史,如不在APP显示,请选择【下架】操作");
  1504. }
  1505. Db::beginTransaction();
  1506. try {
  1507. Goods::whereIn('goods_id', $ids)->delete();
  1508. GoodsDetail::whereIn('join_detail_goods_id', $ids)->delete();
  1509. GoodsLabel::whereIn('join_label_goods_id', $ids)->delete();
  1510. GoodsRunning::whereIn('join_running_goods_id', $ids)->delete();
  1511. GoodsSku::whereIn('join_sku_goods_id', $ids)->delete();
  1512. Db::commit();
  1513. _syslog("删除商品 / 套餐", "ID:【" . implode(',', $ids) . "】", $goods);
  1514. return json_success("商品删除成功");
  1515. } catch (\Exception $e) {
  1516. Db::rollBack();
  1517. return json_fail("商品删除失败~");
  1518. }
  1519. }
  1520. /**
  1521. * @Desc 商品主表
  1522. * @Author Gorden
  1523. * @Date 2024/3/11 11:20
  1524. *
  1525. * @param $params
  1526. * @return mixed|string
  1527. * @throws BusinessException
  1528. */
  1529. public static function mainInsert($params)
  1530. {
  1531. if (!empty($params['goods_cover'])) {
  1532. $params['goods_cover'] = str_replace(getenv('STORAGE_DOMAIN'), '', $params['goods_cover']);
  1533. }
  1534. // 如果产品是待处理状态
  1535. if ($params['goods_status'] == 'PENDING') {
  1536. if (strtotime($params['goods_on_addtimes']) <= time()) {
  1537. $params['goods_status'] = 'ON';
  1538. }
  1539. }
  1540. $category = SysCategory::where('category_id', $params['join_goods_category_id'])->first();
  1541. if (!$category) {
  1542. throw new BusinessException("产品分类不存在~");
  1543. }
  1544. if (empty($params['goods_category'])) {
  1545. $params['goods_category'] = $category->category_classify;
  1546. }
  1547. try {
  1548. $model = new Goods();
  1549. $model->goods_id = $params['goods_id'];
  1550. $model->join_goods_category_id = $params['join_goods_category_id'] ?? 0;
  1551. $model->join_goods_supplier_id = $params['join_goods_supplier_id'] ?? 0;
  1552. $model->goods_classify = $params['goods_classify'] ?? '';
  1553. $model->goods_status = $params['goods_status'] ?? '';
  1554. $model->goods_category = $params['goods_category'] ?? '';
  1555. // $model->goods_prefix = $params['goods_prefix'] ?? 】($category->category_name ? '【' . $category->category_name . '' : '');
  1556. $model->goods_prefix = $params['goods_prefix'] ?? '';
  1557. $model->goods_name = $params['goods_name'];
  1558. $model->goods_market_price = $params['goods_market_price'] ?? 0;
  1559. $model->goods_sales_price = $params['goods_sales_price'] ?? 0;
  1560. $model->goods_sku_json = !empty($params['goods_sku_json_label']) ? json_encode($params['goods_sku_json_label']) : json_encode(['规格' => ['标准']]);
  1561. $model->goods_attribute_json = !empty($params['goods_attribute_json']) ? $params['goods_attribute_json'] : '[]';
  1562. $model->goods_title = $params['goods_title'] ?? '';
  1563. $model->goods_cover = $params['goods_cover'] ?? '';
  1564. $model->goods_on_addtimes = isset($params['goods_on_addtimes']) ? strtotime($params['goods_on_addtimes']) : null;
  1565. $model->goods_sort = $params['goods_sort'] ?? null;
  1566. $model->goods_groupby = $params['goods_groupby'] ?? '';
  1567. $model->goods_remark = $params['goods_remark'] ?? '';
  1568. $model->goods_extend_json = $params['goods_extend_json'] ?? '{}';
  1569. $model->is_support_appointment = $params['is_support_appointment'] ?? 'N';
  1570. $model->creator_user_id = JwtToken::getCurrentId();
  1571. $model->goods_addtimes = time();
  1572. $model->goods_updatetimes = time();
  1573. // {"express":"Y","self":"Y","arrival":"Y"}
  1574. $expressJson = [];
  1575. if (!empty($params['express_json'])) {
  1576. if (in_array('express', $params['express_json'])) {
  1577. $expressJson['express'] = 'Y';
  1578. } else {
  1579. $expressJson['express'] = 'N';
  1580. }
  1581. if (in_array('self', $params['express_json'])) {
  1582. $expressJson['self'] = 'Y';
  1583. } else {
  1584. $expressJson['self'] = 'N';
  1585. }
  1586. if (in_array('arrival', $params['express_json'])) {
  1587. $expressJson['arrival'] = 'Y';
  1588. } else {
  1589. $expressJson['arrival'] = 'N';
  1590. }
  1591. $model->goods_express_json = json_encode($expressJson);
  1592. }
  1593. if (!empty($params['is_support_appointment']) && $params['is_support_appointment'] == 'Y' && !empty($params['appointment_times']) && $params['goods_category'] != 'TRAVEL') {
  1594. $times = [];
  1595. $attributeJsonTimeArr = [];
  1596. $personTotal = 0;
  1597. foreach ($params['appointment_times'] as $time) {
  1598. $attributeJsonTimeArr[] = strtotime(date('Y-m-d ') . $time['appointmentTimeStart']);
  1599. $attributeJsonTimeArr[] = strtotime(date('Y-m-d ') . $time['appointmentTimeEnd']);
  1600. $personTotal += $time['person'];
  1601. $times[$time['appointmentTimeStart']] = [
  1602. 'person' => $time['person'],
  1603. 'duration' => $time['appointmentTimeStart'] . '-' . $time['appointmentTimeEnd']
  1604. ];
  1605. }
  1606. $attributeJsonTime = date('H:i', min($attributeJsonTimeArr)) . '至' . date('H:i', max($attributeJsonTimeArr));
  1607. $newDates = [];
  1608. foreach ($params['dates'] as $date) {
  1609. $key = self::$week[$date];
  1610. $newDates[$key] = $date;
  1611. }
  1612. ksort($newDates);
  1613. $currentDate = current($newDates);
  1614. $lastDate = end($newDates);
  1615. $attributeJsonDate = '';
  1616. if (self::$week[$lastDate] - self::$week[$currentDate] + 1 > count($newDates)) {
  1617. $attributeJsonDate = implode(',', $newDates);
  1618. } else if (self::$week[$lastDate] - self::$week[$currentDate] + 1 == count($newDates)) {
  1619. $attributeJsonDate = $currentDate . '至' . $lastDate;
  1620. }
  1621. // if (isset($params['work_time'])){
  1622. // $workTimeStart = date('H:i',strtotime($params['work_time'][0]));
  1623. // $workTimeEnd = date('H:i',strtotime($params['work_time'][1]));
  1624. // $attributeJsonTime = $workTimeStart.'至'.$workTimeEnd;
  1625. // }
  1626. $attributeJson = [
  1627. 'icon' => $model->goods_cover,
  1628. 'date' => $attributeJsonDate,
  1629. // 'time' => $attributeJsonTime,
  1630. 'dates' => $newDates ? array_values($newDates) : [],
  1631. 'times' => $times,
  1632. 'person' => $personTotal
  1633. ];
  1634. // if(isset($params['address'])){
  1635. // $attributeJson['address'] = $params['address'];
  1636. // }
  1637. // if (isset($params['min_count'])){
  1638. // $attributeJson['min-count'] = $params['min_count'];
  1639. // }else{
  1640. // $attributeJson['min-count'] = 1;
  1641. // }
  1642. // if (isset($params['teachers'])){
  1643. // $attributeJson['teachers'] = $params['teachers'];
  1644. // }
  1645. if (!empty($params['appointment_label'])) {
  1646. $attributeJson['label'] = $params['appointment_label'];
  1647. }
  1648. // if (!empty($params['address'])) {
  1649. // $attributeJson['address'] = $params['address'];
  1650. // }
  1651. // if (!empty($params['position'])){
  1652. // $attributeJson['position'] = $params['position'];
  1653. // }
  1654. // if (isset($params['goods_service_premises'])){
  1655. // $attributeJson['service_premises_id'] = $params['goods_service_premises'];
  1656. // }
  1657. $model->goods_attribute_json = json_encode($attributeJson, JSON_UNESCAPED_UNICODE);
  1658. // $times = [];
  1659. // foreach ($params['appointment_times'] as $time) {
  1660. // $times[$time['appointmentTimeStart']] = [
  1661. // 'person' => $time['person'],
  1662. // 'duration' => $time['appointmentTimeStart'] . '-' . $time['appointmentTimeEnd']
  1663. // ];
  1664. // }
  1665. // $model->goods_attribute_json = json_encode([
  1666. // 'icon' => '',
  1667. // 'dates' => $params['dates'] ?? [],
  1668. // 'times' => $times
  1669. // ]);
  1670. }
  1671. if (!empty($params['is_support_appointment']) && $params['is_support_appointment'] == 'Y' && !empty($params['appointment_times']) && $params['goods_category'] == 'TRAVEL') {
  1672. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  1673. $attributeJson = json_decode($model->goods_attribute_json, true);
  1674. } elseif (empty($model->goods_attribute_json)) {
  1675. $attributeJson = [];
  1676. }
  1677. $attributeJson['travel-day'] = $params['travel_day'];
  1678. $attributeJson['travel-begin'] = $params['travel_begin'];
  1679. $attributeJson['travel-night'] = $params['travel_night'];
  1680. $attributeJson['travel-trans'] = $params['travel_trans'];
  1681. $unixs = [];
  1682. foreach ($params['appointment_times'] as $times) {
  1683. $unix = strtotime($times['days']);
  1684. $unixs[$unix] = $times['person'];
  1685. }
  1686. ksort($unixs);
  1687. $months = [];
  1688. foreach ($unixs as $key => $unix) {
  1689. $month = date('Ym', $key);
  1690. $day = date('d', $key);
  1691. $months[$month][$day] = $unix;
  1692. }
  1693. $attributeJson['month'] = $months;
  1694. $model->goods_attribute_json = json_encode($attributeJson, JSON_UNESCAPED_UNICODE);
  1695. }
  1696. if (!empty($params['goods_json']) && $params['join_goods_category_id'] == 65) {
  1697. $goodsJson = json_decode($params['goods_json'], true);
  1698. // foreach ($goodsJson as $key => $item) {
  1699. // $goodsJson[$key]['color'] = hexToRgb($item['color']);
  1700. // }
  1701. //
  1702. $model->goods_json = json_encode($goodsJson);
  1703. } elseif (!empty($params['goods_json']) && $params['join_goods_category_id'] == 43) {
  1704. $goodsJson = json_decode($params['goods_json'], true);
  1705. $newGoodsJson = [];
  1706. foreach ($goodsJson as $item1) {
  1707. if (empty($item1['title'])) {
  1708. continue;
  1709. }
  1710. $newItem1 = [];
  1711. foreach ($item1['items'] as $item2) {
  1712. $newParams = [];
  1713. foreach ($item2['params'] as $param) {
  1714. if (!empty($param[0]) || !empty($param[1])) {
  1715. $newParams[] = $param;
  1716. }
  1717. }
  1718. $newItem1['items'][$item2['key']] = $newParams;
  1719. }
  1720. $newItem1['service'] = $item1['service'] ?? '';
  1721. $newGoodsJson[$item1['title']] = $newItem1;
  1722. }
  1723. $model->goods_json = json_encode($newGoodsJson);
  1724. } else {
  1725. $model->goods_json = '[]';
  1726. }
  1727. if (!empty($params['goods_premisses'])) {
  1728. $attributeJson = [];
  1729. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  1730. $attributeJson = json_decode($model->goods_attribute_json, true);
  1731. } elseif (empty($model->goods_attribute_json)) {
  1732. $attributeJson = [];
  1733. }
  1734. $attributeJson['premisses'] = $params['goods_premisses'];
  1735. $model->goods_attribute_json = json_encode($attributeJson);
  1736. }
  1737. if (!empty($params['goods_theme_color']) && !empty($params['goods_theme_icon'])) {
  1738. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  1739. $attributeJson = json_decode($model->goods_attribute_json, true);
  1740. } elseif (empty($model->goods_attribute_json)) {
  1741. $attributeJson = [];
  1742. }
  1743. $attributeJson['bg'] = $params['goods_theme_color'];
  1744. $attributeJson['icon'] = str_replace(getenv('STORAGE_DOMAIN'), '', $params['goods_theme_icon']);
  1745. $model->goods_attribute_json = json_encode($attributeJson);
  1746. }
  1747. if (!empty($params['address'])) {
  1748. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  1749. $attributeJson = json_decode($model->goods_attribute_json, true);
  1750. } elseif (empty($model->goods_attribute_json)) {
  1751. $attributeJson = [];
  1752. }
  1753. $attributeJson['address'] = $params['address'];
  1754. $model->goods_attribute_json = json_encode($attributeJson);
  1755. }
  1756. if (!empty($params['position'])) {
  1757. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  1758. $attributeJson = json_decode($model->goods_attribute_json, true);
  1759. } elseif (empty($model->goods_attribute_json)) {
  1760. $attributeJson = [];
  1761. }
  1762. $attributeJson['position'] = $params['position'];
  1763. $model->goods_attribute_json = json_encode($attributeJson);
  1764. }
  1765. if (isset($params['goods_service_premises'])) {
  1766. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  1767. $attributeJson = json_decode($model->goods_attribute_json, true);
  1768. } elseif (empty($model->goods_attribute_json)) {
  1769. $attributeJson = [];
  1770. }
  1771. $attributeJson['service_premises_id'] = $params['goods_service_premises'];
  1772. $model->goods_attribute_json = json_encode($attributeJson);
  1773. }
  1774. if (isset($params['min_count'])) {
  1775. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  1776. $attributeJson = json_decode($model->goods_attribute_json, true);
  1777. } elseif (empty($model->goods_attribute_json)) {
  1778. $attributeJson = [];
  1779. }
  1780. $attributeJson['min-count'] = $params['min_count'];
  1781. $model->goods_attribute_json = json_encode($attributeJson);
  1782. }
  1783. if (isset($params['teachers'])) {
  1784. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  1785. $attributeJson = json_decode($model->goods_attribute_json, true);
  1786. } elseif (empty($model->goods_attribute_json)) {
  1787. $attributeJson = [];
  1788. }
  1789. $attributeJson['teachers'] = $params['teachers'];
  1790. $model->goods_attribute_json = json_encode($attributeJson);
  1791. }
  1792. if (isset($params['work_time'])) {
  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. $workTimeStart = date('H:i', strtotime($params['work_time'][0]));
  1799. $workTimeEnd = date('H:i', strtotime($params['work_time'][1]));
  1800. $attributeJsonTime = $workTimeStart . '至' . $workTimeEnd;
  1801. $attributeJson['time'] = $attributeJsonTime;
  1802. $model->goods_attribute_json = json_encode($attributeJson);
  1803. }
  1804. if (!empty($params['couponContentList'])) {
  1805. $couponList = [];
  1806. foreach ($params['couponContentList'] as $coupon) {
  1807. $couponList[$coupon['coupon_id']] = [
  1808. 'num' => $coupon['nbr'],
  1809. 'name' => $coupon['coupon_name']
  1810. ];
  1811. }
  1812. $attributeJson['coupon'] = $couponList;
  1813. $model->goods_attribute_json = json_encode($attributeJson, JSON_UNESCAPED_UNICODE);
  1814. }
  1815. if (!empty($params['coupon_id']) && !empty($params['coupon_nbr'])) {
  1816. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  1817. $attributeJson = json_decode($model->goods_attribute_json, true);
  1818. } elseif (empty($model->goods_attribute_json)) {
  1819. $attributeJson = [];
  1820. }
  1821. $coupons = Coupon::whereIn('coupon_id', $params['coupon_id'])
  1822. ->select('coupon_id', 'coupon_name')
  1823. ->get()
  1824. ->toArray();
  1825. $couponList = [];
  1826. foreach ($coupons as $coupon) {
  1827. if (isset($params['coupon_nbr'][$coupon['coupon_id']])) {
  1828. $couponList[$coupon['coupon_id']] = [
  1829. 'num' => $params['coupon_nbr'][$coupon['coupon_id']],
  1830. 'name' => $coupon['coupon_name']
  1831. ];
  1832. }
  1833. }
  1834. $attributeJson['coupon'] = $couponList;
  1835. $model->goods_attribute_json = json_encode($attributeJson, JSON_UNESCAPED_UNICODE);
  1836. }
  1837. if ($model->save()) {
  1838. return $model->goods_id;
  1839. }
  1840. // 异常
  1841. throw new BusinessException("数据写入失败~");
  1842. } catch (\Exception $e) {
  1843. dump($e->getMessage());
  1844. throw new BusinessException("数据写入失败~");
  1845. }
  1846. }
  1847. /**
  1848. * @Desc 详情表
  1849. * @Author Gorden
  1850. * @Date 2024/3/11 11:19
  1851. *
  1852. * @param $params
  1853. * @return void
  1854. * @throws BusinessException
  1855. */
  1856. public static function detailInsert($params)
  1857. {
  1858. if (!empty($params['goods_detail_slider_json'])) {
  1859. $params['goods_detail_slider_json'] = str_replace(getenv('STORAGE_DOMAIN'), '', $params['goods_detail_slider_json']);
  1860. $params['goods_detail_slider_json'] = json_encode(['slider' => $params['goods_detail_slider_json']]);
  1861. }
  1862. if (isset($params['curriculum'])) {
  1863. $params['goods_detail_specs_json'] = json_encode([
  1864. [
  1865. 'key' => '课时',
  1866. 'val' => $params['curriculum']['period'] ?? '',
  1867. ],
  1868. [
  1869. 'key' => '群体',
  1870. 'val' => $params['curriculum']['group'] ?? '',
  1871. ],
  1872. [
  1873. 'key' => '课程',
  1874. 'val' => $params['curriculum']['course'] ?? '',
  1875. ],
  1876. ]);
  1877. }
  1878. try {
  1879. $model = new GoodsDetail();
  1880. $model->join_detail_goods_id = $params['goods_id'];
  1881. $model->goods_detail_code_json = $params['goods_detail_code_json'] ?? '{}';
  1882. $model->goods_detail_slider_json = $params['goods_detail_slider_json'] ?? '{}';
  1883. $model->goods_detail_specs_json = $params['goods_detail_specs_json'] ?? '{}';
  1884. $model->goods_detail_content = $params['goods_detail_content'] ?? '';
  1885. if (!$model->save()) {
  1886. // 异常
  1887. throw new BusinessException("轮播图/详情数据写入失败~");
  1888. }
  1889. } catch (\Exception $e) {
  1890. dump($e->getMessage());
  1891. throw new BusinessException("轮播图/详情数据写入失败~");
  1892. }
  1893. }
  1894. public static function componentUpdate($params, $type = 'insert')
  1895. {
  1896. Db::beginTransaction();
  1897. try {
  1898. $goodsSku = '';
  1899. // 有先删除
  1900. if ($type == 'update') {
  1901. GoodsComponent::where('join_component_master_goods_id', $params['goods_id'])->delete();
  1902. $goodsSku = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->first();
  1903. if ($goodsSku) {
  1904. $goodsSku->goods_sku_market_price = $params['goods_market_price'] ?? 0;
  1905. $goodsSku->goods_sku_sales_price = $params['goods_sales_price'] ?? 0;
  1906. $goodsSku->save();
  1907. }
  1908. }
  1909. if ($type == 'insert' || !$goodsSku) {
  1910. Goods::where('goods_id', $params['goods_id'])->update(['goods_sku_json' => '{"规格": ["标准"]}']);
  1911. $skuData = [
  1912. 'join_sku_goods_id' => $params['goods_id'],
  1913. 'goods_sku_status' => 'ON',
  1914. 'goods_sku_specs_json' => '{"规格": "标准"}',
  1915. 'goods_sku_title' => "标准" . $params['goods_name'],
  1916. 'goods_sku_market_price' => $params['goods_market_price'] ?? 0,
  1917. 'goods_sku_sales_price' => $params['goods_sales_price'] ?? 0,
  1918. ];
  1919. GoodsSku::insert($skuData);
  1920. }
  1921. $data = [];
  1922. foreach ($params['goods_content_list'] as $item) {
  1923. if (!in_array($item['goods_id'], $params['join_component_goods_id'])) {
  1924. continue;
  1925. }
  1926. $goods = Goods::where('goods_id', $params['goods_id'])->first();
  1927. if (!$goods) {
  1928. continue;
  1929. }
  1930. $data[] = [
  1931. 'join_component_master_goods_id' => $params['goods_id'],
  1932. 'join_component_goods_id' => $item['goods_id'] ?? '',
  1933. 'join_component_goods_sku_id' => $item['sku_id'] ?? '',
  1934. 'goods_component_price' => $item['goods_sales_price'] ?? '',
  1935. 'goods_component_cover' => $goods->goods_cover,
  1936. 'goods_component_json' => json_encode(['goods_name' => $item['goods_name'], 'nbr' => $item['nbr'], 'sku_id' => $item['sku_id'] ?? '']),
  1937. 'goods_component_addtimes' => time()
  1938. ];
  1939. }
  1940. if ($data) {
  1941. GoodsComponent::insert($data);
  1942. }
  1943. Db::commit();
  1944. } catch (\Exception $e) {
  1945. Db::rollBack();
  1946. dump($e->getMessage());
  1947. throw new BusinessException("数据写入失败~");
  1948. }
  1949. }
  1950. /**
  1951. * @Desc 标签表
  1952. * @Author Gorden
  1953. * @Date 2024/3/11 11:32
  1954. *
  1955. * @param $params
  1956. * @return void
  1957. * @throws BusinessException
  1958. */
  1959. public static function labelInsert($params)
  1960. {
  1961. $model = new GoodsLabel();
  1962. $model->join_label_goods_id = $params['goods_id'];
  1963. $model->goods_label = $params['goods_label'] ? implode(',', $params['goods_label']) : '';
  1964. $model->goods_label_extend_json = !empty($params['goods_label_extend_json']) ? $params['goods_label_extend_json'] : '{}';
  1965. if (!$model->save()) {
  1966. // 异常
  1967. throw new BusinessException('数据写入失败~');
  1968. }
  1969. }
  1970. /**
  1971. * @Desc 产品运行控制信息表
  1972. * @Author Gorden
  1973. * @Date 2024/3/11 11:38
  1974. *
  1975. * @param $params
  1976. * @return void
  1977. * @throws BusinessException
  1978. */
  1979. public static function goodsRunningInsert($params)
  1980. {
  1981. try {
  1982. $model = new GoodsRunning();
  1983. $model->join_running_goods_id = $params['goods_id'];
  1984. $model->goods_running_storage = $params['goods_running_storage'] ?? 0;
  1985. $model->goods_running_sale = $params['goods_running_sale'] ?? 0;
  1986. $model->goods_running_off_type = !empty($params['goods_running_off_type']) ? $params['goods_running_off_type'] : '';
  1987. $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'])]) : '[]';
  1988. if (!$model->save()) {
  1989. throw new BusinessException('数据写入失败');
  1990. }
  1991. } catch (\Exception $e) {
  1992. dump($e->getMessage());
  1993. throw new BusinessException('数据写入失败');
  1994. }
  1995. }
  1996. /**
  1997. * @Desc SKU
  1998. * @Author Gorden
  1999. * @Date 2024/3/11 12:01
  2000. *
  2001. * @param $params
  2002. * @return void
  2003. * @throws BusinessException
  2004. */
  2005. public static function skuInsert($params)
  2006. {
  2007. $model = new GoodsSku();
  2008. $model->join_sku_goods_id = $params['goods_id'];
  2009. $model->goods_sku_status = $params['goods_sku_status'];
  2010. $model->goods_sku_specs_json = $params['goods_sku_specs_json'];
  2011. $model->goods_sku_title = $params['goods_sku_title'];
  2012. $model->goods_sku_images_json = $params['goods_sku_images_json'];
  2013. $model->goods_sku_content = $params['goods_sku_content'];
  2014. $model->goods_sku_market_price = $params['goods_sku_market_price'];
  2015. $model->goods_sku_sales_price = $params['goods_sku_sales_price'];
  2016. $model->goods_sku_storage_json = $params['goods_sku_storage_json'];
  2017. $model->goods_sku_service_json = $params['goods_sku_service_json'];
  2018. $model->goods_sku_extend_json = $params['goods_sku_extend_json'];
  2019. if (!$model->save()) {
  2020. throw new BusinessException('规格数据写入失败~');
  2021. }
  2022. }
  2023. /**
  2024. * @Desc
  2025. * @Author Gorden
  2026. * @Date 2024/3/12 8:44
  2027. *
  2028. * @param $params
  2029. * @return void
  2030. * @throws BusinessException
  2031. */
  2032. public static function mainUpdate($params)
  2033. {
  2034. try {
  2035. $data = self::inputFilter($params, new Goods());
  2036. if (!empty($data['goods_cover'])) {
  2037. $data['goods_cover'] = str_replace(getenv('STORAGE_DOMAIN'), '', $data['goods_cover']);
  2038. }
  2039. $data['goods_on_addtimes'] = isset($data['goods_on_addtimes']) ? strtotime($data['goods_on_addtimes']) : 0;
  2040. $data['goods_sku_json'] = !empty($params['goods_sku_json_label']) ? json_encode($params['goods_sku_json_label']) : json_encode(['规格' => ['标准']]);
  2041. $row = Goods::find($data['goods_id']);
  2042. if ($row->join_goods_category_id != $data['join_goods_category_id']) {
  2043. $category = SysCategory::where('category_id', $params['join_goods_category_id'])->first();
  2044. if (!$category) {
  2045. throw new BusinessException("产品分类不存在~");
  2046. }
  2047. $data['goods_category'] = $category->category_classify ?? '';
  2048. $data['goods_prefix'] = $data['goods_prefix'] ?? ($category->category_name ? '【' . $category->category_name . '】' : '');
  2049. }
  2050. if (!empty($params['goods_recommend_index'])) {
  2051. $data['goods_category'] = $params['goods_recommend_index'];
  2052. }
  2053. // 上架时间有变动
  2054. if ($data['goods_status'] == 'PENDING' && $row->goods_on_addtimes != $data['goods_on_addtimes']) {
  2055. $redis = Redis::connection();
  2056. // 删掉原来的
  2057. $oldKey = Goods::LISTING_KEY_PREFIX . date('YmdHi', $row->goods_on_addtimes);
  2058. $redis->srem($oldKey, $data['goods_id']);
  2059. // 加入新的
  2060. $newKey = Goods::LISTING_KEY_PREFIX . date('YmdHi', $data['goods_on_addtimes']);
  2061. $redis->sadd($newKey, $data['goods_id']);
  2062. }
  2063. $expressJson = [];
  2064. if (!empty($params['express_json'])) {
  2065. if (in_array('express', $params['express_json'])) {
  2066. $expressJson['express'] = 'Y';
  2067. } else {
  2068. $expressJson['express'] = 'N';
  2069. }
  2070. if (in_array('self', $params['express_json'])) {
  2071. $expressJson['self'] = 'Y';
  2072. } else {
  2073. $expressJson['self'] = 'N';
  2074. }
  2075. if (in_array('arrival', $params['express_json'])) {
  2076. $expressJson['arrival'] = 'Y';
  2077. } else {
  2078. $expressJson['arrival'] = 'N';
  2079. }
  2080. $data['goods_express_json'] = json_encode($expressJson);
  2081. }
  2082. if (!empty($params['is_support_appointment']) && $params['is_support_appointment'] == 'Y' && !empty($params['appointment_times']) && $params['goods_category'] != 'TRAVEL') {
  2083. $attributeJson = [];
  2084. if (!empty($row->goods_attribute_json)) {
  2085. $attributeJson = json_decode($row->goods_attribute_json, true);
  2086. }
  2087. $times = [];
  2088. $attributeJsonTimeArr = [];
  2089. $personTotal = 0;
  2090. foreach ($params['appointment_times'] as $time) {
  2091. $attributeJsonTimeArr[] = strtotime(date('Y-m-d ') . $time['appointmentTimeStart']);
  2092. $attributeJsonTimeArr[] = strtotime(date('Y-m-d ') . $time['appointmentTimeEnd']);
  2093. $personTotal += $time['person'];
  2094. $times[$time['appointmentTimeStart']] = [
  2095. 'person' => $time['person'],
  2096. 'duration' => $time['appointmentTimeStart'] . '-' . $time['appointmentTimeEnd']
  2097. ];
  2098. }
  2099. $attributeJsonTime = date('H:i', min($attributeJsonTimeArr)) . '至' . date('H:i', max($attributeJsonTimeArr));
  2100. $newDates = [];
  2101. foreach ($params['dates'] as $date) {
  2102. $key = self::$week[$date];
  2103. $newDates[$key] = $date;
  2104. }
  2105. ksort($newDates);
  2106. $currentDate = current($newDates);
  2107. $lastDate = end($newDates);
  2108. $attributeJsonDate = '';
  2109. if (self::$week[$lastDate] - self::$week[$currentDate] + 1 > count($newDates)) {
  2110. $attributeJsonDate = implode(',', $newDates);
  2111. } else if (self::$week[$lastDate] - self::$week[$currentDate] + 1 == count($newDates)) {
  2112. $attributeJsonDate = $currentDate . '至' . $lastDate;
  2113. }
  2114. $attributeJson['date'] = $attributeJsonDate;
  2115. $attributeJson['icon'] = $data['goods_cover'] ?? '';
  2116. $attributeJson['time'] = $attributeJsonTime;
  2117. $attributeJson['dates'] = $newDates ? array_values($newDates) : [];
  2118. $attributeJson['times'] = $times;
  2119. $attributeJson['person'] = $personTotal;
  2120. if (!empty($params['appointment_label'])) {
  2121. $attributeJson['label'] = $params['appointment_label'];
  2122. }
  2123. // if (!empty($params['address'])) {
  2124. // $attributeJson['address'] = $params['address'];
  2125. // }
  2126. // if (!empty($params['position'])){
  2127. // $attributeJson['position'] = $params['position'];
  2128. // }
  2129. // if (isset($params['goods_service_premises'])){
  2130. // $attributeJson['service_premises_id'] = $params['goods_service_premises'];
  2131. // }
  2132. // if (isset($params['work_time'])){
  2133. // $workTimeStart = date('H:i',strtotime($params['work_time'][0]));
  2134. // $workTimeEnd = date('H:i',strtotime($params['work_time'][1]));
  2135. // $attributeJson['time'] = $workTimeStart.'至'.$workTimeEnd;
  2136. // }
  2137. // if (isset($params['min_count'])){
  2138. // $attributeJson['min-count'] = $params['min_count'];
  2139. // }else{
  2140. // $attributeJson['min-count'] = 1;
  2141. // }
  2142. // if (isset($params['teachers'])){
  2143. // $attributeJson['teachers'] = $params['teachers'];
  2144. // }
  2145. $data['goods_attribute_json'] = json_encode($attributeJson, JSON_UNESCAPED_UNICODE);
  2146. }
  2147. if (!empty($params['is_support_appointment']) && $params['is_support_appointment'] == 'Y' && !empty($params['appointment_times']) && $params['goods_category'] == 'TRAVEL') {
  2148. $attributeJson = [];
  2149. if (!empty($row->goods_attribute_json)) {
  2150. $attributeJson = json_decode($row->goods_attribute_json, true);
  2151. }
  2152. $attributeJson['travel-day'] = $params['travel_day'];
  2153. $attributeJson['travel-begin'] = $params['travel_begin'];
  2154. $attributeJson['travel-night'] = $params['travel_night'];
  2155. $attributeJson['travel-trans'] = $params['travel_trans'];
  2156. $unixs = [];
  2157. foreach ($params['appointment_times'] as $times) {
  2158. $unix = strtotime($times['days']);
  2159. $unixs[$unix] = $times['person'];
  2160. }
  2161. ksort($unixs);
  2162. $months = [];
  2163. foreach ($unixs as $key => $unix) {
  2164. $month = date('Ym', $key);
  2165. $day = date('d', $key);
  2166. $months[$month][$day] = $unix;
  2167. }
  2168. $attributeJson['month'] = $months;
  2169. $data['goods_attribute_json'] = json_encode($attributeJson, JSON_UNESCAPED_UNICODE);
  2170. }
  2171. if (!empty($params['couponContentList'])) {
  2172. $couponList = [];
  2173. foreach ($params['couponContentList'] as $coupon) {
  2174. $couponList[$coupon['coupon_id']] = [
  2175. 'num' => $coupon['nbr'],
  2176. 'name' => $coupon['coupon_name']
  2177. ];
  2178. }
  2179. $attributeJson['coupon'] = $couponList;
  2180. $model->goods_attribute_json = json_encode($attributeJson, JSON_UNESCAPED_UNICODE);
  2181. }
  2182. if (!empty($params['coupon_id']) && !empty($params['coupon_nbr'])) {
  2183. $attributeJson = [];
  2184. if (!empty($row->goods_attribute_json)) {
  2185. $attributeJson = json_decode($row->goods_attribute_json, true);
  2186. }
  2187. $coupons = Coupon::whereIn('coupon_id', $params['coupon_id'])
  2188. ->select('coupon_id', 'coupon_name')
  2189. ->get()
  2190. ->toArray();
  2191. $couponList = [];
  2192. foreach ($coupons as $coupon) {
  2193. if (isset($params['coupon_nbr'][$coupon['coupon_id']])) {
  2194. $couponList[$coupon['coupon_id']] = [
  2195. 'num' => $params['coupon_nbr'][$coupon['coupon_id']],
  2196. 'name' => $coupon['coupon_name']
  2197. ];
  2198. }
  2199. }
  2200. $attributeJson['coupon'] = $couponList;
  2201. $data['goods_attribute_json'] = json_encode($attributeJson, JSON_UNESCAPED_UNICODE);
  2202. }
  2203. if (!empty($params['goods_premisses'])) {
  2204. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  2205. if (is_json($data['goods_attribute_json'])) {
  2206. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  2207. } else {
  2208. $data['goods_attribute_json'] = [];
  2209. }
  2210. } elseif (empty($data['goods_attribute_json'])) {
  2211. $data['goods_attribute_json'] = [];
  2212. }
  2213. $data['goods_attribute_json']['premisses'] = $params['goods_premisses'];
  2214. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  2215. }
  2216. if (!empty($data['goods_json']) && $data['join_goods_category_id'] == 65) {
  2217. // $goodsJson = json_decode($data['goods_json'], true);
  2218. // foreach ($goodsJson as $key => $item) {
  2219. // $goodsJson[$key]['color'] = hexToRgb($item['color']);
  2220. // }
  2221. //
  2222. // $data['goods_json'] = json_encode($goodsJson);
  2223. } elseif (!empty($data['goods_json']) && $data['join_goods_category_id'] == 43) {
  2224. $goodsJson = json_decode($data['goods_json'], true);
  2225. $newGoodsJson = [];
  2226. foreach ($goodsJson as $item1) {
  2227. if (empty($item1['title'])) {
  2228. continue;
  2229. }
  2230. $newItem1 = [];
  2231. foreach ($item1['items'] as $item2) {
  2232. $newParams = [];
  2233. foreach ($item2['params'] as $param) {
  2234. if (!empty($param[0]) || !empty($param[1])) {
  2235. $newParams[] = $param;
  2236. }
  2237. }
  2238. $newItem1['items'][$item2['key']] = $newParams;
  2239. }
  2240. $newItem1['service'] = $item1['service'];
  2241. $newGoodsJson[$item1['title']] = $newItem1;
  2242. }
  2243. $data['goods_json'] = json_encode($newGoodsJson);
  2244. } else {
  2245. $data['goods_json'] = '[]';
  2246. }
  2247. if (!empty($params['goods_theme_color']) && !empty($params['goods_theme_icon'])) {
  2248. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  2249. if (is_json($data['goods_attribute_json'])) {
  2250. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  2251. } else {
  2252. $data['goods_attribute_json'] = [];
  2253. }
  2254. } elseif (empty($data['goods_attribute_json'])) {
  2255. $data['goods_attribute_json'] = [];
  2256. }
  2257. $data['goods_attribute_json']['bg'] = $params['goods_theme_color'];
  2258. $data['goods_attribute_json']['icon'] = str_replace(getenv('STORAGE_DOMAIN'), '', $params['goods_theme_icon']);
  2259. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  2260. }
  2261. if (!empty($params['address'])) {
  2262. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  2263. if (is_json($data['goods_attribute_json'])) {
  2264. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  2265. } else {
  2266. $data['goods_attribute_json'] = [];
  2267. }
  2268. } elseif (empty($data['goods_attribute_json'])) {
  2269. $data['goods_attribute_json'] = [];
  2270. }
  2271. $data['goods_attribute_json']['address'] = $params['address'];
  2272. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  2273. }
  2274. if (!empty($params['position'])) {
  2275. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  2276. if (is_json($data['goods_attribute_json'])) {
  2277. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  2278. } else {
  2279. $data['goods_attribute_json'] = [];
  2280. }
  2281. } elseif (empty($data['goods_attribute_json'])) {
  2282. $data['goods_attribute_json'] = [];
  2283. }
  2284. $data['goods_attribute_json']['position'] = $params['position'];
  2285. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  2286. }
  2287. if (isset($params['goods_service_premises'])) {
  2288. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  2289. if (is_json($data['goods_attribute_json'])) {
  2290. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  2291. } else {
  2292. $data['goods_attribute_json'] = [];
  2293. }
  2294. } elseif (empty($data['goods_attribute_json'])) {
  2295. $data['goods_attribute_json'] = [];
  2296. }
  2297. $data['goods_attribute_json']['service_premises_id'] = $params['goods_service_premises'];
  2298. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  2299. }
  2300. if (isset($params['work_time'])) {
  2301. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  2302. if (is_json($data['goods_attribute_json'])) {
  2303. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  2304. } else {
  2305. $data['goods_attribute_json'] = [];
  2306. }
  2307. } elseif (empty($data['goods_attribute_json'])) {
  2308. $data['goods_attribute_json'] = [];
  2309. }
  2310. $workTimeStart = date('H:i', strtotime($params['work_time'][0]));
  2311. $workTimeEnd = date('H:i', strtotime($params['work_time'][1]));
  2312. $data['goods_attribute_json']['time'] = $workTimeStart . '至' . $workTimeEnd;
  2313. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  2314. }
  2315. if (isset($params['min_count'])) {
  2316. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  2317. if (is_json($data['goods_attribute_json'])) {
  2318. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  2319. } else {
  2320. $data['goods_attribute_json'] = [];
  2321. }
  2322. } elseif (empty($data['goods_attribute_json'])) {
  2323. $data['goods_attribute_json'] = [];
  2324. }
  2325. $data['goods_attribute_json']['min-count'] = $params['min_count'];
  2326. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  2327. }
  2328. if (isset($params['teachers'])) {
  2329. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  2330. if (is_json($data['goods_attribute_json'])) {
  2331. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  2332. } else {
  2333. $data['goods_attribute_json'] = [];
  2334. }
  2335. } elseif (empty($data['goods_attribute_json'])) {
  2336. $data['goods_attribute_json'] = [];
  2337. }
  2338. $data['goods_attribute_json']['teachers'] = $params['teachers'];
  2339. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  2340. }
  2341. foreach ($data as $key => $val) {
  2342. $row->{$key} = $val;
  2343. }
  2344. $row->goods_updatetimes = time();
  2345. $row->updator_user_id = JwtToken::getCurrentId();
  2346. $row->save();
  2347. } catch (BusinessException $e) {
  2348. throw new BusinessException($e->getMessage());
  2349. } catch (\Exception $e) {
  2350. dump($e->getTrace());
  2351. throw new BusinessException('数据更新异常~1');
  2352. }
  2353. }
  2354. /**
  2355. * @Desc
  2356. * @Author Gorden
  2357. * @Date 2024/3/12 9:57
  2358. *
  2359. * @param $params
  2360. * @return void
  2361. * @throws BusinessException
  2362. */
  2363. public static function detailUpdate($params)
  2364. {
  2365. try {
  2366. $data = self::inputFilter($params, new GoodsDetail());
  2367. if (!empty($data['goods_detail_slider_json'])) {
  2368. $data['goods_detail_slider_json'] = str_replace(getenv('STORAGE_DOMAIN'), '', $data['goods_detail_slider_json']);
  2369. $data['goods_detail_slider_json'] = json_encode(['slider' => $data['goods_detail_slider_json']]);
  2370. }
  2371. if (isset($params['curriculum'])) {
  2372. $data['goods_detail_specs_json'] = json_encode([
  2373. [
  2374. 'key' => '课时',
  2375. 'val' => $params['curriculum']['period'] ?? '',
  2376. ],
  2377. [
  2378. 'key' => '群体',
  2379. 'val' => $params['curriculum']['group'] ?? '',
  2380. ],
  2381. [
  2382. 'key' => '课程',
  2383. 'val' => $params['curriculum']['course'] ?? '',
  2384. ],
  2385. ]);
  2386. }
  2387. // 根据goods_id 查详情ID
  2388. $detail = GoodsDetail::where('join_detail_goods_id', $params['goods_id'])->first();
  2389. if ($detail) {
  2390. self::doUpdate($detail->join_detail_goods_id, $data, new GoodsDetail());
  2391. } else {
  2392. $data['join_detail_goods_id'] = $params['goods_id'];
  2393. GoodsDetail::insert($data);
  2394. }
  2395. } catch (BusinessException $e) {
  2396. throw new BusinessException($e->getMessage());
  2397. } catch (\Exception $e) {
  2398. dump($e->getMessage());
  2399. throw new BusinessException('轮播图/详情数据更新异常~');
  2400. }
  2401. }
  2402. /**
  2403. * @Desc
  2404. * @Author Gorden
  2405. * @Date 2024/3/12 9:58
  2406. *
  2407. * @param $params
  2408. * @return void
  2409. * @throws BusinessException
  2410. */
  2411. public static function labelUpdate($params)
  2412. {
  2413. try {
  2414. $data = self::inputFilter($params, new GoodsLabel());
  2415. // 根据goods_id 查详情ID
  2416. $detail = GoodsLabel::where('join_label_goods_id', $params['goods_id'])->first();
  2417. if ($detail) {
  2418. self::doUpdate($detail->goods_label_id, $data, new GoodsLabel());
  2419. } else {
  2420. $data['join_label_goods_id'] = $params['goods_id'];
  2421. GoodsLabel::insert($data);
  2422. }
  2423. } catch (BusinessException $e) {
  2424. throw new BusinessException($e->getMessage());
  2425. } catch (\Exception $e) {
  2426. dump($e->getMessage());
  2427. throw new BusinessException('数据更新异常~3');
  2428. }
  2429. }
  2430. /**
  2431. * @Desc
  2432. * @Author Gorden
  2433. * @Date 2024/3/12 9:59
  2434. *
  2435. * @param $params
  2436. * @return void
  2437. * @throws BusinessException
  2438. */
  2439. public static function goodsRunningUpdate($params)
  2440. {
  2441. try {
  2442. $data = self::inputFilter($params, new GoodsRunning());
  2443. // 根据goods_id 查详情ID
  2444. $detail = GoodsRunning::where('join_running_goods_id', $params['goods_id'])->first();
  2445. if (!empty($params['goods_running_off_type']) && $params['goods_running_off_type'] == 'T') {
  2446. $redis = Redis::connection();
  2447. if (!empty($detail->goods_running_off_json)) {
  2448. $goodsRunningOffJson = json_decode($detail->goods_running_off_json, true);
  2449. if (isset($goodsRunningOffJson['time'])) {
  2450. $oldKey = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi', $goodsRunningOffJson['time']);
  2451. $goodsRunningOffJson['time'] = strtotime($params['goods_off_addtimes']);
  2452. $data['goods_running_off_json'] = json_encode($goodsRunningOffJson);
  2453. // 有老的下架时间,删除老的
  2454. $redis->srem($oldKey, $params['goods_id']);
  2455. } else {
  2456. $goodsRunningOffJson['time'] = strtotime($params['goods_off_addtimes']);
  2457. $data['goods_running_off_json'] = json_encode($goodsRunningOffJson);
  2458. }
  2459. } else {
  2460. $data['goods_running_off_json'] = json_encode(['time' => strtotime($params['goods_off_addtimes'])]);
  2461. }
  2462. // 加入自动下架
  2463. $newKey = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi', strtotime($params['goods_off_addtimes']));
  2464. $redis->sAdd($newKey, $params['goods_id']);
  2465. } 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') {
  2466. $goodsRunningOffJson = json_decode($detail->goods_running_off_json, true);
  2467. if (isset($goodsRunningOffJson['time'])) {
  2468. $oldKey = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi', $goodsRunningOffJson['time']);
  2469. $redis = Redis::connection();
  2470. $redis->srem($oldKey, $params['goods_id']);
  2471. }
  2472. }
  2473. if ($detail) {
  2474. self::doUpdate($detail->join_running_goods_id, $data, new GoodsRunning());
  2475. } else {
  2476. // 兼容老数据……
  2477. $data['join_running_goods_id'] = $params['goods_id'];
  2478. GoodsRunning::insert($data);
  2479. }
  2480. } catch (BusinessException $e) {
  2481. throw new BusinessException($e->getMessage());
  2482. } catch (\Exception $e) {
  2483. dump($e->getMessage());
  2484. throw new BusinessException('数据更新异常~');
  2485. }
  2486. }
  2487. /**
  2488. * @Desc sku 设置
  2489. * @Author Gorden
  2490. * @Date 2024/4/10 10:43
  2491. *
  2492. * @param $params
  2493. * @return void
  2494. * @throws BusinessException
  2495. */
  2496. public static function goodsSkuSet($params, $operation = 'insert')
  2497. {
  2498. try {
  2499. Db::beginTransaction();
  2500. $skusOldIds = [];
  2501. if ($operation == 'update') {
  2502. // 查出所有的
  2503. $skusOldIds = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->pluck('goods_sku_id', 'goods_sku_id');
  2504. // 删掉原有的
  2505. // GoodsSku::where('join_sku_goods_id', $params['goods_id'])->delete();
  2506. }
  2507. if (empty($skusOldIds) && empty($params['goods_sku_json_value'])) {
  2508. $skuData = [
  2509. 'join_sku_goods_id' => $params['goods_id'],
  2510. 'goods_sku_status' => 'ON',
  2511. 'goods_sku_specs_json' => '{"规格": "标准"}',
  2512. 'goods_sku_title' => "标准" . $params['goods_name'],
  2513. 'goods_sku_market_price' => $params['goods_market_price'] ?? 0,
  2514. 'goods_sku_sales_price' => $params['goods_sales_price'] ?? 0,
  2515. ];
  2516. GoodsSku::insert($skuData);
  2517. }
  2518. // 入新的
  2519. if (!empty($params['goods_sku_json_value'])) {
  2520. foreach ($params['goods_sku_json_value'] as $item) {
  2521. $skus = explode(',', $item['sku']);
  2522. $skuArr = [];
  2523. for ($i = 1; $i <= count($skus); $i++) {
  2524. $skuName = "skuName" . $i;
  2525. $key = $item[$skuName];
  2526. $skuArr[$key] = $skus[$i - 1];
  2527. }
  2528. $specsJson = json_encode($skuArr);
  2529. $skuTitle = str_replace('-', ',', $item['sku']) . $params['goods_name'];
  2530. if ($operation == 'update' && !empty($item['sku_id'])) {
  2531. $model = GoodsSku::where('goods_sku_id', $item['sku_id'])->where('goods_sku_status', 'ON')->first();
  2532. if (!$model) {
  2533. $model = new GoodsSku();
  2534. } else {
  2535. unset($skusOldIds[$model->goods_sku_id]);
  2536. }
  2537. } else {
  2538. $model = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->where('goods_sku_status', 'ON')->where('goods_sku_title', $skuTitle)->first();
  2539. if (!$model) {
  2540. $model = new GoodsSku();
  2541. } else {
  2542. unset($skusOldIds[$model->goods_sku_id]);
  2543. }
  2544. }
  2545. $model->join_sku_goods_id = $params['goods_id'];
  2546. $model->goods_sku_status = $params['goods_status'];
  2547. $model->goods_sku_specs_json = $specsJson;
  2548. $model->goods_sku_title = $skuTitle;
  2549. $model->goods_sku_market_price = $params['goods_market_price'] ?? 0;
  2550. $model->goods_sku_sales_price = $item['price'];
  2551. $model->goods_sku_storage_json = json_encode(['storage' => $item['stock']]);
  2552. $model->save();
  2553. }
  2554. }
  2555. if ($operation == 'update' && !empty($skusOldIds)) {
  2556. // GoodsSku::whereIn('goods_sku_id', $skusOldIds)->delete();
  2557. GoodsSku::whereIn('goods_sku_id', $skusOldIds)->update(['goods_sku_status' => 'DISABLED']);
  2558. }
  2559. Db::commit();
  2560. } catch (\Exception $e) {
  2561. dump($e->getTrace());
  2562. Db::rollBack();
  2563. throw new BusinessException('规格数据更新异常~');
  2564. }
  2565. }
  2566. /**
  2567. * @Desc
  2568. * @Author Gorden
  2569. * @Date 2024/3/12 8:45
  2570. *
  2571. * @param array $data
  2572. * @param $model
  2573. * @return array
  2574. * @throws BusinessException
  2575. */
  2576. private static function inputFilter(array $data, $model): array
  2577. {
  2578. $table = config('database.connections.mysql.prefix') . $model->getTable();
  2579. $allow_column = $model->getConnection()->select("desc `$table`");
  2580. if (!$allow_column) {
  2581. throw new BusinessException('表不存在', 2);
  2582. }
  2583. $columns = array_column($allow_column, 'Type', 'Field');
  2584. foreach ($data as $col => $item) {
  2585. if (!isset($columns[$col])) {
  2586. unset($data[$col]);
  2587. continue;
  2588. }
  2589. // 非字符串类型传空则为null
  2590. if ($item === '' && strpos(strtolower($columns[$col]), 'varchar') === false && strpos(strtolower($columns[$col]), 'text') === false) {
  2591. $data[$col] = null;
  2592. }
  2593. if (is_array($item)) {
  2594. $data[$col] = implode(',', $item);
  2595. }
  2596. if ($item != '' && (strpos(strtolower($columns[$col]), 'varchar') || strpos(strtolower($columns[$col]), 'text'))) {
  2597. // $data[$col] = htmlspecialchars($item);
  2598. }
  2599. }
  2600. if (empty($data['created_at'])) {
  2601. unset($data['created_at']);
  2602. }
  2603. if (empty($data['updated_at'])) {
  2604. unset($data['updated_at']);
  2605. }
  2606. return $data;
  2607. }
  2608. /**
  2609. * @Desc 执行更新
  2610. * @Author Gorden
  2611. * @Date 2024/3/12 8:43
  2612. *
  2613. * @param $id
  2614. * @param $data
  2615. * @param $model
  2616. * @return void
  2617. */
  2618. private static function doUpdate($id, $data, $model)
  2619. {
  2620. $row = $model->find($id);
  2621. foreach ($data as $key => $val) {
  2622. $row->{$key} = $val;
  2623. }
  2624. $row->save();
  2625. }
  2626. /**
  2627. * @Desc 上架定时
  2628. * @Author Gorden
  2629. * @Date 2024/4/11 15:13
  2630. *
  2631. * @return void
  2632. */
  2633. public static function checkListing()
  2634. {
  2635. $key = Goods::LISTING_KEY_PREFIX . date('YmdHi');
  2636. $redis = Redis::connection();
  2637. if (!$redis->exists($key)) {
  2638. return;
  2639. }
  2640. $goodsIds = $redis->sMembers($key);
  2641. if (Goods::whereIn('goods_id', $goodsIds)->update(['goods_status' => 'ON'])) {
  2642. $redis->del($key);
  2643. }
  2644. }
  2645. /**
  2646. * @Desc 自动下架
  2647. * @Author Gorden
  2648. * @Date 2024/4/26 15:26
  2649. *
  2650. * @return void
  2651. */
  2652. public static function checkOffListing()
  2653. {
  2654. $key = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi');
  2655. $redis = Redis::connection();
  2656. if (!$redis->exists($key)) {
  2657. return;
  2658. }
  2659. $goodsIds = $redis->sMembers($key);
  2660. if (Goods::whereIn('goods_id', $goodsIds)->update(['goods_status' => 'OFF'])) {
  2661. $redis->del($key);
  2662. }
  2663. }
  2664. public static $week = [
  2665. '周一' => 1,
  2666. '周二' => 2,
  2667. '周三' => 3,
  2668. '周四' => 4,
  2669. '周五' => 5,
  2670. '周六' => 6,
  2671. '周日' => 7,
  2672. ];
  2673. }