GoodsService.php 128 KB

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