GoodsService.php 108 KB

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