GoodsService.php 103 KB

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