package com.storlead.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.storlead.framework.common.result.Result;
import com.storlead.system.pojo.dto.SysAppPageDTO;
import com.storlead.system.pojo.entity.SysAppEntity;
import com.storlead.system.service.SysAppService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 系统应用管理
 */
@Api(tags = "平台中心 - 系统: 应用管理")
@RestController
@RequestMapping("/sys/app")
public class SysAppApiController {

    @Resource
    private SysAppService sysAppService;

    @ApiOperation("获取应用")
    @PostMapping("/list")
    public Result<?> list(@RequestBody SysAppPageDTO dto) {
        return Result.result(
                sysAppService.listEnabled()
        );
    }


    @ApiOperation("应用分页")
    @PostMapping("/pagelist")
    public Result<?> pagelist(@RequestBody SysAppPageDTO dto) {
        return Result.result(sysAppService.pageQuery(dto));
    }

    @ApiOperation("已启用应用列表（下拉等）")
    @PostMapping("/listEnabled")
    public Result<?> listEnabled() {
        return Result.result(sysAppService.listEnabled());
    }

    @ApiOperation("详情")
    @PostMapping("/detail")
    public Result<?> detail(@RequestParam Long id) {
        SysAppEntity one = sysAppService.getDetail(id);
        if (one == null) {
            return Result.error("应用不存在或已删除");
        }
        return Result.result(one);
    }

    @ApiOperation("保存或更新")
    @PostMapping("/save")
    public Result<?> save(@RequestBody SysAppEntity body) {
        return sysAppService.saveApp(body);
    }

    @ApiOperation("启用/禁用")
    @PostMapping("/enable")
    public Result<?> enable(@RequestParam Long id, @RequestParam Boolean enabled) {
        return sysAppService.enableApp(id, enabled);
    }

    @ApiOperation("逻辑删除（无关联菜单时）")
    @PostMapping("/delete")
    public Result<?> delete(@RequestParam Long id) {
        return sysAppService.deleteApp(id);
    }
}
