package com.ruoyi.interfaces.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.interfaces.core.page.Page;
import com.ruoyi.interfaces.core.page.PageParam;
import com.ruoyi.interfaces.domain.PtApp;
import com.ruoyi.interfaces.service.PtAppService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@RestController
@RequestMapping("/pt/app")
public class PtAppController extends BaseController {

    @Autowired
    private PtAppService ptAppService;

    @ApiOperation("添加应用")
    @PostMapping(value = "/add")
    public AjaxResult add(@RequestBody PtApp ptApp) {
        ptApp.setUserId(String.valueOf(getUserId()));
        return AjaxResult.check(ptAppService.insert(ptApp) > 0);
    }

    @ApiOperation("修改应用")
    @PostMapping(value = "/update")
    public AjaxResult update(@RequestBody PtApp ptApp) {
        return AjaxResult.check(ptAppService.update(ptApp) > 0);
    }

    @ApiOperation("删除应用")
    @GetMapping(value = "/delete")
    public AjaxResult deletes(String keys) {
        return AjaxResult.check(ptAppService.del(keys));
    }

    @ApiOperation("查询所有应用")
    @PostMapping(value = "/list")
    public Page list(@RequestBody PageParam<PtApp> ptAppPageParam) {
        ptAppPageParam.setData(Optional.of(ptAppPageParam).map(PageParam::getData).orElse(new PtApp()));
        ptAppPageParam.getData().setUserId(String.valueOf(getUserId()));
        Optional.of(ptAppPageParam.getData())
                .map(PtApp::getAppName)
                .filter(StringUtils::isNotBlank)
                .ifPresent(appName -> ptAppPageParam.getData().setAppName(appName.trim()));
        return ptAppService.listOfPage(ptAppPageParam);
    }

    @ApiOperation("查询所有应用")
    @PostMapping(value = "/lists")
    public AjaxResult lists(PtApp ptApp) {
        ptApp.setUserId(String.valueOf(getUserId()));
        Optional.of(ptApp)
                .map(PtApp::getAppName)
                .filter(StringUtils::isNotBlank)
                .ifPresent(appName -> ptApp.setAppName(appName.trim()));
        return AjaxResult.success(ptAppService.list(ptApp));
    }
}
