From ef5505e22c84a63d966f35cee78afa544ece57df Mon Sep 17 00:00:00 2001
From: wzy <wzy19931122ai@163.com>
Date: Sun, 07 Aug 2022 14:33:32 +0800
Subject: [PATCH] fix

---
 src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/ColumnServiceImpl.java |  199 +++++++++++++++++++++++++++++++++++--------------
 1 files changed, 143 insertions(+), 56 deletions(-)

diff --git a/src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/ColumnServiceImpl.java b/src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/ColumnServiceImpl.java
index d5aeb3a..7e9986d 100644
--- a/src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/ColumnServiceImpl.java
+++ b/src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/ColumnServiceImpl.java
@@ -6,39 +6,50 @@
 import com.baomidou.mybatisplus.core.toolkit.StringPool;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.xcong.farmer.cms.common.contants.AppContants;
 import com.xcong.farmer.cms.common.response.Result;
+import com.xcong.farmer.cms.conversion.ColumnConversion;
 import com.xcong.farmer.cms.modules.system.dto.AdminAddColumnDto;
 import com.xcong.farmer.cms.modules.system.dto.AdminColumnDto;
 import com.xcong.farmer.cms.modules.system.dto.AdminDeleteDto;
 import com.xcong.farmer.cms.modules.system.dto.AdminUpdateColumnDto;
-import com.xcong.farmer.cms.modules.system.entity.ColumnEntity;
-import com.xcong.farmer.cms.modules.system.entity.NavigationBarEntity;
-import com.xcong.farmer.cms.modules.system.entity.UserEntity;
+import com.xcong.farmer.cms.modules.system.entity.*;
+import com.xcong.farmer.cms.modules.system.mapper.ArticleMapper;
 import com.xcong.farmer.cms.modules.system.mapper.ColumnMapper;
+import com.xcong.farmer.cms.modules.system.mapper.CompanyMapper;
 import com.xcong.farmer.cms.modules.system.service.IColumnService;
 import com.xcong.farmer.cms.modules.system.util.LoginUserUtil;
 import com.xcong.farmer.cms.modules.system.vo.AdminColumnVo;
 import com.xcong.farmer.cms.modules.system.vo.AdminSeeColumnInfoVo;
+import com.xcong.farmer.cms.modules.system.vo.WebColumnVo;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.core.util.ObjectUtil;
 import org.springframework.transaction.annotation.Transactional;
 
+import javax.servlet.http.HttpServletRequest;
 import java.util.ArrayList;
 import java.util.List;
 
 @Service
 @Slf4j
 public class ColumnServiceImpl extends ServiceImpl<ColumnMapper, ColumnEntity> implements IColumnService {
+
+    @Autowired
+    private ArticleMapper articleMapper;
+    @Autowired
+    private CompanyMapper companyMapper;
+
     @Override
     public Result getColumnInPage(AdminColumnDto adminColumnDto) {
         UserEntity userlogin = LoginUserUtil.getLoginUser();
-        long belongId = userlogin.getBelongId() == null ? 0L : userlogin.getBelongId();
+        long companyId = userlogin.getCompanyId() == null ? UserEntity.USER_BELONG_TOP : userlogin.getCompanyId();
         Page<AdminColumnVo> page = new Page<>(adminColumnDto.getPageNum(), adminColumnDto.getPageSize());
         ColumnEntity columnEntity = new ColumnEntity();
-        columnEntity.setBelongId(belongId);
+        columnEntity.setCompanyId(companyId);
         columnEntity.setParentId(ColumnEntity.PARENTID_DEFAULT);
         IPage<AdminColumnVo> list = this.baseMapper.selectAdminColumnVoInPage(page,columnEntity);
         List<AdminColumnVo> records = list.getRecords();
@@ -47,7 +58,7 @@
                 Long id = adminColumnVo.getId();
                 QueryWrapper<ColumnEntity> objectQueryWrapper = new QueryWrapper<>();
                 objectQueryWrapper.eq("parent_id",id);
-                objectQueryWrapper.eq("belong_id",belongId);
+                objectQueryWrapper.eq("company_id",companyId);
                 List<ColumnEntity> columnEntities = this.baseMapper.selectList(objectQueryWrapper);
                 List<AdminColumnVo> adminColumnVoChilds = new ArrayList<>();
                 if(CollUtil.isNotEmpty(columnEntities)){
@@ -72,33 +83,26 @@
     @Transactional
     public Result addColumn(AdminAddColumnDto adminAddColumnDto) {
         UserEntity userlogin = LoginUserUtil.getLoginUser();
-        long belongId = userlogin.getBelongId() == null ? 0L : userlogin.getBelongId();
+        long companyId = userlogin.getCompanyId() == null ? UserEntity.USER_BELONG_TOP : userlogin.getCompanyId();
         ColumnEntity columnEntity = new ColumnEntity();
-        columnEntity.setBelongId(belongId);
+        columnEntity.setCompanyId(companyId);
         String columnName = adminAddColumnDto.getColumnName();
-        if(StrUtil.isEmpty(columnName)){
-            return Result.fail("请输入栏目名称");
-        }
         columnEntity.setColumnName(columnName);
         String columnCode = adminAddColumnDto.getColumnCode();
-        if(StrUtil.isEmpty(columnCode)){
-            return Result.fail("请输入栏目编码");
-        }
         QueryWrapper<ColumnEntity> objectQueryWrapper = new QueryWrapper<>();
         objectQueryWrapper.eq("column_code",columnCode);
+        if(UserEntity.USER_BELONG_TOP != companyId){
+            objectQueryWrapper.eq("company_id",companyId);
+        }
         List<ColumnEntity> columnEntities = this.baseMapper.selectList(objectQueryWrapper);
         if(CollUtil.isNotEmpty(columnEntities)){
             return Result.fail("栏目编码不能重复");
         }
         columnEntity.setColumnCode(columnCode);
         String remark = adminAddColumnDto.getRemark();
-        if(StrUtil.isNotEmpty(remark)){
             columnEntity.setRemark(remark);
-        }
         String pic = adminAddColumnDto.getPic();
-        if(StrUtil.isNotEmpty(pic)){
             columnEntity.setPic(pic);
-        }
         Integer orderNum = adminAddColumnDto.getOrderNum();
         if(ObjectUtil.isNotEmpty(orderNum)){
             columnEntity.setOrderNum(orderNum);
@@ -106,11 +110,22 @@
         Long parentId = adminAddColumnDto.getParentId();
         if(ObjectUtil.isEmpty(parentId)){
             columnEntity.setParentId(ColumnEntity.PARENTID_DEFAULT);
+            columnEntity.setPath("/" + columnEntity.getColumnCode());
         }else{
             columnEntity.setParentId(parentId);
+            ColumnEntity parent = this.baseMapper.selectById(parentId);
+            columnEntity.setPath(parent.getPath() + "/" + columnEntity.getColumnCode());
         }
+
+        columnEntity.setListTemplate(adminAddColumnDto.getListTemplate());
+        columnEntity.setArticleTemplate(adminAddColumnDto.getArticleTemplate());
+        columnEntity.setType(adminAddColumnDto.getType());
+        columnEntity.setTargetType(adminAddColumnDto.getTargetType());
+        columnEntity.setTargetUrl(adminAddColumnDto.getTargetUrl());
+        columnEntity.setIsNav(adminAddColumnDto.getIsNav());
+        columnEntity.setContentType(adminAddColumnDto.getContentType());
         this.baseMapper.insert(columnEntity);
-        return Result.ok("添加成功");
+        return Result.ok("添加成功", columnEntity.getId());
     }
 
     @Override
@@ -144,69 +159,98 @@
         adminSeeColumnInfoVo.setParentId(columnEntity.getParentId());
         adminSeeColumnInfoVo.setOrderNum(columnEntity.getOrderNum());
         adminSeeColumnInfoVo.setPic(columnEntity.getPic());
+        adminSeeColumnInfoVo.setListTemplate(columnEntity.getListTemplate());
+        adminSeeColumnInfoVo.setArticleTemplate(columnEntity.getArticleTemplate());
+        adminSeeColumnInfoVo.setIsNav(columnEntity.getIsNav());
+        adminSeeColumnInfoVo.setType(columnEntity.getType());
+        adminSeeColumnInfoVo.setTargetType(columnEntity.getTargetType());
+        adminSeeColumnInfoVo.setContentType(columnEntity.getContentType());
+
+        CompanyEntity companyEntity = companyMapper.selectById(LoginUserUtil.getCompanyId());
+
+        if (columnEntity.getType() == 2) {
+            if (columnEntity.getTargetType() == 1) {
+                ArticleEntity articleEntity = articleMapper.selectById(Long.parseLong(columnEntity.getTargetUrl()));
+                adminSeeColumnInfoVo.setTargetName(articleEntity.getTitle());
+                adminSeeColumnInfoVo.setUrl(companyEntity.getWebAddress() + columnEntity.getPath() + "/" + columnEntity.getTargetUrl() + ".html");
+            } else if (columnEntity.getTargetType() == 2) {
+                ColumnEntity column = this.baseMapper.selectByCodeAndCompanyId(columnEntity.getTargetUrl(), LoginUserUtil.getCompanyId());
+                adminSeeColumnInfoVo.setTargetName(column.getColumnName());
+                adminSeeColumnInfoVo.setUrl(companyEntity.getWebAddress() + column.getPath());
+            }
+        } else {
+            adminSeeColumnInfoVo.setUrl(companyEntity.getWebAddress() + columnEntity.getPath());
+        }
+        adminSeeColumnInfoVo.setTargetUrl(columnEntity.getTargetUrl());
         return Result.ok(adminSeeColumnInfoVo);
     }
 
     @Override
     @Transactional
     public Result updateColumn(AdminUpdateColumnDto adminUpdateColumnDto) {
-        Long id = adminUpdateColumnDto.getId();
-        if(ObjectUtil.isEmpty(id)){
-            return Result.fail("栏目不存在");
-        }
-        ColumnEntity columnEntity = this.baseMapper.selectById(id);
+        Long companyId = LoginUserUtil.getCompanyId();
+        ColumnEntity columnEntity = this.baseMapper.selectById(adminUpdateColumnDto.getId());
         if(ObjectUtil.isEmpty(columnEntity)){
             return Result.fail("栏目不存在");
         }
-        String columnName = adminUpdateColumnDto.getColumnName();
-        if(StrUtil.isEmpty(columnName)){
-            return Result.fail("请输入栏目名称");
-        }
-        columnEntity.setColumnName(columnName);
+
         String columnCode = adminUpdateColumnDto.getColumnCode();
-        if(StrUtil.isEmpty(columnCode)){
-            return Result.fail("请输入栏目编码");
+        ColumnEntity updateEntity = ColumnConversion.INSTANCE.updateDtoToEntity(adminUpdateColumnDto);
+
+        if (!columnEntity.getColumnCode().equals(columnCode)) {
+            ColumnEntity hasExist = this.baseMapper.selectByCodeAndCompanyId(columnCode, companyId);
+            if (ObjectUtil.isNotEmpty(hasExist)) {
+                return Result.fail("栏目编码不能重复");
+            }
         }
-        QueryWrapper<ColumnEntity> objectQueryWrapper = new QueryWrapper<>();
-        objectQueryWrapper.eq("column_code",columnCode);
-        List<ColumnEntity> columnEntities = this.baseMapper.selectList(objectQueryWrapper);
-        if(CollUtil.isNotEmpty(columnEntities) && columnEntities.size() > 1){
-            return Result.fail("栏目编码不能重复");
-        }
-        columnEntity.setColumnCode(columnCode);
-        String remark = adminUpdateColumnDto.getRemark();
-        if(StrUtil.isNotEmpty(remark)){
-            columnEntity.setRemark(remark);
-        }
-        String pic = adminUpdateColumnDto.getPic();
-        if(StrUtil.isNotEmpty(pic)){
-            columnEntity.setPic(pic);
-        }
-        Integer orderNum = adminUpdateColumnDto.getOrderNum();
-        if(ObjectUtil.isNotEmpty(orderNum)){
-            columnEntity.setOrderNum(orderNum);
-        }
+
         Long parentId = adminUpdateColumnDto.getParentId();
         if(ObjectUtil.isEmpty(parentId)){
-            columnEntity.setParentId(ColumnEntity.PARENTID_DEFAULT);
+            updateEntity.setParentId(ColumnEntity.PARENTID_DEFAULT);
+            updateEntity.setPath("/" + updateEntity.getColumnCode());
         }else{
-            columnEntity.setParentId(parentId);
+            updateEntity.setParentId(parentId);
+            ColumnEntity parent = this.baseMapper.selectById(parentId);
+            updateEntity.setPath(parent.getPath() + "/" + updateEntity.getColumnCode());
         }
-        this.baseMapper.updateById(columnEntity);
+
+        // 若编码进行了修改且该栏目为父栏目,则需要同步修改子栏目的path。
+        if (!columnEntity.getColumnCode().equals(columnCode)) {
+            if ("-1".equals(columnEntity.getBeforeColumnCode())) {
+                updateEntity.setBeforeColumnCode(columnEntity.getColumnCode());
+            }
+
+            if (updateEntity.getParentId() == 0L) {
+                List<ColumnEntity> childColumn = this.baseMapper.selectColumnByParentId(columnEntity.getId(), companyId, 2);
+                if (CollUtil.isNotEmpty(childColumn)) {
+                    for (ColumnEntity child : childColumn) {
+                        child.setPath(updateEntity.getPath() + "/" + child.getColumnCode());
+                        this.baseMapper.updateById(child);
+                    }
+                }
+            }
+        }
+
+        // 栏目修改时,需判断父级ID及栏目编码的修改。若栏目父级ID修改了,则需记录原父级ID,在发布时,需连同原父级栏目同时编译
+        if (!columnEntity.getParentId().equals(updateEntity.getParentId()) && columnEntity.getBeforeParentId() == -1) {
+            updateEntity.setBeforeParentId(columnEntity.getParentId());
+        }
+
+        this.baseMapper.updateById(updateEntity);
         return Result.ok("更新成功");
     }
 
     @Override
     public Result getColumnInList() {
         UserEntity userlogin = LoginUserUtil.getLoginUser();
-        long belongId = userlogin.getBelongId() == null ? 0L : userlogin.getBelongId();
-        List<AdminColumnVo> records = this.baseMapper.selectColumnInListByParentId(ColumnEntity.PARENTID_DEFAULT,belongId);
+        long companyId = userlogin.getCompanyId() == null ? UserEntity.USER_BELONG_TOP : userlogin.getCompanyId();
+        List<AdminColumnVo> records = this.baseMapper.selectColumnInListByParentId(ColumnEntity.PARENTID_DEFAULT,companyId);
         if(CollUtil.isNotEmpty(records)){
             for(AdminColumnVo adminColumnVo : records){
                 Long id = adminColumnVo.getId();
                 QueryWrapper<ColumnEntity> objectQueryWrapper = new QueryWrapper<>();
                 objectQueryWrapper.eq("parent_id",id);
-                objectQueryWrapper.eq("belong_id",belongId);
+                objectQueryWrapper.eq("company_id",companyId);
                 List<ColumnEntity> columnEntities = this.baseMapper.selectList(objectQueryWrapper);
                 List<AdminColumnVo> adminColumnVoChilds = new ArrayList<>();
                 if(CollUtil.isNotEmpty(columnEntities)){
@@ -218,6 +262,7 @@
                         child.setRemark(columnEntityChild.getRemark());
                         child.setPic(columnEntityChild.getPic());
                         child.setOrderNum(columnEntityChild.getOrderNum());
+                        child.setContentType(columnEntityChild.getContentType());
                         adminColumnVoChilds.add(child);
                     }
                 }
@@ -243,4 +288,46 @@
         }
         return Result.ok("删除成功");
     }
+
+    @Override
+    public Result getWebColumnInList(HttpServletRequest request) {
+        String website = request.getHeader(AppContants.WEBSITE_HEADER);
+        Long companyId = getCompanyIdFromWebsite(website);
+        List<WebColumnVo> records = this.baseMapper.selectWebColumnInListByParentId(ColumnEntity.PARENTID_DEFAULT,companyId);
+        if(CollUtil.isNotEmpty(records)){
+            for(WebColumnVo webColumnVo : records){
+                Long id = webColumnVo.getId();
+                QueryWrapper<ColumnEntity> objectQueryWrapper = new QueryWrapper<>();
+                objectQueryWrapper.eq("parent_id",id);
+                objectQueryWrapper.eq("company_id",companyId);
+                List<ColumnEntity> columnEntities = this.baseMapper.selectList(objectQueryWrapper);
+                List<WebColumnVo> adminColumnVoChilds = new ArrayList<>();
+                if(CollUtil.isNotEmpty(columnEntities)){
+                    for(ColumnEntity columnEntityChild : columnEntities){
+                        WebColumnVo child = new WebColumnVo();
+                        child.setId(columnEntityChild.getId());
+                        child.setColumnName(columnEntityChild.getColumnName());
+                        child.setColumnCode(columnEntityChild.getColumnCode());
+                        adminColumnVoChilds.add(child);
+                    }
+                }
+                webColumnVo.setChild(adminColumnVoChilds);
+            }
+        }
+        return Result.ok(records);
+    }
+
+    private Long getCompanyIdFromWebsite(String website){
+        List<CompanyEntity> companyEntities = companyMapper.selectList(new QueryWrapper<>());
+        Long companyId = 0L;
+        if(CollUtil.isNotEmpty(companyEntities)){
+            for(CompanyEntity companyEntity : companyEntities){
+                boolean contains = StrUtil.contains(companyEntity.getWebAddress(), website);
+                if(contains){
+                    companyId = companyEntity.getId();
+                }
+            }
+        }
+        return companyId;
+    }
 }

--
Gitblit v1.9.1