${package}

import com.fastchar.core.FastAction;
import com.fastchar.core.FastFile;
import com.fastchar.database.FastPage;
import com.fastchar.utils.FastStringUtils;

import java.util.List;
import java.util.Map;

${imports}


/**
 * ${markName} 控制器
 * @author ${author}
 * @date ${datetime}
 */
public class ${actionName} extends FastAction {
    @Override
    protected String getRoute() {
        return "${actionRoute}";
    }

    /**
     * 添加或修改${markName}数据
     * 参数：
     * __batch 是否批量操作 {Boolean} 如果true，则以下所有参数必须以 data[i]. 为前缀，其中 i 是可变的下标数字
     * ${addParamOption}
     */
    public void pull() throws Exception {
        boolean batch = getParamToBoolean("__batch");
        if(batch){
            List<${entityName}> entityList = getParamToEntityList("data", ${entityName}.class);
            for (${entityName} entity : entityList) {
                doPush(entity,true);
            }
            responseJson(0, "操作成功！", entityList);
        }else{
            ${entityName} entity = ${entityName}.newInstance();
            entity.setAll(getParamToMap());
            doPush(entity,false);
            responseJson(-1, "操作失败！" + entity.getError());
        }
    }


     private void doPush(${entityName} entity, boolean batch) throws Exception {
            ${pullOptions}
            if (entity.isEmpty("${primaryKey}")) {
                if (entity.save() && !batch) {
                    responseJson(0, "发布成功！", entity);
                } else if (!batch) {
                    responseJson(-1, "发布失败！" + entity.getError());
                }
            } else {
                if (entity.update() && !batch) {
                    responseJson(0, "更新成功！", entity);
                } else if (!batch) {
                    responseJson(-1, "更新失败！" + entity.getError(), entity);
                }
            }
     }


    /**
     * 删除${markName}数据
     * 参数：
     * ${primaryKey} 数据Id {Array}
     */
    public void del(){
        Integer[] ${primaryKey}s = getParamToIntArray("${primaryKey}", true);
        for (Integer ${primaryKey} : ${primaryKey}s) {
            ${entityName}.dao().deleteById(${primaryKey});
        }
        responseJson(0, "删除成功！");
    }


    /**
     * 获取${markName}数据详情
     * 参数：
     * ${primaryKey} 数据Id {int}
     */
    public void info() {
        int ${primaryKey} = getParamToInt("${primaryKey}", true);
        ${entityName} details = ${entityName}.dao().getDetails(${primaryKey});
        if (details != null) {
            responseJson(0, "获取成功！", details);
        }
        responseJson(-1, "获取失败！数据不存在！");
    }


    /**
     * 获取${markName}数据列表
     * 参数：
     * page 页数
     * pageSize 每页大小，默认20
     * ${whereParamOption}
     * ${sortParamOption}
     */
    public void list() {
        int page = getParamToInt("page", true);
        int pageSize = getParamToInt("pageSize", 20);

        Map<String, Object> where = getParamToMap("where.");
        Map<String, Object> sort = getParamToMap("sort.");

        FastPage<${entityName}> list = ${entityName}.dao().getList(page, pageSize, where,sort);
        responseJson(0, "获取成功！", list);
    }

    /**
     * 统计${markName}数据
     * 参数：
     * ${whereParamOption}
     */
    public void count() {
        ${entityName} entity = ${entityName}.newInstance();
        entity.setAll(getParamToMap("where."));
        responseJson(0, "统计成功！", entity.count());
    }
}

=========
            FastFile<?> ${fileColumnName} = getParamFile(entityPrefix+"${fileColumnName}");
            if (${fileColumnName} != null) {
                entity.set("${fileColumnName}", ${fileColumnName}.getUrl());
            }
=========
            List<String> ${fileColumnName}Urls = new ArrayList<>();
            if (isParamExists(entityPrefix+"${fileColumnName}")) {
                String[] ${fileColumnName}Values = getParamToArray(entityPrefix+"${fileColumnName}");
                if(${fileColumnName}Values!=null) {
                    ${fileColumnName}Values = FastArrayUtils.trimEmpty(${fileColumnName}Values);
                    ${fileColumnName}Urls.addAll(Arrays.asList(${fileColumnName}Values));
                }
        
                FastFile<?>[] ${fileColumnName}Files = getParamFiles(entityPrefix+"${fileColumnName}");
                if (${fileColumnName}Files != null) {
                    for (FastFile<?> file : ${fileColumnName}Files) {
                        ${fileColumnName}Urls.add(file.getUrl());
                    }
                }
                entity.set("${fileColumnName}", FastChar.getJson().toJson(${fileColumnName}Urls));
            }


========

            String entityPrefix = entity.getString(DATA_PREFIX, "");
            if (FastStringUtils.isNotEmpty(entityPrefix)) {
                entityPrefix = entityPrefix + ".";
            }