package com.codvision.auth.controller.manager;

import com.codvision.commoncore.common.ResponseEntity;
import com.codvision.auth.entity.vo.SysAppVO;
import com.codvision.auth.entity.SysApp;
import com.codvision.auth.service.ISysAppService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codvision.webcore.bean.QueryPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 系统应用 管理模块
 *
 * @author lingee
 * @date 2024-04-29
 */
@RestController
@RequestMapping("/v1/manager/sysApp")
@Api(tags = "平台管理端-系统应用模块")
public class ManagerSysAppController {

    @Autowired
    private ISysAppService sysAppService;

    @ApiOperation(value = "获取系统应用详情", notes = "获取系统应用详情接口")
    @PostMapping("/getDetail")
    public ResponseEntity<SysAppVO> getById(@ApiParam(value = "系统应用ID", required = true) @RequestParam Integer id) {
        SysAppVO vo = sysAppService.getDetail(id);
        return new ResponseEntity<SysAppVO>().ok().data(vo);
    }

    @ApiOperation(value = "获取系统应用分页列表", notes = "获取系统应用分页列表接口")
    @PostMapping("/getPageList")
    public ResponseEntity<IPage<SysAppVO>> getPageList(
            @ApiParam(value = "应用名称") @RequestParam(required = false) String name,
            @ApiParam(value = "页号", required = true, defaultValue = "1") @RequestParam(defaultValue = "1") Integer pageNumber,
            @ApiParam(value = "页面大小", required = true, defaultValue = "10") @RequestParam(defaultValue = "10") Integer pageSize) {
        SysApp queryBean = new SysApp();
        queryBean.setName(name);
        QueryPage<SysApp> queryPage = new QueryPage<>(queryBean, pageNumber, pageSize);
        IPage<SysAppVO> pageResult = sysAppService.getPageResult(queryPage);
        return new ResponseEntity<IPage<SysAppVO>>().ok().data(pageResult);
    }

    @ApiOperation(value = "新增系统应用", notes = "新增系统应用接口")
    @PostMapping("/save")
    public ResponseEntity<String> saveSysApp(@Valid @RequestBody SysApp sysApp) {
        sysAppService.save(sysApp);
        return new ResponseEntity<String>().ok().message("添加成功");
    }

    @ApiOperation(value = "删除系统应用", notes = "删除系统应用接口")
    @PostMapping("/delete")
    public ResponseEntity<String> deleteById(@ApiParam(value = "系统应用ID", required = true) @RequestParam Integer id) {
        sysAppService.removeById(id);
        return new ResponseEntity<String>().ok().message("删除成功");
    }

    @ApiOperation(value = "编辑系统应用", notes = "编辑系统应用接口")
    @PostMapping("/update")
    public ResponseEntity<String> updateSysApp(@RequestBody SysApp sysApp) {
        sysAppService.updateById(sysApp);
        return new ResponseEntity<String>().ok().message("编辑成功");
    }

    @ApiOperation(value = "获取系统应用所有列表" , notes = "获取系统应用所有列表接口")
    @PostMapping("/listAll")
    public ResponseEntity<List<SysAppVO>> listAll() {
        SysApp queryBean = new SysApp();
        List<SysAppVO> list = sysAppService.listAll(queryBean);
        return new ResponseEntity<List<SysAppVO>>().ok().data(list);
    }
}
