package ${packageName}.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
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.bs.common.annotation.Log;
import com.bs.common.core.controller.BaseController;
import com.bs.common.core.domain.AjaxResult;
import com.bs.common.core.page.TableDataInfo;
import com.bs.common.enums.BusinessType;
import com.bs.common.utils.poi.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import cn.hutool.core.lang.Validator;
import ${packageName}.domain.${ClassName};
import ${packageName}.service.I${ClassName}Service;
import javax.annotation.Resource;
#if($table.crud || $table.sub)
#elseif($table.tree)
#end

/**
 * ${functionName}Controller
 *
 * @author ${author}
 * @date ${datetime}
 */
@Api(tags = "${functionName}")
@RestController
@RequestMapping("/${moduleName}/${businessName}")
public class ${ClassName}Controller extends BaseController {
    @Resource
    private I${ClassName}Service ${className}Service;

    /**
     * 分页查询${functionName}列表
     */
    @ApiOperation("分页查询${functionName}列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(${ClassName} ${className}) {
        startPage();
        LambdaQueryWrapper<${ClassName}> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,${className});
        List<${ClassName}> list = ${className}Service.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询${functionName}列表
     */
    @ApiOperation("查询${functionName}列表")
    @GetMapping("/list")
    public AjaxResult list(${ClassName} ${className}) {
        LambdaQueryWrapper<${ClassName}> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,${className});
        List<${ClassName}> list = ${className}Service.list(queryWrapper);
        return success(list);
    }

    /**
     * 导出${functionName}列表
     */
    @ApiOperation("导出${functionName}列表")
    @Log(title = "${functionName}导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ${ClassName} ${className}) {
        LambdaQueryWrapper<${ClassName}> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,${className});
        List<${ClassName}> list = ${className}Service.list(queryWrapper);
        ExcelUtil<${ClassName}> util = new ExcelUtil<${ClassName}>(${ClassName}. class);
        util.exportExcel(response, list, "${functionName}数据");
    }

    /**
     * 获取${functionName}详细信息
     */
    @ApiOperation("获取${functionName}详细信息")
    @GetMapping(value = "/{${pkColumn.javaField}}")
    public AjaxResult getInfo(@PathVariable("${pkColumn.javaField}") ${pkColumn.javaType} ${pkColumn.javaField}) {
        return success(${className}Service.getById(${pkColumn.javaField}));
    }

    /**
     * 新增${functionName}
     */
    @ApiOperation("新增${functionName}")
    @Log(title = "${functionName}新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ${ClassName} ${className}) {
        return toAjax(${className}Service.save(${className}));
    }

    /**
     * 修改${functionName}
     */
    @ApiOperation("修改${functionName}")
    @Log(title = "${functionName}修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ${ClassName} ${className}) {
        return toAjax(${className}Service.updateById(${className}));
    }

    /**
     * 删除${functionName}
     */
    @ApiOperation("删除${functionName}")
    @Log(title = "${functionName}删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{${pkColumn.javaField}s}")
    public AjaxResult remove(@PathVariable List<Long> ${pkColumn.javaField}s) {
        return toAjax(${className}Service.removeBatchByIds(${pkColumn.javaField}s));
    }

    /**
      * 条件设置
      */
    private void condition (LambdaQueryWrapper<${ClassName}> queryWrapper,${ClassName} ${className}){

#foreach ($column in $columns)
    #if(!$column.javaField.equals("updatestamp")&&!$column.javaField.equals("syncstamp"))

        #if($column.javaField.length() > 2 && $column.javaField.substring(1,2).matches("[A-Z]"))
        #set($AttrName=$column.javaField)
    #else
        #set($AttrName=$column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})
    #end
        //${column.columnComment}
        if(Validator.isNotEmpty(${className}.get${AttrName}())){
            queryWrapper.eq(${ClassName}::get${AttrName},${className}.get${AttrName}());
        }
    #end
#end

        }

}
