GoodsService.php 100 KB

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