GoodsService.php 111 KB

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