GoodsService.php 100 KB

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