|
@@ -0,0 +1,366 @@
|
|
|
|
+package com.zkh360.api.home.service.impl;
|
|
|
|
+
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
|
+import com.zkh360.api.dto.GWResponseEnum;
|
|
|
|
+import com.zkh360.api.dto.GWResponseVO;
|
|
|
|
+import com.zkh360.api.dto.ResponseCode;
|
|
|
|
+import com.zkh360.api.dto.ResultResponse;
|
|
|
|
+import com.zkh360.api.home.dto.*;
|
|
|
|
+import com.zkh360.api.home.service.IHomeService;
|
|
|
|
+import com.zkh360.api.home.util.PedestalResponseMSG;
|
|
|
|
+import com.zkh360.core.exception.ZkhRuntimeException;
|
|
|
|
+import com.zkh360.core.util.ZkhApiUtil;
|
|
|
|
+import com.zkh360.core.util.ZkhGWInvoiceId;
|
|
|
|
+import com.zkh360.core.util.ZkhGWapiEnum;
|
|
|
|
+import com.zkh360.core.util.redis.RedisService;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import net.sf.json.JSONArray;
|
|
|
|
+import net.sf.json.JSONObject;
|
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
|
+
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
+import java.util.concurrent.CompletableFuture;
|
|
|
|
+import java.util.concurrent.Future;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 首页实现类
|
|
|
|
+ */
|
|
|
|
+@Service
|
|
|
|
+@Slf4j
|
|
|
|
+public class HomeServiceImpl implements IHomeService {
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private ZkhApiUtil zkhApiUtil;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private RedisService redisService;
|
|
|
|
+ @Value("${zkh.website.url}")
|
|
|
|
+ private String domainUrl;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private RestTemplate restTemplate;
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public ResultResponse getHome() throws Exception {
|
|
|
|
+
|
|
|
|
+ HomeResultDto homeResultDto = new HomeResultDto();
|
|
|
|
+ String message = null;
|
|
|
|
+ final String invoiceId = getInvoiceId();
|
|
|
|
+ //多线程请求官网api
|
|
|
|
+ Future<List> futureBannerList = CompletableFuture.supplyAsync(() -> getBanner(restTemplate,invoiceId));
|
|
|
|
+ Future<List> futureBrandList = CompletableFuture.supplyAsync(() -> getAuthenticBrand(restTemplate,invoiceId));
|
|
|
|
+ Future<List> futureCatalogueList = CompletableFuture.supplyAsync(() -> getCatalogue(restTemplate,invoiceId));
|
|
|
|
+ Future<List> futureTopCatalogueList = CompletableFuture.supplyAsync(() -> getTopCatalogue(restTemplate,invoiceId));
|
|
|
|
+ Future<List> futureRecommendList = CompletableFuture.supplyAsync(() -> getRecommends(restTemplate,invoiceId));
|
|
|
|
+ List<Banner> bannerList = futureBannerList.get();
|
|
|
|
+ List<Brand> brandList = futureBrandList.get();
|
|
|
|
+ List<FineCategory> catalogueList = futureCatalogueList.get();
|
|
|
|
+ List<Category> topCatalogueList = futureTopCatalogueList.get();
|
|
|
|
+ List<RecommendProduct> recommends = futureRecommendList.get();
|
|
|
|
+ homeResultDto.setBanners(bannerList);
|
|
|
|
+ homeResultDto.setBrands(brandList);
|
|
|
|
+ homeResultDto.setCategorys(topCatalogueList);
|
|
|
|
+ homeResultDto.setFineCategorys(catalogueList);
|
|
|
|
+ homeResultDto.setRecommends(recommends);
|
|
|
|
+ ResultResponse rs = new ResultResponse();
|
|
|
|
+ rs.setStateCode(ResponseCode.CODE_0);
|
|
|
|
+ rs.setMessage(message);
|
|
|
|
+ rs.setData(homeResultDto);
|
|
|
|
+ return rs;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取首页banner图
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public List<Banner> getBanner(RestTemplate restTemplate,String invoiceId){
|
|
|
|
+ List<Banner> bannerList = new ArrayList<>();
|
|
|
|
+ List<BannerDto> list = null;
|
|
|
|
+ String url = domainUrl + ZkhGWapiEnum.RECOMMENT_BANNER.getApi()+"?access_token="+redisService.get("access_token")+"&userInvoiceId="+invoiceId;
|
|
|
|
+ JSONArray result = requestGWApi(restTemplate, url);
|
|
|
|
+ if(result != null && result.size()>0) {
|
|
|
|
+ String jsonString = JSON.toJSONString(result);
|
|
|
|
+ list = JSON.parseArray(jsonString, BannerDto.class);
|
|
|
|
+ }
|
|
|
|
+ if(list != null && list.size() > 0){
|
|
|
|
+ for (BannerDto bannerDto : list) {
|
|
|
|
+ Banner banner = new Banner();
|
|
|
|
+ BeanUtils.copyProperties(bannerDto,banner);
|
|
|
|
+ bannerList.add(banner);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return bannerList;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取正品大牌
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public List<Brand> getAuthenticBrand(RestTemplate restTemplate, String invoiceId){
|
|
|
|
+ List<Brand> brandList = new ArrayList<>();
|
|
|
|
+ List<BannerDto> list = null;
|
|
|
|
+ String url = domainUrl + ZkhGWapiEnum.RECOMMENT_BRAND.getApi()+"?access_token="+redisService.get("access_token")+"&userInvoiceId="+invoiceId;
|
|
|
|
+ JSONArray result = requestGWApi(restTemplate, url);
|
|
|
|
+ if(result != null && result.size()>0) {
|
|
|
|
+ String jsonString = JSON.toJSONString(result);
|
|
|
|
+ list = JSON.parseArray(jsonString, BannerDto.class);
|
|
|
|
+ }
|
|
|
|
+ if(list != null && list.size() > 0){
|
|
|
|
+ for (BannerDto bannerDto : list) {
|
|
|
|
+ Brand brand = new Brand();
|
|
|
|
+ brand.setDetail(Integer.parseInt(bannerDto.getDetail()));
|
|
|
|
+ brand.setThumbnail(bannerDto.getThumbnail());
|
|
|
|
+ String title = bannerDto.getTitle();
|
|
|
|
+ brand.setBrandName(title.substring(0,title.indexOf("^")));
|
|
|
|
+ brandList.add(brand);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return brandList;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取精选品类
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public List<FineCategory> getCatalogue(RestTemplate restTemplate,String invoiceId){
|
|
|
|
+ List<FineCategory> catalogueList = new ArrayList<>();
|
|
|
|
+ List<BannerDto> list = null;
|
|
|
|
+ String url = domainUrl + ZkhGWapiEnum.RECOMMENT_CATALOGUE.getApi()+"?access_token="+redisService.get("access_token")+"&userInvoiceId="+invoiceId;
|
|
|
|
+ JSONArray result = requestGWApi(restTemplate, url);
|
|
|
|
+ if(result != null && result.size()>0) {
|
|
|
|
+ String jsonString = JSON.toJSONString(result);
|
|
|
|
+ list = JSON.parseArray(jsonString, BannerDto.class);
|
|
|
|
+ }
|
|
|
|
+ if(list != null && list.size() > 0){
|
|
|
|
+ for (BannerDto bannerDto : list) {
|
|
|
|
+ FineCategory fineCategory = new FineCategory();
|
|
|
|
+ fineCategory.setId(bannerDto.getId());
|
|
|
|
+ fineCategory.setThumbnail(bannerDto.getThumbnail());
|
|
|
|
+ fineCategory.setDetail(Integer.parseInt(bannerDto.getDetail()));
|
|
|
|
+ String title = bannerDto.getTitle();
|
|
|
|
+ int i = title.indexOf("^");
|
|
|
|
+ int index = title.indexOf("|");
|
|
|
|
+ String titleName = title.substring(0,i);
|
|
|
|
+ String subTitle = title.substring(index+1);
|
|
|
|
+ fineCategory.setSubTitle(subTitle);
|
|
|
|
+ fineCategory.setTitle(titleName);
|
|
|
|
+ fineCategory.setTitleName(titleName);
|
|
|
|
+ catalogueList.add(fineCategory);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return catalogueList;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取一级目录推荐
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public List<Category> getTopCatalogue(RestTemplate restTemplate,String invoiceId){
|
|
|
|
+ List<Category> topCatalogueList = new ArrayList<>();
|
|
|
|
+ List<BannerDto> list = null;
|
|
|
|
+ String url = domainUrl + ZkhGWapiEnum.RECOMMENT_TOPCATALOGUE.getApi()+"?access_token="+redisService.get("access_token")+"&userInvoiceId="+invoiceId;
|
|
|
|
+ JSONArray result = requestGWApi(restTemplate, url);
|
|
|
|
+ if(result != null && result.size()>0) {
|
|
|
|
+ String jsonString = JSON.toJSONString(result);
|
|
|
|
+ list = JSON.parseArray(jsonString, BannerDto.class);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if(list != null && list.size() > 0){
|
|
|
|
+ for (BannerDto bannerDto : list) {
|
|
|
|
+ Category category = new Category();
|
|
|
|
+ category.setThumbnail(bannerDto.getThumbnail());
|
|
|
|
+ int detail = Integer.parseInt(bannerDto.getDetail());
|
|
|
|
+ int id = detail < 0 ? 0 : detail;
|
|
|
|
+ category.setDetail(id);
|
|
|
|
+ String title = bannerDto.getTitle();
|
|
|
|
+ category.setTitle(title.substring(0,title.indexOf("^")));
|
|
|
|
+ topCatalogueList.add(category);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return topCatalogueList;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ *
|
|
|
|
+ * 获取热销产品推荐
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public List<RecommendProduct> getRecommends(RestTemplate restTemplate,String invoiceId){
|
|
|
|
+ List<RecommendProduct> list = new ArrayList<>();
|
|
|
|
+ String url = domainUrl + ZkhGWapiEnum.RECOMMENT_PRODUCT.getApi()+"?access_token="+redisService.get("access_token")+"&userInvoiceId="+invoiceId;
|
|
|
|
+ log.info("请求官网url===>"+url);
|
|
|
|
+ long startTime = System.currentTimeMillis();
|
|
|
|
+ ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
|
|
|
|
+ String body = responseEntity.getBody();
|
|
|
|
+ log.info("耗时 : " + (System.currentTimeMillis() - startTime));
|
|
|
|
+ JSONObject resultBody = JSONObject.fromObject(body);
|
|
|
|
+ Boolean success = (Boolean)resultBody.get("success");
|
|
|
|
+ if(!success){
|
|
|
|
+ throw new ZkhRuntimeException((String)resultBody.get("message"));
|
|
|
|
+ }
|
|
|
|
+ JSONObject result = resultBody.getJSONObject("result");
|
|
|
|
+ if(null != result && result.has("content")){
|
|
|
|
+ JSONArray content = result.getJSONArray("content");
|
|
|
|
+ if(content != null && content.size()>0) {
|
|
|
|
+ String s = JSON.toJSONString(content);
|
|
|
|
+ list = JSON.parseArray(s,RecommendProduct.class);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if(list != null && list.size() > 0){
|
|
|
|
+ for (RecommendProduct recommend : list) {
|
|
|
|
+ recommend.setGoodsThumb(recommend.getLeftImageList().get(0));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return list;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public ResultResponse changePedestalForJava(PedestalInfo pedestalInfo) {
|
|
|
|
+ ResultResponse resultResponse = new ResultResponse();
|
|
|
|
+ resultResponse.setStateCode(ResponseCode.CODE_0);
|
|
|
|
+ resultResponse.setMessage("修改成功");
|
|
|
|
+ if(pedestalInfo.getPassCode().hashCode() != PedestalResponseMSG.PASSCODE){
|
|
|
|
+ resultResponse.setStateCode(ResponseCode.CODE_1);
|
|
|
|
+ resultResponse.setMessage(PedestalResponseMSG.PASSCODE_WRONG);
|
|
|
|
+ return resultResponse;
|
|
|
|
+ }
|
|
|
|
+ if(!pedestalInfo.getDevice().equals(PedestalResponseMSG.ANDROID) && !pedestalInfo.getDevice().equals(PedestalResponseMSG.IOS)){
|
|
|
|
+ resultResponse.setStateCode(ResponseCode.CODE_1);
|
|
|
|
+ resultResponse.setMessage(PedestalResponseMSG.DEVICE_WRONG);
|
|
|
|
+ return resultResponse;
|
|
|
|
+ }
|
|
|
|
+ if(!pedestalInfo.getNativeOrH5().equals(PedestalResponseMSG.NATIVE) && !pedestalInfo.getNativeOrH5().equals(PedestalResponseMSG.H5)){
|
|
|
|
+ resultResponse.setStateCode(ResponseCode.CODE_1);
|
|
|
|
+ resultResponse.setMessage(PedestalResponseMSG.PEDESTAL_WRONG);
|
|
|
|
+ return resultResponse;
|
|
|
|
+ }
|
|
|
|
+ if(pedestalInfo.getDevice().equals(PedestalResponseMSG.ANDROID)){
|
|
|
|
+ if(pedestalInfo.getNativeOrH5().equals(PedestalResponseMSG.NATIVE)){
|
|
|
|
+ redisService.set(PedestalResponseMSG.PEDESTAL_FOR_ANDRIOD,PedestalResponseMSG.NATIVE_CODE);
|
|
|
|
+ }else{
|
|
|
|
+ redisService.set(PedestalResponseMSG.PEDESTAL_FOR_ANDRIOD,PedestalResponseMSG.H5_CODE);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if(pedestalInfo.getDevice().equals(PedestalResponseMSG.IOS)){
|
|
|
|
+ if(pedestalInfo.getNativeOrH5().equals(PedestalResponseMSG.NATIVE)){
|
|
|
|
+ redisService.set(PedestalResponseMSG.PEDESTAL_FOR_IOS,PedestalResponseMSG.NATIVE_CODE);
|
|
|
|
+ }else{
|
|
|
|
+ redisService.set(PedestalResponseMSG.PEDESTAL_FOR_IOS,PedestalResponseMSG.H5_CODE);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return resultResponse;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public ResultResponse getNoticeList() throws Exception {
|
|
|
|
+ String url = ZkhGWapiEnum.GLOBALCONFIG_ANNOUNCEMENT.getApi();
|
|
|
|
+ GWResponseVO<Object> gwResponseVO = new GWResponseVO<>();
|
|
|
|
+ List<Notice> list = new ArrayList<>();
|
|
|
|
+ try {
|
|
|
|
+ gwResponseVO = zkhApiUtil.callZKHGWGet(url, new HashMap<>(0), null);
|
|
|
|
+ }catch (ZkhRuntimeException zkhRuntimeException){
|
|
|
|
+ //首页公告配置过期
|
|
|
|
+ if(GWResponseEnum.CONFIG_ANNOUNCEMENT.getMessage().equals(zkhRuntimeException.getMessage())){
|
|
|
|
+ return new ResultResponse(list);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ //首页公告没有配置
|
|
|
|
+ if(gwResponseVO.getResult() == null){
|
|
|
|
+ return new ResultResponse(list);
|
|
|
|
+ }
|
|
|
|
+ String noticeContent = gwResponseVO.getResult().toString();
|
|
|
|
+ Notice notice = new Notice();
|
|
|
|
+ notice.setNoticeContent(noticeContent);
|
|
|
|
+ list.add(notice);
|
|
|
|
+ return new ResultResponse(list);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public ResultResponse getBannerInfo(Integer id) throws Exception {
|
|
|
|
+ String invoiceId = getInvoiceId();
|
|
|
|
+ String url = domainUrl + ZkhGWapiEnum.RECOMMENT_BANNER.getApi()+"?access_token="+redisService.get("access_token")+"&userInvoiceId="+invoiceId;
|
|
|
|
+ String html = "";
|
|
|
|
+ JSONArray jsonArray = requestGWApi(restTemplate, url);
|
|
|
|
+ if(null != jsonArray && jsonArray.size() >0){
|
|
|
|
+ for (int i = 0; i < jsonArray.size(); i++) {
|
|
|
|
+ JSONObject jsonObject = jsonArray.getJSONObject(i);
|
|
|
|
+ if(null != jsonObject && jsonObject.getInt("id") == id){
|
|
|
|
+ html = jsonObject.getString("detail");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return new ResultResponse(html);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public ResultResponse changePedestal(String device) throws Exception {
|
|
|
|
+ ResultResponse resultResponse = new ResultResponse();
|
|
|
|
+ resultResponse.setStateCode(ResponseCode.CODE_0);
|
|
|
|
+ resultResponse.setMessage("成功");
|
|
|
|
+ Map resultMap = new HashMap();
|
|
|
|
+ if(!device.equals(PedestalResponseMSG.ANDROID) && !device.equals(PedestalResponseMSG.IOS)){
|
|
|
|
+ resultResponse.setStateCode(ResponseCode.CODE_1);
|
|
|
|
+ resultResponse.setMessage(PedestalResponseMSG.DEVICE_WRONG);
|
|
|
|
+ return resultResponse;
|
|
|
|
+ }
|
|
|
|
+ Integer pedestalCode = 1;
|
|
|
|
+ if(device.equals(PedestalResponseMSG.ANDROID)){
|
|
|
|
+ if(!redisService.exists(PedestalResponseMSG.PEDESTAL_FOR_ANDRIOD)){
|
|
|
|
+ pedestalCode = 1;
|
|
|
|
+ }else{
|
|
|
|
+ pedestalCode = (Integer) redisService.get(PedestalResponseMSG.PEDESTAL_FOR_ANDRIOD);
|
|
|
|
+ }
|
|
|
|
+ }else{
|
|
|
|
+ if(!redisService.exists(PedestalResponseMSG.PEDESTAL_FOR_IOS)){
|
|
|
|
+ pedestalCode = 1;
|
|
|
|
+ }else{
|
|
|
|
+ pedestalCode = (Integer) redisService.get(PedestalResponseMSG.PEDESTAL_FOR_IOS);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ resultMap.put("nativeOrH5",pedestalCode);
|
|
|
|
+ resultResponse.setData(resultMap);
|
|
|
|
+ return resultResponse;
|
|
|
|
+ }
|
|
|
|
+ @Override
|
|
|
|
+ public JSONArray requestGWApi(RestTemplate restTemplate,String url){
|
|
|
|
+ log.info("请求官网url===>"+url);
|
|
|
|
+ long startTime = System.currentTimeMillis();
|
|
|
|
+ ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
|
|
|
|
+ String body = responseEntity.getBody();
|
|
|
|
+ log.info("耗时 : " + (System.currentTimeMillis() - startTime));
|
|
|
|
+ JSONObject resultBody = JSONObject.fromObject(body);
|
|
|
|
+ Boolean success = (Boolean)resultBody.get("success");
|
|
|
|
+ if(!success){
|
|
|
|
+ throw new ZkhRuntimeException((String)resultBody.get("message"));
|
|
|
|
+ }
|
|
|
|
+ JSONArray result = resultBody.getJSONArray("result");
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //获取发票ID
|
|
|
|
+ public String getInvoiceId(){
|
|
|
|
+ String invoiceId = null;
|
|
|
|
+ String authorization = ZkhGWInvoiceId.getInvoiceId();
|
|
|
|
+ if(StringUtils.isNotBlank(authorization)){
|
|
|
|
+ invoiceId = redisService.get(authorization).toString();
|
|
|
|
+ }
|
|
|
|
+ return invoiceId==null?"":invoiceId;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|