package ${packageName}.controller;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import cn.hutool.core.collection.CollUtil;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.eco.common.core.core.domain.CommonResult;
import org.eco.common.log.annotation.Log;
import org.eco.common.log.enums.BusinessType;
import org.eco.common.security.utils.LoginHelper;
import org.eco.common.core.core.domain.model.LoginUser;
import org.eco.common.web.annotation.RepeatSubmit;
import org.eco.common.excel.utils.ExcelUtil;
import org.eco.common.web.core.BaseController;
import jakarta.annotation.Resource;
import ${packageName}.domain.vo.${ClassName}ImportVo;
import ${packageName}.domain.vo.${ClassName}Vo;
import ${packageName}.domain.bo.${ClassName}Bo;
import ${packageName}.service.I${ClassName}Service;
import org.springframework.web.multipart.MultipartFile;
    #if($table.crud || $table.sub)
    import org.eco.common.core.core.page.PageResult;
    #elseif($table.tree)
    #end

import java.util.List;
import java.util.ArrayList;

/**
 * ${functionName}Controller
 *
 * @author ${author}
 * @date ${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 CommonResult<Void> export(${ClassName}Bo ${className}Bo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        List<${ClassName}Vo> list = ${className}Service.selectList(${className}Bo);
        if (CollUtil.isEmpty(list)) {
            return CommonResult.fail("导出列表为空");
        }
            ${className}Service.asyncExport(list, "${functionName}", loginUser);
        return CommonResult.success();
    }

    @SaCheckPermission("${permissionPrefix}:import")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "${functionName}", ${ClassName}ImportVo.class, response);
    }

    /**
     * 导入${functionName}列表
     */
    @Log(title = "${functionName}", businessType = BusinessType.IMPORT)
    @SaCheckPermission("${permissionPrefix}:import")
    @PostMapping("/importData")
    public CommonResult<Void> importData(MultipartFile file, boolean updateSupport) {
        LoginUser loginUser = LoginHelper.getLoginUser();
            ${className}Service.asyncImportData(file, updateSupport, loginUser);
        return CommonResult.success();
    }

    /**
     * 获取${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();
    }
}
