GoodsService.php 95 KB

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