GoodsService.php 113 KB

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