MaterialServiceImpl.java 7.89 KB
package com.bs.reception.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bs.common.utils.SecurityUtils;
import com.bs.reception.entity.materials.Material;
import com.bs.reception.mapper.MaterialMapper;
import com.bs.reception.model.materials.HomepageMaterialVO;
import com.bs.reception.service.IMaterialService;
import com.bs.reception.model.materials.MaterialPageQuery;
import com.bs.reception.model.materials.MaterialPageVO;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MaterialServiceImpl extends ServiceImpl<MaterialMapper, Material> implements IMaterialService {
    @Override
    /** 查询首页展示素材(含业务类型与文件夹信息) */
    public List<HomepageMaterialVO> listHomepageMaterials() {
        return this.baseMapper.selectHomepageMaterials();
    }

    @Override
    public Page<MaterialPageVO> pageMaterials(MaterialPageQuery query) {
        Integer pageNum = query.getPageNum() == null || query.getPageNum() < 1 ? 1 : query.getPageNum();
        Integer pageSize = query.getPageSize() == null || query.getPageSize() < 1 ? 10 : query.getPageSize();
        Page<MaterialPageVO> page = new Page<>(pageNum, pageSize);
        java.util.List<MaterialPageVO> rows = this.baseMapper.selectMaterialPage(page, query);
        page.setRecords(rows);
        return page;
    }

    @Override
    public List<Material> getMaterialList(Material body) {
        LambdaQueryWrapper<Material> qw = new LambdaQueryWrapper<>();
        qw.eq(body.getTypeCode() != null, Material::getTypeCode, body.getTypeCode())
                .eq(body.getFolderCode() != null, Material::getFolderCode, body.getFolderCode())
                .like(body.getMaterialName() != null && !body.getMaterialName().isBlank(), Material::getMaterialName, body.getMaterialName())
                .eq(body.getShowOnHomepage() != null, Material::getShowOnHomepage, body.getShowOnHomepage())
                .eq(body.getStatus() != null, Material::getStatus, body.getStatus())
                .eq(Material::getDelFlag, 0)
                .orderByAsc(Material::getSortOrder);
        return this.list(qw);
    }

    @Override
    public Material createMaterial(Material body) {
        if (body.getMaterialName() == null || body.getMaterialName().isBlank()) {
            throw new RuntimeException("素材名称不能为空");
        }
        if (body.getTypeCode() == null || body.getTypeCode().isBlank()) {
            throw new RuntimeException("业务类型编码不能为空");
        }
        if (body.getFolderCode() == null || body.getFolderCode().isBlank()) {
            throw new RuntimeException("文件夹编码不能为空");
        }
        if (body.getMaterialUrl() == null || body.getMaterialUrl().isBlank()) {
            throw new RuntimeException("素材URL不能为空");
        }
        if (body.getShowOnHomepage() == null) {
            throw new RuntimeException("是否首页展示不能为空");
        }

        body.setId(null);
        body.setMaterialCode("MC" + System.currentTimeMillis());
        // 校验素材名称是否已存在(同业务类型、同文件夹下)
        LambdaQueryWrapper<Material> qw = new LambdaQueryWrapper<>();
        qw.eq(Material::getTypeCode, body.getTypeCode())
                .eq(Material::getFolderCode, body.getFolderCode())
                .eq(Material::getMaterialName, body.getMaterialName())
                .eq(Material::getDelFlag, 0);
        List<Material> list = this.list(qw);
        if (list != null && !list.isEmpty()) {
            throw new RuntimeException("素材名称已存在");
        }
        body.setCreateBy(SecurityUtils.getUserName());
        body.setCreateById(SecurityUtils.getUserId());
        body.setCreateTime(new java.util.Date());
        this.save(body);
        return body;
    }

    @Override
    public boolean updateMaterial(Material body) {
        if (body.getId() == null) {
            throw new RuntimeException("ID不能为空");
        }
        if (body.getMaterialName() == null || body.getMaterialName().isBlank()) {
            throw new RuntimeException("素材名称不能为空");
        }
        if (body.getTypeCode() == null || body.getTypeCode().isBlank()) {
            throw new RuntimeException("业务类型编码不能为空");
        }
        if (body.getFolderCode() == null || body.getFolderCode().isBlank()) {
            throw new RuntimeException("文件夹编码不能为空");
        }
        if (body.getMaterialUrl() == null || body.getMaterialUrl().isBlank()) {
            throw new RuntimeException("素材URL不能为空");
        }
        if (body.getShowOnHomepage() == null) {
            throw new RuntimeException("是否首页展示不能为空");
        }
        body.setMaterialCode(null);
        // 校验素材名称是否已存在(同业务类型、同文件夹下)
        LambdaQueryWrapper<Material> qw = new LambdaQueryWrapper<>();
        qw.eq(Material::getTypeCode, body.getTypeCode())
                .eq(Material::getFolderCode, body.getFolderCode())
                .eq(Material::getMaterialName, body.getMaterialName())
                .ne(Material::getId, body.getId())
                .eq(Material::getDelFlag, 0);
        List<Material> list = this.list(qw);
        if (list != null && !list.isEmpty()) {
            throw new RuntimeException("素材名称已存在");
        }
        body.setUpdateBy(SecurityUtils.getUserName());
        body.setUpdateById(SecurityUtils.getUserId());
        body.setUpdateTime(new java.util.Date());
        return this.updateById(body);
    }

    @Override
    public boolean setMaterialSort(Material body) {
        if (body.getId() == null) {
            throw new RuntimeException("ID不能为空");
        }
        if (body.getSortOrder() == null) {
            throw new RuntimeException("排序不能为空");
        }
        return this.update(new LambdaUpdateWrapper<Material>()
                .eq(Material::getId, body.getId())
                .set(Material::getUpdateBy, SecurityUtils.getUserName())
                .set(Material::getUpdateById, SecurityUtils.getUserId())
                .set(Material::getUpdateTime, new java.util.Date())
                .set(Material::getSortOrder, body.getSortOrder()));
    }

    @Override
    public boolean setMaterialHomepage(Material body) {
        if (body.getId() == null) {
            throw new RuntimeException("ID不能为空");
        }
        if (body.getShowOnHomepage() == null) {
            throw new RuntimeException("是否首页展示不能为空");
        }
        return this.update(new LambdaUpdateWrapper<Material>()
                .eq(Material::getId, body.getId())
                .set(Material::getUpdateBy, SecurityUtils.getUserName())
                .set(Material::getUpdateById, SecurityUtils.getUserId())
                .set(Material::getUpdateTime, new java.util.Date())
                .set(Material::getShowOnHomepage, body.getShowOnHomepage()));
    }

    @Override
    public boolean deleteMaterial(Material body) {
        if (body.getId() == null) {
            throw new RuntimeException("ID不能为空");
        }
        return this.update(new LambdaUpdateWrapper<Material>()
                .eq(Material::getId, body.getId())
                .set(Material::getUpdateTime, new java.util.Date())
                .set(Material::getUpdateBy, SecurityUtils.getUserName())
                .set(Material::getUpdateById, SecurityUtils.getUserId())
                .set(Material::getDelFlag, 1));
    }

    @Override
    public Material getMaterialDetail(Material body) {
        if (body.getId() == null) {
            throw new RuntimeException("ID不能为空");
        }
        return this.getById(body.getId());
    }
}