package ${packageName}.controller;

import com.starry.common.enums.BusinessType;
import com.starry.common.result.R;
import com.starry.common.annotation.Log;
import ${packageName}.entity.${ClassName}Entity;
import ${packageName}.vo.${ClassName}QueryVo;
import ${packageName}.vo.${ClassName}AddVo;
import ${packageName}.service.I${ClassName}Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import com.baomidou.mybatisplus.core.metadata.IPage;

/**
 * ${functionName}Controller
 *
 * @author ${author}
 * @since  ${datetime}
 */
@Api(tags = "${functionName}管理", description = "${functionName}信息管理相关接口，包括查询、新增、修改、删除等操作")
@RestController
@RequestMapping("/${moduleName}/${businessName}")
public class ${ClassName}Controller {
    @Resource
    private I${ClassName}Service ${className}Service;

/**
 * 分页查询${functionName}列表
 */
@ApiOperation(value = "分页查询${functionName}", notes = "分页查询${functionName}信息列表")
@ApiResponses({
    @ApiResponse(code = 200, message = "操作成功", response = ${ClassName}Entity.class, responseContainer = "Page")
})
@PreAuthorize("@customSs.hasPermission('${permissionPrefix}:list')")
@PostMapping("/listByPage")
#if($table.crud || $table.sub)
public R listByPage(@ApiParam(value = "查询条件", required = true) @Validated @RequestBody ${ClassName}QueryVo vo) {
    return R.ok(${className}Service.selectByPage(vo));
}
#elseif($table.tree)
public R listByPage(@ApiParam(value = "查询条件", required = true) @Validated @RequestBody ${ClassName}QueryVo vo) {
    return R.ok(${className}Service.select${ClassName}List(vo));
}
#end

/**
 * 查询所有${functionName}列表
 */
@ApiOperation(value = "查询所有${functionName}", notes = "获取所有${functionName}信息列表")
@ApiResponses({
    @ApiResponse(code = 200, message = "操作成功", response = ${ClassName}Entity.class, responseContainer = "List")
})
@PreAuthorize("@customSs.hasPermission('${permissionPrefix}:list')")
@GetMapping("/listAll")
public R listAll() {
    return R.ok(${className}Service.selectAll());
}

    /**
     * 获取${functionName}详细信息
     */
    @ApiOperation(value = "获取${functionName}详情", notes = "根据ID获取${functionName}详细信息")
    @ApiImplicitParam(name = "${pkColumn.javaField}", value = "${functionName}ID", required = true, paramType = "path", dataType = "${pkColumn.javaType}", example = "1")
    @ApiResponses({
        @ApiResponse(code = 200, message = "操作成功", response = ${ClassName}Entity.class)
    })
    @PreAuthorize("@customSs.hasPermission('${permissionPrefix}:query')")
    @GetMapping(value = "/{${pkColumn.javaField}}")
    public R getInfo(@PathVariable("${pkColumn.javaField}") ${pkColumn.javaType} ${pkColumn.javaField}) {
        return R.ok(${className}Service.select${ClassName}By${pkColumn.capJavaField}(${pkColumn.javaField}));
    }

    /**
     * 新增${functionName}
     */
    @ApiOperation(value = "新增${functionName}", notes = "创建新的${functionName}信息")
    @ApiResponses({
        @ApiResponse(code = 200, message = "操作成功"),
        @ApiResponse(code = 500, message = "添加失败")
    })
    @PreAuthorize("@customSs.hasPermission('${permissionPrefix}:create')")
    @Log(title = "${functionName}", businessType = BusinessType.INSERT)
    @PostMapping("/create")
    public R create(@ApiParam(value = "${functionName}信息", required = true) @Validated @RequestBody ${ClassName}AddVo vo) {
        boolean success = ${className}Service.create(vo);
        if (success) {
            return R.ok();
        }
        return R.error("添加失败");
    }

    /**
     * 修改${functionName}
     */
    @ApiOperation(value = "修改${functionName}", notes = "根据ID修改${functionName}信息")
    @ApiImplicitParam(name = "${pkColumn.javaField}", value = "${functionName}ID", required = true, paramType = "path", dataType = "${pkColumn.javaType}", example = "1")
    @ApiResponses({
        @ApiResponse(code = 200, message = "操作成功"),
        @ApiResponse(code = 500, message = "修改失败")
    })
    @PreAuthorize("@customSs.hasPermission('${permissionPrefix}:edit')")
    @Log(title = "${functionName}", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/update/{${pkColumn.javaField}}")
    public R update(@PathVariable ${pkColumn.javaType} ${pkColumn.javaField}, @ApiParam(value = "${functionName}信息", required = true) @RequestBody ${ClassName}Entity ${className}) {
        ${className}.set${pkColumn.capJavaField}(${pkColumn.javaField});
        boolean success = ${className}Service.update(${className});
        if (success) {
            return R.ok();
        }
        return R.error("修改失败");
    }

    /**
     * 删除${functionName}
     */
    @ApiOperation(value = "删除${functionName}", notes = "根据ID批量删除${functionName}信息")
    @ApiImplicitParam(name = "${pkColumn.javaField}s", value = "${functionName}ID数组", required = true, paramType = "path", dataType = "${pkColumn.javaType}[]", example = "1,2,3")
    @ApiResponses({
        @ApiResponse(code = 200, message = "操作成功", response = Integer.class)
    })
    @PreAuthorize("@customSs.hasPermission('${permissionPrefix}:remove')")
    @Log(title = "${functionName}", businessType = BusinessType.DELETE)
    @DeleteMapping("/{${pkColumn.javaField}s}")
    public R remove(@PathVariable ${pkColumn.javaType}[] ${pkColumn.javaField}s) {
        return R.ok(${className}Service.delete${ClassName}By${pkColumn.capJavaField}s(${pkColumn.javaField}s));
    }
}
