package com.hc.web.controller.browser.front;

import com.hc.business.dto.RetrievePasswordDto;
import com.hc.business.dto.SmsLoginDto;
import com.hc.business.model.User;
import com.hc.business.model.UserEditMobile;
import com.hc.business.model.login.AuthEntity;
import com.hc.business.service.UserService;
import com.hc.core.utils.result.Result;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;


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


/**
 * 用户表(User)表控制层
 *
 * @author java
 * @since 2020-11-09 10:04:14
 */

@RestController
@RequestMapping("/new/api/front/user")
public class FrontUserController {

    /**
     * 服务对象
     */
    private final UserService userService;

    public FrontUserController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "依据主键获取 User", response = Result.class)
    public Result selectFrontDetail(@PathVariable("id") Integer id) {
        return Result.success(this.userService.queryById(id));
    }

    @PostMapping("/login")
    @ApiOperation(value = "用户登录(账号,密码)", response = Result.class)
    public Result userLogin(@RequestBody AuthEntity authEntity) {
        return Result.success(userService.userLogin(authEntity));
    }

    @PostMapping("/login/sms")
    @ApiOperation(value = "用户登录(短信登录)", response = Result.class)
    public Result userLoginSms(@Valid @RequestBody SmsLoginDto smsLoginDto) {
        return Result.success(userService.userLoginSms(smsLoginDto));
    }

    @PostMapping("/register")
    @ApiOperation(value = "用户注册", response = Result.class)
    public Result userRegister(@Valid @RequestBody RetrievePasswordDto retrievePasswordDto) {
        return Result.success(userService.userRegister(retrievePasswordDto));
    }

    @PostMapping("/send")
    @ApiOperation(value = "短信发送验证码", response = Result.class)
    public Result sendAuthCode(@ApiParam(value = "手机号") @RequestParam String userMobile) {
        return Result.success(userService.sendAuthCode(userMobile));
    }

    @GetMapping("/mobile")
    @ApiOperation(value = "依据手机号查询用户", response = Result.class)
    public Result queryByMobile(@ApiParam(value = "手机号") @RequestParam String userMobile) {
        return Result.success(userService.queryByMobile(userMobile));
    }

    @PostMapping("/retrievePassword")
    @ApiOperation(value = "找回密码", response = Result.class)
    public Result retrievePassword(@Valid @RequestBody RetrievePasswordDto retrievePasswordDto) {
        return Result.success(userService.retrievePassword(retrievePasswordDto));
    }

    @PostMapping("/editMobile")
    @ApiOperation(value = "修改手机号码", response = Result.class)
    public Result editMobile(@Valid @RequestBody UserEditMobile userEditMobile) {
        User user = userService.queryById(userEditMobile.getUserId());
        user.setUserMobile(userEditMobile.getUserMobile());
        //查找这个手机得使用情况
        List<User> users = userService.queryByMobile(userEditMobile.getUserMobile());
        if(users.stream().count()>0)
        {
            User user0 = users.get(0);
            if(!user.getUserId().equals(user0.getUserId()))
            {
                return Result.exception(611, "手机号码已经被使用，无法修改");
            }
        }
        return Result.success(userService.updateMobile(user));
    }



    @PostMapping("/insert")
    @ApiOperation(value = "新增 User", response = Result.class)
    public Result addUser(@Valid @RequestBody User user) {
        return Result.success(userService.insert(user));
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改 User", response = Result.class)
    public Result updateUser(@RequestBody User user) {
        return Result.success(userService.update(user));
    }

}