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.UserInfo;
import com.lutao.cloud.admin.api.entity.SysSocialDetails;
import com.lutao.cloud.admin.service.SysSocialDetailsService;
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;

/**
 * 系统社交登录账号表
 *

 * @date 2018-08-16 21:30:41
 */
@Api(value = "/social", tags ={"系统社交登录账号表"})
@ApiIgnore
@RestController
@RequestMapping("/social")
@AllArgsConstructor
public class SysSocialDetailsController {

	private final SysSocialDetailsService sysSocialDetailsService;

	/**
	 * 社交登录账户简单分页查询
	 * @param page 分页对象
	 * @param sysSocialDetails 社交登录
	 * @return
	 */
	@ApiOperation(value = "社交登录账户简单分页查询", notes = "社交登录账户简单分页查询", httpMethod = "GET")
	@GetMapping("/page")
	public MessageInfo<IPage<SysSocialDetails>> getSocialDetailsPage(Page page, SysSocialDetails sysSocialDetails){
		return MessageInfo.ok(sysSocialDetailsService.page(page, Wrappers.query(sysSocialDetails)));
	}

	/**
	 * 信息
	 * @param type 类型
	 * @return R
	 */
	@ApiOperation(value = "信息", notes = "信息", httpMethod = "GET")
	@GetMapping("/{type}")
	public MessageInfo<List<SysSocialDetails>> getByType(@PathVariable("type") String type){
		return MessageInfo.ok(sysSocialDetailsService
				.list(Wrappers.<SysSocialDetails>lambdaQuery().eq(SysSocialDetails::getType, type)));
	}

	/**
	 * 保存
	 * @param sysSocialDetails
	 * @return R
	 */
	@ApiOperation(value = "保存", notes = "保存", httpMethod = "POST")
	@SysLog("保存三方信息")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('sys_social_details_add')")
	public MessageInfo<Boolean> save(@Valid @RequestBody SysSocialDetails sysSocialDetails){
		return MessageInfo.ok(sysSocialDetailsService.save(sysSocialDetails));
	}

	/**
	 * 修改
	 * @param sysSocialDetails
	 * @return R
	 */
	@ApiOperation(value = "修改", notes = "修改", httpMethod = "PUT")
	@SysLog("修改三方信息")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('sys_social_details_edit')")
	public MessageInfo<Boolean> updateById(@Valid @RequestBody SysSocialDetails sysSocialDetails){
		sysSocialDetailsService.updateById(sysSocialDetails);
		return MessageInfo.ok(Boolean.TRUE);
	}

	/**
	 * 删除
	 * @param id
	 * @return R
	 */
	@ApiOperation(value = "删除", notes = "删除", httpMethod = "DELETE")
	@SysLog("删除三方信息")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('sys_social_details_del')")
	public MessageInfo<Boolean> removeById(@PathVariable Integer id){
		return MessageInfo.ok(sysSocialDetailsService.removeById(id));
	}

	/**
	 * 通过社交账号、手机号查询用户、角色信息
	 * @param inStr appid@code
	 * @return
	 */
	@ApiOperation(value = "通过社交账号、手机号查询用户、角色信息", notes = "通过社交账号、手机号查询用户、角色信息", httpMethod = "GET")
	@Inner
	@GetMapping("/info/{inStr}")
	public MessageInfo<UserInfo> getUserInfo(@PathVariable String inStr){
		return MessageInfo.ok(sysSocialDetailsService.getUserInfo(inStr));
	}

	/**
	 * 绑定社交账号
	 * @param state 类型
	 * @param code code
	 * @return
	 */
	@ApiOperation(value = "绑定社交账号", notes = "绑定社交账号", httpMethod = "POST")
	@PostMapping("/bind")
	public MessageInfo<Boolean> bindSocial(String state, String code){
		return MessageInfo.ok(sysSocialDetailsService.bindSocial(state, code));
	}

}
