CardService.php 85 KB

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