GoodsService.php 131 KB

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