GoodsService.php 90 KB

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