GoodsService.php 126 KB

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