GoodsService.php 87 KB

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