package ${packageName}.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.taais.common.core.core.domain.CommonResult;
import com.taais.common.excel.utils.ExcelUtil;
import com.taais.common.log.annotation.Log;
import com.taais.common.log.enums.BusinessType;
import com.taais.common.web.annotation.RepeatSubmit;
import com.taais.common.web.core.BaseController;
import jakarta.annotation.Resource;
import ${packageName}.domain.vo.${ClassName}Vo;
import ${packageName}.domain.bo.${ClassName}Bo;
import ${packageName}.service.I${ClassName}Service;

#if($table.crud || $table.sub)
import com.taais.common.core.core.page.PageResult;
#elseif($table.tree)
#end

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

    /**
     * 查询${functionName}列表
     */
    @SaCheckPermission("${permissionPrefix}:list")
    @GetMapping("/list")
    #if($table.crud || $table.sub)
    public CommonResult<PageResult<${ClassName}Vo>> list(${ClassName}Bo ${className}Bo) {
        return CommonResult.success(${className}Service.selectPage(${className}Bo));
    }
    #elseif($table.tree)
    public CommonResult<List<${ClassName}Vo>> list(${ClassName}Bo ${className}Bo) {
        List<${ClassName}Vo> list = ${className}Service.selectList(${className}Bo);
        return CommonResult.success(list);
    }
    #end

    /**
     * 导出${functionName}列表
     */
    @SaCheckPermission("${permissionPrefix}:export")
    @Log(title = "${functionName}", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ${ClassName}Bo ${className}Bo) {
        List<${ClassName}Vo> list = ${className}Service.selectList(${className}Bo);
        ExcelUtil.exportExcel(list, "${functionName}", ${ClassName}Vo.class, response);
    }

    /**
     * 获取${functionName}详细信息
     */
    @SaCheckPermission("${permissionPrefix}:query")
    @GetMapping(value = "/{${pkColumn.javaField}}")
    public CommonResult<${ClassName}Vo> getInfo(@PathVariable ${pkColumn.javaType} ${pkColumn.javaField}) {
        return CommonResult.success(${className}Service.selectById(${pkColumn.javaField}));
    }

    /**
     * 新增${functionName}
     */
    @SaCheckPermission("${permissionPrefix}:add")
    @Log(title = "${functionName}", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public CommonResult<Void> add(@Validated @RequestBody ${ClassName}Bo ${className}Bo) {
        boolean inserted = ${className}Service.insert(${className}Bo);
        if (!inserted) {
            return CommonResult.fail("新增${functionName}记录失败！");
        }
        return CommonResult.success();
    }

    /**
     * 修改${functionName}
     */
    @SaCheckPermission("${permissionPrefix}:edit")
    @Log(title = "${functionName}", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public CommonResult<Void> edit(@Validated @RequestBody ${ClassName}Bo ${className}Bo) {
        Boolean updated = ${className}Service.update(${className}Bo);
        if (!updated) {
            return CommonResult.fail("修改${functionName}记录失败!");
        }
        return CommonResult.success();
    }

    /**
     * 删除${functionName}
     */
    @SaCheckPermission("${permissionPrefix}:remove")
    @Log(title = "${functionName}", businessType = BusinessType.DELETE)
    @DeleteMapping("/{${pkColumn.javaField}s}")
    public CommonResult<Void> remove(@PathVariable ${pkColumn.javaType}[] ${pkColumn.javaField}s) {
        boolean deleted = ${className}Service.deleteByIds(${pkColumn.javaField}s);
        if (!deleted) {
            return CommonResult.fail("删除${functionName}记录失败!");
        }
        return CommonResult.success();
    }
}
