GoodsService.php 132 KB

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