package com.cm.central.control.controller.apis.client;

import com.cm.central.control.pojo.dtos.client.ClientLicenseAuthorizationDTO;
import com.cm.common.annotation.CheckRequestBodyAnnotation;
import com.cm.common.base.AbstractController;
import com.cm.common.component.SecurityComponent;
import com.cm.common.constants.ISystemConstant;
import com.cm.common.exception.RemoveException;
import com.cm.common.exception.SearchException;
import com.cm.common.pojo.ListPage;
import com.cm.common.pojo.dtos.CurrentUserIdInfoDTO;
import com.cm.common.result.ErrorResult;
import com.cm.common.result.SuccessResult;
import com.cm.common.result.SuccessResultData;
import com.cm.common.result.SuccessResultList;
import com.cm.central.control.pojo.dtos.client.ClientDTO;
import com.cm.central.control.pojo.vos.client.ClientVO;
import com.cm.central.control.service.client.IClientService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @ClassName: ClientController
 * @Description: 客户端
 * @Author: WenG
 * @Date: 2020-07-30 19:12
 * @Version: 1.0
 **/
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "客户端接口")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/client")
public class ClientController extends AbstractController {

    @Autowired
    private IClientService clientService;
    @Autowired
    private SecurityComponent securityComponent;

    @ApiOperation(value = "新增客户端", notes = "新增客户端接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("saveclient")
    @CheckRequestBodyAnnotation
    public SuccessResult saveClient(@RequestBody ClientVO clientVO) throws Exception {
        return clientService.saveClient(clientVO);
    }

    @ApiOperation(value = "删除客户端(id列表)", notes = "删除客户端(id列表)接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "ID列表，用下划线分隔", paramType = "path", example = "1_2_3")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @DeleteMapping("removeclient/{ids}")
    public SuccessResult removeClient(@PathVariable("ids") String ids) throws RemoveException {
        return clientService.removeClient(ids);
    }

    @ApiOperation(value = "修改客户端", notes = "修改客户端接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "clientId", value = "客户端ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("updateclient/{clientId}")
    @CheckRequestBodyAnnotation
    public SuccessResult updateClient(@PathVariable("clientId") String clientId, @RequestBody ClientVO clientVO) throws Exception {
        return clientService.updateClient(clientId, clientVO);
    }

    @ApiOperation(value = "修改客户端授权", notes = "修改客户端授权接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "clientId", value = "客户端ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("updateclientlicense/{clientId}")
    @CheckRequestBodyAnnotation
    public SuccessResult updateClientLicense(@PathVariable("clientId") String clientId, @RequestBody ClientLicenseAuthorizationDTO clientLicenseAuthorizationDTO) throws Exception {
        return clientService.updateClientLicense(clientId, clientLicenseAuthorizationDTO);
    }

    @ApiOperation(value = "客户端详情(通过ID)", notes = "客户端详情(通过ID)接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "clientId", value = "客户端ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("getclientbyid/{clientId}")
    public ClientDTO getClientById(@PathVariable("clientId") String clientId) throws SearchException {
        return clientService.getClientById(clientId);
    }

    @ApiOperation(value = "客户端列表", notes = "客户端列表接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listclient")
    public List<ClientDTO> listClient() throws SearchException {
        Map<String, Object> params = requestParams();
        return clientService.listClient(params);
    }

    @ApiOperation(value = "客户端分页列表", notes = "客户端分页列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "rows", value = "显示数量", paramType = "query", dataType = "int", defaultValue = "20"),
            @ApiImplicitParam(name = "keywords", value = "关键字", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listpageclient")
    public SuccessResultList<List<ClientDTO>> listPageClient(ListPage page) throws SearchException {
        Map<String, Object> params = requestParams();
        page.setParams(params);
        return clientService.listPageClient(page);
    }

    @ApiOperation(value = "客户端统计", notes = "客户端统计接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("countclient")
    SuccessResultData<Integer> countClient() throws SearchException {
        Map<String, Object> params = requestParams();
        return clientService.countClient(params);
    }

    @ApiOperation(value = "客户端密码(通过ID)", notes = "客户端密码(通过ID)接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "clientId", value = "客户端ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("getclientsecretbyclientid/{clientId}")
    public SuccessResultData<String> getClientSecretByClientId(@PathVariable("clientId") String clientId) throws Exception {
        return clientService.getClientSecretByClientId(clientId);
    }

    @ApiOperation(value = "客户端license授权(通过ID)", notes = "客户端license授权(通过ID)接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "clientId", value = "客户端ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("getclientlicenseauthorizationbyclientid/{clientId}")
    public ClientLicenseAuthorizationDTO getClientLicenseAuthorizationByClientId(@PathVariable("clientId") String clientId) {
        return clientService.getClientLicenseAuthorizationByClientId(clientId);
    }

    @ApiOperation(value = "获取客户端license", notes = "获取客户端license接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "clientId", value = "客户端ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("getclientlicensebyclientid/{clientId}")
    public SuccessResultData<String> getClientLicenseByClientId(@PathVariable("clientId") String clientId) {
        return clientService.getClientLicenseByClientId(clientId);
    }

    @ApiOperation(value = "当前用户id信息", notes = "当前用户id信息接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("getcurrentuseridinfo")
    public CurrentUserIdInfoDTO getCurrentUserIdInfo() {
        return securityComponent.getCurrentUserIdInfo();
    }

    @ApiOperation(value = "获取客户端服务器信息", notes = "获取客户端服务器信息接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "clientId", value = "客户端ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("getclientinfobyclientid/{clientId}")
    public SuccessResultData<Map<String, Object>> getClientInfoByClientId(@PathVariable("clientId") String clientId) throws SearchException {
        return clientService.getClientInfoByClientId(clientId);
    }

}