Procházet zdrojové kódy

seo管理 关键词管理调整

zq940222 před 4 hodinami
rodič
revize
a9e01e5cd3

+ 164 - 92
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/seo/controller/SeoKeywordsController.java

@@ -1,30 +1,30 @@
 package org.jeecg.modules.adweb.seo.controller;
 
+import cn.hutool.core.collection.CollectionUtil;
 import cn.hutool.core.io.IoUtil;
 import cn.hutool.poi.excel.ExcelUtil;
 import cn.hutool.poi.excel.ExcelWriter;
-
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.xkcoding.http.util.StringUtil;
-
 import io.swagger.v3.oas.annotations.Operation;
 import io.swagger.v3.oas.annotations.tags.Tag;
-
 import jakarta.servlet.ServletOutputStream;
 import jakarta.servlet.http.HttpServletRequest;
 import jakarta.servlet.http.HttpServletResponse;
-
 import lombok.extern.slf4j.Slf4j;
-
+import org.apache.commons.lang.RandomStringUtils;
+import org.apache.poi.ss.usermodel.CellType;
 import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.xssf.usermodel.XSSFCell;
+import org.apache.poi.xssf.usermodel.XSSFRow;
+import org.apache.poi.xssf.usermodel.XSSFSheet;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.jeecg.common.api.vo.Result;
 import org.jeecg.common.aspect.annotation.AutoLog;
 import org.jeecg.common.system.api.ISysBaseAPI;
 import org.jeecg.common.system.base.controller.JeecgController;
-import org.jeecg.common.system.vo.LoginUser;
-import org.jeecg.config.security.utils.SecureUtil;
 import org.jeecg.modules.adweb.common.constant.AdwebConstant;
 import org.jeecg.modules.adweb.seo.dto.AvesApiSearchKeywordsDTO;
 import org.jeecg.modules.adweb.seo.entity.SeoKeywords;
@@ -32,19 +32,23 @@ import org.jeecg.modules.adweb.seo.service.ISeoKeywordsRankService;
 import org.jeecg.modules.adweb.seo.service.ISeoKeywordsService;
 import org.jeecg.modules.adweb.site.entity.AdwebSite;
 import org.jeecg.modules.adweb.site.service.IAdwebSiteService;
-import org.jeecg.modules.system.entity.SysUser;
-import org.jeecgframework.poi.excel.def.NormalExcelConstants;
-import org.jeecgframework.poi.excel.entity.ExportParams;
-import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
+import org.springframework.web.multipart.MultipartHttpServletRequest;
 import org.springframework.web.servlet.ModelAndView;
 
+import java.io.File;
+import java.io.FileInputStream;
 import java.io.IOException;
 import java.nio.charset.StandardCharsets;
+import java.text.SimpleDateFormat;
 import java.util.*;
 
+import static cn.hutool.core.collection.CollUtil.distinct;
+
 /**
  * @Description: 关键词 @Author: jeecg-boot @Date: 2024-10-15 @Version: V1.0
  */
@@ -63,6 +67,9 @@ public class SeoKeywordsController extends JeecgController<SeoKeywords, ISeoKeyw
 
     @Autowired private IAdwebSiteService adwebSiteService;
 
+    @Value("${jeecg.path.upload}")
+    private String uploadUrl;
+
     /**
      * 分页列表查询
      *
@@ -111,16 +118,16 @@ public class SeoKeywordsController extends JeecgController<SeoKeywords, ISeoKeyw
     @PreAuthorize("@jps.requiresPermissions('keywords:seo_keywords:add')")
     @PostMapping(value = "/add")
     public Result<String> add(@RequestBody SeoKeywords seoKeywords) {
-        AdwebSite siteByCode = adwebSiteService.getSiteByCode(seoKeywords.getSiteCode());
-        if (siteByCode == null) {
-            return Result.error("未找到对应站点");
-        }
+//        AdwebSite siteByCode = adwebSiteService.getSiteByCode(seoKeywords.getSiteCode());
+//        if (siteByCode == null) {
+//            return Result.error("未找到对应站点");
+//        }
         seoKeywords.setAppKey(AdwebConstant.APPKEY);
         // 根据siteCode获取站点域名
-        String domain = siteByCode.getDomain();
-        seoKeywords.setDomain(domain);
+//        String domain = siteByCode.getDomain();
+//        seoKeywords.setDomain(domain);
         seoKeywords.setStatus(1);
-        seoKeywords.setLang("en");
+//        seoKeywords.setLang("en");
         seoKeywordsService.save(seoKeywords);
         return Result.OK("添加成功!");
     }
@@ -196,80 +203,8 @@ public class SeoKeywordsController extends JeecgController<SeoKeywords, ISeoKeyw
      */
     @PreAuthorize("@jps.requiresPermissions('keywords:seo_keywords:exportXls')")
     @RequestMapping(value = "/exportXls")
-    public ModelAndView exportXls(HttpServletRequest request) throws Exception {
-        // Step.1 组装查询条件
-        //		QueryWrapper<SysUser> queryWrapper = QueryGenerator.initQueryWrapper(sysUser,
-        // request.getParameterMap());
-        // Step.2 AutoPoi 导出Excel
-        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
-        // update-begin--Author:kangxiaolin  Date:20180825
-        // for:[03]用户导出,如果选择数据则只导出相关数据--------------------
-        //		String selections = request.getParameter("selections");
-        //		if(!oConvertUtils.isEmpty(selections)){
-        //			queryWrapper.in("id",selections.split(","));
-        //		}
-        // update-end--Author:kangxiaolin  Date:20180825
-        // for:[03]用户导出,如果选择数据则只导出相关数据----------------------
-        //		List<SysUser> pageList = sysUserService.list(queryWrapper);
-        AvesApiSearchKeywordsDTO avesApiSearchKeywordsDTO = new AvesApiSearchKeywordsDTO();
-        Result<?> res = this.queryRankInfoList(avesApiSearchKeywordsDTO, 1, 5000, request);
-        if (res == null || res.getResult() == null) {
-            return null;
-        }
-        IPage<SeoKeywords> pageList = (IPage<SeoKeywords>) res.getResult();
-        if (pageList.getRecords().isEmpty()) {
-            return null;
-        }
-        ArrayList<String> columns = null;
-        // 表头信息
-        if (sysBaseAPI.isAdmin()) {
-            columns = new ArrayList<>(Arrays.asList("序号", "关键词", "url", "关键词类型"));
-        } else {
-            columns = new ArrayList<>(Arrays.asList("序号", "关键词", "关键词类型"));
-        }
-
-        // 将关键词信息改为可以导出的格式
-        List<Map<String, Object>> list = new ArrayList<>();
-        List<SeoKeywords> enquiryList = pageList.getRecords();
-
-        List<String> dates = pageList.getRecords().get(0).getDateList();
-        columns.addAll(dates);
-
-        boolean isAdmin = sysBaseAPI.isAdmin();
-
-        for (int i = 0; i < enquiryList.size(); i++) {
-            SeoKeywords seoKeywords = enquiryList.get(i);
-            Map<String, Object> map = new LinkedHashMap<>();
-            map.put("序号", i + 1);
-            map.put("关键词", seoKeywords.getKeywords());
-            if (isAdmin) {
-                map.put("url", seoKeywords.getPositionUrl());
-            }
-            if (seoKeywords.getKeywordType() == 1) {
-                map.put("关键词类型", "指定词");
-            } else if (seoKeywords.getKeywordType() == 2) {
-                map.put("关键词类型", "长尾词");
-            }
-            // 遍历日期
-            for (String date : dates) {
-                map.put(
-                        date,
-                        Integer.valueOf(
-                                seoKeywords.getRankInfo().get(date) == null
-                                        ? "0"
-                                        : seoKeywords.getRankInfo().get(date)));
-            }
-            list.add(map);
-        }
-        // 导出文件名称
-        mv.addObject(NormalExcelConstants.FILE_NAME, "关键词排行");
-        mv.addObject(NormalExcelConstants.CLASS, SysUser.class);
-        LoginUser user = SecureUtil.currentUser();
-        ExportParams exportParams =
-                new ExportParams("关键词排行数据", "导出人:" + user.getRealname(), "导出信息");
-        mv.addObject(NormalExcelConstants.PARAMS, exportParams);
-        mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
-        return mv;
+    public ModelAndView exportXls(HttpServletRequest request, SeoKeywords seoKeywords) {
+        return super.exportXls(request, seoKeywords, SeoKeywords.class, "关键词");
     }
 
     /** 关键词排名页面 - 列表数据 */
@@ -430,6 +365,143 @@ public class SeoKeywordsController extends JeecgController<SeoKeywords, ISeoKeyw
     }
 
     /**
+     * 通过excel导入数据
+     *
+     * @param request
+     * @return
+     */
+    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
+    public Result<?> importExcel(HttpServletRequest request) {
+        String siteId = request.getParameter("siteId");
+        log.info("siteId:" + siteId);
+        String subscriptionId = request.getParameter("subscriptionId");
+        log.info("subscriptionId:" + subscriptionId);
+        if (StringUtil.isEmpty(siteId)) {
+            return Result.error("站点id为空!");
+        }
+
+        AdwebSite site = adwebSiteService.getById(siteId);
+        if (site == null) {
+            return Result.error("站点不存在!");
+        }
+
+        if (StringUtil.isEmpty(site.getDomain())) {
+            return Result.error("站点域名未设置!");
+        }
+
+        Date now = new Date();
+
+        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
+        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
+        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
+            MultipartFile file = entity.getValue();
+            FileInputStream stream = null;
+            try {
+                SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMdd");
+                String formattedDate = dateFormat.format(now);
+                File realFile = new File(uploadUrl + "excel/", formattedDate + RandomStringUtils.random(4,true,true) + ".xlsx");
+                file.transferTo(realFile);
+                stream = new FileInputStream(realFile);
+                XSSFWorkbook workbook = new XSSFWorkbook(stream);
+                XSSFSheet sheet = workbook.getSheetAt(0);
+                int last = sheet.getLastRowNum();
+
+                List<SeoKeywords> list = new ArrayList<>();
+                for (int i = 1; i <= last; i++) {
+                    XSSFRow row = sheet.getRow(i);
+
+                    XSSFCell cell1 = row.getCell(0);
+                    if (cell1 == null || cell1.getCellTypeEnum() == CellType.BLANK) {
+                        continue;
+                    }
+                    cell1.setCellType(CellType.STRING);
+                    String value1 = cell1.getStringCellValue();
+                    value1 = value1.trim();
+                    log.info(value1 + " ");
+
+                    XSSFCell cell2 = row.getCell(1);
+                    if (cell2 == null || cell2.getCellTypeEnum() == CellType.BLANK) {
+                        continue;
+                    }
+                    cell2.setCellType(CellType.STRING);
+                    String value2 = cell2.getStringCellValue();
+                    value2 = value2.trim();
+                    value2 = value2.toLowerCase();
+                    log.info(value2 + " ");
+                    if (StringUtil.isEmpty(value2)) {
+                        continue;
+                    }
+                    SeoKeywords seoKeywords = new SeoKeywords();
+                    seoKeywords.setAppKey("adweb");
+                    seoKeywords.setSiteCode(site.getCode());
+                    seoKeywords.setKeywords(value2);
+                    seoKeywords.setDomain(site.getDomain());
+                    seoKeywords.setCreateTime(now);
+                    seoKeywords.setStatus(1);
+                    seoKeywords.setPriority(3);
+                    seoKeywords.setOptimizeProcess(0);
+                    seoKeywords.setOptimizeStatus(0);
+                    if ("指定关键词".equals(value1)) {
+                        seoKeywords.setKeywordType(1);
+                    } else {
+                        seoKeywords.setKeywordType(2);
+                    }
+                    seoKeywords.setLastRank(0);
+                    seoKeywords.setSearchStatus(0);
+                    seoKeywords.setSubscriptionId(subscriptionId);
+                    list.add(seoKeywords);
+                }
+
+                //新增关键词本身去重
+                Integer originalSize =  list.size();
+                List<SeoKeywords> listNew = distinct(list);
+                // 去重
+                QueryWrapper<SeoKeywords> queryWrapper = new QueryWrapper<>();
+                queryWrapper.eq("user_flag", site.getCode());
+                queryWrapper.ne("status", 0);
+                queryWrapper.eq("history_id", subscriptionId);
+                List<SeoKeywords> existingKeywordList = seoKeywordsService.list(queryWrapper);
+                if (CollectionUtil.isNotEmpty(existingKeywordList)) {
+                    List<String> existingKeywords = new ArrayList<>();
+                    for (SeoKeywords keywords : existingKeywordList) {
+                        existingKeywords.add(keywords.getKeywords());
+                    }
+                    for (int i = listNew.size() - 1; i >= 0; i--) {
+                        SeoKeywords word = listNew.get(i);
+                        if (existingKeywords.contains(word.getKeywords())) {
+                            listNew.remove(i);
+                        }
+                    }
+                }
+
+                if (CollectionUtil.isNotEmpty(listNew)) {
+                    return Result.OK("关键词导入个数为0!");
+                }
+
+                String msg = "关键词导入成功!关键词个数:" + listNew.size();
+                if(originalSize>listNew.size()){
+                    msg = "关键词导入成功!关键词个数:" + listNew.size()+",重复关键词已去除!重复个数:"+(originalSize-listNew.size());
+                }
+
+                return seoKeywordsService.saveBatch(listNew) ? Result.OK(msg) : Result.error("关键词导入失败!");
+
+            } catch (Exception e) {
+                log.error(e.getMessage(), e);
+                return Result.error("文件导入失败:" + e.getMessage());
+            } finally {
+                try {
+                    stream.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+        return Result.error("文件导入失败!");
+    }
+
+
+
+    /**
      * @param seoKeywords
      * @return
      */

+ 21 - 0
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/seo/service/impl/SeoKeywordsServiceImpl.java

@@ -1,20 +1,41 @@
 package org.jeecg.modules.adweb.seo.service.impl;
 
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 
+import com.xkcoding.http.util.StringUtil;
 import jakarta.annotation.Resource;
 
+import jakarta.servlet.http.HttpServletRequest;
 import org.apache.commons.collections4.CollectionUtils;
+import org.apache.poi.ss.usermodel.CellType;
+import org.apache.poi.xssf.usermodel.XSSFCell;
+import org.apache.poi.xssf.usermodel.XSSFRow;
+import org.apache.poi.xssf.usermodel.XSSFSheet;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.jeecg.common.api.vo.Result;
+import org.jeecg.modules.adweb.common.constant.NumConstant;
 import org.jeecg.modules.adweb.seo.entity.SeoKeywords;
 import org.jeecg.modules.adweb.seo.mapper.SeoKeywordsMapper;
 import org.jeecg.modules.adweb.seo.service.ISeoKeywordsService;
 import org.jeecg.modules.adweb.seo.vo.RankInfoVO;
+import org.jeecg.modules.adweb.site.entity.AdwebSite;
+import org.jeecg.modules.adweb.site.service.IAdwebSiteService;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
+import org.springframework.web.multipart.MultipartHttpServletRequest;
 
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
 import java.text.ParseException;
+import java.util.ArrayList;
+import java.util.Date;
 import java.util.List;
+import java.util.Map;
 
 /**
  * @Description: 关键词