GoodsService.php 103 KB

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