package com.playlet.web.controller.app;

import com.github.pagehelper.PageInfo;
import com.playlet.common.core.domain.Result;
import com.playlet.system.domain.PlayletUser;
import com.playlet.web.req.PlayUserReq;
import com.playlet.web.service.app.PlayletUserAppService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Slf4j
@Api(tags = "小程序*短剧用户接口")
@RestController
@RequestMapping(value = "/app/user")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PlayletUserAppController {

    private final PlayletUserAppService playletUserAppService;

    @ResponseBody
    @PostMapping(value = "/loginByOpenid")
    @ApiOperation(value = "小程序通过openid登录系统(可选择是否带分销入参)", httpMethod = "POST")
    public Result<PlayletUser> loginByOpenid(@RequestParam(value = "openid") String openid,
                                             @RequestParam(value = "parentId", required = false) String parentId){
        try {
            return Result.success(playletUserAppService.getByOpenId(openid, parentId));
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @ResponseBody
    @PostMapping(value = "/loginByPhone")
    @ApiOperation(value = "小程序通过手机验证码登录系统", httpMethod = "POST")
    public Result<PlayletUser> loginByOpenid(@RequestBody PlayUserReq userReq){
        try {
            return Result.success(playletUserAppService.getByPhone(userReq));
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @ResponseBody
    @PostMapping(value = "/loginByPassword")
    @ApiOperation(value = "小程序通过手机和密码登录系统", httpMethod = "POST")
    public Result<PlayletUser> loginByPassword(@RequestBody PlayUserReq userReq){
        try {
            return Result.success(playletUserAppService.getByPassword(userReq));
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @ResponseBody
    @PostMapping(value = "/updatePlayletUser")
    @ApiOperation(value = "修改用户资料", httpMethod = "POST")
    public Result<String> updatePlayletUser(@RequestBody PlayletUser playletUser){
        try {
            playletUserAppService.updatePlayletUser(playletUser);
            return Result.success();
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @ResponseBody
    @PostMapping(value = "/getPlayletUserPage")
    @ApiOperation(value = "分页查询用户数据(暂分销列表使用)", httpMethod = "POST")
    public Result<PageInfo<PlayletUser>> getPlayletUserPage(@RequestBody PlayletUser playletUser,
                                                            @RequestParam(value = "pageNum") Integer pageNum,
                                                            @RequestParam(value = "pageSize") Integer pageSize){
        try {
            return Result.success(playletUserAppService.getPlayletUserPage(playletUser, pageNum, pageSize));
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @ResponseBody
    @PostMapping(value = "/updatePasswordByCode")
    @ApiOperation(value = "验证码修改密码", httpMethod = "POST")
    public Result<String> updatePasswordByCode(@RequestBody PlayUserReq playUserReq){
        try {
            playletUserAppService.updatePasswordByCode(playUserReq);
            return Result.success();
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

}
