GoodsService.php 77 KB

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