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

group.rober.dataform.service.DataFormService Maven / Gradle / Ivy

There is a newer version: 3.2.2
Show newest version
package group.rober.dataform.service;

import group.rober.dataform.autoconfigure.DataFormProperties;
import group.rober.dataform.exception.DataFormException;
import group.rober.dataform.mapper.DataFormMapper;
import group.rober.dataform.model.DataForm;
import group.rober.dataform.model.DataFormElement;
import group.rober.dataform.model.types.ElementDataEditStyle;
import group.rober.runtime.kit.*;
import group.rober.runtime.lang.PairBond;
import group.rober.sql.converter.impl.UnderlinedNameConverter;
import group.rober.sql.core.SqlQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * DataForm功能实现
 * Created by tisir [email protected] on 2017-05-30
 */
@Service
public class DataFormService {
    @Autowired
    private DataFormMapper dataFormMapper;
    @Autowired
    private DataFormProperties dataFormProperties;

    public DataFormMapper getDataFormMapper() {
        return dataFormMapper;
    }

    public void setDataFormMapper(DataFormMapper dataFormMapper) {
        this.dataFormMapper = dataFormMapper;
    }

    public void clearCacheAll() {
        dataFormMapper.clearCacheAll();
    }

    public void clearCacheItem(String formId) {
        dataFormMapper.clearCacheItem(formId);
    }

    private boolean fieldExists(String field,DataForm dataForm){
        List elements = dataForm.getElements();
        for(DataFormElement element : elements){
            if(field.equalsIgnoreCase(element.getCode())||field.equalsIgnoreCase(element.getColumn())){
                return true;
            }
        }
        return false;
    }

    private void touchMustElement(DataForm dataForm,String field){
        //添加元素
        DataFormElement newElement = new DataFormElement();
        DataFormElement.FormElementUIHint newUIHint = newElement.getElementUIHint();
        newElement
                .setDataformId(dataForm.getId())
                .setName(field)
                .setCode(field)
                .setColumn(StringKit.camelToUnderline(field))
                .setGroup("")
                .setGroupI18nCode("")
                .setSubGroup("")
                .setSubGroupI18nCode("")
                .setSortCode("ZZ")
                .setTable(null)
                .setUpdateable(false)
                .setPersist(false)
        ;
        newUIHint
                .setEditStyle(ElementDataEditStyle.Label)
                .setReadonly(true)
                .setReading(true)
                .setVisible(false)
                .setHtmlStyle("")
        ;

        newElement.setElementUIHint(newUIHint);
        dataForm.getElements().add(newElement);

        //补一下SQLQuery对象
        SqlQuery query = dataForm.getQuery();
        List items = query.getSelectItems();
        if(items == null || items.isEmpty())return;
        PairBond pairBond = new PairBond();
        pairBond.setLeft(field);
        pairBond.setRight(StringKit.camelToUnderline(field));
        items.add(pairBond);


    }
    /**
     * 自动补充必需要填充的字段
     *
     * @param dataForm dataForm
     */
    public void touchMustTouchFields(DataForm dataForm){
        String dataFormId = dataForm.getId();
        Map mustTouchFields = dataFormProperties.getMustTouchFields();
        if(mustTouchFields == null || mustTouchFields.isEmpty())return;
        Iterator> iterator = mustTouchFields.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry entry = iterator.next();
            Pattern pattern = Pattern.compile(entry.getKey());
            //如果显示模板匹配上了,则主动添加字段
            if(pattern.matcher(dataFormId).find()){
                String strFields = entry.getValue();
                if(StringKit.isBlank(strFields))continue;
                String[] fields = strFields.split(",");
                for(String field : fields){
                    field = StringKit.trim(field);
                    if(StringKit.isBlank(field))continue;
                    boolean exists = fieldExists(field,dataForm);
                    if(!exists){
                        touchMustElement(dataForm,field);
                    }
                }
            }
        }

    }
    /**
     * 仅取显示模板目录列表
     *
     * @return list
     */
    public List getBriefDataFormList(){
        List dataForms = dataFormMapper.getAllDataForms();
        return dataForms.stream().filter(dataForm -> StringKit.isNotBlank(dataForm.getId())).collect(Collectors.toList());
    }

    public DataForm getDataForm(String dataform){
        int lastDotIdx = dataform.lastIndexOf("-"); //URL路径中,多个点号传输会出问题
        DataForm dataForm = null;
        if(lastDotIdx>0){
            String pack = dataform.substring(0,lastDotIdx);
            String formId = dataform.substring(lastDotIdx+1);
            dataForm = dataFormMapper.getDataForm(pack,formId);
        }else{
            dataForm = dataFormMapper.getDataForm("",dataform);
        }
        ValidateKit.notNull(dataForm, "DataForm不存在,form=" + dataform);

        fillSelectedItems(dataForm);

        return dataForm;
    }

    /**
     * 根据显示模板列出的字段,填写select语句
     *
     * @param dataForm dataform
     */
    private void fillSelectedItems(DataForm dataForm){
        SqlQuery query = dataForm.getQuery();
        List items = query.getSelectItems();
        if(items==null) items = new ArrayList();
        query.setSelectItems(items);
        if(items.size()>0)return ;
        List elements = dataForm.getElements();
        UnderlinedNameConverter converter = new UnderlinedNameConverter();

        Set scannedCols = new HashSet();
        for(DataFormElement element : elements){
            String column = element.getColumn();
            String columnFixed = column ;
            String alias = element.getCode();
            if(StringKit.isBlank(column)&&StringKit.isBlank(alias))continue;

            //如果是虚字段,并且字段别名不为空的情况下,把虚字段AS一下
            if(SQLKit.isConstColumn(column)&&StringKit.isNotBlank(alias)){
                column = String.join(" AS ",column,StringKit.camelToUnderline(alias));
            //如果字段名重复,也要作AS,否则,在select * (Name,Name 。。。)时,会出SQL语法错误
            }else{
                if(StringKit.isBlank(column)){
                    column = converter.getColumnName(alias);
                }
                if (!StringKit.isBlank(element.getTable()))  {
                    column = String.join(".",element.getTable(),column);
                }
                //名称重复字段的处理
                if(scannedCols.contains(columnFixed) && StringKit.isNotBlank(alias)){
                    column = String.join(" AS ",column,StringKit.camelToUnderline(alias));
                }

                if(StringKit.isBlank(alias)){
                    alias = converter.getPropertyName(column);
                }
                //如果字段是另一个select语句,则处理一下
                if(SQLKit.isSelect(column)){
                    String lowerColumn = column.toLowerCase();
                    int asIndex = lowerColumn.lastIndexOf(") AS ");
                    if(asIndex>0){
                        throw new DataFormException("字段{0}是一个子查询,请不要使用[(select * from table_name) AS column],直接使用[select count(1) from table_name]即可",alias);
                    }
                    column = StringKit.format("({0}) AS {1}",column,StringKit.camelToUnderline(alias));
                }
            }
            items.add(new PairBond(alias,column));
            scannedCols.add(columnFixed);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy