GoodsService.php 126 KB

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