GoodsService.php 125 KB

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