package ${packageName}.controller;

import java.io.Serializable;
import java.util.List;
import java.util.regex.Pattern;


import com.ebts.common.constant.ReturnConstants;
import com.ebts.common.core.controller.EBTSController;
import com.ebts.common.utils.ServerResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
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.RestController;
import com.ebts.common.annotation.Log;
import com.ebts.common.core.entity.AjaxResult;
import com.ebts.common.enums.BusinessType;
import com.ebts.common.exception.EbtsException;
import ${packageName}.entity.${ClassName};
import ${packageName}.service.${ClassName}Service;
import com.ebts.common.utils.poi.ExcelUtil;
#if($table.crud || $table.sub)
import com.ebts.common.core.page.TableDataInfo;
#elseif($table.tree)
#elseif($table.tplCategory.equals("rel"))
import org.springframework.validation.annotation.Validated;
#end

/**
 * ${functionName}Controller
 * 
 * @author ${author}
 * @date ${datetime}
 */

@Api(value = "${functionName}管理",tags = "${functionName}管理")
@RestController
@RequestMapping("/${moduleName}/${businessName}")
public class ${ClassName}Controller extends EBTSController {
    protected final Logger logger = LoggerFactory.getLogger(${ClassName}Controller.class);

    @Autowired
    private ${ClassName}Service ${className}Service;

    /**
     * 查询${functionName}列表
     */
    @ApiOperation("查询${functionName}列表")
    @PreAuthorize("@ebts.hasPermi('${permissionPrefix}:list')")
    @PostMapping("/list")
    public Serializable list(@RequestBody ${ClassName} ${className}) {
        startPage(${className}.getPageInfo());
        ServerResult<List<${ClassName}>> genServerResult = ${className}Service.select${ClassName}List(${className});
        if (genServerResult.isStart()) {
#if($table.crud || $table.sub || $tplCategory.equals("rel"))
            return getDataTable(genServerResult.getData());
#elseif($table.tree)
            return AjaxResult.success(genServerResult.getData());
#end
        } else {
            return AjaxResult.info(genServerResult.getMsg());
        }
    }

    /**
     * 导出${functionName}列表
     */
    @ApiOperation("导出${functionName}列表")
    @PreAuthorize("@ebts.hasPermi('${permissionPrefix}:export')")
    @Log(title = "${functionName}", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(@RequestBody ${ClassName} ${className}) {
        ServerResult<List<${ClassName}>> genServerResult = ${className}Service.select${ClassName}List(${className});
        ExcelUtil<${ClassName}> util = new ExcelUtil<${ClassName}>(${ClassName}. class);
        if (genServerResult.isStart()) {
            return util.exportExcel(genServerResult.getData(), "${businessName}");
        } else {
            return AjaxResult.error(genServerResult.getMsg());
        }
    }

    /**
     * 获取${functionName}详细信息
     */
    @ApiOperation("获取${functionName}详细信息")
    @ApiImplicitParam(name = "${pkColumn.javaField}" , value = "${functionName}${pkColumn.javaField}" , required = true, dataType = "${pkColumn.javaType}" , paramType = "path")
    @PreAuthorize("@ebts.hasPermi('${permissionPrefix}:query')")
    @GetMapping(value = "/{${pkColumn.javaField}}")
    public AjaxResult getInfo(@PathVariable("${pkColumn.javaField}") ${pkColumn.javaType} ${pkColumn.javaField}) {
        ServerResult<${ClassName}> genServerResult = ${className}Service.select${ClassName}ById(${pkColumn.javaField});
        if (genServerResult.isStart()) {
            return AjaxResult.success(genServerResult.getData());
        } else {
            return AjaxResult.info(genServerResult.getMsg());
        }
    }

    /**
     * 新增${functionName}
     */
    @ApiOperation("新增${functionName}")
    @ApiImplicitParam(name = "${className}" , value = "新增${functionName}信息" , dataType = "${ClassName}")
    @PreAuthorize("@ebts.hasPermi('${permissionPrefix}:add')")
    @Log(title = "${functionName}", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody ${ClassName} ${className}) {
        ServerResult<Integer> genServerResult = ${className}Service.insert${ClassName}(${className});
        if (genServerResult.isStart()) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error(genServerResult.getMsg());
        }
    }

    /**
     * 修改${functionName}
     */
    @ApiOperation("修改${functionName}")
    @ApiImplicitParam(name = "${className}" , value = "修改${functionName}信息" , dataType = "${ClassName}")
    @PreAuthorize("@ebts.hasPermi('${permissionPrefix}:edit')")
    @Log(title = "${functionName}", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody ${ClassName} ${className}) {
        ServerResult<Integer> genServerResult = ${className}Service.update${ClassName}(${className});
        if (genServerResult.isStart()) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error(genServerResult.getMsg());
        }
    }

    /**
     * 删除${functionName}
     */
    @ApiOperation("删除${functionName}")
    @ApiImplicitParam(name = "${pkColumn.javaField}s" , value = "${functionName}${pkColumn.javaField}s" , required = true, dataType = "${pkColumn.javaType}" , paramType = "path")
    @PreAuthorize("@ebts.hasPermi('${permissionPrefix}:remove')")
    @Log(title = "${functionName}", businessType = BusinessType.DELETE)
	@DeleteMapping("/{${pkColumn.javaField}s}")
    public AjaxResult remove(@PathVariable ${pkColumn.javaType}[] ${pkColumn.javaField}s) {
        if (${pkColumn.javaField}s.length<0){
            return AjaxResult.error("id不能为空!");
        }
        ServerResult<Integer> genServerResult = ${className}Service.delete${ClassName}ByIds(${pkColumn.javaField}s);
        if (genServerResult.isStart()) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error(genServerResult.getMsg());
        }
    }
}
