GoodsService.php 89 KB

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