GoodsService.php 103 KB

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