package com.sundot.airport.web.controller.system;

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

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.sundot.airport.common.annotation.Log;
import com.sundot.airport.common.core.controller.BaseController;
import com.sundot.airport.common.core.domain.AjaxResult;
import com.sundot.airport.common.enums.BusinessType;
import com.sundot.airport.system.domain.SysApp;
import com.sundot.airport.system.service.ISysAppService;
import com.sundot.airport.common.utils.poi.ExcelUtil;
import com.sundot.airport.common.core.page.TableDataInfo;

/**
 * 应用Controller
 *
 * @author ruoyi
 * @date 2025-11-20
 */
@RestController
@RequestMapping("/system/app")
public class SysAppController extends BaseController {
    @Autowired
    private ISysAppService sysAppService;

    /**
     * 查询应用列表
     */
    @PreAuthorize("@ss.hasPermi('system:app:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysApp sysApp) {
        startPage();
        List<SysApp> list = sysAppService.selectSysAppList(sysApp);
        return getDataTable(list);
    }

    /**
     * 导出应用列表
     */
    @PreAuthorize("@ss.hasPermi('system:app:export')")
    @Log(title = "应用", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysApp sysApp) {
        List<SysApp> list = sysAppService.selectSysAppList(sysApp);
        ExcelUtil<SysApp> util = new ExcelUtil<SysApp>(SysApp.class);
        util.exportExcel(response, list, "应用数据");
    }

    /**
     * 获取应用详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:app:query')")
    @GetMapping(value = "/{appId}")
    public AjaxResult getInfo(@PathVariable("appId") Long appId) {
        return success(sysAppService.selectSysAppByAppId(appId));
    }

    /**
     * 新增应用
     */
    @PreAuthorize("@ss.hasPermi('system:app:add')")
    @Log(title = "应用", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysApp sysApp) {
        sysApp.setCreateByUserId(getUserId());
        sysApp.setCreateBy(getLoginUser().getUser().getNickName());
        return toAjax(sysAppService.insertSysApp(sysApp));
    }

    /**
     * 修改应用
     */
    @PreAuthorize("@ss.hasPermi('system:app:edit')")
    @Log(title = "应用", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysApp sysApp) {
        sysApp.setUpdateByUserId(getUserId());
        sysApp.setUpdateBy(getLoginUser().getUser().getNickName());
        return toAjax(sysAppService.updateSysApp(sysApp));
    }

    /**
     * 删除应用
     */
    @PreAuthorize("@ss.hasPermi('system:app:remove')")
    @Log(title = "应用", businessType = BusinessType.DELETE)
    @DeleteMapping("/{appIds}")
    public AjaxResult remove(@PathVariable Long[] appIds) {
        return toAjax(sysAppService.deleteSysAppByAppIds(appIds));
    }

    /**
     * 查询全部应用列表
     */
    @PreAuthorize("@ss.hasPermi('system:app:list')")
    @GetMapping("/listAll")
    public AjaxResult listAll(SysApp sysApp) {
        List<SysApp> list = sysAppService.selectSysAppList(sysApp);
        return success(list);
    }

    /**
     * 移动端查询全部应用列表
     * homePage:0-非首页应用;1-首页应用
     */
//    @PreAuthorize("@ss.hasPermi('system:app:list')")
    @GetMapping("/listBy")
    public AjaxResult listBy(SysApp sysApp) {
        List<SysApp> list = sysAppService.listBy(sysApp);
        return success(list);
    }

    /**
     * 根据角色ID查询应用列表
     */
    @GetMapping(value = "/roleAppSelect/{roleId}")
    public AjaxResult roleAppSelect(@PathVariable("roleId") Long roleId) {
        AjaxResult result = sysAppService.roleAppSelect(roleId);
        return result;
    }
}
