GoodsService.php 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042
  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. $data['goods_on_addtimes'] = date('Y-m-d\TH:i:s.u\Z', $data['goods_on_addtimes'] - 60 * 60 * 8);
  770. return json_success('', $data);
  771. } catch (\Exception $e) {
  772. dump($e->getTrace());
  773. return json_fail("查询错误~");
  774. }
  775. }
  776. /**
  777. * @Desc 添加商品
  778. * @Author Gorden
  779. * @Date 2024/3/11 10:20
  780. *
  781. * @param $params
  782. * @return Response
  783. */
  784. public static function insert($params): Response
  785. {
  786. Db::beginTransaction();
  787. try {
  788. $params['goods_id'] = "GD" . sprintf('%016d', SysSerial::getSerial()) . random_string(6, 'up');
  789. // 主表
  790. self::mainInsert($params);
  791. // 商品详情表
  792. self::detailInsert($params);
  793. // 商品标签表
  794. self::labelInsert($params);
  795. // 产品运行控制信息表
  796. self::goodsRunningInsert($params);
  797. // sku表
  798. self::goodsSkuSet($params, 'insert');
  799. // 待上架状态,上架时间大于当前时间
  800. if ($params['goods_status'] == 'PENDING' && strtotime($params['goods_on_addtimes']) > time()) {
  801. $redis = Redis::connection();
  802. $key = date('YmdHi', strtotime($params['goods_on_addtimes']));
  803. $redis->sAdd(Goods::LISTING_KEY_PREFIX . $key, $params['goods_id']);
  804. }
  805. // 自动下架
  806. if (!empty($params['goods_running_off_type']) && $params['goods_running_off_type'] == 'T' && !empty($params['goods_off_addtimes'])) {
  807. $redis = Redis::connection();
  808. $key = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi', strtotime($params['goods_off_addtimes']));
  809. $redis->sAdd($key, $params['goods_id']);
  810. }
  811. Db::commit();
  812. } catch (\PDOException $e) {
  813. Db::rollBack();
  814. dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
  815. return json_fail('数据写入失败~');
  816. } catch (BusinessException $e) {
  817. Db::rollBack();
  818. dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
  819. return json_fail($e->getMessage());
  820. } catch (\Exception $e) {
  821. Db::rollBack();
  822. dump($e->getTrace());
  823. return json_fail('数据写入失败~');
  824. }
  825. _syslog("添加商品", "商品名【" . $params['goods_name'] . "】");
  826. return json_success('success');
  827. }
  828. public static function insertRecharge($params)
  829. {
  830. try {
  831. Db::beginTransaction();
  832. $goods = new Goods();
  833. $goods->goods_id = "GD" . date('YmdHis') . random_string(6, 'up');
  834. $goods->join_goods_category_id = 59;
  835. $goods->goods_classify = 'RECHARGE';
  836. $goods->goods_status = $params['goods_status'];
  837. $goods->goods_sort = $params['goods_sort'];
  838. $goods->goods_category = $params['goods_category'];
  839. $goods->goods_name = $params['goods_name'];
  840. $goods->goods_market_price = $params['goods_sales_price'];
  841. $goods->goods_sales_price = $params['goods_sales_price'];
  842. $goods->goods_sku_json = json_encode(['规格' => [$params['goods_sales_price'] . '元']]);
  843. $goods->goods_attribute_json = json_encode(['added' => ['nbr' => $params['goods_rate'] / 100, 'mode' => 'rate'], 'min-count' => 1]);
  844. $goods->goods_cover = '/images/app/common/null-service.png';
  845. $goods->goods_process_json = json_encode(['mode' => 'do_shopping']);
  846. $goods->goods_addtimes = time();
  847. $goods->save();
  848. $sku = new GoodsSku();
  849. $sku->join_sku_goods_id = $goods->goods_id;
  850. $sku->goods_sku_status = $params['goods_status'];
  851. $sku->goods_sku_specs_json = json_encode(['规格' => $params['goods_sales_price'] . '元']);
  852. $sku->goods_sku_market_price = $params['goods_sales_price'];
  853. $sku->goods_sku_sales_price = $params['goods_sales_price'];
  854. $sku->goods_sku_storage_json = json_encode(['storage' => 9999]);
  855. $sku->save();
  856. Db::commit();
  857. return json_success('success');
  858. } catch (\Exception $e) {
  859. Db::rollBack();
  860. return json_fail('添加充值产品失败');
  861. }
  862. }
  863. public static function updateRecharge($params)
  864. {
  865. try {
  866. Db::beginTransaction();
  867. $goods = Goods::where('goods_id', $params['goods_id'])->first();
  868. if (!$goods) {
  869. return json_fail("数据异常");
  870. }
  871. $goods->goods_market_price = $params['goods_sales_price'];
  872. $goods->goods_sales_price = $params['goods_sales_price'];
  873. $goods->goods_status = $params['goods_status'];
  874. $goods->goods_sku_json = json_encode(['规格' => [$params['goods_sales_price'] . '元']]);
  875. $goods->goods_sort = $params['goods_sort'];
  876. if (!empty($goods->goods_attribute_json)) {
  877. $attributeJson = json_decode($goods->goods_attribute_json, true);
  878. $attributeJson['added']['nbr'] = $params['goods_rate'] / 100;
  879. $goods->goods_attribute_json = json_encode($attributeJson);
  880. }
  881. $goods->save();
  882. $sku = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->first();
  883. $sku->goods_sku_status = $params['goods_status'];
  884. $sku->goods_sku_specs_json = json_encode(['规格' => $params['goods_sales_price'] . '元']);
  885. $sku->goods_sku_market_price = $params['goods_sales_price'];
  886. $sku->goods_sku_sales_price = $params['goods_sales_price'];
  887. $sku->save();
  888. Db::commit();
  889. return json_success("success");
  890. } catch (\Exception $e) {
  891. Db::rollBack();
  892. dump($e->getMessage());
  893. return json_fail('编辑充值产品失败');
  894. }
  895. }
  896. public static function insertPackage($params): Response
  897. {
  898. Db::beginTransaction();
  899. try {
  900. $params['goods_id'] = "GD" . sprintf('%016d', SysSerial::getSerial()) . random_string(6, 'up');
  901. // 主表
  902. self::mainInsert($params);
  903. // 商品详情表
  904. self::detailInsert($params);
  905. // 套包组件表
  906. self::componentUpdate($params, 'insert');
  907. // 商品标签表
  908. self::labelInsert($params);
  909. // 产品运行控制信息表
  910. self::goodsRunningInsert($params);
  911. // sku表
  912. self::goodsSkuSet($params, 'insert');
  913. // 待上架状态,上架时间大于当前时间
  914. if ($params['goods_status'] == 'PENDING' && strtotime($params['goods_on_addtimes']) > time()) {
  915. $redis = Redis::connection();
  916. $key = date('YmdHi', strtotime($params['goods_on_addtimes']));
  917. $redis->sAdd(Goods::LISTING_KEY_PREFIX . $key, $params['goods_id']);
  918. }
  919. Db::commit();
  920. } catch (\PDOException $e) {
  921. Db::rollBack();
  922. dump($e->getMessage());
  923. return json_fail('数据写入失败~');
  924. } catch (BusinessException $e) {
  925. Db::rollBack();
  926. dump($e->getMessage());
  927. return json_fail($e->getMessage());
  928. } catch (\Exception $e) {
  929. Db::rollBack();
  930. dump($e->getTrace());
  931. return json_fail('数据写入失败~');
  932. }
  933. _syslog("添加套餐", "商品名【" . $params['goods_name'] . "】");
  934. return json_success('success');
  935. }
  936. public static function update($params)
  937. {
  938. Db::beginTransaction();
  939. try {
  940. // 主表
  941. self::mainUpdate($params);
  942. // 商品详情表
  943. self::detailUpdate($params);
  944. // 商品标签表
  945. self::labelUpdate($params);
  946. // 产品运行控制信息表
  947. self::goodsRunningUpdate($params);
  948. // sku表
  949. self::goodsSkuSet($params, 'update');
  950. Db::commit();
  951. } catch (BusinessException $e) {
  952. Db::rollBack();
  953. return json_fail($e->getMessage());
  954. } catch (\Exception $e) {
  955. Db::rollBack();
  956. return json_fail('数据更新失败~');
  957. }
  958. _syslog("编辑商品", "商品名【" . $params['goods_name'] . "】" ?? "商品ID:【" . $params['goods_id'] . "】");
  959. return json_success('success');
  960. }
  961. public static function changeStatus($params)
  962. {
  963. try {
  964. Goods::where('goods_id', $params['goods_id'])->update(['goods_status' => $params['goods_status']]);
  965. return json_success('修改成功');
  966. } catch (\Exception $e) {
  967. return json_fail('修改状态失败');
  968. }
  969. }
  970. public static function updatePackage($params)
  971. {
  972. Db::beginTransaction();
  973. try {
  974. // 主表
  975. self::mainUpdate($params);
  976. // 商品详情表
  977. self::detailUpdate($params);
  978. // 套包组件表
  979. self::componentUpdate($params, 'update');
  980. // 商品标签表
  981. self::labelUpdate($params);
  982. // 产品运行控制信息表
  983. self::goodsRunningUpdate($params);
  984. Db::commit();
  985. } catch (BusinessException $e) {
  986. Db::rollBack();
  987. return json_fail($e->getMessage());
  988. } catch (\Exception $e) {
  989. Db::rollBack();
  990. dump($e->getTrace());
  991. return json_fail('数据更新失败~');
  992. }
  993. _syslog("编辑套餐", "商品名【" . $params['goods_name'] . "】" ?? "商品ID:【" . $params['goods_id'] . "】");
  994. return json_success('success');
  995. }
  996. /**
  997. * @Desc 删除商品
  998. * @Author Gorden
  999. * @Date 2024/3/28 13:20
  1000. *
  1001. * @param $ids
  1002. * @return Response
  1003. */
  1004. public static function delete($ids)
  1005. {
  1006. if (!$ids) {
  1007. return json_fail("数据错误~");
  1008. }
  1009. if (!is_array($ids)) {
  1010. $ids = [$ids];
  1011. }
  1012. $goods = Goods::whereIn('goods_id', $ids)->get()->toArray();
  1013. if (!$goods) {
  1014. return json_fail("数据错误~");
  1015. }
  1016. Db::beginTransaction();
  1017. try {
  1018. Goods::whereIn('goods_id', $ids)->delete();
  1019. GoodsDetail::whereIn('join_detail_goods_id', $ids)->delete();
  1020. GoodsLabel::whereIn('join_label_goods_id', $ids)->delete();
  1021. GoodsRunning::whereIn('join_running_goods_id', $ids)->delete();
  1022. GoodsSku::whereIn('join_sku_goods_id', $ids)->delete();
  1023. Db::commit();
  1024. _syslog("删除商品 / 套餐", "ID:【" . implode(',', $ids) . "】", $goods);
  1025. return json_success("商品删除成功");
  1026. } catch (\Exception $e) {
  1027. Db::rollBack();
  1028. return json_fail("商品删除失败~");
  1029. }
  1030. }
  1031. /**
  1032. * @Desc 商品主表
  1033. * @Author Gorden
  1034. * @Date 2024/3/11 11:20
  1035. *
  1036. * @param $params
  1037. * @return mixed|string
  1038. * @throws BusinessException
  1039. */
  1040. public static function mainInsert($params)
  1041. {
  1042. if (!empty($params['goods_cover'])) {
  1043. $params['goods_cover'] = str_replace(getenv('STORAGE_DOMAIN'), '', $params['goods_cover']);
  1044. }
  1045. // 如果产品是待处理状态
  1046. if ($params['goods_status'] == 'PENDING') {
  1047. if (strtotime($params['goods_on_addtimes']) <= time()) {
  1048. $params['goods_status'] = 'ON';
  1049. }
  1050. }
  1051. $category = SysCategory::where('category_id', $params['join_goods_category_id'])->first();
  1052. if (!$category) {
  1053. throw new BusinessException("产品分类不存在~");
  1054. }
  1055. if (empty($params['goods_category'])) {
  1056. $params['goods_category'] = $category->category_classify;
  1057. }
  1058. try {
  1059. $model = new Goods();
  1060. $model->goods_id = $params['goods_id'];
  1061. $model->join_goods_category_id = $params['join_goods_category_id'] ?? 0;
  1062. $model->join_goods_supplier_id = $params['join_goods_supplier_id'] ?? 0;
  1063. $model->goods_classify = $params['goods_classify'] ?? '';
  1064. $model->goods_status = $params['goods_status'] ?? '';
  1065. $model->goods_category = $params['goods_category'] ?? '';
  1066. // $model->goods_prefix = $params['goods_prefix'] ?? 】($category->category_name ? '【' . $category->category_name . '' : '');
  1067. $model->goods_prefix = $params['goods_prefix'] ?? '';
  1068. $model->goods_name = $params['goods_name'];
  1069. $model->goods_market_price = $params['goods_market_price'] ?? 0;
  1070. $model->goods_sales_price = $params['goods_sales_price'] ?? 0;
  1071. $model->goods_sku_json = !empty($params['goods_sku_json_label']) ? json_encode($params['goods_sku_json_label']) : json_encode(['规格' => ['标准']]);
  1072. $model->goods_attribute_json = !empty($params['goods_attribute_json']) ? $params['goods_attribute_json'] : '[]';
  1073. $model->goods_title = $params['goods_title'] ?? '';
  1074. $model->goods_cover = $params['goods_cover'] ?? '';
  1075. $model->goods_on_addtimes = isset($params['goods_on_addtimes']) ? strtotime($params['goods_on_addtimes']) : null;
  1076. $model->goods_sort = $params['goods_sort'] ?? null;
  1077. $model->goods_groupby = $params['goods_groupby'] ?? '';
  1078. $model->goods_remark = $params['goods_remark'] ?? '';
  1079. $model->goods_extend_json = $params['goods_extend_json'] ?? '{}';
  1080. $model->is_support_appointment = $params['is_support_appointment'] ?? 'N';
  1081. $model->creator_user_id = JwtToken::getCurrentId();
  1082. $model->goods_addtimes = time();
  1083. $model->goods_updatetimes = time();
  1084. if (!empty($params['is_support_appointment']) && $params['is_support_appointment'] == 'Y' && !empty($params['appointment_times']) && $params['goods_category'] != 'TRAVEL') {
  1085. $times = [];
  1086. $attributeJsonTimeArr = [];
  1087. $personTotal = 0;
  1088. foreach ($params['appointment_times'] as $time) {
  1089. $attributeJsonTimeArr[] = strtotime(date('Y-m-d ') . $time['appointmentTimeStart']);
  1090. $attributeJsonTimeArr[] = strtotime(date('Y-m-d ') . $time['appointmentTimeEnd']);
  1091. $personTotal += $time['person'];
  1092. $times[$time['appointmentTimeStart']] = [
  1093. 'person' => $time['person'],
  1094. 'duration' => $time['appointmentTimeStart'] . '-' . $time['appointmentTimeEnd']
  1095. ];
  1096. }
  1097. $attributeJsonTime = date('H:i', min($attributeJsonTimeArr)) . '至' . date('H:i', max($attributeJsonTimeArr));
  1098. $newDates = [];
  1099. foreach ($params['dates'] as $date) {
  1100. $key = self::$week[$date];
  1101. $newDates[$key] = $date;
  1102. }
  1103. ksort($newDates);
  1104. $currentDate = current($newDates);
  1105. $lastDate = end($newDates);
  1106. $attributeJsonDate = '';
  1107. if (self::$week[$lastDate] - self::$week[$currentDate] + 1 > count($newDates)) {
  1108. $attributeJsonDate = implode(',', $newDates);
  1109. } else if (self::$week[$lastDate] - self::$week[$currentDate] + 1 == count($newDates)) {
  1110. $attributeJsonDate = $currentDate . '至' . $lastDate;
  1111. }
  1112. if (isset($params['work_time'])){
  1113. $workTimeStart = date('H:i',strtotime($params['work_time'][0]));
  1114. $workTimeEnd = date('H:i',strtotime($params['work_time'][1]));
  1115. $attributeJsonTime = $workTimeStart.'至'.$workTimeEnd;
  1116. }
  1117. $attributeJson = [
  1118. 'icon' => $model->goods_cover,
  1119. 'date' => $attributeJsonDate,
  1120. 'time' => $attributeJsonTime,
  1121. 'dates' => $newDates ? array_values($newDates) : [],
  1122. 'times' => $times,
  1123. 'person' => $personTotal
  1124. ];
  1125. if(isset($params['address'])){
  1126. $attributeJson['address'] = $params['address'];
  1127. }
  1128. if (isset($params['min_count'])){
  1129. $attributeJson['min-count'] = $params['min_count'];
  1130. }else{
  1131. $attributeJson['min-count'] = 1;
  1132. }
  1133. if (isset($params['teachers'])){
  1134. $attributeJson['teachers'] = $params['teachers'];
  1135. }
  1136. if (!empty($params['appointment_label'])) {
  1137. $attributeJson['label'] = $params['appointment_label'];
  1138. }
  1139. if (!empty($params['address'])) {
  1140. $attributeJson['address'] = $params['address'];
  1141. }
  1142. if (!empty($params['position'])){
  1143. $attributeJson['position'] = $params['position'];
  1144. }
  1145. if (isset($params['goods_service_premises'])){
  1146. $attributeJson['service_premises_id'] = $params['goods_service_premises'];
  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 ($model->save()) {
  1239. return $model->goods_id;
  1240. }
  1241. // 异常
  1242. throw new BusinessException("数据写入失败~");
  1243. } catch (\Exception $e) {
  1244. dump($e->getMessage());
  1245. throw new BusinessException("数据写入失败~");
  1246. }
  1247. }
  1248. /**
  1249. * @Desc 详情表
  1250. * @Author Gorden
  1251. * @Date 2024/3/11 11:19
  1252. *
  1253. * @param $params
  1254. * @return void
  1255. * @throws BusinessException
  1256. */
  1257. public static function detailInsert($params)
  1258. {
  1259. if (!empty($params['goods_detail_slider_json'])) {
  1260. $params['goods_detail_slider_json'] = str_replace(getenv('STORAGE_DOMAIN'), '', $params['goods_detail_slider_json']);
  1261. $params['goods_detail_slider_json'] = json_encode(['slider' => $params['goods_detail_slider_json']]);
  1262. }
  1263. try {
  1264. $model = new GoodsDetail();
  1265. $model->join_detail_goods_id = $params['goods_id'];
  1266. $model->goods_detail_code_json = $params['goods_detail_code_json'] ?? '{}';
  1267. $model->goods_detail_slider_json = $params['goods_detail_slider_json'] ?? '{}';
  1268. $model->goods_detail_specs_json = $params['goods_detail_specs_json'] ?? '{}';
  1269. $model->goods_detail_content = $params['goods_detail_content'] ?? '';
  1270. if (!$model->save()) {
  1271. // 异常
  1272. throw new BusinessException("轮播图/详情数据写入失败~");
  1273. }
  1274. } catch (\Exception $e) {
  1275. dump($e->getMessage());
  1276. throw new BusinessException("轮播图/详情数据写入失败~");
  1277. }
  1278. }
  1279. public static function componentUpdate($params, $type = 'insert')
  1280. {
  1281. Db::beginTransaction();
  1282. try {
  1283. // 有先删除
  1284. if ($type == 'update') {
  1285. GoodsComponent::where('join_component_master_goods_id', $params['goods_id'])->delete();
  1286. } else {
  1287. Goods::where('goods_id', $params['goods_id'])->update(['goods_sku_json' => '{"规格": ["标准"]}']);
  1288. $skuData = [
  1289. 'join_sku_goods_id' => $params['goods_id'],
  1290. 'goods_sku_status' => 'ON',
  1291. 'goods_sku_specs_json' => '{"规格": "标准"}',
  1292. 'goods_sku_title' => "标准" . $params['goods_name'],
  1293. 'goods_sku_market_price' => $params['goods_market_price'] ?? 0,
  1294. 'goods_sku_sales_price' => $params['goods_sales_price'] ?? 0,
  1295. ];
  1296. GoodsSku::insert($skuData);
  1297. }
  1298. $data = [];
  1299. foreach ($params['goods_content_list'] as $item) {
  1300. $goods = Goods::where('goods_id', $params['goods_id'])->first();
  1301. if (!$goods) {
  1302. continue;
  1303. }
  1304. $data[] = [
  1305. 'join_component_master_goods_id' => $params['goods_id'],
  1306. 'join_component_goods_id' => $item['goods_id'] ?? '',
  1307. 'join_component_goods_sku_id' => $item['sku_id'] ?? '',
  1308. 'goods_component_price' => $item['goods_sales_price'] ?? '',
  1309. 'goods_component_cover' => $goods->goods_cover,
  1310. 'goods_component_json' => json_encode(['goods_name' => $item['goods_name'], 'nbr' => $item['nbr'], 'sku_id' => $item['sku_id'] ?? '']),
  1311. 'goods_component_addtimes' => time()
  1312. ];
  1313. }
  1314. if ($data) {
  1315. GoodsComponent::insert($data);
  1316. }
  1317. Db::commit();
  1318. } catch (\Exception $e) {
  1319. Db::rollBack();
  1320. dump($e->getMessage());
  1321. throw new BusinessException("数据写入失败~");
  1322. }
  1323. }
  1324. /**
  1325. * @Desc 标签表
  1326. * @Author Gorden
  1327. * @Date 2024/3/11 11:32
  1328. *
  1329. * @param $params
  1330. * @return void
  1331. * @throws BusinessException
  1332. */
  1333. public static function labelInsert($params)
  1334. {
  1335. $model = new GoodsLabel();
  1336. $model->join_label_goods_id = $params['goods_id'];
  1337. $model->goods_label = $params['goods_label'] ? implode(',', $params['goods_label']) : '';
  1338. $model->goods_label_extend_json = !empty($params['goods_label_extend_json']) ? $params['goods_label_extend_json'] : '{}';
  1339. if (!$model->save()) {
  1340. // 异常
  1341. throw new BusinessException('数据写入失败~');
  1342. }
  1343. }
  1344. /**
  1345. * @Desc 产品运行控制信息表
  1346. * @Author Gorden
  1347. * @Date 2024/3/11 11:38
  1348. *
  1349. * @param $params
  1350. * @return void
  1351. * @throws BusinessException
  1352. */
  1353. public static function goodsRunningInsert($params)
  1354. {
  1355. try {
  1356. $model = new GoodsRunning();
  1357. $model->join_running_goods_id = $params['goods_id'];
  1358. $model->goods_running_storage = $params['goods_running_storage'] ?? 0;
  1359. $model->goods_running_sale = $params['goods_running_sale'] ?? 0;
  1360. $model->goods_running_off_type = !empty($params['goods_running_off_type']) ? $params['goods_running_off_type'] : '';
  1361. $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'])]) : '[]';
  1362. if (!$model->save()) {
  1363. throw new BusinessException('数据写入失败');
  1364. }
  1365. } catch (\Exception $e) {
  1366. dump($e->getMessage());
  1367. throw new BusinessException('数据写入失败');
  1368. }
  1369. }
  1370. /**
  1371. * @Desc SKU
  1372. * @Author Gorden
  1373. * @Date 2024/3/11 12:01
  1374. *
  1375. * @param $params
  1376. * @return void
  1377. * @throws BusinessException
  1378. */
  1379. public static function skuInsert($params)
  1380. {
  1381. $model = new GoodsSku();
  1382. $model->join_sku_goods_id = $params['goods_id'];
  1383. $model->goods_sku_status = $params['goods_sku_status'];
  1384. $model->goods_sku_specs_json = $params['goods_sku_specs_json'];
  1385. $model->goods_sku_title = $params['goods_sku_title'];
  1386. $model->goods_sku_images_json = $params['goods_sku_images_json'];
  1387. $model->goods_sku_content = $params['goods_sku_content'];
  1388. $model->goods_sku_market_price = $params['goods_sku_market_price'];
  1389. $model->goods_sku_sales_price = $params['goods_sku_sales_price'];
  1390. $model->goods_sku_storage_json = $params['goods_sku_storage_json'];
  1391. $model->goods_sku_service_json = $params['goods_sku_service_json'];
  1392. $model->goods_sku_extend_json = $params['goods_sku_extend_json'];
  1393. if (!$model->save()) {
  1394. throw new BusinessException('规格数据写入失败~');
  1395. }
  1396. }
  1397. /**
  1398. * @Desc
  1399. * @Author Gorden
  1400. * @Date 2024/3/12 8:44
  1401. *
  1402. * @param $params
  1403. * @return void
  1404. * @throws BusinessException
  1405. */
  1406. public static function mainUpdate($params)
  1407. {
  1408. try {
  1409. $data = self::inputFilter($params, new Goods());
  1410. if (!empty($data['goods_cover'])) {
  1411. $data['goods_cover'] = str_replace(getenv('STORAGE_DOMAIN'), '', $data['goods_cover']);
  1412. }
  1413. $data['goods_on_addtimes'] = isset($data['goods_on_addtimes']) ? strtotime($data['goods_on_addtimes']) : 0;
  1414. $data['goods_sku_json'] = !empty($params['goods_sku_json_label']) ? json_encode($params['goods_sku_json_label']) : json_encode(['规格' => []]);
  1415. $row = Goods::find($data['goods_id']);
  1416. if ($row->join_goods_category_id != $data['join_goods_category_id']) {
  1417. $category = SysCategory::where('category_id', $params['join_goods_category_id'])->first();
  1418. if (!$category) {
  1419. throw new BusinessException("产品分类不存在~");
  1420. }
  1421. $data['goods_category'] = $category->category_classify ?? '';
  1422. $data['goods_prefix'] = $data['goods_prefix'] ?? ($category->category_name ? '【' . $category->category_name . '】' : '');
  1423. }
  1424. // 上架时间有变动
  1425. if ($data['goods_status'] == 'PENDING' && $row->goods_on_addtimes != $data['goods_on_addtimes']) {
  1426. $redis = Redis::connection();
  1427. // 删掉原来的
  1428. $oldKey = Goods::LISTING_KEY_PREFIX . date('YmdHi', $row->goods_on_addtimes);
  1429. $redis->srem($oldKey, $data['goods_id']);
  1430. // 加入新的
  1431. $newKey = Goods::LISTING_KEY_PREFIX . date('YmdHi', $data['goods_on_addtimes']);
  1432. $redis->sadd($newKey, $data['goods_id']);
  1433. }
  1434. if (!empty($params['is_support_appointment']) && $params['is_support_appointment'] == 'Y' && !empty($params['appointment_times']) && $params['goods_category'] != 'TRAVEL') {
  1435. $attributeJson = [];
  1436. if (!empty($row->goods_attribute_json)) {
  1437. $attributeJson = json_decode($row->goods_attribute_json, true);
  1438. }
  1439. $times = [];
  1440. $attributeJsonTimeArr = [];
  1441. $personTotal = 0;
  1442. foreach ($params['appointment_times'] as $time) {
  1443. $attributeJsonTimeArr[] = strtotime(date('Y-m-d ') . $time['appointmentTimeStart']);
  1444. $attributeJsonTimeArr[] = strtotime(date('Y-m-d ') . $time['appointmentTimeEnd']);
  1445. $personTotal += $time['person'];
  1446. $times[$time['appointmentTimeStart']] = [
  1447. 'person' => $time['person'],
  1448. 'duration' => $time['appointmentTimeStart'] . '-' . $time['appointmentTimeEnd']
  1449. ];
  1450. }
  1451. $attributeJsonTime = date('H:i', min($attributeJsonTimeArr)) . '至' . date('H:i', max($attributeJsonTimeArr));
  1452. $newDates = [];
  1453. foreach ($params['dates'] as $date) {
  1454. $key = self::$week[$date];
  1455. $newDates[$key] = $date;
  1456. }
  1457. ksort($newDates);
  1458. $currentDate = current($newDates);
  1459. $lastDate = end($newDates);
  1460. $attributeJsonDate = '';
  1461. if (self::$week[$lastDate] - self::$week[$currentDate] + 1 > count($newDates)) {
  1462. $attributeJsonDate = implode(',', $newDates);
  1463. } else if (self::$week[$lastDate] - self::$week[$currentDate] + 1 == count($newDates)) {
  1464. $attributeJsonDate = $currentDate . '至' . $lastDate;
  1465. }
  1466. $attributeJson['date'] = $attributeJsonDate;
  1467. $attributeJson['icon'] = $data['goods_cover'] ?? '';
  1468. $attributeJson['time'] = $attributeJsonTime;
  1469. $attributeJson['dates'] = $newDates ? array_values($newDates) : [];
  1470. $attributeJson['times'] = $times;
  1471. $attributeJson['person'] = $personTotal;
  1472. if (isset($params['work_time'])){
  1473. $workTimeStart = date('H:i',strtotime($params['work_time'][0]));
  1474. $workTimeEnd = date('H:i',strtotime($params['work_time'][1]));
  1475. $attributeJson['time'] = $workTimeStart.'至'.$workTimeEnd;
  1476. }
  1477. if (!empty($params['appointment_label'])) {
  1478. $attributeJson['label'] = $params['appointment_label'];
  1479. }
  1480. if (!empty($params['address'])) {
  1481. $attributeJson['address'] = $params['address'];
  1482. }
  1483. if (!empty($params['position'])){
  1484. $attributeJson['position'] = $params['position'];
  1485. }
  1486. if (isset($params['min_count'])){
  1487. $attributeJson['min-count'] = $params['min_count'];
  1488. }else{
  1489. $attributeJson['min-count'] = 1;
  1490. }
  1491. if (isset($params['teachers'])){
  1492. $attributeJson['teachers'] = $params['teachers'];
  1493. }
  1494. if (isset($params['goods_service_premises'])){
  1495. $attributeJson['service_premises_id'] = $params['goods_service_premises'];
  1496. }
  1497. $data['goods_attribute_json'] = json_encode($attributeJson, JSON_UNESCAPED_UNICODE);
  1498. }
  1499. if (!empty($params['is_support_appointment']) && $params['is_support_appointment'] == 'Y' && !empty($params['appointment_times']) && $params['goods_category'] == 'TRAVEL') {
  1500. $attributeJson = [];
  1501. if (!empty($row->goods_attribute_json)) {
  1502. $attributeJson = json_decode($row->goods_attribute_json, true);
  1503. }
  1504. $attributeJson['travel-day'] = $params['travel_day'];
  1505. $attributeJson['travel-begin'] = $params['travel_begin'];
  1506. $attributeJson['travel-night'] = $params['travel_night'];
  1507. $attributeJson['travel-trans'] = $params['travel_trans'];
  1508. $unixs = [];
  1509. foreach ($params['appointment_times'] as $times) {
  1510. $unix = strtotime($times['days']);
  1511. $unixs[$unix] = $times['person'];
  1512. }
  1513. ksort($unixs);
  1514. $months = [];
  1515. foreach ($unixs as $key => $unix) {
  1516. $month = date('Ym', $key);
  1517. $day = date('d', $key);
  1518. $months[$month][$day] = $unix;
  1519. }
  1520. $attributeJson['month'] = $months;
  1521. $data['goods_attribute_json'] = json_encode($attributeJson, JSON_UNESCAPED_UNICODE);
  1522. }
  1523. if (!empty($params['goods_premisses'])) {
  1524. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  1525. if (is_json($data['goods_attribute_json'])) {
  1526. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  1527. } else {
  1528. $data['goods_attribute_json'] = [];
  1529. }
  1530. } elseif (empty($data['goods_attribute_json'])) {
  1531. $data['goods_attribute_json'] = [];
  1532. }
  1533. $data['goods_attribute_json']['premisses'] = $params['goods_premisses'];
  1534. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  1535. }
  1536. if (!empty($data['goods_json']) && $data['join_goods_category_id'] == 65) {
  1537. // $goodsJson = json_decode($data['goods_json'], true);
  1538. // foreach ($goodsJson as $key => $item) {
  1539. // $goodsJson[$key]['color'] = hexToRgb($item['color']);
  1540. // }
  1541. //
  1542. // $data['goods_json'] = json_encode($goodsJson);
  1543. } elseif (!empty($data['goods_json']) && $data['join_goods_category_id'] == 43) {
  1544. $goodsJson = json_decode($data['goods_json'], true);
  1545. $newGoodsJson = [];
  1546. foreach ($goodsJson as $item1) {
  1547. if (empty($item1['title'])) {
  1548. continue;
  1549. }
  1550. $newItem1 = [];
  1551. foreach ($item1['items'] as $item2) {
  1552. $newParams = [];
  1553. foreach ($item2['params'] as $param) {
  1554. if (!empty($param[0]) || !empty($param[1])) {
  1555. $newParams[] = $param;
  1556. }
  1557. }
  1558. $newItem1['items'][$item2['key']] = $newParams;
  1559. }
  1560. $newItem1['service'] = $item1['service'];
  1561. $newGoodsJson[$item1['title']] = $newItem1;
  1562. }
  1563. $data['goods_json'] = json_encode($newGoodsJson);
  1564. } else {
  1565. $data['goods_json'] = '[]';
  1566. }
  1567. if (!empty($params['goods_theme_color']) && !empty($params['goods_theme_icon'])) {
  1568. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  1569. if (is_json($data['goods_attribute_json'])) {
  1570. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  1571. } else {
  1572. $data['goods_attribute_json'] = [];
  1573. }
  1574. } elseif (empty($data['goods_attribute_json'])) {
  1575. $data['goods_attribute_json'] = [];
  1576. }
  1577. $data['goods_attribute_json']['bg'] = $params['goods_theme_color'];
  1578. $data['goods_attribute_json']['icon'] = str_replace(getenv('STORAGE_DOMAIN'), '', $params['goods_theme_icon']);
  1579. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  1580. }
  1581. foreach ($data as $key => $val) {
  1582. $row->{$key} = $val;
  1583. }
  1584. $row->goods_updatetimes = time();
  1585. $row->save();
  1586. } catch (BusinessException $e) {
  1587. throw new BusinessException($e->getMessage());
  1588. } catch (\Exception $e) {
  1589. dump($e->getTrace());
  1590. throw new BusinessException('数据更新异常~1');
  1591. }
  1592. }
  1593. /**
  1594. * @Desc
  1595. * @Author Gorden
  1596. * @Date 2024/3/12 9:57
  1597. *
  1598. * @param $params
  1599. * @return void
  1600. * @throws BusinessException
  1601. */
  1602. public static function detailUpdate($params)
  1603. {
  1604. try {
  1605. $data = self::inputFilter($params, new GoodsDetail());
  1606. if (!empty($data['goods_detail_slider_json'])) {
  1607. $data['goods_detail_slider_json'] = str_replace(getenv('STORAGE_DOMAIN'), '', $data['goods_detail_slider_json']);
  1608. $data['goods_detail_slider_json'] = json_encode(['slider' => $data['goods_detail_slider_json']]);
  1609. }
  1610. // 根据goods_id 查详情ID
  1611. $detail = GoodsDetail::where('join_detail_goods_id', $params['goods_id'])->first();
  1612. if ($detail) {
  1613. self::doUpdate($detail->join_detail_goods_id, $data, new GoodsDetail());
  1614. } else {
  1615. $data['join_detail_goods_id'] = $params['goods_id'];
  1616. GoodsDetail::insert($data);
  1617. }
  1618. } catch (BusinessException $e) {
  1619. throw new BusinessException($e->getMessage());
  1620. } catch (\Exception $e) {
  1621. dump($e->getMessage());
  1622. throw new BusinessException('轮播图/详情数据更新异常~');
  1623. }
  1624. }
  1625. /**
  1626. * @Desc
  1627. * @Author Gorden
  1628. * @Date 2024/3/12 9:58
  1629. *
  1630. * @param $params
  1631. * @return void
  1632. * @throws BusinessException
  1633. */
  1634. public static function labelUpdate($params)
  1635. {
  1636. try {
  1637. $data = self::inputFilter($params, new GoodsLabel());
  1638. // 根据goods_id 查详情ID
  1639. $detail = GoodsLabel::where('join_label_goods_id', $params['goods_id'])->first();
  1640. if ($detail) {
  1641. self::doUpdate($detail->goods_label_id, $data, new GoodsLabel());
  1642. } else {
  1643. $data['join_label_goods_id'] = $params['goods_id'];
  1644. GoodsLabel::insert($data);
  1645. }
  1646. } catch (BusinessException $e) {
  1647. throw new BusinessException($e->getMessage());
  1648. } catch (\Exception $e) {
  1649. dump($e->getMessage());
  1650. throw new BusinessException('数据更新异常~3');
  1651. }
  1652. }
  1653. /**
  1654. * @Desc
  1655. * @Author Gorden
  1656. * @Date 2024/3/12 9:59
  1657. *
  1658. * @param $params
  1659. * @return void
  1660. * @throws BusinessException
  1661. */
  1662. public static function goodsRunningUpdate($params)
  1663. {
  1664. try {
  1665. $data = self::inputFilter($params, new GoodsRunning());
  1666. // 根据goods_id 查详情ID
  1667. $detail = GoodsRunning::where('join_running_goods_id', $params['goods_id'])->first();
  1668. if (!empty($params['goods_running_off_type']) && $params['goods_running_off_type'] == 'T') {
  1669. $redis = Redis::connection();
  1670. if (!empty($detail->goods_running_off_json)) {
  1671. $goodsRunningOffJson = json_decode($detail->goods_running_off_json, true);
  1672. if (isset($goodsRunningOffJson['time'])) {
  1673. $oldKey = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi', $goodsRunningOffJson['time']);
  1674. $goodsRunningOffJson['time'] = strtotime($params['goods_off_addtimes']);
  1675. $data['goods_running_off_json'] = json_encode($goodsRunningOffJson);
  1676. // 有老的下架时间,删除老的
  1677. $redis->srem($oldKey, $params['goods_id']);
  1678. } else {
  1679. $goodsRunningOffJson['time'] = strtotime($params['goods_off_addtimes']);
  1680. $data['goods_running_off_json'] = json_encode($goodsRunningOffJson);
  1681. }
  1682. } else {
  1683. $data['goods_running_off_json'] = json_encode(['time' => strtotime($params['goods_off_addtimes'])]);
  1684. }
  1685. // 加入自动下架
  1686. $newKey = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi', strtotime($params['goods_off_addtimes']));
  1687. $redis->sAdd($newKey, $params['goods_id']);
  1688. } 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') {
  1689. $goodsRunningOffJson = json_decode($detail->goods_running_off_json, true);
  1690. if (isset($goodsRunningOffJson['time'])) {
  1691. $oldKey = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi', $goodsRunningOffJson['time']);
  1692. $redis = Redis::connection();
  1693. $redis->srem($oldKey, $params['goods_id']);
  1694. }
  1695. }
  1696. if ($detail) {
  1697. self::doUpdate($detail->join_running_goods_id, $data, new GoodsRunning());
  1698. } else {
  1699. // 兼容老数据……
  1700. $data['join_running_goods_id'] = $params['goods_id'];
  1701. GoodsRunning::insert($data);
  1702. }
  1703. } catch (BusinessException $e) {
  1704. throw new BusinessException($e->getMessage());
  1705. } catch (\Exception $e) {
  1706. dump($e->getMessage());
  1707. throw new BusinessException('数据更新异常~');
  1708. }
  1709. }
  1710. /**
  1711. * @Desc sku 设置
  1712. * @Author Gorden
  1713. * @Date 2024/4/10 10:43
  1714. *
  1715. * @param $params
  1716. * @return void
  1717. * @throws BusinessException
  1718. */
  1719. public static function goodsSkuSet($params, $operation = 'insert')
  1720. {
  1721. try {
  1722. Db::beginTransaction();
  1723. $skusOldIds = [];
  1724. if ($operation == 'update') {
  1725. // 查出所有的
  1726. $skusOldIds = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->pluck('goods_sku_id', 'goods_sku_id');
  1727. // 删掉原有的
  1728. // GoodsSku::where('join_sku_goods_id', $params['goods_id'])->delete();
  1729. }
  1730. if (empty($skusOldIds) && empty($params['goods_sku_json_value'])) {
  1731. $skuData = [
  1732. 'join_sku_goods_id' => $params['goods_id'],
  1733. 'goods_sku_status' => 'ON',
  1734. 'goods_sku_specs_json' => '{"规格": "标准"}',
  1735. 'goods_sku_title' => "标准" . $params['goods_name'],
  1736. 'goods_sku_market_price' => $params['goods_market_price'] ?? 0,
  1737. 'goods_sku_sales_price' => $params['goods_sales_price'] ?? 0,
  1738. ];
  1739. GoodsSku::insert($skuData);
  1740. }
  1741. // 入新的
  1742. if (!empty($params['goods_sku_json_value'])) {
  1743. foreach ($params['goods_sku_json_value'] as $item) {
  1744. $skus = explode(',', $item['sku']);
  1745. $skuArr = [];
  1746. for ($i = 1; $i <= count($skus); $i++) {
  1747. $skuName = "skuName" . $i;
  1748. $key = $item[$skuName];
  1749. $skuArr[$key] = $skus[$i - 1];
  1750. }
  1751. $specsJson = json_encode($skuArr);
  1752. $skuTitle = str_replace('-', ',', $item['sku']) . $params['goods_name'];
  1753. if ($operation == 'update' && !empty($item['sku_id'])) {
  1754. $model = GoodsSku::where('goods_sku_id', $item['sku_id'])->first();
  1755. if (!$model) {
  1756. $model = new GoodsSku();
  1757. } else {
  1758. unset($skusOldIds[$model->goods_sku_id]);
  1759. }
  1760. } else {
  1761. $model = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->where('goods_sku_title', $skuTitle)->first();
  1762. if (!$model) {
  1763. $model = new GoodsSku();
  1764. } else {
  1765. unset($skusOldIds[$model->goods_sku_id]);
  1766. }
  1767. }
  1768. $model->join_sku_goods_id = $params['goods_id'];
  1769. $model->goods_sku_status = $params['goods_status'];
  1770. $model->goods_sku_specs_json = $specsJson;
  1771. $model->goods_sku_title = $skuTitle;
  1772. $model->goods_sku_market_price = $params['goods_market_price'] ?? 0;
  1773. $model->goods_sku_sales_price = $item['price'];
  1774. $model->goods_sku_storage_json = json_encode(['storage' => $item['stock']]);
  1775. $model->save();
  1776. }
  1777. }
  1778. if ($operation == 'update' && !empty($skusOldIds)) {
  1779. GoodsSku::whereIn('goods_sku_id', $skusOldIds)->delete();
  1780. }
  1781. Db::commit();
  1782. } catch (\Exception $e) {
  1783. dump($e->getTrace());
  1784. Db::rollBack();
  1785. throw new BusinessException('规格数据更新异常~');
  1786. }
  1787. }
  1788. /**
  1789. * @Desc
  1790. * @Author Gorden
  1791. * @Date 2024/3/12 8:45
  1792. *
  1793. * @param array $data
  1794. * @param $model
  1795. * @return array
  1796. * @throws BusinessException
  1797. */
  1798. private static function inputFilter(array $data, $model): array
  1799. {
  1800. $table = config('database.connections.mysql.prefix') . $model->getTable();
  1801. $allow_column = $model->getConnection()->select("desc `$table`");
  1802. if (!$allow_column) {
  1803. throw new BusinessException('表不存在', 2);
  1804. }
  1805. $columns = array_column($allow_column, 'Type', 'Field');
  1806. foreach ($data as $col => $item) {
  1807. if (!isset($columns[$col])) {
  1808. unset($data[$col]);
  1809. continue;
  1810. }
  1811. // 非字符串类型传空则为null
  1812. if ($item === '' && strpos(strtolower($columns[$col]), 'varchar') === false && strpos(strtolower($columns[$col]), 'text') === false) {
  1813. $data[$col] = null;
  1814. }
  1815. if (is_array($item)) {
  1816. $data[$col] = implode(',', $item);
  1817. }
  1818. if ($item != '' && (strpos(strtolower($columns[$col]), 'varchar') || strpos(strtolower($columns[$col]), 'text'))) {
  1819. $data[$col] = htmlspecialchars($item);
  1820. }
  1821. }
  1822. if (empty($data['created_at'])) {
  1823. unset($data['created_at']);
  1824. }
  1825. if (empty($data['updated_at'])) {
  1826. unset($data['updated_at']);
  1827. }
  1828. return $data;
  1829. }
  1830. /**
  1831. * @Desc 执行更新
  1832. * @Author Gorden
  1833. * @Date 2024/3/12 8:43
  1834. *
  1835. * @param $id
  1836. * @param $data
  1837. * @param $model
  1838. * @return void
  1839. */
  1840. private static function doUpdate($id, $data, $model)
  1841. {
  1842. $row = $model->find($id);
  1843. foreach ($data as $key => $val) {
  1844. $row->{$key} = $val;
  1845. }
  1846. $row->save();
  1847. }
  1848. /**
  1849. * @Desc 上架定时
  1850. * @Author Gorden
  1851. * @Date 2024/4/11 15:13
  1852. *
  1853. * @return void
  1854. */
  1855. public static function checkListing()
  1856. {
  1857. $key = Goods::LISTING_KEY_PREFIX . date('YmdHi');
  1858. $redis = Redis::connection();
  1859. if (!$redis->exists($key)) {
  1860. return;
  1861. }
  1862. $goodsIds = $redis->sMembers($key);
  1863. if (Goods::whereIn('goods_id', $goodsIds)->update(['goods_status' => 'ON'])) {
  1864. $redis->del($key);
  1865. }
  1866. }
  1867. /**
  1868. * @Desc 自动下架
  1869. * @Author Gorden
  1870. * @Date 2024/4/26 15:26
  1871. *
  1872. * @return void
  1873. */
  1874. public static function checkOffListing()
  1875. {
  1876. $key = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi');
  1877. $redis = Redis::connection();
  1878. if (!$redis->exists($key)) {
  1879. return;
  1880. }
  1881. $goodsIds = $redis->sMembers($key);
  1882. if (Goods::whereIn('goods_id', $goodsIds)->update(['goods_status' => 'OFF'])) {
  1883. $redis->del($key);
  1884. }
  1885. }
  1886. public static $week = [
  1887. '周一' => 1,
  1888. '周二' => 2,
  1889. '周三' => 3,
  1890. '周四' => 4,
  1891. '周五' => 5,
  1892. '周六' => 6,
  1893. '周日' => 7,
  1894. ];
  1895. }