package ${package.Controller};

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ${package.Parent}.common.ResponseBean;
import ${package.Parent}.common.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import ${package.Service}.${table.serviceName};
import ${package.Entity}.${table.entityName};
<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>

/**
 * <p>
    * ${table.comment!} 前端控制器
    * </p>
 *
 * @author ${author}
 * @since ${date}
 */

@Api(tags = "${table.comment}")
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
@RequestMapping("/")
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
    <#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
    <#else>
public class ${table.controllerName} {
    </#if>

    private final Logger logger = LoggerFactory.getLogger(${table.controllerName}.class);

    @Autowired
    public ${table.serviceName} i${entity}Service;


    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/${table.entityPath}",method= RequestMethod.GET)
    @ApiOperation(value="列表", notes = "列表", httpMethod = "GET", response = ResponseBean.class)
    public ResponseBean ${table.entityPath}List(@ApiParam("页码") @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
									 @ApiParam("单页数据量") @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize) throws Exception{

		    IPage<${entity}> pg = new Page<>(pageNum, pageSize);
            QueryWrapper<${entity}> queryWrapper = new QueryWrapper<>();
            queryWrapper.orderByDesc("create_date");

            IPage<${entity}> result = i${entity}Service.page(pg, queryWrapper);
            return ResponseBean.success(result);
    }

    /**
     * 保存对象
     * @param ${table.entityPath} 实体对象
     * @return
     */
    @RequestMapping(value="/${table.entityPath}",method= RequestMethod.POST)
    @ApiOperation(value="保存", notes = "保存", httpMethod = "POST", response = ResponseBean.class)
    public ResponseBean ${table.entityPath}Add(@ApiParam("保存内容") @RequestBody ${entity} ${table.entityPath}) throws Exception{

        if (i${entity}Service.save(${table.entityPath})){
            return ResponseBean.success(${table.entityPath});
        }else {
            return ResponseBean.error("保存失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
        }
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     */
    @RequestMapping(value="/${table.entityPath}/{id}", method= RequestMethod.DELETE)
    @ApiOperation(value="删除", notes = "删除", httpMethod = "DELETE", response = ResponseBean.class)
    public ResponseBean ${table.entityPath}Delete(@ApiParam("对象ID") @PathVariable Integer id) throws Exception{
        if(i${entity}Service.removeById(id)){
            return ResponseBean.success("success");
        }else {
            return ResponseBean.error("删除失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
        }
    }

    /**
     * 修改对象
     * @param id  实体ID
     * @param ${table.entityPath} 实体对象
     * @return
     */
    @RequestMapping(value="/${table.entityPath}/{id}",method= RequestMethod.PUT)
    @ApiOperation(value="更新", notes = "更新", httpMethod = "PUT", response = ResponseBean.class)
    public ResponseBean ${table.entityPath}Update(@ApiParam("对象ID") @PathVariable Integer id,
                                        @ApiParam("更新内容") @RequestBody ${entity} ${table.entityPath}) throws Exception{

        if (i${entity}Service.updateById(${table.entityPath})){
            return ResponseBean.success(i${entity}Service.getById(id));
        }else {
            return ResponseBean.error("修改失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
        }
    }

    /**
     * 根据id查询对象
     * @param id  实体ID
     */
    @RequestMapping(value="/${table.entityPath}/{id}",method= RequestMethod.GET)
    @ApiOperation(value="详情", notes = "详情", httpMethod = "GET", response = ResponseBean.class)
    public ResponseBean ${table.entityPath}Get(@ApiParam("对象ID") @PathVariable Integer id) throws Exception{
        return ResponseBean.success(i${entity}Service.getById(id));
    }
}
</#if>
