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

com.ape9527.core.service.BaseObjService Maven / Gradle / Ivy

The newest version!
package com.ape9527.core.service;

import com.ape9527.core.entity.BaseObj;
import com.ape9527.core.mapper.BaseObjFieldMapper;
import com.ape9527.core.mapper.BaseObjMapper;
import com.ape9527.core.model.AjaxResult;
import com.ape9527.utils.string.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 基础数据对象操作业务层
 *
 * @author YuanShuai[[email protected]]
 */
@Service
public class BaseObjService {

    private final BaseObjMapper mapper;

    private final BaseObjFieldService baseObjFieldService;

    public BaseObjService(BaseObjMapper mapper, BaseObjFieldService baseObjFieldService) {
        this.mapper = mapper;
        this.baseObjFieldService = baseObjFieldService;
    }

    /**
     * 查询数据对象列表
     *
     * @param baseObj 参数
     * @return 数据对象列表
     */
    public List queryList(BaseObj baseObj) {
        return mapper.selectList(baseObj);
    }

    /**
     * 分页查询数据对象列表
     *
     * @param baseObj 参数
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 数据对象列表分页对象
     */
    public PageInfo queryListByPage(BaseObj baseObj, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List baseObjs = queryList(baseObj);
        return new PageInfo<>(baseObjs);
    }

    /**
     * 根据ID查询数据对象
     *
     * @param uuid 数据对象唯一ID
     * @return 数据对象
     */
    public BaseObj queryById(String uuid) {
        return mapper.selectById(uuid);
    }

    /**
     * 保存数据对象
     * 数据对象名称和编码均不允许重复
     *
     * @param baseObj 数据对象
     * @return 是否保存成功
     */
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult save(BaseObj baseObj) {
        int i;
        // 根据数据对象编码在表中查询记录
        BaseObj dbBaseObj = mapper.selectByObjCode(baseObj.getObjCode());
        if(StringUtil.isEmpty(baseObj.getUuid())){
            // 没查到即可新增,查到视为重复
            if(dbBaseObj != null){
                return AjaxResult.error("数据对象编码不允许重复," + baseObj.getObjCode() + "已存在");
            }
            i = mapper.insert(baseObj);
            // 初始化数据对象属性 和 数据表
            baseObjFieldService.initBaseObjField(baseObj);
        }else{
            if(dbBaseObj!= null && !dbBaseObj.getUuid().equals(baseObj.getUuid())){
                return AjaxResult.error("数据对象编码不允许重复," + baseObj.getObjCode() + "已存在");
            }
            final BaseObj oldBaseObj = mapper.selectById(baseObj.getUuid());
            i = mapper.update(baseObj);
            if(!oldBaseObj.getObjCode().equals(baseObj.getObjCode())){
                // 变更数据对象属性中的数据对象编码
                baseObjFieldService.editObjCode(oldBaseObj.getObjCode(), baseObj.getObjCode());
                // 变更表名
                mapper.updateTableName(oldBaseObj.getObjCode(), baseObj.getObjCode());
            }
            if(!oldBaseObj.getObjName().equals(baseObj.getObjName())){
                // 变更表注释
                mapper.updateTableCommnt(baseObj.getObjCode(), baseObj.getObjName());
            }
        }
        return i == 1 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 根据ID对数据对象进行逻辑删除
     *
     * @param uuid 数据对象唯一ID
     * @return 是否删除成功
     */
    public boolean delById(String uuid) {
        String[] uuids = {uuid};
        return delByIds(uuids);
    }

    /**
     * 根据ID数组对多个数据对象进行逻辑删除
     *
     * @param uuids 数据对象唯一ID
     * @return 是否删除成功
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean delByIds(String[] uuids) {
        List objCodes = mapper.selectCodeBatch(uuids);
        mapper.dropTables(objCodes);
        baseObjFieldService.delByObjCodes(objCodes);
        return mapper.updateDelByIds(uuids) == uuids.length;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy