package org.springblade.gateway.login_gateway.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.api.R;
import org.springblade.gateway.login_gateway.service.LoginService;
import org.springblade.sing.user.entity.LoginUser;
import org.springblade.sing.user.service.ILoginUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * @author cy-computer
 */
@RestController
@RequestMapping("/user/login")
@Api(value = "用户登录控制器", tags = "登录接口")
@AllArgsConstructor
public class LoginController {

	private LoginService loginService;
	private ILoginUserService loginUserService;

	/**
	 * 登陆校验
	 * 判断是否为移动用户
	 */
	@GetMapping("/isCMCC")
	public R<Boolean> isCMCC(@ApiParam(value = "手机号",required = true) @RequestParam String phone){
		return R.data(loginService.isCMCC(phone));
	}

	/**
	 * 用户登录
	 */
	@PostMapping("/login")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "用户登录", notes = "传入loginUser")
	public R login(@Valid @RequestBody LoginUser loginUser) {
		LoginUser user = loginUserService.getOne(Condition.getQueryWrapper(new LoginUser()).lambda().eq(LoginUser::getOpenid, loginUser.getOpenid()));
		if(user==null){
			user = new LoginUser();
			user.setAvatar(loginUser.getAvatar());
			user.setNickName(loginUser.getNickName());
			user.setOpenid(loginUser.getOpenid());
			user.setSex(loginUser.getSex());
			loginUserService.save(user);
		}
		return R.data(user);
	}
}
