GoodsService.php 85 KB

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