GoodsService.php 87 KB

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