GoodsService.php 100 KB

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