GoodsService.php 108 KB

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