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 com.community.huiju.common.model.ResponseBean;
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}
 */
<#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)
    public ResponseBean ${table.entityPath}List(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
									 @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize){
        ResponseBean responseBean = new ResponseBean();
        try {
            //使用分页插件
		    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);
            responseBean.addSuccess(result);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("${table.entityPath}List -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 保存对象
     * @param ${table.entityPath} 实体对象
     * @return
     */
    @RequestMapping(value="/${table.entityPath}",method= RequestMethod.POST)
    public ResponseBean ${table.entityPath}Add(@RequestBody ${entity} ${table.entityPath}){
        ResponseBean responseBean = new ResponseBean();
        try {
            if (i${entity}Service.save(${table.entityPath})){
                responseBean.addSuccess(${table.entityPath});
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("${table.entityPath}Add -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     */
    @ResponseBody
    @RequestMapping(value="/${table.entityPath}/{id}", method= RequestMethod.DELETE)
    public ResponseBean ${table.entityPath}Delete(@PathVariable Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            if(i${entity}Service.removeById(id)){
                responseBean.addSuccess("success");
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("${table.entityPath}Delete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 修改对象
     * @param id  实体ID
     * @param ${table.entityPath} 实体对象
     * @return
     */
    @RequestMapping(value="/${table.entityPath}/{id}",method= RequestMethod.PUT)
    public ResponseBean ${table.entityPath}Update(@PathVariable Integer id,
                                        @RequestBody ${entity} ${table.entityPath}){
        ResponseBean responseBean = new ResponseBean();
        try {
            if (i${entity}Service.updateById(${table.entityPath})){
                responseBean.addSuccess(${table.entityPath});
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("${table.entityPath}Update -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id查询对象
     * @param id  实体ID
     */
    @RequestMapping(value="/${table.entityPath}/{id}",method= RequestMethod.GET)
    public ResponseBean ${table.entityPath}Get(@PathVariable Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.addSuccess(i${entity}Service.getById(id));
        }catch (Exception e){
            e.printStackTrace();
            logger.error("${table.entityPath}Delete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
}
</#if>
