package com.cf.imes.module.system.controller.admin.application;

import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.common.util.object.BeanUtils;
import com.cf.imes.module.system.controller.admin.application.vo.application.ApplicationPageReqVO;
import com.cf.imes.module.system.controller.admin.application.vo.application.ApplicationRespVO;
import com.cf.imes.module.system.controller.admin.application.vo.application.ApplicationSaveReqVO;
import com.cf.imes.module.system.controller.admin.application.vo.auth.ApplicationLoginRespVO;
import com.cf.imes.module.system.dal.dataobject.application.ApplicationDO;
import com.cf.imes.module.system.service.application.ApplicationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static com.cf.imes.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 应用注册信息")
@RestController
@RequestMapping("/system/application")
@Validated
public class ApplicationController {

    @Resource
    private ApplicationService applicationService;

    @PostMapping("/create")
    @Operation(summary = "新建应用注册")
    @PreAuthorize("@ss.hasPermission('system:application:create')")
    public CommonResult<Integer> createApplication(@Valid @RequestBody ApplicationSaveReqVO createReqVO) {
        return success(applicationService.createApplication(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新应用注册")
    @PreAuthorize("@ss.hasPermission('system:application:update')")
    public CommonResult<Boolean> updateApplication(@Valid @RequestBody ApplicationSaveReqVO updateReqVO) {
        applicationService.updateApplication(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除应用注册")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:application:delete')")
    public CommonResult<Boolean> deleteApplication(@RequestParam("id") Long id) {
        applicationService.deleteApplication(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得应用注册")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:application:query')")
    public CommonResult<ApplicationRespVO> getApplication(@RequestParam("id") Long id) {
        ApplicationDO applicationDO = applicationService.getApplication(id);
        return success(BeanUtils.toBean(applicationDO, ApplicationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得应用注册分页")
    @PreAuthorize("@ss.hasPermission('system:application:query')")
    public CommonResult<PageResult<ApplicationRespVO>> getApplicationPage(@Valid ApplicationPageReqVO pageReqVO) {
        PageResult<ApplicationDO> pageResult = applicationService.getApplicationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ApplicationRespVO.class));
    }

    @PostMapping("/appLogin")
    @Operation(summary = "应用登陆")
    @PreAuthorize("@ss.hasPermission('system:application:create')")
    public CommonResult<ApplicationLoginRespVO> loginApplication(@Valid @RequestBody ApplicationRespVO applicationRespVO) {
        return success(applicationService.loginApplication(applicationRespVO));
    }

}
