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

import com.hc.business.dto.ZccmRetrievePasswordDto;
import com.hc.business.dto.ZccmSmsLoginDto;
import com.hc.business.model.ZccmUser;
import com.hc.business.model.login.AuthEntity;
import com.hc.business.service.ZccmUserService;
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;


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

@RestController
@RequestMapping("/new/api/app/user")
public class AppUserController {

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

    public AppUserController(ZccmUserService userService) {
        this.userService = userService;
    }

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

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

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

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

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

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

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

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

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

}