GoodsService.php 132 KB

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