package ${packageName}.controller${childPackageName};

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
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.ximai.common.annotation.Log;
import com.ximai.common.core.controller.BaseController;
import com.ximai.common.core.domain.AjaxResult;
import com.ximai.common.enums.BusinessType;
import io.swagger.annotations.ApiOperation;
import ${packageName}.domain${childPackageName}.${ClassName};
import ${packageName}.dto${childPackageName}.${ClassName}Dto;
import ${packageName}.dto${childPackageName}.${ClassName}Query;
import ${packageName}.dto${childPackageName}.${ClassName}Create;
import ${packageName}.dto${childPackageName}.${ClassName}Update;
import ${packageName}.dto${childPackageName}.${ClassName}ExcelExport;
import ${packageName}.service${childPackageName}.I${ClassName}Service;
import com.ximai.common.utils.poi.ExcelUtil;
import cn.hutool.core.bean.BeanUtil;
#if($table.crud || $table.sub)
import com.ximai.common.core.page.TableDataInfo;
#elseif($table.tree)
#end

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

    /**
     * 查询${functionName}列表
     */
    @ApiOperation("查询${functionName}列表")
    @PreAuthorize("@ss.hasPermi('${permissionPrefix}:list')")
    @GetMapping("/list")
#if($table.crud || $table.sub)
    public TableDataInfo<${ClassName}Dto> list(${ClassName}Query ${className}Query) {
        startPage();
        QueryWrapper<${ClassName}> query = new QueryWrapper<>();
#foreach($column in $columns)
#set($queryType=$column.queryType)
#set($javaField=$column.javaField)
#set($javaType=$column.javaType)
#set($columnName=$column.columnName)
#set($AttrName=$column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})
#if($column.query)
#if($column.queryType == "EQ")
        query.eq(#if($javaType == 'String')StringUtils.isNotEmpty(${className}Query.get${AttrName}())#else${className}Query.get${AttrName}()!=null#end, "m.$columnName", ${className}Query.get${AttrName}());
#elseif($column.queryType == "NE")
        query.ne(#if($javaType == 'String')StringUtils.isNotEmpty(${className}Query.get${AttrName}())#else${className}Query.get${AttrName}()!=null#end, "m.$columnName", ${className}Query.get${AttrName}());
#elseif($column.queryType == "GT")
        query.gt(#if($javaType == 'String')StringUtils.isNotEmpty(${className}Query.get${AttrName}())#else${className}Query.get${AttrName}()!=null#end, "m.$columnName", ${className}Query.get${AttrName}());
#elseif($column.queryType == "GTE")
        query.ge(#if($javaType == 'String')StringUtils.isNotEmpty(${className}Query.get${AttrName}())#else${className}Query.get${AttrName}()!=null#end, "m.$columnName", ${className}Query.get${AttrName}());
#elseif($column.queryType == "LT")
        query.lt(#if($javaType == 'String')StringUtils.isNotEmpty(${className}Query.get${AttrName}())#else${className}Query.get${AttrName}()!=null#end, "m.$columnName", ${className}Query.get${AttrName}());
#elseif($column.queryType == "LTE")
        query.le(#if($javaType == 'String')StringUtils.isNotEmpty(${className}Query.get${AttrName}())#else${className}Query.get${AttrName}()!=null#end, "m.$columnName", ${className}Query.get${AttrName}());
#elseif($column.queryType == "LIKE")
        query.like(#if($javaType == 'String')StringUtils.isNotEmpty(${className}Query.get${AttrName}())#else${className}Query.get${AttrName}()!=null#end, "m.$columnName", "%" + ${className}Query.get${AttrName}() + "%");
#end
#end
#end
        List<${ClassName}Dto> list = ${className}Service.select${ClassName}DtoList(query);
        return getDataTable(list);
    }
#elseif($table.tree)
    public AjaxResult<${ClassName}Dto> list(${ClassName} ${className}) {
        List<${ClassName}Dto> list = ${className}Service.select${ClassName}List(${className});
        return AjaxResult.success(list);
    }
#end

    /**
     * 导出${functionName}列表
     */
    @ApiOperation("导出${functionName}列表")
    @PreAuthorize("@ss.hasPermi('${permissionPrefix}:export')")
    @Log(title = "${functionName}", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ${ClassName} ${className}) {
        List<${ClassName}Dto> list = ${className}Service.select${ClassName}List(${className});
        ExcelUtil<${ClassName}ExcelExport> util = new ExcelUtil<${ClassName}ExcelExport>(${ClassName}ExcelExport.class);
        List<${ClassName}ExcelExport> tempList = BeanUtil.copyToList(list, ${ClassName}ExcelExport.class);
        util.exportExcel(response, tempList, "${functionName}数据");
    }

    /**
     * 获取${functionName}详细信息
     */
    @ApiOperation("获取${functionName}详细信息")
    @PreAuthorize("@ss.hasPermi('${permissionPrefix}:query')")
    @GetMapping(value = "/{${pkColumn.javaField}}")
    public AjaxResult<${ClassName}Dto> getInfo(@PathVariable("${pkColumn.javaField}") ${pkColumn.javaType} ${pkColumn.javaField}) {
        return AjaxResult.success(${className}Service.select${ClassName}DtoBy${pkColumn.capJavaField}(${pkColumn.javaField}));
    }

    /**
     * 新增${functionName}
     */
    @ApiOperation("新增${functionName}")
    @PreAuthorize("@ss.hasPermi('${permissionPrefix}:add')")
    @Log(title = "${functionName}", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ${ClassName}Create ${className}Create) {
        return toAjax(${className}Service.insert${ClassName}(${className}Create));
    }

    /**
     * 修改${functionName}
     */
    @ApiOperation("修改${functionName}")
    @PreAuthorize("@ss.hasPermi('${permissionPrefix}:edit')")
    @Log(title = "${functionName}", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ${ClassName}Update ${className}Update) {
        return toAjax(${className}Service.update${ClassName}(${className}Update));
    }

    /**
     * 删除${functionName}
     */
    @ApiOperation("删除${functionName}")
    @PreAuthorize("@ss.hasPermi('${permissionPrefix}:remove')")
    @Log(title = "${functionName}", businessType = BusinessType.DELETE)
	@DeleteMapping("/{${pkColumn.javaField}s}")
    public AjaxResult remove(@PathVariable ${pkColumn.javaType}[] ${pkColumn.javaField}s) {
        return toAjax(${className}Service.delete${ClassName}By${pkColumn.capJavaField}s(${pkColumn.javaField}s));
    }
}
