package ${packageName}.controller;

import java.util.List;
import java.util.HashMap;
import java.util.Map;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.domain.R;
import com.github.pagehelper.PageInfo;
import com.aidex.common.core.page.PageDomain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.poi.ExcelUtil;
import com.aidex.framework.cache.DictUtils;
import javax.validation.constraints.*;
import org.springframework.web.bind.annotation.*;
import com.aidex.common.core.controller.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import ${packageName}.domain.${ClassName};
import ${packageName}.service.${ClassName}Service;

/**
 * ${functionName}Controller
 * @author ${functionAuthor}
 * @email ${functionAuthorEmail}
 * @date ${datetime}
 */
@RestController
@RequestMapping("/${moduleName}/${businessName}")
public class ${ClassName}Controller extends BaseController {

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

    /**
     * 查询${functionName}列表
     */
    @PreAuthorize("@ss.hasPermi('${permissionPrefix}:list')")
    @GetMapping("/list")
    public R<PageInfo> list(${ClassName} ${className}, HttpServletRequest request, HttpServletResponse response) {
        ${className}.setPage(new PageDomain(request, response));
        return R.data(${className}Service.findPage(${className}));
    }

    /**
     * 获取${functionName}详细信息
     */
    @PreAuthorize("@ss.hasPermi('${permissionPrefix}:query')")
    @GetMapping(value = "/{${pkColumn.javaField}}")
    public R<${ClassName}> detail(@PathVariable("${pkColumn.javaField}") ${pkColumn.javaType} ${pkColumn.javaField}) {
        return R.data(${className}Service.get(${pkColumn.javaField}));
    }

    /**
     * 新增${functionName}
     */
    @PreAuthorize("@ss.hasPermi('${permissionPrefix}:add')")
    @Log(title = "${functionName}", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated  ${ClassName} ${className}) {
#if($hasAttach)
        ${className}Service.save(${className});
        return R.data(${className});
#else
        return R.status(${className}Service.save(${className}));
#end
    }

    /**
     * 修改${functionName}
     */
    @PreAuthorize("@ss.hasPermi('${permissionPrefix}:edit')")
    @Log(title = "${functionName}", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated ${ClassName} ${className}) {
#if($hasAttach)
        ${className}Service.save(${className});
        return R.data(${className});
#else
        return R.status(${className}Service.save(${className}));
#end
    }

#if($hasDisableEnable)
    /**
     * 更新${functionName}状态
     */
    @PreAuthorize("@ss.hasPermi('${permissionPrefix}:edit')")
    @Log(title = "${functionName}", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStatus")
    public R updateStatus(@RequestBody ${ClassName} ${className}) {
        return R.status(${className}Service.updateStatus(${className}));
    }

#end
    /**
     * 删除${functionName}
     */
    @PreAuthorize("@ss.hasPermi('${permissionPrefix}:remove')")
    @Log(title = "${functionName}", businessType = BusinessType.DELETE)
    @DeleteMapping("/{${pkColumn.javaField}s}")
    public R remove(@PathVariable ${pkColumn.javaType}[] ids) {
        return R.status(${className}Service.delete${ClassName}ByIds(ids));
    }

#foreach($column in $columns)
#if($column.columnName == "sort")
    /**
     * 获取最大编号
     */
    @PreAuthorize("@ss.hasPermi('${permissionPrefix}:query')")
    @GetMapping("/findMaxSort")
    public R findMaxSort() {
        return R.data(${className}Service.findMaxSort(new ${ClassName}()));
    }
#break
#end
#end

#foreach($column in $uniqueColumns)
#set($attrName = $column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})
    /**
     * 校验${column.columnComment}的唯一性
     */
    @PreAuthorize("@ss.hasPermi('${permissionPrefix}:query')")
    @Log(title = "${functionName}", businessType = BusinessType.CHECK)
    @GetMapping(value = {"/check${attrName}Unique/{${column.javaField}}/{id}", "/check${attrName}Unique/{${column.javaField}}"})
    public R check${attrName}Unique(@NotBlank(message = "${column.columnComment}不允许为空") @PathVariable("${column.javaField}") String ${column.javaField}, @PathVariable(value = "id", required = false) String id) {
        ${ClassName} ${className} = new ${ClassName}();
        ${className}.set${attrName}(${column.javaField});
        ${className}.setId(id);
        ${className}Service.check${attrName}Unique(${className});
        return R.status(Boolean.TRUE);
    }

#end
    /**
     * 导出${functionName}列表
     */
    @PreAuthorize("@ss.hasPermi('${permissionPrefix}:export')")
    @Log(title = "${functionName}", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(${ClassName} ${className}) {
        List<${ClassName}> list = ${className}Service.findList(${className});
        ExcelUtil<${ClassName}> util = new ExcelUtil<${ClassName}>(${ClassName}.class);
        return util.exportExcel(list, "${functionName}数据");
    }
	
    /**
     * 根据字典类型查询字典数据信息等其他自定义信息
     */
    @GetMapping(value = "/getInitData/{dictTypes}")
    public R getInitData(@PathVariable String dictTypes) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.putAll(DictUtils.getMultiDictList(dictTypes));
        return R.data(dataMap);
    }

}
