package ${package.Controller};

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xunmei.common.core.web.domain.AjaxResult;
import org.springframework.web.bind.annotation.*;
import ${package.Service}.${table.serviceName};
import ${package.Entity}.${entity};
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import com.xunmei.common.security.annotation.RequiresPermissions;
import com.xunmei.common.core.web.controller.BaseController;

<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>

/**
 * <p>
 * ${table.controllerName!} 前端控制器
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
<#if restControllerStyle>
@Api(tags = {"${table.controllerName!}"})
@RestController
<#else>
@Controller
</#if>@RequestMapping("/${entity?uncap_first}")
<#if kotlin>class ${table.controllerName}<#if superControllerClass??>:${superControllerClass}()</#if><#else><#if superControllerClass??>public class ${table.controllerName} extends ${superControllerClass}{<#else>public class ${table.controllerName} extends BaseController {</#if>

    private final Logger log = LoggerFactory.getLogger(getClass());
    @Resource
    private ${table.serviceName} ${(table.serviceName?substring(1))?uncap_first};


    @ApiOperation(value = "新增${table.comment!}")
    @RequiresPermissions("${package.Controller?split('.')[2]}:${entity?uncap_first}:add")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ${entity} ${entity?uncap_first}){
        try {
            return ${(table.serviceName?substring(1))?uncap_first}.add(${entity?uncap_first});
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return error();
        }
    }

    @ApiOperation(value = "删除${table.comment!}")
    @RequiresPermissions("${package.Controller?split('.')[2]}:${entity?uncap_first}:delete")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            return ${(table.serviceName?substring(1))?uncap_first}.delete(id);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return error();
        }
    }

    @ApiOperation(value = "更新${table.comment!}")
    @RequiresPermissions("${package.Controller?split('.')[2]}:${entity?uncap_first}:update")
    @PutMapping("/update")
    public AjaxResult update(@RequestBody ${entity} ${entity?uncap_first}){
        try {
            return ${(table.serviceName?substring(1))?uncap_first}.updateData(${entity?uncap_first});
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return error();
        }
    }

    @ApiOperation(value = "查询${table.comment!}分页数据")
    @RequiresPermissions("${package.Controller?split('.')[2]}:${entity?uncap_first}:findByPage")
    @GetMapping("/findByPage")
    public AjaxResult findListByPage(Page<${entity}> page,${entity} ${entity?uncap_first}){
        try {
            return ${(table.serviceName?substring(1))?uncap_first}.findListByPage(page, ${entity?uncap_first});
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return error();
        }
    }

    @ApiOperation(value = "id查询${table.comment!}")
    @RequiresPermissions("${package.Controller?split('.')[2]}:${entity?uncap_first}:findById")
    @GetMapping("/{id}")
    public AjaxResult findById(@PathVariable Long id){
        try {
            return ${(table.serviceName?substring(1))?uncap_first}.findById(id);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return error();
        }
    }

}
</#if>