All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.mingsoft.cms.aop.CategoryAop Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (c) 2012-present 铭软科技(mingsoft.net)
 * 本软件及相关文档文件(以下简称“软件”)的版权归 铭软科技 所有
 * 遵循 铭软科技《服务协议》中的《保密条款》
 */



package net.mingsoft.cms.aop;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import net.mingsoft.base.constant.Const;
import net.mingsoft.base.entity.ResultData;
import net.mingsoft.base.exception.BusinessException;
import net.mingsoft.base.util.BundleUtil;
import net.mingsoft.basic.util.BasicUtil;
import net.mingsoft.cms.constant.e.CategoryDisplayEnum;
import net.mingsoft.cms.constant.e.CategoryIsSearchEnum;
import net.mingsoft.cms.constant.e.CategoryTypeEnum;
import net.mingsoft.cms.dao.ICategoryDao;
import net.mingsoft.cms.dao.IContentDao;
import net.mingsoft.cms.entity.CategoryEntity;
import net.mingsoft.cms.entity.ContentEntity;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 增加移动审批权限
 */

@Aspect
@Component("CategoryAop")
public class CategoryAop extends net.mingsoft.basic.aop.BaseAop {


    @Value("${ms.diy.html-dir:html}")
    private String htmlDir;

    @Autowired
    private ICategoryDao categoryDao;

    @Autowired
    private IContentDao contentDao;

    @Pointcut("execution(* net.mingsoft.cms.action.CategoryAction.delete(..)) ")
    public void delete() {
    }

    /**
     * 栏目保存接口切面
     */
    @Pointcut("execution(* net.mingsoft.cms.action.CategoryAction.save(..)) ")
    public void save() {}

    /**
     * 栏目更新接口切面
     */
    @Pointcut("execution(* net.mingsoft.cms.action.CategoryAction.update(..)) ")
    public void update() {}



    @Around("save() || update()")
    public ResultData move(ProceedingJoinPoint pjp) throws Throwable {
        CategoryEntity category = getType(pjp, CategoryEntity.class);
        if (category == null) {
            throw new BusinessException("栏目不存在!");
        }

        // 获取返回值
        Object obj = pjp.proceed(pjp.getArgs());
        ResultData resultData = JSONUtil.toBean(JSONUtil.toJsonStr(obj), ResultData.class);
        category = resultData.getData(CategoryEntity.class);

        if (category == null) {
            return resultData;
        }

        // 如果栏目被设置为不显示,将栏目下所有子栏目也设置为不显示
        if (CategoryDisplayEnum.DISABLE.toString().equalsIgnoreCase(category.getCategoryDisplay())){
            List ids = categoryDao.queryChildren(category).stream().map(CategoryEntity::getId).collect(Collectors.toList());
            LambdaUpdateWrapper wrapper = new UpdateWrapper().lambda();
            wrapper.set(CategoryEntity::getCategoryDisplay,CategoryDisplayEnum.DISABLE.toString());
            wrapper.in(CategoryEntity::getId,ids);
            categoryDao.update(null,wrapper);

        }

        // 如果栏目被设置为不被搜索,将栏目下所有子栏目也设置为不被搜索
        if (CategoryIsSearchEnum.DISABLE.toString().equalsIgnoreCase(category.getCategoryIsSearch())){
            List ids = categoryDao.queryChildren(category).stream().map(CategoryEntity::getId).collect(Collectors.toList());
            LambdaUpdateWrapper wrapper = new UpdateWrapper().lambda();
            wrapper.set(CategoryEntity::getCategoryIsSearch,CategoryIsSearchEnum.DISABLE.toString());
            wrapper.in(CategoryEntity::getId,ids);
            categoryDao.update(null,wrapper);

        }

        CategoryEntity parent = categoryDao.selectById(category.getCategoryId());
        if (parent == null) {
            return resultData;
        }

        // 用于判断父级栏目之前是否是子栏目
        // 只有父节点之前为子节点 && 父栏目类型为列表 && 子栏目为列表
        boolean flag = !parent.getLeaf() && StringUtils.equals(parent.getCategoryType(), CategoryTypeEnum.LIST.toString());
        if (flag) {
            // 将父栏目的内容模板清空
            parent.setCategoryUrl("");
            categoryDao.updateById(parent);
            CategoryEntity returnCategory = JSONUtil.toBean(resultData.get(ResultData.DATA_KEY).toString(), CategoryEntity.class);
            // 获取父栏目ID集合
            String categoryIds = StringUtils.isEmpty(parent.getCategoryParentIds())
                    ? returnCategory.getId() : parent.getCategoryParentIds() + "," + returnCategory.getId();
            if (!StringUtils.equals(returnCategory.getCategoryType(), CategoryTypeEnum.LIST.toString())) {
                // 如果子栏目不为列表,将直接删除父栏目下的文章
                LambdaQueryWrapper contentDeleteWrapper = new QueryWrapper().lambda();
                contentDeleteWrapper.eq(ContentEntity::getCategoryId, parent.getId());
                List list = contentDao.selectList(contentDeleteWrapper);
                if (CollectionUtil.isNotEmpty(list)) {
                    String[] ids = ArrayUtil.toArray(list.stream().map(ContentEntity::getId).collect(Collectors.toList()), String.class);
                    contentDao.delete(ids);
                }
            }
            // 将父栏目下的文章移动到子栏目下
            LambdaUpdateWrapper contentWrapper = new UpdateWrapper().lambda();
            contentWrapper.set(ContentEntity::getCategoryId, returnCategory.getId());
            contentWrapper.eq(ContentEntity::getCategoryId, parent.getId());
            contentDao.update(new ContentEntity(), contentWrapper);

            return resultData;
        }
        return resultData;
    }


    /**
     * 删除栏目后并删除文章对应的静态化文件
     *
     * @param jp
     */
    @After("delete()")
    public void delete(JoinPoint jp) {
        List categoryEntities = (List) getJsonParam(jp);
        // appDir
        String appDir = BasicUtil.getApp().getAppDir() + File.separator;
        for (CategoryEntity categoryEntity : categoryEntities) {
            // 删除静态文件
            deleteCategoryHtml(appDir + categoryEntity.getTypelink());
        }
    }


    /**
     * @param categoryLink 栏目链接
     *                     删除栏目静态文件
     */
    public void deleteCategoryHtml(String categoryLink) {
        // 过滤非法路径
        if (StringUtils.isEmpty(categoryLink) || categoryLink.contains("../") || categoryLink.contains("..\\")) {
            LOG.error("非法路径:{}", categoryLink);
            throw new BusinessException(BundleUtil.getString(Const.RESOURCES,"err.error",BundleUtil.getString(net.mingsoft.basic.constant.Const.RESOURCES,"file.path")));
        }
        // html真实路径
        String htmlPath = BasicUtil.getRealPath(htmlDir);
        // 删除静态文件
        // 文件夹路径组成 html真实路径 + 栏目路径
        boolean flag = FileUtil.del(htmlPath
                + File.separator + categoryLink
        );
        if (flag) {
            LOG.info("删除静态文件夹成功!");
        } else {
            LOG.info("删除失败!");
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy