GoodsService.php 79 KB

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