package cn.ezhizao.project.system.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import cn.ezhizao.project.system.domain.SysVersionMenu;
import cn.ezhizao.project.system.service.ISysVersionMenuService;
import org.springframework.security.access.prepost.PreAuthorize;
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 cn.ezhizao.framework.aspectj.lang.annotation.Log;
import cn.ezhizao.framework.aspectj.lang.enums.BusinessType;
import cn.ezhizao.project.system.domain.SysVersion;
import cn.ezhizao.project.system.service.ISysVersionService;
import cn.ezhizao.framework.web.controller.BaseController;
import cn.ezhizao.framework.web.domain.AjaxResult;
import cn.ezhizao.common.utils.poi.ExcelUtil;
import cn.ezhizao.framework.web.page.TableDataInfo;

/**
 * 版本管理Controller
 *
 * @author ruoyi
 * @date 2023-10-24
 */
@RestController
@RequestMapping("/system/version")
public class SysVersionController extends BaseController
{
    @Resource
    private ISysVersionService sysVersionService;
    @Resource
    private ISysVersionMenuService sysVersionMenuService;

    /**
     * 查询版本管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:version:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysVersion sysVersion) throws NoSuchFieldException, IllegalAccessException
    {
//        setTenantId(sysVersion);
        startPage();
        List<SysVersion> list = sysVersionService.getList(sysVersion);
        return getDataTable(list);
    }

    /**
     * 导出版本管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:version:export')")
    @Log(title = "版本管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysVersion sysVersion) throws NoSuchFieldException, IllegalAccessException
    {
//        setTenantId(sysVersion);
        List<SysVersion> list = sysVersionService.getList(sysVersion);
        ExcelUtil<SysVersion> util = new ExcelUtil<SysVersion>(SysVersion.class);
        util.exportExcel(response, list, "版本管理数据");
    }

    /**
     * 获取版本管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:version:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        SysVersion version = sysVersionService.getById(id);
        List<SysVersionMenu> sysVersionMenus = sysVersionMenuService.query().eq("version_id", version.getId()).list();
        version.setModules(sysVersionMenus);
        return success(version);
    }

    /**
     * 新增版本管理
     */
    @PreAuthorize("@ss.hasPermi('system:version:add')")
    @Log(title = "版本管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysVersion sysVersion) throws NoSuchFieldException, IllegalAccessException
    {
//        setTenantId(sysVersion);
        return toAjax(sysVersionService.save(sysVersion));
    }

    /**
     * 修改版本管理
     */
    @PreAuthorize("@ss.hasPermi('system:version:edit')")
    @Log(title = "版本管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysVersion sysVersion) throws NoSuchFieldException, IllegalAccessException
    {
//        setTenantId(sysVersion);
        return toAjax(sysVersionService.updateById(sysVersion));
    }

    /**
     * 删除版本管理
     */
    @PreAuthorize("@ss.hasPermi('system:version:remove')")
    @Log(title = "版本管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids)
    {
        return toAjax(sysVersionService.removeBatchByIds(ids));
    }
}
