MemberRechargeBenefitService.php 87 KB

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