GoodsService.php 132 KB

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