GoodsService.php 84 KB

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