GoodsService.php 113 KB

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