GoodsService.php 107 KB

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