GoodsService.php 133 KB

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