GoodsService.php 108 KB

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