GoodsService.php 103 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340
  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['express_json'] = [];
  588. if(!empty($data['goods_express_json'])){
  589. $goodsExpressJson = json_decode($data['goods_express_json'],true);
  590. if(isset($goodsExpressJson['express']) && $goodsExpressJson['express'] == 'Y'){
  591. $data['express_json'][] = 'express';
  592. }
  593. if(isset($goodsExpressJson['self']) && $goodsExpressJson['self'] == 'Y'){
  594. $data['express_json'][] = 'self';
  595. }
  596. if(isset($goodsExpressJson['arrival']) && $goodsExpressJson['arrival'] == 'Y'){
  597. $data['express_json'][] = 'arrival';
  598. }
  599. }
  600. $data['appointment_times'] = [];
  601. if ($data['is_support_appointment'] == 'Y' && isset($extendJson['dates'])) {
  602. $data['dates'] = $extendJson['dates'] ?? [];
  603. if (isset($extendJson['times'])) {
  604. $times = [];
  605. foreach ($extendJson['times'] as $time) {
  606. if (!empty($time['duration'])) {
  607. $startEndTime = explode('-', $time['duration']);
  608. $times[] = [
  609. 'person' => $time['person'],
  610. 'appointmentTimeStart' => $startEndTime[0],
  611. 'appointmentTimeEnd' => $startEndTime[1],
  612. ];
  613. }
  614. }
  615. $data['appointment_times'] = $times;
  616. }
  617. if (isset($extendJson['time'])){
  618. $extendJsonTime = explode('至',$extendJson['time']);
  619. if(isset($extendJsonTime[0]) && isset($extendJsonTime[1])){
  620. $data['work_time'] = [
  621. date('Y-m-d H:i:s',strtotime(date('Y-m-d ').$extendJsonTime[0])),
  622. date('Y-m-d H:i:s',strtotime(date('Y-m-d ').$extendJsonTime[1])),
  623. ];
  624. }
  625. }
  626. // if (isset($extendJson['teachers'])){
  627. // $data['teachers'] = $extendJson['teachers'];
  628. // }
  629. // if (isset($extendJson['address'])) {
  630. // $data['address'] = $extendJson['address'];
  631. // }
  632. // if (isset($extendJson['min-count'])){
  633. // $data['min_count'] = $extendJson['min-count'];
  634. // }
  635. // if (isset($extendJson['position'])) {
  636. // $data['position'] = $extendJson['position'];
  637. // }
  638. if (isset($extendJson['label'])) {
  639. $data['appointment_label'] = $extendJson['label'];
  640. }
  641. }
  642. if (isset($extendJson['month']) && $data['is_support_appointment'] == 'Y'){
  643. $times = [];
  644. foreach ($extendJson['month'] as $monKey => $months){
  645. foreach ($months as $day => $person){
  646. $times[] = [
  647. 'days'=>date('Y-m-d',strtotime($monKey.$day)),
  648. 'person'=>$person
  649. ];
  650. }
  651. }
  652. $data['appointment_times'] = $times;
  653. $data['travel_begin'] = $extendJson['travel-begin'];
  654. $data['travel_day'] = $extendJson['travel-day'];
  655. $data['travel_night'] = $extendJson['travel-night'];
  656. $data['travel_trans'] = $extendJson['travel-trans'];
  657. }
  658. if (!empty($data['goods_json']) && $data['join_goods_category_id'] == 65) {
  659. // foreach ($data['goods_json'] as $key => $datum) {
  660. // $data['goods_json'][$key]['color'] = rgbToHex($datum['color']);
  661. // }
  662. } elseif (!empty($data['goods_json']) && $data['join_goods_category_id'] == 43) {
  663. $goodsJsonNew = [];
  664. foreach ($data['goods_json'] as $key1 => $item1) {
  665. $itemsNew = [];
  666. if (isset($item1['items'])) {
  667. foreach ($item1['items'] as $key2 => $item2) {
  668. $itemsNew[] = [
  669. 'key' => $key2,
  670. 'params' => $item2
  671. ];
  672. }
  673. }
  674. $goodsJsonNew[] = [
  675. 'title' => $key1,
  676. 'service' => $item1['service'] ?? '',
  677. 'items' => $itemsNew
  678. ];
  679. }
  680. $data['goods_json'] = $goodsJsonNew;
  681. }
  682. if (isset($extendJson['teachers'])){
  683. $data['teachers'] = $extendJson['teachers'];
  684. }
  685. if (isset($extendJson['address'])) {
  686. $data['address'] = $extendJson['address'];
  687. }
  688. if (isset($extendJson['min-count'])){
  689. $data['min_count'] = $extendJson['min-count'];
  690. }
  691. if (isset($extendJson['position'])) {
  692. $data['position'] = $extendJson['position'];
  693. }
  694. $data['goods_on_addtimes'] = date('Y-m-d\TH:i:s.u\Z', $data['goods_on_addtimes'] - 60 * 60 * 8);
  695. // 详情表数据
  696. if (!empty($data['goods_detail_specs_json'])){
  697. $specsJson = json_decode($data['goods_detail_specs_json'],true);
  698. foreach($specsJson as $itemSpecsJson){
  699. if (isset($itemSpecsJson['key']) && $itemSpecsJson['key'] == '课时'){
  700. $data['curriculum']['period'] = $itemSpecsJson['val'];
  701. }elseif (isset($itemSpecsJson['key']) && $itemSpecsJson['key'] == '群体'){
  702. $data['curriculum']['group'] = $itemSpecsJson['val'];
  703. }elseif (isset($itemSpecsJson['key']) && $itemSpecsJson['key'] == '课程'){
  704. $data['curriculum']['course'] = $itemSpecsJson['val'];
  705. }
  706. }
  707. }
  708. return json_success('', $data);
  709. } catch (\Exception $e) {
  710. dump($e->getMessage());
  711. return json_fail("查询错误~");
  712. }
  713. }
  714. public static function infoPackage($goodsId)
  715. {
  716. try {
  717. // 商品主表
  718. $main = Goods::where('goods_id', $goodsId)->first();
  719. if (!empty($main)) {
  720. $main = $main->toArray();
  721. $main['goods_sku_json'] = json_decode($main['goods_sku_json'], true);
  722. $main['specList'] = [];
  723. foreach ($main['goods_sku_json'] as $key => $sku) {
  724. $main['specList'][] = [
  725. 'label' => $key,
  726. 'tags' => $sku
  727. ];
  728. }
  729. } else {
  730. $main = [];
  731. }
  732. // 详情表
  733. $detail = GoodsDetail::where('join_detail_goods_id', $goodsId)->first();
  734. if (!empty($detail)) {
  735. $detail = $detail->toArray();
  736. } else {
  737. $detail = [];
  738. }
  739. // 标签表
  740. $label = GoodsLabel::where('join_label_goods_id', $goodsId)->first();
  741. if (!empty($label)) {
  742. $label = $label->toArray();
  743. } else {
  744. $label = [];
  745. }
  746. // Running表
  747. $running = GoodsRunning::where('join_running_goods_id', $goodsId)->first();
  748. if (!empty($running)) {
  749. $running = $running->toArray();
  750. if (!empty($running['goods_running_off_json']) && is_json($running['goods_running_off_json'])) {
  751. $goodsRunningOffJson = json_decode($running['goods_running_off_json'], true);
  752. $running['goods_off_addtimes'] = isset($goodsRunningOffJson['time']) ? date("Y-m-d H:i", $goodsRunningOffJson['time']) : '';
  753. }
  754. } else {
  755. $running = [];
  756. }
  757. // 组件表
  758. $component = GoodsComponent::where('join_component_master_goods_id', $goodsId)->get()->toArray();
  759. $componentArr['join_component_goods_id'] = [];
  760. $componentArr['goodsContentList'] = [];
  761. if ($component) {
  762. $ids = [];
  763. $contentList = [];
  764. foreach ($component as $item) {
  765. if (empty($item['join_component_goods_id'])) {
  766. continue;
  767. }
  768. $skus = GoodsSku::where('join_sku_goods_id', $item['join_component_goods_id'])
  769. ->select('goods_sku_id', 'join_sku_goods_id', 'goods_sku_specs_json', 'goods_sku_sales_price')
  770. ->get()
  771. ->toArray();
  772. foreach ($skus as $key => $sku2) {
  773. if (!empty($sku2['goods_sku_specs_json'])) {
  774. $skus[$key]['goods_sku_specs_json'] = json_decode($sku2['goods_sku_specs_json'], true);
  775. $skuName = '';
  776. foreach ($skus[$key]['goods_sku_specs_json'] as $specsKey => $skuSpecs) {
  777. if (is_array($skuSpecs)) {
  778. $skuName = $skuName . ' ' . $specsKey . ' ' . implode(' ', $skuSpecs);
  779. } else {
  780. $skuName = $skuName . ' ' . $specsKey . ' ' . $skuSpecs;
  781. }
  782. }
  783. $skus[$key]['sku_name'] = $skuName;
  784. }
  785. }
  786. $ids[] = $item['join_component_goods_id'];
  787. $configJson = json_decode($item['goods_component_json'], true);
  788. $contentList[] = [
  789. 'goods_id' => $item['join_component_goods_id'],
  790. 'goods_name' => $configJson['goods_name'] ?? '',
  791. 'goods_sales_price' => $item['goods_component_price'],
  792. 'nbr' => $configJson['nbr'] ?? 0,
  793. 'sku_id' => $configJson['sku_id'] ?? '',
  794. 'skus' => $skus
  795. ];
  796. }
  797. $componentArr['join_component_goods_id'] = $ids;
  798. $componentArr['goodsContentList'] = $contentList;
  799. }
  800. // 合并数据
  801. $data = array_merge($main, $detail, $label, $running, $componentArr);
  802. $data['goods_sku_json_label'] = [];
  803. $data['goods_label'] = !empty($data['goods_label']) ? explode(',', $data['goods_label']) : [];
  804. $data['goods_cover'] = getenv('STORAGE_DOMAIN') . $data['goods_cover'];
  805. if (!empty($data['goods_detail_slider_json'])) {
  806. $data['goods_detail_slider_json'] = json_decode($data['goods_detail_slider_json'], true);
  807. $slider = '';
  808. foreach ($data['goods_detail_slider_json'] as $item) {
  809. $slider .= getenv('STORAGE_DOMAIN') . $item . ',';
  810. }
  811. $data['goods_detail_slider_json'] = rtrim($slider, ',');
  812. }
  813. $extendJson = [];
  814. if (!empty($data['goods_attribute_json'])) {
  815. $extendJson = json_decode($data['goods_attribute_json'], true);
  816. $data['goods_attribute_json'] = $extendJson;
  817. if (isset($extendJson['premisses'])) {
  818. $data['goods_premisses'] = $extendJson['premisses'];
  819. $data['goods_premisses_str'] = SysDept::whereIn('dept_id', $extendJson['premisses'])->pluck('dept_name');
  820. }
  821. }
  822. $data['goods_on_addtimes'] = date('Y-m-d\TH:i:s.u\Z', $data['goods_on_addtimes'] - 60 * 60 * 8);
  823. return json_success('', $data);
  824. } catch (\Exception $e) {
  825. dump($e->getTrace());
  826. return json_fail("查询错误~");
  827. }
  828. }
  829. /**
  830. * @Desc 添加商品
  831. * @Author Gorden
  832. * @Date 2024/3/11 10:20
  833. *
  834. * @param $params
  835. * @return Response
  836. */
  837. public static function insert($params): Response
  838. {
  839. Db::beginTransaction();
  840. try {
  841. $params['goods_id'] = "GD" . sprintf('%016d', SysSerial::getSerial()) . random_string(6, 'up');
  842. // 主表
  843. self::mainInsert($params);
  844. // 商品详情表
  845. self::detailInsert($params);
  846. // 商品标签表
  847. self::labelInsert($params);
  848. // 产品运行控制信息表
  849. self::goodsRunningInsert($params);
  850. // sku表
  851. self::goodsSkuSet($params, 'insert');
  852. // 待上架状态,上架时间大于当前时间
  853. if ($params['goods_status'] == 'PENDING' && strtotime($params['goods_on_addtimes']) > time()) {
  854. $redis = Redis::connection();
  855. $key = date('YmdHi', strtotime($params['goods_on_addtimes']));
  856. $redis->sAdd(Goods::LISTING_KEY_PREFIX . $key, $params['goods_id']);
  857. }
  858. // 自动下架
  859. if (!empty($params['goods_running_off_type']) && $params['goods_running_off_type'] == 'T' && !empty($params['goods_off_addtimes'])) {
  860. $redis = Redis::connection();
  861. $key = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi', strtotime($params['goods_off_addtimes']));
  862. $redis->sAdd($key, $params['goods_id']);
  863. }
  864. Db::commit();
  865. } catch (\PDOException $e) {
  866. Db::rollBack();
  867. dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
  868. return json_fail('数据写入失败~');
  869. } catch (BusinessException $e) {
  870. Db::rollBack();
  871. dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
  872. return json_fail($e->getMessage());
  873. } catch (\Exception $e) {
  874. Db::rollBack();
  875. dump($e->getTrace());
  876. return json_fail('数据写入失败~');
  877. }
  878. _syslog("添加商品", "商品名【" . $params['goods_name'] . "】");
  879. return json_success('success');
  880. }
  881. public static function insertRecharge($params)
  882. {
  883. try {
  884. Db::beginTransaction();
  885. $goods = new Goods();
  886. $goods->goods_id = "GD" . date('YmdHis') . random_string(6, 'up');
  887. $goods->join_goods_category_id = 59;
  888. $goods->goods_classify = 'RECHARGE';
  889. $goods->goods_status = $params['goods_status'];
  890. $goods->goods_sort = $params['goods_sort'];
  891. $goods->goods_category = $params['goods_category'];
  892. $goods->goods_name = $params['goods_name'];
  893. $goods->goods_market_price = $params['goods_sales_price'];
  894. $goods->goods_sales_price = $params['goods_sales_price'];
  895. $goods->goods_sku_json = json_encode(['规格' => [$params['goods_sales_price'] . '元']]);
  896. $goods->goods_attribute_json = json_encode(['added' => ['nbr' => $params['goods_rate'] / 100, 'mode' => 'rate'], 'min-count' => 1]);
  897. $goods->goods_cover = '/images/app/common/null-service.png';
  898. $goods->goods_process_json = json_encode(['mode' => 'do_shopping']);
  899. $goods->goods_addtimes = time();
  900. $goods->save();
  901. $sku = new GoodsSku();
  902. $sku->join_sku_goods_id = $goods->goods_id;
  903. $sku->goods_sku_status = $params['goods_status'];
  904. $sku->goods_sku_specs_json = json_encode(['规格' => $params['goods_sales_price'] . '元']);
  905. $sku->goods_sku_market_price = $params['goods_sales_price'];
  906. $sku->goods_sku_sales_price = $params['goods_sales_price'];
  907. $sku->goods_sku_storage_json = json_encode(['storage' => 9999]);
  908. $sku->save();
  909. Db::commit();
  910. return json_success('success');
  911. } catch (\Exception $e) {
  912. Db::rollBack();
  913. return json_fail('添加充值产品失败');
  914. }
  915. }
  916. public static function updateRecharge($params)
  917. {
  918. try {
  919. Db::beginTransaction();
  920. $goods = Goods::where('goods_id', $params['goods_id'])->first();
  921. if (!$goods) {
  922. return json_fail("数据异常");
  923. }
  924. $goods->goods_market_price = $params['goods_sales_price'];
  925. $goods->goods_sales_price = $params['goods_sales_price'];
  926. $goods->goods_status = $params['goods_status'];
  927. $goods->goods_sku_json = json_encode(['规格' => [$params['goods_sales_price'] . '元']]);
  928. $goods->goods_sort = $params['goods_sort'];
  929. if (!empty($goods->goods_attribute_json)) {
  930. $attributeJson = json_decode($goods->goods_attribute_json, true);
  931. $attributeJson['added']['nbr'] = $params['goods_rate'] / 100;
  932. $goods->goods_attribute_json = json_encode($attributeJson);
  933. }
  934. $goods->save();
  935. $sku = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->first();
  936. $sku->goods_sku_status = $params['goods_status'];
  937. $sku->goods_sku_specs_json = json_encode(['规格' => $params['goods_sales_price'] . '元']);
  938. $sku->goods_sku_market_price = $params['goods_sales_price'];
  939. $sku->goods_sku_sales_price = $params['goods_sales_price'];
  940. $sku->save();
  941. Db::commit();
  942. return json_success("success");
  943. } catch (\Exception $e) {
  944. Db::rollBack();
  945. dump($e->getMessage());
  946. return json_fail('编辑充值产品失败');
  947. }
  948. }
  949. public static function insertPackage($params): Response
  950. {
  951. Db::beginTransaction();
  952. try {
  953. $params['goods_id'] = "GD" . sprintf('%016d', SysSerial::getSerial()) . random_string(6, 'up');
  954. // 主表
  955. self::mainInsert($params);
  956. // 商品详情表
  957. self::detailInsert($params);
  958. // 套包组件表
  959. self::componentUpdate($params, 'insert');
  960. // 商品标签表
  961. self::labelInsert($params);
  962. // 产品运行控制信息表
  963. self::goodsRunningInsert($params);
  964. // sku表
  965. // self::goodsSkuSet($params, 'insert');
  966. // 待上架状态,上架时间大于当前时间
  967. if ($params['goods_status'] == 'PENDING' && strtotime($params['goods_on_addtimes']) > time()) {
  968. $redis = Redis::connection();
  969. $key = date('YmdHi', strtotime($params['goods_on_addtimes']));
  970. $redis->sAdd(Goods::LISTING_KEY_PREFIX . $key, $params['goods_id']);
  971. }
  972. Db::commit();
  973. } catch (\PDOException $e) {
  974. Db::rollBack();
  975. dump($e->getMessage());
  976. return json_fail('数据写入失败~');
  977. } catch (BusinessException $e) {
  978. Db::rollBack();
  979. dump($e->getMessage());
  980. return json_fail($e->getMessage());
  981. } catch (\Exception $e) {
  982. Db::rollBack();
  983. dump($e->getTrace());
  984. return json_fail('数据写入失败~');
  985. }
  986. _syslog("添加套餐", "商品名【" . $params['goods_name'] . "】");
  987. return json_success('success');
  988. }
  989. public static function update($params)
  990. {
  991. Db::beginTransaction();
  992. try {
  993. // 主表
  994. self::mainUpdate($params);
  995. // 商品详情表
  996. self::detailUpdate($params);
  997. // 商品标签表
  998. self::labelUpdate($params);
  999. // 产品运行控制信息表
  1000. self::goodsRunningUpdate($params);
  1001. // sku表
  1002. self::goodsSkuSet($params, 'update');
  1003. Db::commit();
  1004. } catch (BusinessException $e) {
  1005. Db::rollBack();
  1006. return json_fail($e->getMessage());
  1007. } catch (\Exception $e) {
  1008. Db::rollBack();
  1009. return json_fail('数据更新失败~');
  1010. }
  1011. _syslog("编辑商品", "商品名【" . $params['goods_name'] . "】" ?? "商品ID:【" . $params['goods_id'] . "】");
  1012. return json_success('success');
  1013. }
  1014. public static function changeStatus($params)
  1015. {
  1016. try {
  1017. Goods::where('goods_id', $params['goods_id'])->update(['goods_status' => $params['goods_status']]);
  1018. return json_success('修改成功');
  1019. } catch (\Exception $e) {
  1020. return json_fail('修改状态失败');
  1021. }
  1022. }
  1023. public static function updatePackage($params)
  1024. {
  1025. Db::beginTransaction();
  1026. try {
  1027. // 主表
  1028. self::mainUpdate($params);
  1029. // 商品详情表
  1030. self::detailUpdate($params);
  1031. // 套包组件表
  1032. self::componentUpdate($params, 'update');
  1033. // 商品标签表
  1034. self::labelUpdate($params);
  1035. // 产品运行控制信息表
  1036. self::goodsRunningUpdate($params);
  1037. Db::commit();
  1038. } catch (BusinessException $e) {
  1039. Db::rollBack();
  1040. return json_fail($e->getMessage());
  1041. } catch (\Exception $e) {
  1042. Db::rollBack();
  1043. dump($e->getTrace());
  1044. return json_fail('数据更新失败~');
  1045. }
  1046. _syslog("编辑套餐", "商品名【" . $params['goods_name'] . "】" ?? "商品ID:【" . $params['goods_id'] . "】");
  1047. return json_success('success');
  1048. }
  1049. /**
  1050. * @Desc 删除商品
  1051. * @Author Gorden
  1052. * @Date 2024/3/28 13:20
  1053. *
  1054. * @param $ids
  1055. * @return Response
  1056. */
  1057. public static function delete($ids)
  1058. {
  1059. if (!$ids) {
  1060. return json_fail("数据错误~");
  1061. }
  1062. if (!is_array($ids)) {
  1063. $ids = [$ids];
  1064. }
  1065. $goods = Goods::whereIn('goods_id', $ids)->get()->toArray();
  1066. if (!$goods) {
  1067. return json_fail("数据错误~");
  1068. }
  1069. // 是否在套包里
  1070. if (GoodsComponent::whereIn('join_component_goods_id',$ids)->exists()){
  1071. return json_fail("当前商品存在于套包中,请先在套包中删除");
  1072. }
  1073. // 是否已被购买过
  1074. if (OrderSheet::whereIn('join_sheet_goods_id',$ids)->exists()){
  1075. return json_fail("当前商品已有购买历史,如不在APP显示,请选择【下架】操作");
  1076. }
  1077. // 是否预约过
  1078. if (Appointment::whereIn('join_appointment_goods_id',$ids)->exists()){
  1079. return json_fail("当前商品已有预约历史,如不在APP显示,请选择【下架】操作");
  1080. }
  1081. Db::beginTransaction();
  1082. try {
  1083. Goods::whereIn('goods_id', $ids)->delete();
  1084. GoodsDetail::whereIn('join_detail_goods_id', $ids)->delete();
  1085. GoodsLabel::whereIn('join_label_goods_id', $ids)->delete();
  1086. GoodsRunning::whereIn('join_running_goods_id', $ids)->delete();
  1087. GoodsSku::whereIn('join_sku_goods_id', $ids)->delete();
  1088. Db::commit();
  1089. _syslog("删除商品 / 套餐", "ID:【" . implode(',', $ids) . "】", $goods);
  1090. return json_success("商品删除成功");
  1091. } catch (\Exception $e) {
  1092. Db::rollBack();
  1093. return json_fail("商品删除失败~");
  1094. }
  1095. }
  1096. /**
  1097. * @Desc 商品主表
  1098. * @Author Gorden
  1099. * @Date 2024/3/11 11:20
  1100. *
  1101. * @param $params
  1102. * @return mixed|string
  1103. * @throws BusinessException
  1104. */
  1105. public static function mainInsert($params)
  1106. {
  1107. if (!empty($params['goods_cover'])) {
  1108. $params['goods_cover'] = str_replace(getenv('STORAGE_DOMAIN'), '', $params['goods_cover']);
  1109. }
  1110. // 如果产品是待处理状态
  1111. if ($params['goods_status'] == 'PENDING') {
  1112. if (strtotime($params['goods_on_addtimes']) <= time()) {
  1113. $params['goods_status'] = 'ON';
  1114. }
  1115. }
  1116. $category = SysCategory::where('category_id', $params['join_goods_category_id'])->first();
  1117. if (!$category) {
  1118. throw new BusinessException("产品分类不存在~");
  1119. }
  1120. if (empty($params['goods_category'])) {
  1121. $params['goods_category'] = $category->category_classify;
  1122. }
  1123. try {
  1124. $model = new Goods();
  1125. $model->goods_id = $params['goods_id'];
  1126. $model->join_goods_category_id = $params['join_goods_category_id'] ?? 0;
  1127. $model->join_goods_supplier_id = $params['join_goods_supplier_id'] ?? 0;
  1128. $model->goods_classify = $params['goods_classify'] ?? '';
  1129. $model->goods_status = $params['goods_status'] ?? '';
  1130. $model->goods_category = $params['goods_category'] ?? '';
  1131. // $model->goods_prefix = $params['goods_prefix'] ?? 】($category->category_name ? '【' . $category->category_name . '' : '');
  1132. $model->goods_prefix = $params['goods_prefix'] ?? '';
  1133. $model->goods_name = $params['goods_name'];
  1134. $model->goods_market_price = $params['goods_market_price'] ?? 0;
  1135. $model->goods_sales_price = $params['goods_sales_price'] ?? 0;
  1136. $model->goods_sku_json = !empty($params['goods_sku_json_label']) ? json_encode($params['goods_sku_json_label']) : json_encode(['规格' => ['标准']]);
  1137. $model->goods_attribute_json = !empty($params['goods_attribute_json']) ? $params['goods_attribute_json'] : '[]';
  1138. $model->goods_title = $params['goods_title'] ?? '';
  1139. $model->goods_cover = $params['goods_cover'] ?? '';
  1140. $model->goods_on_addtimes = isset($params['goods_on_addtimes']) ? strtotime($params['goods_on_addtimes']) : null;
  1141. $model->goods_sort = $params['goods_sort'] ?? null;
  1142. $model->goods_groupby = $params['goods_groupby'] ?? '';
  1143. $model->goods_remark = $params['goods_remark'] ?? '';
  1144. $model->goods_extend_json = $params['goods_extend_json'] ?? '{}';
  1145. $model->is_support_appointment = $params['is_support_appointment'] ?? 'N';
  1146. $model->creator_user_id = JwtToken::getCurrentId();
  1147. $model->goods_addtimes = time();
  1148. $model->goods_updatetimes = time();
  1149. // {"express":"Y","self":"Y","arrival":"Y"}
  1150. $expressJson = [];
  1151. if (!empty($params['express_json'])){
  1152. if(in_array('express',$params['express_json'])){
  1153. $expressJson['express'] = 'Y';
  1154. }else{
  1155. $expressJson['express'] = 'N';
  1156. }
  1157. if (in_array('self',$params['express_json'])){
  1158. $expressJson['self'] = 'Y';
  1159. }else{
  1160. $expressJson['self'] = 'N';
  1161. }
  1162. if (in_array('arrival',$params['express_json'])){
  1163. $expressJson['arrival'] = 'Y';
  1164. }else{
  1165. $expressJson['arrival'] = 'N';
  1166. }
  1167. $model->goods_express_json = json_encode($expressJson);
  1168. }
  1169. if (!empty($params['is_support_appointment']) && $params['is_support_appointment'] == 'Y' && !empty($params['appointment_times']) && $params['goods_category'] != 'TRAVEL') {
  1170. $times = [];
  1171. $attributeJsonTimeArr = [];
  1172. $personTotal = 0;
  1173. foreach ($params['appointment_times'] as $time) {
  1174. $attributeJsonTimeArr[] = strtotime(date('Y-m-d ') . $time['appointmentTimeStart']);
  1175. $attributeJsonTimeArr[] = strtotime(date('Y-m-d ') . $time['appointmentTimeEnd']);
  1176. $personTotal += $time['person'];
  1177. $times[$time['appointmentTimeStart']] = [
  1178. 'person' => $time['person'],
  1179. 'duration' => $time['appointmentTimeStart'] . '-' . $time['appointmentTimeEnd']
  1180. ];
  1181. }
  1182. $attributeJsonTime = date('H:i', min($attributeJsonTimeArr)) . '至' . date('H:i', max($attributeJsonTimeArr));
  1183. $newDates = [];
  1184. foreach ($params['dates'] as $date) {
  1185. $key = self::$week[$date];
  1186. $newDates[$key] = $date;
  1187. }
  1188. ksort($newDates);
  1189. $currentDate = current($newDates);
  1190. $lastDate = end($newDates);
  1191. $attributeJsonDate = '';
  1192. if (self::$week[$lastDate] - self::$week[$currentDate] + 1 > count($newDates)) {
  1193. $attributeJsonDate = implode(',', $newDates);
  1194. } else if (self::$week[$lastDate] - self::$week[$currentDate] + 1 == count($newDates)) {
  1195. $attributeJsonDate = $currentDate . '至' . $lastDate;
  1196. }
  1197. // if (isset($params['work_time'])){
  1198. // $workTimeStart = date('H:i',strtotime($params['work_time'][0]));
  1199. // $workTimeEnd = date('H:i',strtotime($params['work_time'][1]));
  1200. // $attributeJsonTime = $workTimeStart.'至'.$workTimeEnd;
  1201. // }
  1202. $attributeJson = [
  1203. 'icon' => $model->goods_cover,
  1204. 'date' => $attributeJsonDate,
  1205. // 'time' => $attributeJsonTime,
  1206. 'dates' => $newDates ? array_values($newDates) : [],
  1207. 'times' => $times,
  1208. 'person' => $personTotal
  1209. ];
  1210. // if(isset($params['address'])){
  1211. // $attributeJson['address'] = $params['address'];
  1212. // }
  1213. // if (isset($params['min_count'])){
  1214. // $attributeJson['min-count'] = $params['min_count'];
  1215. // }else{
  1216. // $attributeJson['min-count'] = 1;
  1217. // }
  1218. // if (isset($params['teachers'])){
  1219. // $attributeJson['teachers'] = $params['teachers'];
  1220. // }
  1221. if (!empty($params['appointment_label'])) {
  1222. $attributeJson['label'] = $params['appointment_label'];
  1223. }
  1224. // if (!empty($params['address'])) {
  1225. // $attributeJson['address'] = $params['address'];
  1226. // }
  1227. // if (!empty($params['position'])){
  1228. // $attributeJson['position'] = $params['position'];
  1229. // }
  1230. // if (isset($params['goods_service_premises'])){
  1231. // $attributeJson['service_premises_id'] = $params['goods_service_premises'];
  1232. // }
  1233. $model->goods_attribute_json = json_encode($attributeJson, JSON_UNESCAPED_UNICODE);
  1234. // $times = [];
  1235. // foreach ($params['appointment_times'] as $time) {
  1236. // $times[$time['appointmentTimeStart']] = [
  1237. // 'person' => $time['person'],
  1238. // 'duration' => $time['appointmentTimeStart'] . '-' . $time['appointmentTimeEnd']
  1239. // ];
  1240. // }
  1241. // $model->goods_attribute_json = json_encode([
  1242. // 'icon' => '',
  1243. // 'dates' => $params['dates'] ?? [],
  1244. // 'times' => $times
  1245. // ]);
  1246. }
  1247. if (!empty($params['is_support_appointment']) && $params['is_support_appointment'] == 'Y' && !empty($params['appointment_times']) && $params['goods_category'] == 'TRAVEL') {
  1248. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  1249. $attributeJson = json_decode($model->goods_attribute_json, true);
  1250. } elseif (empty($model->goods_attribute_json)) {
  1251. $attributeJson = [];
  1252. }
  1253. $attributeJson['travel-day'] = $params['travel_day'];
  1254. $attributeJson['travel-begin'] = $params['travel_begin'];
  1255. $attributeJson['travel-night'] = $params['travel_night'];
  1256. $attributeJson['travel-trans'] = $params['travel_trans'];
  1257. $unixs = [];
  1258. foreach ($params['appointment_times'] as $times) {
  1259. $unix = strtotime($times['days']);
  1260. $unixs[$unix] = $times['person'];
  1261. }
  1262. ksort($unixs);
  1263. $months = [];
  1264. foreach ($unixs as $key => $unix) {
  1265. $month = date('Ym', $key);
  1266. $day = date('d', $key);
  1267. $months[$month][$day] = $unix;
  1268. }
  1269. $attributeJson['month'] = $months;
  1270. $model->goods_attribute_json = json_encode($attributeJson, JSON_UNESCAPED_UNICODE);
  1271. }
  1272. if (!empty($params['goods_json']) && $params['join_goods_category_id'] == 65) {
  1273. $goodsJson = json_decode($params['goods_json'], true);
  1274. // foreach ($goodsJson as $key => $item) {
  1275. // $goodsJson[$key]['color'] = hexToRgb($item['color']);
  1276. // }
  1277. //
  1278. $model->goods_json = json_encode($goodsJson);
  1279. } elseif (!empty($params['goods_json']) && $params['join_goods_category_id'] == 43) {
  1280. $goodsJson = json_decode($params['goods_json'], true);
  1281. $newGoodsJson = [];
  1282. foreach ($goodsJson as $item1) {
  1283. if (empty($item1['title'])) {
  1284. continue;
  1285. }
  1286. $newItem1 = [];
  1287. foreach ($item1['items'] as $item2) {
  1288. $newParams = [];
  1289. foreach ($item2['params'] as $param) {
  1290. if (!empty($param[0]) || !empty($param[1])) {
  1291. $newParams[] = $param;
  1292. }
  1293. }
  1294. $newItem1['items'][$item2['key']] = $newParams;
  1295. }
  1296. $newItem1['service'] = $item1['service'] ?? '';
  1297. $newGoodsJson[$item1['title']] = $newItem1;
  1298. }
  1299. $model->goods_json = json_encode($newGoodsJson);
  1300. } else {
  1301. $model->goods_json = '[]';
  1302. }
  1303. if (!empty($params['goods_premisses'])) {
  1304. $attributeJson = [];
  1305. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  1306. $attributeJson = json_decode($model->goods_attribute_json, true);
  1307. } elseif (empty($model->goods_attribute_json)) {
  1308. $attributeJson = [];
  1309. }
  1310. $attributeJson['premisses'] = $params['goods_premisses'];
  1311. $model->goods_attribute_json = json_encode($attributeJson);
  1312. }
  1313. if (!empty($params['goods_theme_color']) && !empty($params['goods_theme_icon'])) {
  1314. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  1315. $attributeJson = json_decode($model->goods_attribute_json, true);
  1316. } elseif (empty($model->goods_attribute_json)) {
  1317. $attributeJson = [];
  1318. }
  1319. $attributeJson['bg'] = $params['goods_theme_color'];
  1320. $attributeJson['icon'] = str_replace(getenv('STORAGE_DOMAIN'), '', $params['goods_theme_icon']);
  1321. $model->goods_attribute_json = json_encode($attributeJson);
  1322. }
  1323. if (!empty($params['address'])) {
  1324. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  1325. $attributeJson = json_decode($model->goods_attribute_json, true);
  1326. } elseif (empty($model->goods_attribute_json)) {
  1327. $attributeJson = [];
  1328. }
  1329. $attributeJson['address'] = $params['address'];
  1330. $model->goods_attribute_json = json_encode($attributeJson);
  1331. }
  1332. if (!empty($params['position'])){
  1333. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  1334. $attributeJson = json_decode($model->goods_attribute_json, true);
  1335. } elseif (empty($model->goods_attribute_json)) {
  1336. $attributeJson = [];
  1337. }
  1338. $attributeJson['position'] = $params['position'];
  1339. $model->goods_attribute_json = json_encode($attributeJson);
  1340. }
  1341. if (isset($params['goods_service_premises'])){
  1342. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  1343. $attributeJson = json_decode($model->goods_attribute_json, true);
  1344. } elseif (empty($model->goods_attribute_json)) {
  1345. $attributeJson = [];
  1346. }
  1347. $attributeJson['service_premises_id'] = $params['goods_service_premises'];
  1348. $model->goods_attribute_json = json_encode($attributeJson);
  1349. }
  1350. if (isset($params['min_count'])){
  1351. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  1352. $attributeJson = json_decode($model->goods_attribute_json, true);
  1353. } elseif (empty($model->goods_attribute_json)) {
  1354. $attributeJson = [];
  1355. }
  1356. $attributeJson['min-count'] = $params['min_count'];
  1357. $model->goods_attribute_json = json_encode($attributeJson);
  1358. }
  1359. if (isset($params['teachers'])){
  1360. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  1361. $attributeJson = json_decode($model->goods_attribute_json, true);
  1362. } elseif (empty($model->goods_attribute_json)) {
  1363. $attributeJson = [];
  1364. }
  1365. $attributeJson['teachers'] = $params['teachers'];
  1366. $model->goods_attribute_json = json_encode($attributeJson);
  1367. }
  1368. if (isset($params['work_time'])){
  1369. if (!empty($model->goods_attribute_json) && !is_array($model->goods_attribute_json)) {
  1370. $attributeJson = json_decode($model->goods_attribute_json, true);
  1371. } elseif (empty($model->goods_attribute_json)) {
  1372. $attributeJson = [];
  1373. }
  1374. $workTimeStart = date('H:i',strtotime($params['work_time'][0]));
  1375. $workTimeEnd = date('H:i',strtotime($params['work_time'][1]));
  1376. $attributeJsonTime = $workTimeStart.'至'.$workTimeEnd;
  1377. $attributeJson['time'] = $attributeJsonTime;
  1378. $model->goods_attribute_json = json_encode($attributeJson);
  1379. }
  1380. if ($model->save()) {
  1381. return $model->goods_id;
  1382. }
  1383. // 异常
  1384. throw new BusinessException("数据写入失败~");
  1385. } catch (\Exception $e) {
  1386. dump($e->getMessage());
  1387. throw new BusinessException("数据写入失败~");
  1388. }
  1389. }
  1390. /**
  1391. * @Desc 详情表
  1392. * @Author Gorden
  1393. * @Date 2024/3/11 11:19
  1394. *
  1395. * @param $params
  1396. * @return void
  1397. * @throws BusinessException
  1398. */
  1399. public static function detailInsert($params)
  1400. {
  1401. if (!empty($params['goods_detail_slider_json'])) {
  1402. $params['goods_detail_slider_json'] = str_replace(getenv('STORAGE_DOMAIN'), '', $params['goods_detail_slider_json']);
  1403. $params['goods_detail_slider_json'] = json_encode(['slider' => $params['goods_detail_slider_json']]);
  1404. }
  1405. if(isset($params['curriculum'])){
  1406. $params['goods_detail_specs_json'] = json_encode([
  1407. [
  1408. 'key'=>'课时',
  1409. 'val'=>$params['curriculum']['period'] ?? '',
  1410. ],
  1411. [
  1412. 'key'=>'群体',
  1413. 'val'=>$params['curriculum']['group'] ?? '',
  1414. ],
  1415. [
  1416. 'key'=>'课程',
  1417. 'val'=>$params['curriculum']['course'] ?? '',
  1418. ],
  1419. ]);
  1420. }
  1421. try {
  1422. $model = new GoodsDetail();
  1423. $model->join_detail_goods_id = $params['goods_id'];
  1424. $model->goods_detail_code_json = $params['goods_detail_code_json'] ?? '{}';
  1425. $model->goods_detail_slider_json = $params['goods_detail_slider_json'] ?? '{}';
  1426. $model->goods_detail_specs_json = $params['goods_detail_specs_json'] ?? '{}';
  1427. $model->goods_detail_content = $params['goods_detail_content'] ?? '';
  1428. if (!$model->save()) {
  1429. // 异常
  1430. throw new BusinessException("轮播图/详情数据写入失败~");
  1431. }
  1432. } catch (\Exception $e) {
  1433. dump($e->getMessage());
  1434. throw new BusinessException("轮播图/详情数据写入失败~");
  1435. }
  1436. }
  1437. public static function componentUpdate($params, $type = 'insert')
  1438. {
  1439. Db::beginTransaction();
  1440. try {
  1441. $goodsSku = '';
  1442. // 有先删除
  1443. if ($type == 'update') {
  1444. GoodsComponent::where('join_component_master_goods_id', $params['goods_id'])->delete();
  1445. $goodsSku = GoodsSku::where('join_sku_goods_id',$params['goods_id'])->first();
  1446. if ($goodsSku){
  1447. $goodsSku->goods_sku_market_price = $params['goods_market_price'] ?? 0;
  1448. $goodsSku->goods_sku_sales_price = $params['goods_sales_price'] ?? 0;
  1449. $goodsSku->save();
  1450. }
  1451. }
  1452. if ($type == 'insert' || !$goodsSku){
  1453. Goods::where('goods_id', $params['goods_id'])->update(['goods_sku_json' => '{"规格": ["标准"]}']);
  1454. $skuData = [
  1455. 'join_sku_goods_id' => $params['goods_id'],
  1456. 'goods_sku_status' => 'ON',
  1457. 'goods_sku_specs_json' => '{"规格": "标准"}',
  1458. 'goods_sku_title' => "标准" . $params['goods_name'],
  1459. 'goods_sku_market_price' => $params['goods_market_price'] ?? 0,
  1460. 'goods_sku_sales_price' => $params['goods_sales_price'] ?? 0,
  1461. ];
  1462. GoodsSku::insert($skuData);
  1463. }
  1464. $data = [];
  1465. foreach ($params['goods_content_list'] as $item) {
  1466. if(!in_array($item['goods_id'],$params['join_component_goods_id'])){
  1467. continue;
  1468. }
  1469. $goods = Goods::where('goods_id', $params['goods_id'])->first();
  1470. if (!$goods) {
  1471. continue;
  1472. }
  1473. $data[] = [
  1474. 'join_component_master_goods_id' => $params['goods_id'],
  1475. 'join_component_goods_id' => $item['goods_id'] ?? '',
  1476. 'join_component_goods_sku_id' => $item['sku_id'] ?? '',
  1477. 'goods_component_price' => $item['goods_sales_price'] ?? '',
  1478. 'goods_component_cover' => $goods->goods_cover,
  1479. 'goods_component_json' => json_encode(['goods_name' => $item['goods_name'], 'nbr' => $item['nbr'], 'sku_id' => $item['sku_id'] ?? '']),
  1480. 'goods_component_addtimes' => time()
  1481. ];
  1482. }
  1483. if ($data) {
  1484. GoodsComponent::insert($data);
  1485. }
  1486. Db::commit();
  1487. } catch (\Exception $e) {
  1488. Db::rollBack();
  1489. dump($e->getMessage());
  1490. throw new BusinessException("数据写入失败~");
  1491. }
  1492. }
  1493. /**
  1494. * @Desc 标签表
  1495. * @Author Gorden
  1496. * @Date 2024/3/11 11:32
  1497. *
  1498. * @param $params
  1499. * @return void
  1500. * @throws BusinessException
  1501. */
  1502. public static function labelInsert($params)
  1503. {
  1504. $model = new GoodsLabel();
  1505. $model->join_label_goods_id = $params['goods_id'];
  1506. $model->goods_label = $params['goods_label'] ? implode(',', $params['goods_label']) : '';
  1507. $model->goods_label_extend_json = !empty($params['goods_label_extend_json']) ? $params['goods_label_extend_json'] : '{}';
  1508. if (!$model->save()) {
  1509. // 异常
  1510. throw new BusinessException('数据写入失败~');
  1511. }
  1512. }
  1513. /**
  1514. * @Desc 产品运行控制信息表
  1515. * @Author Gorden
  1516. * @Date 2024/3/11 11:38
  1517. *
  1518. * @param $params
  1519. * @return void
  1520. * @throws BusinessException
  1521. */
  1522. public static function goodsRunningInsert($params)
  1523. {
  1524. try {
  1525. $model = new GoodsRunning();
  1526. $model->join_running_goods_id = $params['goods_id'];
  1527. $model->goods_running_storage = $params['goods_running_storage'] ?? 0;
  1528. $model->goods_running_sale = $params['goods_running_sale'] ?? 0;
  1529. $model->goods_running_off_type = !empty($params['goods_running_off_type']) ? $params['goods_running_off_type'] : '';
  1530. $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'])]) : '[]';
  1531. if (!$model->save()) {
  1532. throw new BusinessException('数据写入失败');
  1533. }
  1534. } catch (\Exception $e) {
  1535. dump($e->getMessage());
  1536. throw new BusinessException('数据写入失败');
  1537. }
  1538. }
  1539. /**
  1540. * @Desc SKU
  1541. * @Author Gorden
  1542. * @Date 2024/3/11 12:01
  1543. *
  1544. * @param $params
  1545. * @return void
  1546. * @throws BusinessException
  1547. */
  1548. public static function skuInsert($params)
  1549. {
  1550. $model = new GoodsSku();
  1551. $model->join_sku_goods_id = $params['goods_id'];
  1552. $model->goods_sku_status = $params['goods_sku_status'];
  1553. $model->goods_sku_specs_json = $params['goods_sku_specs_json'];
  1554. $model->goods_sku_title = $params['goods_sku_title'];
  1555. $model->goods_sku_images_json = $params['goods_sku_images_json'];
  1556. $model->goods_sku_content = $params['goods_sku_content'];
  1557. $model->goods_sku_market_price = $params['goods_sku_market_price'];
  1558. $model->goods_sku_sales_price = $params['goods_sku_sales_price'];
  1559. $model->goods_sku_storage_json = $params['goods_sku_storage_json'];
  1560. $model->goods_sku_service_json = $params['goods_sku_service_json'];
  1561. $model->goods_sku_extend_json = $params['goods_sku_extend_json'];
  1562. if (!$model->save()) {
  1563. throw new BusinessException('规格数据写入失败~');
  1564. }
  1565. }
  1566. /**
  1567. * @Desc
  1568. * @Author Gorden
  1569. * @Date 2024/3/12 8:44
  1570. *
  1571. * @param $params
  1572. * @return void
  1573. * @throws BusinessException
  1574. */
  1575. public static function mainUpdate($params)
  1576. {
  1577. try {
  1578. $data = self::inputFilter($params, new Goods());
  1579. if (!empty($data['goods_cover'])) {
  1580. $data['goods_cover'] = str_replace(getenv('STORAGE_DOMAIN'), '', $data['goods_cover']);
  1581. }
  1582. $data['goods_on_addtimes'] = isset($data['goods_on_addtimes']) ? strtotime($data['goods_on_addtimes']) : 0;
  1583. $data['goods_sku_json'] = !empty($params['goods_sku_json_label']) ? json_encode($params['goods_sku_json_label']) : json_encode(['规格' => ['标准']]);
  1584. $row = Goods::find($data['goods_id']);
  1585. if ($row->join_goods_category_id != $data['join_goods_category_id']) {
  1586. $category = SysCategory::where('category_id', $params['join_goods_category_id'])->first();
  1587. if (!$category) {
  1588. throw new BusinessException("产品分类不存在~");
  1589. }
  1590. $data['goods_category'] = $category->category_classify ?? '';
  1591. $data['goods_prefix'] = $data['goods_prefix'] ?? ($category->category_name ? '【' . $category->category_name . '】' : '');
  1592. }
  1593. // 上架时间有变动
  1594. if ($data['goods_status'] == 'PENDING' && $row->goods_on_addtimes != $data['goods_on_addtimes']) {
  1595. $redis = Redis::connection();
  1596. // 删掉原来的
  1597. $oldKey = Goods::LISTING_KEY_PREFIX . date('YmdHi', $row->goods_on_addtimes);
  1598. $redis->srem($oldKey, $data['goods_id']);
  1599. // 加入新的
  1600. $newKey = Goods::LISTING_KEY_PREFIX . date('YmdHi', $data['goods_on_addtimes']);
  1601. $redis->sadd($newKey, $data['goods_id']);
  1602. }
  1603. $expressJson = [];
  1604. if (!empty($params['express_json'])){
  1605. if(in_array('express',$params['express_json'])){
  1606. $expressJson['express'] = 'Y';
  1607. }else{
  1608. $expressJson['express'] = 'N';
  1609. }
  1610. if (in_array('self',$params['express_json'])){
  1611. $expressJson['self'] = 'Y';
  1612. }else{
  1613. $expressJson['self'] = 'N';
  1614. }
  1615. if (in_array('arrival',$params['express_json'])){
  1616. $expressJson['arrival'] = 'Y';
  1617. }else{
  1618. $expressJson['arrival'] = 'N';
  1619. }
  1620. $data['goods_express_json'] = json_encode($expressJson);
  1621. }
  1622. if (!empty($params['is_support_appointment']) && $params['is_support_appointment'] == 'Y' && !empty($params['appointment_times']) && $params['goods_category'] != 'TRAVEL') {
  1623. $attributeJson = [];
  1624. if (!empty($row->goods_attribute_json)) {
  1625. $attributeJson = json_decode($row->goods_attribute_json, true);
  1626. }
  1627. $times = [];
  1628. $attributeJsonTimeArr = [];
  1629. $personTotal = 0;
  1630. foreach ($params['appointment_times'] as $time) {
  1631. $attributeJsonTimeArr[] = strtotime(date('Y-m-d ') . $time['appointmentTimeStart']);
  1632. $attributeJsonTimeArr[] = strtotime(date('Y-m-d ') . $time['appointmentTimeEnd']);
  1633. $personTotal += $time['person'];
  1634. $times[$time['appointmentTimeStart']] = [
  1635. 'person' => $time['person'],
  1636. 'duration' => $time['appointmentTimeStart'] . '-' . $time['appointmentTimeEnd']
  1637. ];
  1638. }
  1639. $attributeJsonTime = date('H:i', min($attributeJsonTimeArr)) . '至' . date('H:i', max($attributeJsonTimeArr));
  1640. $newDates = [];
  1641. foreach ($params['dates'] as $date) {
  1642. $key = self::$week[$date];
  1643. $newDates[$key] = $date;
  1644. }
  1645. ksort($newDates);
  1646. $currentDate = current($newDates);
  1647. $lastDate = end($newDates);
  1648. $attributeJsonDate = '';
  1649. if (self::$week[$lastDate] - self::$week[$currentDate] + 1 > count($newDates)) {
  1650. $attributeJsonDate = implode(',', $newDates);
  1651. } else if (self::$week[$lastDate] - self::$week[$currentDate] + 1 == count($newDates)) {
  1652. $attributeJsonDate = $currentDate . '至' . $lastDate;
  1653. }
  1654. $attributeJson['date'] = $attributeJsonDate;
  1655. $attributeJson['icon'] = $data['goods_cover'] ?? '';
  1656. $attributeJson['time'] = $attributeJsonTime;
  1657. $attributeJson['dates'] = $newDates ? array_values($newDates) : [];
  1658. $attributeJson['times'] = $times;
  1659. $attributeJson['person'] = $personTotal;
  1660. if (!empty($params['appointment_label'])) {
  1661. $attributeJson['label'] = $params['appointment_label'];
  1662. }
  1663. // if (!empty($params['address'])) {
  1664. // $attributeJson['address'] = $params['address'];
  1665. // }
  1666. // if (!empty($params['position'])){
  1667. // $attributeJson['position'] = $params['position'];
  1668. // }
  1669. // if (isset($params['goods_service_premises'])){
  1670. // $attributeJson['service_premises_id'] = $params['goods_service_premises'];
  1671. // }
  1672. // if (isset($params['work_time'])){
  1673. // $workTimeStart = date('H:i',strtotime($params['work_time'][0]));
  1674. // $workTimeEnd = date('H:i',strtotime($params['work_time'][1]));
  1675. // $attributeJson['time'] = $workTimeStart.'至'.$workTimeEnd;
  1676. // }
  1677. // if (isset($params['min_count'])){
  1678. // $attributeJson['min-count'] = $params['min_count'];
  1679. // }else{
  1680. // $attributeJson['min-count'] = 1;
  1681. // }
  1682. // if (isset($params['teachers'])){
  1683. // $attributeJson['teachers'] = $params['teachers'];
  1684. // }
  1685. $data['goods_attribute_json'] = json_encode($attributeJson, JSON_UNESCAPED_UNICODE);
  1686. }
  1687. if (!empty($params['is_support_appointment']) && $params['is_support_appointment'] == 'Y' && !empty($params['appointment_times']) && $params['goods_category'] == 'TRAVEL') {
  1688. $attributeJson = [];
  1689. if (!empty($row->goods_attribute_json)) {
  1690. $attributeJson = json_decode($row->goods_attribute_json, true);
  1691. }
  1692. $attributeJson['travel-day'] = $params['travel_day'];
  1693. $attributeJson['travel-begin'] = $params['travel_begin'];
  1694. $attributeJson['travel-night'] = $params['travel_night'];
  1695. $attributeJson['travel-trans'] = $params['travel_trans'];
  1696. $unixs = [];
  1697. foreach ($params['appointment_times'] as $times) {
  1698. $unix = strtotime($times['days']);
  1699. $unixs[$unix] = $times['person'];
  1700. }
  1701. ksort($unixs);
  1702. $months = [];
  1703. foreach ($unixs as $key => $unix) {
  1704. $month = date('Ym', $key);
  1705. $day = date('d', $key);
  1706. $months[$month][$day] = $unix;
  1707. }
  1708. $attributeJson['month'] = $months;
  1709. $data['goods_attribute_json'] = json_encode($attributeJson, JSON_UNESCAPED_UNICODE);
  1710. }
  1711. if (!empty($params['goods_premisses'])) {
  1712. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  1713. if (is_json($data['goods_attribute_json'])) {
  1714. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  1715. } else {
  1716. $data['goods_attribute_json'] = [];
  1717. }
  1718. } elseif (empty($data['goods_attribute_json'])) {
  1719. $data['goods_attribute_json'] = [];
  1720. }
  1721. dump($data['goods_attribute_json']);
  1722. $data['goods_attribute_json']['premisses'] = $params['goods_premisses'];
  1723. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  1724. }
  1725. if (!empty($data['goods_json']) && $data['join_goods_category_id'] == 65) {
  1726. // $goodsJson = json_decode($data['goods_json'], true);
  1727. // foreach ($goodsJson as $key => $item) {
  1728. // $goodsJson[$key]['color'] = hexToRgb($item['color']);
  1729. // }
  1730. //
  1731. // $data['goods_json'] = json_encode($goodsJson);
  1732. } elseif (!empty($data['goods_json']) && $data['join_goods_category_id'] == 43) {
  1733. $goodsJson = json_decode($data['goods_json'], true);
  1734. $newGoodsJson = [];
  1735. foreach ($goodsJson as $item1) {
  1736. if (empty($item1['title'])) {
  1737. continue;
  1738. }
  1739. $newItem1 = [];
  1740. foreach ($item1['items'] as $item2) {
  1741. $newParams = [];
  1742. foreach ($item2['params'] as $param) {
  1743. if (!empty($param[0]) || !empty($param[1])) {
  1744. $newParams[] = $param;
  1745. }
  1746. }
  1747. $newItem1['items'][$item2['key']] = $newParams;
  1748. }
  1749. $newItem1['service'] = $item1['service'];
  1750. $newGoodsJson[$item1['title']] = $newItem1;
  1751. }
  1752. $data['goods_json'] = json_encode($newGoodsJson);
  1753. } else {
  1754. $data['goods_json'] = '[]';
  1755. }
  1756. if (!empty($params['goods_theme_color']) && !empty($params['goods_theme_icon'])) {
  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. $data['goods_attribute_json']['bg'] = $params['goods_theme_color'];
  1767. $data['goods_attribute_json']['icon'] = str_replace(getenv('STORAGE_DOMAIN'), '', $params['goods_theme_icon']);
  1768. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  1769. }
  1770. if (!empty($params['address'])) {
  1771. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  1772. if (is_json($data['goods_attribute_json'])) {
  1773. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  1774. } else {
  1775. $data['goods_attribute_json'] = [];
  1776. }
  1777. } elseif (empty($data['goods_attribute_json'])) {
  1778. $data['goods_attribute_json'] = [];
  1779. }
  1780. $data['goods_attribute_json']['address'] = $params['address'];
  1781. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  1782. }
  1783. if (!empty($params['position'])){
  1784. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  1785. if (is_json($data['goods_attribute_json'])) {
  1786. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  1787. } else {
  1788. $data['goods_attribute_json'] = [];
  1789. }
  1790. } elseif (empty($data['goods_attribute_json'])) {
  1791. $data['goods_attribute_json'] = [];
  1792. }
  1793. $data['goods_attribute_json']['position'] = $params['position'];
  1794. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  1795. }
  1796. if (isset($params['goods_service_premises'])){
  1797. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  1798. if (is_json($data['goods_attribute_json'])) {
  1799. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  1800. } else {
  1801. $data['goods_attribute_json'] = [];
  1802. }
  1803. } elseif (empty($data['goods_attribute_json'])) {
  1804. $data['goods_attribute_json'] = [];
  1805. }
  1806. $data['goods_attribute_json']['service_premises_id'] = $params['goods_service_premises'];
  1807. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  1808. }
  1809. if (isset($params['work_time'])){
  1810. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  1811. if (is_json($data['goods_attribute_json'])) {
  1812. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  1813. } else {
  1814. $data['goods_attribute_json'] = [];
  1815. }
  1816. } elseif (empty($data['goods_attribute_json'])) {
  1817. $data['goods_attribute_json'] = [];
  1818. }
  1819. $workTimeStart = date('H:i',strtotime($params['work_time'][0]));
  1820. $workTimeEnd = date('H:i',strtotime($params['work_time'][1]));
  1821. $data['goods_attribute_json']['time'] = $workTimeStart.'至'.$workTimeEnd;
  1822. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  1823. }
  1824. if (isset($params['min_count'])){
  1825. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  1826. if (is_json($data['goods_attribute_json'])) {
  1827. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  1828. } else {
  1829. $data['goods_attribute_json'] = [];
  1830. }
  1831. } elseif (empty($data['goods_attribute_json'])) {
  1832. $data['goods_attribute_json'] = [];
  1833. }
  1834. $data['goods_attribute_json']['min-count'] = $params['min_count'];
  1835. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  1836. }
  1837. if (isset($params['teachers'])){
  1838. if (!empty($data['goods_attribute_json']) && !is_array($data['goods_attribute_json'])) {
  1839. if (is_json($data['goods_attribute_json'])) {
  1840. $data['goods_attribute_json'] = json_decode($data['goods_attribute_json'], true);
  1841. } else {
  1842. $data['goods_attribute_json'] = [];
  1843. }
  1844. } elseif (empty($data['goods_attribute_json'])) {
  1845. $data['goods_attribute_json'] = [];
  1846. }
  1847. $data['goods_attribute_json']['teachers'] = $params['teachers'];
  1848. $data['goods_attribute_json'] = json_encode($data['goods_attribute_json']);
  1849. }
  1850. foreach ($data as $key => $val) {
  1851. $row->{$key} = $val;
  1852. }
  1853. $row->goods_updatetimes = time();
  1854. $row->save();
  1855. } catch (BusinessException $e) {
  1856. throw new BusinessException($e->getMessage());
  1857. } catch (\Exception $e) {
  1858. dump($e->getTrace());
  1859. throw new BusinessException('数据更新异常~1');
  1860. }
  1861. }
  1862. /**
  1863. * @Desc
  1864. * @Author Gorden
  1865. * @Date 2024/3/12 9:57
  1866. *
  1867. * @param $params
  1868. * @return void
  1869. * @throws BusinessException
  1870. */
  1871. public static function detailUpdate($params)
  1872. {
  1873. try {
  1874. $data = self::inputFilter($params, new GoodsDetail());
  1875. if (!empty($data['goods_detail_slider_json'])) {
  1876. $data['goods_detail_slider_json'] = str_replace(getenv('STORAGE_DOMAIN'), '', $data['goods_detail_slider_json']);
  1877. $data['goods_detail_slider_json'] = json_encode(['slider' => $data['goods_detail_slider_json']]);
  1878. }
  1879. if(isset($params['curriculum'])){
  1880. $data['goods_detail_specs_json'] = json_encode([
  1881. [
  1882. 'key'=>'课时',
  1883. 'val'=>$params['curriculum']['period'] ?? '',
  1884. ],
  1885. [
  1886. 'key'=>'群体',
  1887. 'val'=>$params['curriculum']['group'] ?? '',
  1888. ],
  1889. [
  1890. 'key'=>'课程',
  1891. 'val'=>$params['curriculum']['course'] ?? '',
  1892. ],
  1893. ]);
  1894. }
  1895. // 根据goods_id 查详情ID
  1896. $detail = GoodsDetail::where('join_detail_goods_id', $params['goods_id'])->first();
  1897. if ($detail) {
  1898. self::doUpdate($detail->join_detail_goods_id, $data, new GoodsDetail());
  1899. } else {
  1900. $data['join_detail_goods_id'] = $params['goods_id'];
  1901. GoodsDetail::insert($data);
  1902. }
  1903. } catch (BusinessException $e) {
  1904. throw new BusinessException($e->getMessage());
  1905. } catch (\Exception $e) {
  1906. dump($e->getMessage());
  1907. throw new BusinessException('轮播图/详情数据更新异常~');
  1908. }
  1909. }
  1910. /**
  1911. * @Desc
  1912. * @Author Gorden
  1913. * @Date 2024/3/12 9:58
  1914. *
  1915. * @param $params
  1916. * @return void
  1917. * @throws BusinessException
  1918. */
  1919. public static function labelUpdate($params)
  1920. {
  1921. try {
  1922. $data = self::inputFilter($params, new GoodsLabel());
  1923. // 根据goods_id 查详情ID
  1924. $detail = GoodsLabel::where('join_label_goods_id', $params['goods_id'])->first();
  1925. if ($detail) {
  1926. self::doUpdate($detail->goods_label_id, $data, new GoodsLabel());
  1927. } else {
  1928. $data['join_label_goods_id'] = $params['goods_id'];
  1929. GoodsLabel::insert($data);
  1930. }
  1931. } catch (BusinessException $e) {
  1932. throw new BusinessException($e->getMessage());
  1933. } catch (\Exception $e) {
  1934. dump($e->getMessage());
  1935. throw new BusinessException('数据更新异常~3');
  1936. }
  1937. }
  1938. /**
  1939. * @Desc
  1940. * @Author Gorden
  1941. * @Date 2024/3/12 9:59
  1942. *
  1943. * @param $params
  1944. * @return void
  1945. * @throws BusinessException
  1946. */
  1947. public static function goodsRunningUpdate($params)
  1948. {
  1949. try {
  1950. $data = self::inputFilter($params, new GoodsRunning());
  1951. // 根据goods_id 查详情ID
  1952. $detail = GoodsRunning::where('join_running_goods_id', $params['goods_id'])->first();
  1953. if (!empty($params['goods_running_off_type']) && $params['goods_running_off_type'] == 'T') {
  1954. $redis = Redis::connection();
  1955. if (!empty($detail->goods_running_off_json)) {
  1956. $goodsRunningOffJson = json_decode($detail->goods_running_off_json, true);
  1957. if (isset($goodsRunningOffJson['time'])) {
  1958. $oldKey = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi', $goodsRunningOffJson['time']);
  1959. $goodsRunningOffJson['time'] = strtotime($params['goods_off_addtimes']);
  1960. $data['goods_running_off_json'] = json_encode($goodsRunningOffJson);
  1961. // 有老的下架时间,删除老的
  1962. $redis->srem($oldKey, $params['goods_id']);
  1963. } else {
  1964. $goodsRunningOffJson['time'] = strtotime($params['goods_off_addtimes']);
  1965. $data['goods_running_off_json'] = json_encode($goodsRunningOffJson);
  1966. }
  1967. } else {
  1968. $data['goods_running_off_json'] = json_encode(['time' => strtotime($params['goods_off_addtimes'])]);
  1969. }
  1970. // 加入自动下架
  1971. $newKey = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi', strtotime($params['goods_off_addtimes']));
  1972. $redis->sAdd($newKey, $params['goods_id']);
  1973. } 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') {
  1974. $goodsRunningOffJson = json_decode($detail->goods_running_off_json, true);
  1975. if (isset($goodsRunningOffJson['time'])) {
  1976. $oldKey = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi', $goodsRunningOffJson['time']);
  1977. $redis = Redis::connection();
  1978. $redis->srem($oldKey, $params['goods_id']);
  1979. }
  1980. }
  1981. if ($detail) {
  1982. self::doUpdate($detail->join_running_goods_id, $data, new GoodsRunning());
  1983. } else {
  1984. // 兼容老数据……
  1985. $data['join_running_goods_id'] = $params['goods_id'];
  1986. GoodsRunning::insert($data);
  1987. }
  1988. } catch (BusinessException $e) {
  1989. throw new BusinessException($e->getMessage());
  1990. } catch (\Exception $e) {
  1991. dump($e->getMessage());
  1992. throw new BusinessException('数据更新异常~');
  1993. }
  1994. }
  1995. /**
  1996. * @Desc sku 设置
  1997. * @Author Gorden
  1998. * @Date 2024/4/10 10:43
  1999. *
  2000. * @param $params
  2001. * @return void
  2002. * @throws BusinessException
  2003. */
  2004. public static function goodsSkuSet($params, $operation = 'insert')
  2005. {
  2006. try {
  2007. Db::beginTransaction();
  2008. $skusOldIds = [];
  2009. if ($operation == 'update') {
  2010. // 查出所有的
  2011. $skusOldIds = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->pluck('goods_sku_id', 'goods_sku_id');
  2012. // 删掉原有的
  2013. // GoodsSku::where('join_sku_goods_id', $params['goods_id'])->delete();
  2014. }
  2015. if (empty($skusOldIds) && empty($params['goods_sku_json_value'])) {
  2016. $skuData = [
  2017. 'join_sku_goods_id' => $params['goods_id'],
  2018. 'goods_sku_status' => 'ON',
  2019. 'goods_sku_specs_json' => '{"规格": "标准"}',
  2020. 'goods_sku_title' => "标准" . $params['goods_name'],
  2021. 'goods_sku_market_price' => $params['goods_market_price'] ?? 0,
  2022. 'goods_sku_sales_price' => $params['goods_sales_price'] ?? 0,
  2023. ];
  2024. GoodsSku::insert($skuData);
  2025. }
  2026. // 入新的
  2027. if (!empty($params['goods_sku_json_value'])) {
  2028. foreach ($params['goods_sku_json_value'] as $item) {
  2029. $skus = explode(',', $item['sku']);
  2030. $skuArr = [];
  2031. for ($i = 1; $i <= count($skus); $i++) {
  2032. $skuName = "skuName" . $i;
  2033. $key = $item[$skuName];
  2034. $skuArr[$key] = $skus[$i - 1];
  2035. }
  2036. $specsJson = json_encode($skuArr);
  2037. $skuTitle = str_replace('-', ',', $item['sku']) . $params['goods_name'];
  2038. if ($operation == 'update' && !empty($item['sku_id'])) {
  2039. $model = GoodsSku::where('goods_sku_id', $item['sku_id'])->first();
  2040. if (!$model) {
  2041. $model = new GoodsSku();
  2042. } else {
  2043. unset($skusOldIds[$model->goods_sku_id]);
  2044. }
  2045. } else {
  2046. $model = GoodsSku::where('join_sku_goods_id', $params['goods_id'])->where('goods_sku_title', $skuTitle)->first();
  2047. if (!$model) {
  2048. $model = new GoodsSku();
  2049. } else {
  2050. unset($skusOldIds[$model->goods_sku_id]);
  2051. }
  2052. }
  2053. $model->join_sku_goods_id = $params['goods_id'];
  2054. $model->goods_sku_status = $params['goods_status'];
  2055. $model->goods_sku_specs_json = $specsJson;
  2056. $model->goods_sku_title = $skuTitle;
  2057. $model->goods_sku_market_price = $params['goods_market_price'] ?? 0;
  2058. $model->goods_sku_sales_price = $item['price'];
  2059. $model->goods_sku_storage_json = json_encode(['storage' => $item['stock']]);
  2060. $model->save();
  2061. }
  2062. }
  2063. if ($operation == 'update' && !empty($skusOldIds)) {
  2064. GoodsSku::whereIn('goods_sku_id', $skusOldIds)->delete();
  2065. }
  2066. Db::commit();
  2067. } catch (\Exception $e) {
  2068. dump($e->getTrace());
  2069. Db::rollBack();
  2070. throw new BusinessException('规格数据更新异常~');
  2071. }
  2072. }
  2073. /**
  2074. * @Desc
  2075. * @Author Gorden
  2076. * @Date 2024/3/12 8:45
  2077. *
  2078. * @param array $data
  2079. * @param $model
  2080. * @return array
  2081. * @throws BusinessException
  2082. */
  2083. private static function inputFilter(array $data, $model): array
  2084. {
  2085. $table = config('database.connections.mysql.prefix') . $model->getTable();
  2086. $allow_column = $model->getConnection()->select("desc `$table`");
  2087. if (!$allow_column) {
  2088. throw new BusinessException('表不存在', 2);
  2089. }
  2090. $columns = array_column($allow_column, 'Type', 'Field');
  2091. foreach ($data as $col => $item) {
  2092. if (!isset($columns[$col])) {
  2093. unset($data[$col]);
  2094. continue;
  2095. }
  2096. // 非字符串类型传空则为null
  2097. if ($item === '' && strpos(strtolower($columns[$col]), 'varchar') === false && strpos(strtolower($columns[$col]), 'text') === false) {
  2098. $data[$col] = null;
  2099. }
  2100. if (is_array($item)) {
  2101. $data[$col] = implode(',', $item);
  2102. }
  2103. if ($item != '' && (strpos(strtolower($columns[$col]), 'varchar') || strpos(strtolower($columns[$col]), 'text'))) {
  2104. $data[$col] = htmlspecialchars($item);
  2105. }
  2106. }
  2107. if (empty($data['created_at'])) {
  2108. unset($data['created_at']);
  2109. }
  2110. if (empty($data['updated_at'])) {
  2111. unset($data['updated_at']);
  2112. }
  2113. return $data;
  2114. }
  2115. /**
  2116. * @Desc 执行更新
  2117. * @Author Gorden
  2118. * @Date 2024/3/12 8:43
  2119. *
  2120. * @param $id
  2121. * @param $data
  2122. * @param $model
  2123. * @return void
  2124. */
  2125. private static function doUpdate($id, $data, $model)
  2126. {
  2127. $row = $model->find($id);
  2128. foreach ($data as $key => $val) {
  2129. $row->{$key} = $val;
  2130. }
  2131. $row->save();
  2132. }
  2133. /**
  2134. * @Desc 上架定时
  2135. * @Author Gorden
  2136. * @Date 2024/4/11 15:13
  2137. *
  2138. * @return void
  2139. */
  2140. public static function checkListing()
  2141. {
  2142. $key = Goods::LISTING_KEY_PREFIX . date('YmdHi');
  2143. $redis = Redis::connection();
  2144. if (!$redis->exists($key)) {
  2145. return;
  2146. }
  2147. $goodsIds = $redis->sMembers($key);
  2148. if (Goods::whereIn('goods_id', $goodsIds)->update(['goods_status' => 'ON'])) {
  2149. $redis->del($key);
  2150. }
  2151. }
  2152. /**
  2153. * @Desc 自动下架
  2154. * @Author Gorden
  2155. * @Date 2024/4/26 15:26
  2156. *
  2157. * @return void
  2158. */
  2159. public static function checkOffListing()
  2160. {
  2161. $key = Goods::LISTING_OFF_KEY_PREFIX . date('YmdHi');
  2162. $redis = Redis::connection();
  2163. if (!$redis->exists($key)) {
  2164. return;
  2165. }
  2166. $goodsIds = $redis->sMembers($key);
  2167. if (Goods::whereIn('goods_id', $goodsIds)->update(['goods_status' => 'OFF'])) {
  2168. $redis->del($key);
  2169. }
  2170. }
  2171. public static $week = [
  2172. '周一' => 1,
  2173. '周二' => 2,
  2174. '周三' => 3,
  2175. '周四' => 4,
  2176. '周五' => 5,
  2177. '周六' => 6,
  2178. '周日' => 7,
  2179. ];
  2180. }