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

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

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

import com.ape9527.core.constant.Const;
import com.ape9527.core.entity.BaseObj;
import com.ape9527.core.entity.BaseObjField;
import com.ape9527.core.mapper.BaseObjFieldMapper;
import com.ape9527.core.mapper.BaseObjMapper;
import com.ape9527.core.model.AjaxResult;
import com.ape9527.core.model.base.CorrelatedBaseObj;
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.ArrayList;
import java.util.List;
import java.util.Map;

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

    private final BaseObjFieldMapper mapper;

    public BaseObjFieldService(BaseObjFieldMapper mapper) {
        this.mapper = mapper;
    }

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

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

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

    /**
     * 保存数据对象属性
     * 数据对象编码相同时,属性编码不允许重复
     *
     * @param field 数据对象属性
     * @return 是否保存成功
     */
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult save(BaseObjField field) {
        int i;
        BaseObjField dbBaseObjField = mapper.selectByFieldCode(field.getObjCode(), field.getFieldCode());
        if (StringUtil.isEmpty(field.getUuid())) {
            if (dbBaseObjField != null) {
                return AjaxResult.error("数据对象属性编码不允许重复," + field.getObjCode() + "-" + field.getFieldCode() + "已存在");
            }
            field.setIsBaseObj(Const.BASE_OBJ.equals(field.getFieldType()));
            i = mapper.insert(field);
            // 新增表字段
            if (Const.BASE_OBJ.equals(field.getFieldType())) {
                field.setFieldType("varchar");
                field.setFieldLength("32");
                field.setDecimalDigits("0");
                field.setDefaultValue("");
                field.setIsBaseObj(true);
            }
            mapper.insertColumn(field);
        } else {
            if (dbBaseObjField != null && !dbBaseObjField.getUuid().equals(field.getUuid())) {
                return AjaxResult.error("数据对象属性编码不允许重复," + field.getObjCode() + "-" + field.getFieldCode() + "已存在");
            }
            final BaseObjField oldField = mapper.selectById(field.getUuid());
            field.setIsBaseObj(Const.BASE_OBJ.equals(field.getFieldType()));
            i = mapper.update(field);
            // 修改表字段
            if (Const.BASE_OBJ.equals(field.getFieldType())) {
                field.setFieldType("varchar");
                field.setFieldLength("32");
                field.setDecimalDigits("0");
                field.setDefaultValue("");
                field.setIsBaseObj(true);
            }
            mapper.updateColumn(oldField.getFieldCode(), field);
        }
        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> codes = mapper.selectCodeBatch(uuids);
        mapper.dropColumns(codes);
        return mapper.updateDelByIds(uuids) == uuids.length;
    }

    /**
     * 初始化数据对象公共基本属性
     *
     * @param baseObj 数据对象
     */
    public void initBaseObjField(BaseObj baseObj) {
        String[] fieldName = {"UUID", "创建时间", "更新时间", "删除标志"};
        String[] fieldCode = {"uuid", "create_time", "update_time", "del"};
        String[] fieldType = {"varchar", "datetime", "datetime", "tinyint"};
        String[] fieldLength = {"32", "0", "0", "0"};
        boolean[] notNull = {true, true, false, true};
        boolean[] primaryKey = {true, false, false, false};
        boolean[] unique = {true, false, false, false};
        String[] defaultValue = {null, null, null, "0"};

        List baseObjFields = new ArrayList<>();
        for (int i = 0; i < fieldName.length; i++) {
            BaseObjField field = new BaseObjField();
            field.setObjCode(baseObj.getObjCode());
            field.setFieldName(fieldName[i]);
            field.setFieldCode(fieldCode[i]);
            field.setFieldType(fieldType[i]);
            field.setFieldLength(fieldLength[i]);
            field.setNotNull(notNull[i]);
            field.setPrimaryKey(primaryKey[i]);
            field.setUnique(unique[i]);
            field.setDefaultValue(defaultValue[i]);
            baseObjFields.add(field);
        }
        mapper.insertBatch(baseObjFields);
        mapper.initTable(baseObj, baseObjFields);
    }

    /**
     * 变更数据对象属性所属数据对象
     *
     * @param oldObjCode 旧的数据对象编码
     * @param objCode    新的数据对象编码
     */
    public void editObjCode(String oldObjCode, String objCode) {
        mapper.updateObjCodeByObjCode(oldObjCode, objCode);
    }

    /**
     * 根据数据对象编码对数据对象属性进行逻辑删除
     *
     * @param objCodes 数据对象编码集合
     */
    public void delByObjCodes(List objCodes) {
        mapper.updateDelByObjCodes(objCodes);
    }

    /**
     * 根据数据对象编码查询数据对象属性编码(不包含关联对象属性,即属性类型为base_obj的属性)
     *
     * @param objectCode 数据对象编码
     * @return 数据对象属性编码
     */
    public List queryFieldCodes(String objectCode) {
        return mapper.selectFieldCodes(objectCode);
    }

    /**
     * 查询关联数据对象属性
     *
     * @param objectCode
     * @return
     */
    public List queryCorrelatedBaseObj(String objectCode) {
        return mapper.selectCorrelatedBaseObj(objectCode);
    }

    /**
     * 判断当前数据对象是否存在“dept_code”属性
     *
     * @param objectCode
     * @return
     */
    public boolean existDeptCode(String objectCode) {
        List fieldCodes = mapper.selectFieldCodes(objectCode);
        return fieldCodes.contains("dept_code");
    }

    /**
     * 查询标记“数据唯一”的属性
     *
     * @param objectCode
     * @return
     */
    public List queryUniqueFields(String objectCode) {
        return mapper.selectUniqueFields(objectCode);
    }

    /**
     * 根据数据对象编码和属性编码查询属性名称
     *
     * @param objectCode
     * @param fieldCode
     * @return
     */
    public String queryFieldName(String objectCode, String fieldCode) {
        return mapper.selectFieldName(objectCode, fieldCode);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy