GoodsService.php 77 KB

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