package com.lutao.cloud.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lutao.cloud.admin.api.dto.SysOauthClientDetailsDTO;
import com.lutao.cloud.admin.api.entity.SysOauthClientDetails;
import com.lutao.cloud.admin.service.SysOauthClientDetailsService;
import com.lutao.cloud.common.core.util.MessageInfo;
import com.lutao.cloud.common.log.annotation.SysLog;
import com.lutao.cloud.common.security.annotation.Inner;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

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

/**
 * <p>
 * 前端控制器
 * </p>
 *

 * @since 2018-05-15
 */
@Api(value = "/client", tags ={"<p> 前端控制器 </p>"})
@ApiIgnore
@RestController
@AllArgsConstructor
@RequestMapping("/client")
public class SysClientController{

	private final SysOauthClientDetailsService clientDetailsService;

	/**
	 * 通过ID查询
	 * @param clientId clientId
	 * @return SysOauthClientDetails
	 */
	@ApiOperation(value = "通过ID查询", notes = "通过ID查询", httpMethod = "GET")
	@GetMapping("/{clientId}")
	public MessageInfo<List<SysOauthClientDetails>> getByClientId(@PathVariable String clientId){
		return MessageInfo.ok(clientDetailsService
				.list(Wrappers.<SysOauthClientDetails>lambdaQuery().eq(SysOauthClientDetails::getClientId, clientId)));
	}

	/**
	 * 简单分页查询
	 * @param page 分页对象
	 * @param sysOauthClientDetails 系统终端
	 * @return
	 */
	@ApiOperation(value = "简单分页查询", notes = "简单分页查询", httpMethod = "GET")
	@GetMapping("/page")
	public MessageInfo<IPage<SysOauthClientDetailsDTO>> getOauthClientDetailsPage(Page page, SysOauthClientDetails sysOauthClientDetails){
		return MessageInfo.ok(clientDetailsService.queryPage(page, sysOauthClientDetails));
	}

	/**
	 * 添加
	 * @param clientDetailsDTO 实体
	 * @return success/false
	 */
	@ApiOperation(value = "添加", notes = "添加", httpMethod = "POST")
	@SysLog("添加终端")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('sys_client_add')")
	public MessageInfo<Boolean> add(@Valid @RequestBody SysOauthClientDetailsDTO clientDetailsDTO){
		return MessageInfo.ok(clientDetailsService.saveClient(clientDetailsDTO));
	}

	/**
	 * 删除
	 * @param clientId ID
	 * @return success/false
	 */
	@ApiOperation(value = "删除", notes = "删除", httpMethod = "DELETE")
	@SysLog("删除终端")
	@DeleteMapping("/{clientId}")
	@PreAuthorize("@pms.hasPermission('sys_client_del')")
	public MessageInfo<Boolean> removeById(@PathVariable String clientId){
		return MessageInfo.ok(clientDetailsService.removeByClientId(clientId));
	}

	/**
	 * 编辑
	 * @param clientDetailsDTO 实体
	 * @return success/false
	 */
	@ApiOperation(value = "编辑", notes = "编辑", httpMethod = "PUT")
	@SysLog("编辑终端")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('sys_client_edit')")
	public MessageInfo<Boolean> update(@Valid @RequestBody SysOauthClientDetailsDTO clientDetailsDTO){
		return MessageInfo.ok(clientDetailsService.updateClientById(clientDetailsDTO));
	}

	@ApiOperation(value = "", notes = "", httpMethod = "GET")
	@Inner(false)
	@GetMapping("/getClientDetailsById/{clientId}")
	public MessageInfo<SysOauthClientDetails> getClientDetailsById(@PathVariable String clientId){
		return MessageInfo.ok(clientDetailsService.getOne(
				Wrappers.<SysOauthClientDetails>lambdaQuery().eq(SysOauthClientDetails::getClientId, clientId), false));
	}

}
