GoodsService.php 77 KB

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