package com.ruoyi.web.controller.system;
import java.util.List;
import jakarta.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.system.utils.Auth;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
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.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.AppDownload;
import com.ruoyi.system.service.IAppDownloadService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * app下载地址Controller
 *
 * @author ruoyi
 * @date 2025-06-20
 */
@RestController
@RequestMapping("/system/appDownload")
public class AppDownloadController extends BaseController
{
    @Autowired
    private IAppDownloadService appDownloadService;


    @Anonymous
    @GetMapping("/getAppDownload")
    public  AjaxResult getAppDownload()
    {
        AppDownload appDownload = appDownloadService.getOne(new LambdaQueryWrapper<AppDownload>().ne(AppDownload::getId,0L).orderByDesc(AppDownload::getId));
        return AjaxResult.success(appDownload);
    }

    /**
     * 查询app下载地址列表
     */
    @PreAuthorize("@ss.hasPermi('system:download:list')")
    @GetMapping("/list")
    public TableDataInfo list(AppDownload appDownload)
    {
        startPage();
        List<AppDownload> list = appDownloadService.selectAppDownloadList(appDownload);
        return getDataTable(list);
    }

    /**
     * 导出app下载地址列表
     */
    @PreAuthorize("@ss.hasPermi('system:download:export')")
    @Log(title = "app下载地址", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AppDownload appDownload)
    {
        List<AppDownload> list = appDownloadService.selectAppDownloadList(appDownload);
        ExcelUtil<AppDownload> util = new ExcelUtil<AppDownload>(AppDownload.class);
        util.exportExcel(response, list, "app下载地址数据");
    }

    /**
     * 获取app下载地址详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:download:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(appDownloadService.selectAppDownloadById(id));
    }

    /**
     * 新增app下载地址
     */
    @PreAuthorize("@ss.hasPermi('system:download:add')")
    @Log(title = "app下载地址", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AppDownload appDownload)
    {
        return toAjax(appDownloadService.insertAppDownload(appDownload));
    }

    /**
     * 修改app下载地址
     */
    @PreAuthorize("@ss.hasPermi('system:download:edit')")
    @Log(title = "app下载地址", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppDownload appDownload)
    {
        return toAjax(appDownloadService.updateAppDownload(appDownload));
    }

    /**
     * 删除app下载地址
     */
    @PreAuthorize("@ss.hasPermi('system:download:remove')")
    @Log(title = "app下载地址", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(appDownloadService.deleteAppDownloadByIds(ids));
    }
}
