package org.dromara.app.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.app.domain.bo.AppVersionBo;
import org.dromara.app.domain.vo.AppVersionVo;
import org.dromara.app.service.IAppVersionService;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * APP版本
 *
 * @author Lion Li
 * @date 2026-07-01
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/version")
public class AppVersionController extends BaseController {

    private final IAppVersionService appVersionService;

    /**
     * 查询APP版本列表
     */
    @SaCheckPermission("app:version:list")
    @GetMapping("/list")
    public TableDataInfo<AppVersionVo> list(AppVersionBo bo, PageQuery pageQuery) {
        return appVersionService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出APP版本列表
     */
    @SaCheckPermission("app:version:export")
    @Log(title = "APP版本", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppVersionBo bo, HttpServletResponse response) {
        List<AppVersionVo> list = appVersionService.queryList(bo);
        ExcelUtil.exportExcel(list, "APP版本", AppVersionVo.class, response);
    }

    /**
     * 获取APP版本详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("app:version:query")
    @GetMapping("/{id}")
    public R<AppVersionVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(appVersionService.queryById(id));
    }

    /**
     * 新增APP版本
     */
    @SaCheckPermission("app:version:add")
    @Log(title = "APP版本", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppVersionBo bo) {
        return toAjax(appVersionService.insertByBo(bo));
    }

    /**
     * 修改APP版本
     */
    @SaCheckPermission("app:version:edit")
    @Log(title = "APP版本", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AppVersionBo bo) {
        return toAjax(appVersionService.updateByBo(bo));
    }

    /**
     * 删除APP版本
     *
     * @param ids 主键串
     */
    @SaCheckPermission("app:version:remove")
    @Log(title = "APP版本", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(appVersionService.deleteWithValidByIds(List.of(ids), true));
    }
}
