GoodsService.php 127 KB

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