GoodsService.php 133 KB

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