package com.ruoyi.app.utils;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.system.mapper.AppVersionMapper;
import com.ruoyi.system.utils.GetArea;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
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.AppVersion;
import com.ruoyi.system.service.IAppVersionService;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * versionController
 * 
 * @author ruoyi
 * @date 2023-06-13
 */
@RestController
@RequestMapping("/system/version")
public class AppVersionController extends BaseController
{
    @Autowired
    private IAppVersionService appVersionService;
    @Autowired
    private AppVersionMapper appVersionMapper;

    //获取应用版本
    @Anonymous
    @GetMapping("/getversion")
    public AjaxResult getversion(@RequestParam Long platform,@RequestParam String name)
    {
        AppVersion version = appVersionMapper.getVersion(platform,name);
        return success(version);
    }
    /**
     * 查询version列表
     */
    @PreAuthorize("@ss.hasPermi('system:version:list')")
    @GetMapping("/list")
    public TableDataInfo list(AppVersion appVersion)
    {
        startPage();
        List<AppVersion> list = appVersionService.selectAppVersionList(appVersion);
        return getDataTable(list);
    }

    /**
     * 导出version列表
     */
    @PreAuthorize("@ss.hasPermi('system:version:export')")
    @Log(title = "version", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AppVersion appVersion)
    {
        List<AppVersion> list = appVersionService.selectAppVersionList(appVersion);
        ExcelUtil<AppVersion> util = new ExcelUtil<AppVersion>(AppVersion.class);
        util.exportExcel(response, list, MessageUtils.message("no.export.excel.version"));
    }

    /**
     * 获取version详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:version:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(appVersionService.selectAppVersionById(id));
    }

    /**
     * 新增version
     */
    @PreAuthorize("@ss.hasPermi('system:version:add')")
    @Log(title = "version", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AppVersion appVersion)
    {
        return toAjax(appVersionService.insertAppVersion(appVersion));
    }

    /**
     * 修改version
     */
    @PreAuthorize("@ss.hasPermi('system:version:edit')")
    @Log(title = "version", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppVersion appVersion)
    {
        return toAjax(appVersionService.updateAppVersion(appVersion));
    }

    /**
     * 删除version
     */
    @PreAuthorize("@ss.hasPermi('system:version:remove')")
    @Log(title = "version", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(appVersionService.deleteAppVersionByIds(ids));
    }
}
