package org.dromara.system.controller.app;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.domain.AjaxResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
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.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.UpdateAppVo;
import org.dromara.system.domain.bo.UpdateAppBo;
import org.dromara.system.service.IUpdateAppService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * APP版本描述
 *
 * @author boman
 * @date 2023-09-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/app")
public class UpdateAppController extends BaseController {

    private final IUpdateAppService updateAppService;

    /**
     * 查询APP版本描述列表
     */
    @SaCheckPermission("system:app:list")
    @GetMapping("/list")
    public TableDataInfo<UpdateAppVo> list(UpdateAppBo bo, PageQuery pageQuery) {
        return updateAppService.queryPageList(bo, pageQuery);
    }

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

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

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

    /**
     * 修改APP版本描述
     */
    @SaCheckPermission("system:app:edit")
    @Log(title = "APP版本描述", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/put")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody UpdateAppBo bo) {
        return toAjax(updateAppService.updateByBo(bo));
    }

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

    /**
     * 获取当前最新版本数据
     */
    @GetMapping(value = "/new")
    public AjaxResult getInfoNew(UpdateAppBo bo)
    {
        return updateAppService.getInfoNew(bo);
    }
}
