package ${packageName}.controller;

import java.util.Arrays;
import java.util.List;

import com.muyu.common.core.web.domain.BaseEntity;
import jakarta.servlet.http.HttpServletResponse;
import javax.annotation.Resource;
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.muyu.common.security.annotation.RequiresPermissions;
import ${packageName}.domain.${ClassName};
import ${packageName}.service.I${ClassName}Service;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.security.utils.SecurityUtils;
import org.springframework.validation.annotation.Validated;
#if($table.crud || $table.sub)
import com.muyu.common.core.web.page.TableDataInfo;
#elseif($table.tree)
#end

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

    /**
     * 查询${functionName}列表
     */
    @RequiresPermissions("${permissionPrefix}:list")
    @GetMapping("/list")
#if($table.crud || $table.sub)
    public Result<TableDataInfo<${ClassName}>> list(${ClassName} ${className})
    {
        startPage();
        List<${ClassName}> list = ${className}Service.select${ClassName}List(${className});
        return getDataTable(list);
    }
#elseif($table.tree)
    public Result<${ClassName}> list(${ClassName} ${className})
    {
        List<${ClassName}> list = ${className}Service.select${ClassName}List(${className});
        return success(list);
    }
#end

    /**
     * 导出${functionName}列表
     */
    @RequiresPermissions("${permissionPrefix}:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, ${ClassName} ${className})
    {
        List<${ClassName}> list = ${className}Service.select${ClassName}List(${className});
        ExcelUtil<${ClassName}> util = new ExcelUtil<${ClassName}>(${ClassName}.class);
        util.exportExcel(response, list, "${functionName}数据");
    }

    /**
     * 获取${functionName}详细信息
     */
    @RequiresPermissions("${permissionPrefix}:query")
    @GetMapping(value = "/{${pkColumn.javaField}}")
    public Result<List<${ClassName}>> getInfo(@PathVariable("${pkColumn.javaField}") ${pkColumn.javaType} ${pkColumn.javaField})
    {
        return success(${className}Service.select${ClassName}By${pkColumn.capJavaField}(${pkColumn.javaField}));
    }

    /**
     * 新增${functionName}
     */
    @RequiresPermissions("${permissionPrefix}:add")
    @PostMapping
    public Result<Integer> add(
            @Validated @RequestBody ${ClassName} ${className})
    {
        if (${className}Service.checkIdUnique(${className})) {
            return error("新增 ${functionName} '" + ${className} + "'失败，${functionName}已存在");
        }
        ${className}.setCreateBy(SecurityUtils.getUsername());
        return toAjax(${className}Service.save(${className}));
    }

    /**
     * 修改${functionName}
     */
    @RequiresPermissions("${permissionPrefix}:edit")
    @PutMapping
    public Result<Integer> edit(
            @Validated @RequestBody ${ClassName} ${className})
    {
        if (!${className}Service.checkIdUnique(${className})) {
            return error("修改 ${functionName} '" + ${className} + "'失败，${functionName}不存在");
        }
        ${className}.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(${className}Service.updateById(${className}));
    }

    /**
     * 删除${functionName}
     */
    @RequiresPermissions("${permissionPrefix}:remove")
	@DeleteMapping("/{${pkColumn.javaField}s}")
    public Result<Integer> remove(@PathVariable("${pkColumn.javaField}s") ${pkColumn.javaType}[] ${pkColumn.javaField}s)
    {
        ${className}Service.removeBatchByIds(Arrays.asList(${pkColumn.javaField}s));
        return success();
    }
}
