package ${packageName}.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.nexo.common.core.domain.R;
import com.nexo.common.core.validate.AddGroup;
import com.nexo.common.core.validate.EditGroup;
import com.nexo.common.core.web.controller.BaseController;
import com.nexo.common.excel.utils.ExcelUtil;
import com.nexo.common.log.annotation.Log;
import com.nexo.common.log.enums.BusinessType;
import com.nexo.common.mybatis.core.page.PageQuery;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import ${packageName}.service.I${ClassName}Service;
#if($table.crud || $table.sub)
import com.nexo.common.mybatis.core.page.TableDataInfo;
#elseif($table.tree)
#end

import java.util.List;
import java.util.Arrays;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.servlet.http.HttpServletResponse;

/**
 * ${functionName}控制器
 * 前端访问路由地址为:/${moduleName}/${businessName}
 *
 * @author ${author}
 * @date ${datetime}
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/${businessName}")
public class ${ClassName}Controller extends BaseController {

    private final I${ClassName}Service i${ClassName}Service;

    /**
     * 查询${functionName}列表
     */
    @SaCheckPermission("${permissionPrefix}:list")
    @GetMapping("/list")
#if($table.crud || $table.sub)
    public TableDataInfo<${ClassName}> list(${ClassName} item, PageQuery pageQuery) {
        return i${ClassName}Service.queryPageList(item, pageQuery);
    }
#elseif($table.tree)
    public R<List<${ClassName}>> list(${ClassName} item) {
        List<${ClassName}> list = i${ClassName}Service.queryList (item);
        return R.ok(list);
    }
#end

    /**
     * 导出${functionName}列表
     */
    @SaCheckPermission("${permissionPrefix}:export")
    @Log(title = "${functionName}", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(${ClassName} item, HttpServletResponse response) {
        List<${ClassName}> list = i${ClassName}Service.queryList (item);
        ExcelUtil.exportExcel(list, "${functionName}", ${ClassName}.class, response);
    }

    /**
     * 获取${functionName}详细信息
     *
     * @param ${pkColumn.javaField} 主键
     */
    @SaCheckPermission("${permissionPrefix}:query")
    @GetMapping("/{${pkColumn.javaField}}")
    public R<${ClassName}> getInfo(@NotNull(message = "主键不能为空") @PathVariable ${pkColumn.javaType} ${pkColumn.javaField}) {
        return R.ok(i${ClassName}Service.queryById(${pkColumn.javaField}));
    }

    /**
     * 新增${functionName}
     */
    @SaCheckPermission("${permissionPrefix}:add")
    @Log(title = "${functionName}", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<id> add(@Validated(AddGroup.class) @Requestdy ${ClassName} item) {
        return toAjax(i${ClassName}Service.insertBy (item));
    }

    /**
     * 修改${functionName}
     */
    @SaCheckPermission("${permissionPrefix}:edit")
    @Log(title = "${functionName}", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<id> edit(@Validated(EditGroup.class) @Requestdy ${ClassName} item) {
        return toAjax(i${ClassName}Service.updateBy (item));
    }

    /**
     * 删除${functionName}
     *
     * @param ${pkColumn.javaField}s 主键串
     */
    @SaCheckPermission("${permissionPrefix}:remove")
    @Log(title = "${functionName}", businessType = BusinessType.DELETE)
    @DeleteMapping("/{${pkColumn.javaField}s}")
    public R<id> remove(@NotEmpty(message = "主键不能为空") @PathVariable ${pkColumn.javaType}[] ${pkColumn.javaField}s) {
        return toAjax(i${ClassName}Service.deleteWithValidByIds(Arrays.asList(${pkColumn.javaField}s), true));
    }
}
