package ${package}.${moduleName}.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hccake.ballcat.common.core.result.R;
import com.hccake.ballcat.commom.log.operation.annotation.OperationLogging;
import ${package}.${moduleName}.model.entity.${className};
import ${package}.${moduleName}.service.${className}Service;
import com.hccake.ballcat.common.core.result.ResultStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * ${comments}
 *
 * @author ${author}
 * @date ${datetime}
 */
@RestController
@AllArgsConstructor
@RequestMapping("/${moduleName}/${pathName}" )
@Api(value = "${pathName}", tags = "${comments}管理")
public class ${className}Controller {

    private final  ${className}Service ${classname}Service;

    /**
     * 分页查询
     * @param page 分页对象
     * @param ${classname} ${comments}
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public R<IPage<${className}>> get${className}Page(
            Page<${className}> page, ${className} ${classname}) {
        return R.ok(${classname}Service.page(page, Wrappers.query(${classname})));
    }


    /**
     * 通过id查询${comments}
     * @param ${pk.lowerAttrName} id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{${pk.lowerAttrName}}" )
    public R<${className}> getById(@PathVariable("${pk.lowerAttrName}" ) ${pk.attrType} ${pk.lowerAttrName}) {
        return R.ok(${classname}Service.getById(${pk.lowerAttrName}));
    }

    /**
     * 新增${comments}
     * @param ${classname} ${comments}
     * @return R
     */
    @ApiOperation(value = "新增${comments}", notes = "新增${comments}")
    @OperationLogging("新增${comments}" )
    @PostMapping
    @PreAuthorize("@per.hasPermission('${moduleName}_${pathName}_add')" )
    public R save(@RequestBody ${className} ${classname}) {
        return ${classname}Service.save(${classname}) ?
                R.ok() : R.failed(ResultStatus.SAVE_ERROR, "新增${comments}失败");
    }

    /**
     * 修改${comments}
     * @param ${classname} ${comments}
     * @return R
     */
    @ApiOperation(value = "修改${comments}", notes = "修改${comments}")
    @OperationLogging("修改${comments}" )
    @PutMapping
    @PreAuthorize("@per.hasPermission('${moduleName}_${pathName}_edit')" )
    public R updateById(@RequestBody ${className} ${classname}) {
        return ${classname}Service.updateById(${classname}) ?
                R.ok() : R.failed(ResultStatus.SAVE_ERROR, "修改${comments}失败");
    }

    /**
     * 通过id删除${comments}
     * @param ${pk.lowerAttrName} id
     * @return R
     */
    @ApiOperation(value = "通过id删除${comments}", notes = "通过id删除${comments}")
    @OperationLogging("通过id删除${comments}" )
    @DeleteMapping("/{${pk.lowerAttrName}}" )
    @PreAuthorize("@per.hasPermission('${moduleName}_${pathName}_del')" )
    public R removeById(@PathVariable ${pk.attrType} ${pk.lowerAttrName}) {
        return ${classname}Service.removeById(${pk.lowerAttrName}) ?
              R.ok() : R.failed(ResultStatus.SAVE_ERROR, "通过id删除${comments}失败");
    }

}
