package ${packageName.replace(".${moduleName}", "")}.web.controller.${moduleName};

import org.noear.solon.annotation.*;
import org.noear.solon.auth.annotation.AuthPermissions;
import org.noear.solon.core.handle.Result;
import org.noear.solon.validation.annotation.Validated;
import com.mybatisflex.core.query.QueryWrapper;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ExcelUtil;
import com.ruoyi.common.web.controller.BaseController;
import ${packageName}.domain.${ClassName};
import ${packageName}.service.I${ClassName}Service;
#if($table.crud || $table.sub)
import com.mybatisflex.core.paginate.Page;
import com.ruoyi.common.web.domain.PageData;
import com.ruoyi.framework.annotation.RyLog;
#elseif($table.tree)
#end

import java.util.Arrays;
import java.util.List;

/**
 * ${functionName} 操作处理
 * @author ${author}
 */
@Controller
@Mapping("${moduleName}/${businessName}")
public class ${ClassName}Controller extends BaseController {

    @Inject
    I${ClassName}Service ${className}Service;

    /**
     * 查询${functionName}列表
     */
    @AuthPermissions("${permissionPrefix}:list")
    @Get
    @Mapping("list")
#if($table.crud || $table.sub)
    public PageData<${ClassName}> list(Page<${ClassName}> page, ${ClassName} ${className}) {
        QueryWrapper qw = getQW(${className});
        Page<${ClassName}> result = ${className}Service.page(page, qw);
        return getPageData(result);
    }
#elseif($table.tree)
    public List<${ClassName}> list(${ClassName} ${className}) {
        QueryWrapper qw = getQW(${className});
        return ${className}Service.list(qw);
    }
#end

    /**
     * 导出${functionName}列表
     */
    @RyLog(title = "${functionName}", businessType = BusinessType.EXPORT)
    @AuthPermissions("${permissionPrefix}:export")
    @Post
    @Mapping("export")
    public void export(${ClassName} ${className}) {
        QueryWrapper qw = getQW(${className});
        List<${ClassName}> list = ${className}Service.list(qw);
        ExcelUtil.export(list);
    }

    /**
     * 查询${functionName}详情
     */
    @AuthPermissions("${permissionPrefix}:query")
    @Get
    @Mapping("{${pkColumn.javaField}}")
    public ${ClassName} info(@Path ${pkColumn.javaType} ${pkColumn.javaField}) {
        return ${className}Service.getById(${pkColumn.javaField});
    }

    /**
     * 新增${functionName}
     */
    @RyLog(title = "${functionName}", businessType = BusinessType.INSERT)
    @AuthPermissions("${permissionPrefix}:add")
    @Post
    @Mapping
    public Result<Void> add(@Body @Validated ${ClassName} ${className}) {
        return toResult(${className}Service.save(${className}));
    }

    /**
     * 修改${functionName}
     */
    @RyLog(title = "${functionName}", businessType = BusinessType.UPDATE)
    @AuthPermissions("${permissionPrefix}:edit")
    @Put
    @Mapping
    public Result<Void> edit(@Body @Validated ${ClassName} ${className}) {
        return toResult(${className}Service.updateById(${className}));
    }

    /**
     * 删除${functionName}
     */
    @RyLog(title = "${functionName}", businessType = BusinessType.DELETE)
    @AuthPermissions("${permissionPrefix}:remove")
    @Delete
    @Mapping("{${pkColumn.javaField}s}")
    public Result<Void> delete(@Path ${pkColumn.javaType}[] ${pkColumn.javaField}s) {
        return toResult(${className}Service.removeByIds(Arrays.asList(${pkColumn.javaField}s)));
    }

    private QueryWrapper getQW(${ClassName} ${className}) {
        QueryWrapper qw = QueryWrapper.create();
#foreach($column in $columns)
#if($column.query)
#set($queryType=$column.queryType)
#set($javaField=$column.javaField)
#set($javaType=$column.javaType)
#set($columnName=$column.columnName)
#set($AttrName=$column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})
#set($mpMethod=$column.queryType.toLowerCase())
#if($queryType != 'BETWEEN')
        qw.and(${ClassName}::get$AttrName).$mpMethod(${className}.get$AttrName());
#else
        qw.and(${ClassName}::get$AttrName).between(params.get("begin$AttrName"), params.get("end$AttrName"));
#end
#end
#end
        return qw;
    }

}
