GoodsService.php 74 KB

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