package com.buy507.mall.portal.controller;

import com.buy507.mall.common.api.CommonResult;
import com.buy507.mall.portal.domain.*;
import com.buy507.mall.portal.service.UmsMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 会员管理Controller
 */
@Controller
@Api(tags = "UmsMemberController", description = "会员管理")
@RequestMapping("/member")
public class UmsMemberController {
	
    @Autowired
    private UmsMemberService memberService;
    

    @ApiOperation("注册")
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult register(@RequestParam String telephone,
    							 @RequestParam String password,
                                 @RequestParam String authCode) {
        return memberService.register(telephone, password, authCode);
    }


    @ApiOperation(value = "登录以后返回token")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult login(@RequestParam String telephone, @RequestParam String password) {
        Map<String, String> tokenMap = memberService.login(telephone, password);
        if (tokenMap == null) {
            return CommonResult.validateFailed("用户名或密码错误");
        }
        return CommonResult.success(tokenMap);
    }

    @ApiOperation(value = "微信登录接口，登录成功以后返回token")
    @RequestMapping(value = "/wechatLogin", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<Map<String, String>> wechatLogin(@RequestBody WeChatLogin weChatLogin) {

    	if(StringUtils.isEmpty(weChatLogin.getCode())) {
    		return CommonResult.failed("授权码code不能为空");
    	}

    	Map<String, String> tokenMap = memberService.wechatLogin(weChatLogin);
        if (tokenMap == null) {
            return CommonResult.unauthorized(null);
        }

        return CommonResult.success(tokenMap);
    }

    @ApiOperation("获取验证码")
    @RequestMapping(value = "/getAuthCode", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getAuthCode(@RequestParam String telephone) {
        return memberService.generateAuthCode(telephone);
    }

    /*
    @ApiOperation("修改密码")
    @RequestMapping(value = "/updatePassword", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updatePassword(@RequestParam String telephone,
                                 @RequestParam String password,
                                 @RequestParam String authCode) {
        return memberService.updatePassword(telephone,password,authCode);
    }
    */
    
    @ApiOperation("获取会员个人信息")
    @RequestMapping(value = "/getMemberInfo", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<MemberInfoResult> getMemberInfo() {
		MemberInfoResult result = memberService.getMemberInfo();
        return CommonResult.success(result);
    }
    
    
    @ApiOperation("修改会员昵称")
    @RequestMapping(value = "/updateMemberNickname", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateMemberNickname(
                                 @RequestParam String nickname
                                ) {
        return memberService.updateMemberNickname(nickname);
    }
    
    /*
    @ApiOperation("修改会员性别 gender=性别：0->未知；1->男；2->女")
    @RequestMapping(value = "/updateMemberGender", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateMemberGender(
                                 @RequestParam Integer gender
                                ) {
        return memberService.updateMemberGender(gender);
    } 
   
    @ApiOperation("修改会员头像")
    @RequestMapping(value = "/updateMemberIcon", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateMemberIcon(
                                 @RequestParam String icon
                                ) {
        return memberService.updateMemberIcon(icon);
    }
    */
    
    @ApiOperation("修改会员手机号")
    @RequestMapping(value = "/updateMemberPhone", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateMemberPhone(
                                 @RequestParam String authCode,
                                 @RequestParam String newPhone
                                ) {
        return memberService.updateMemberPhone(newPhone, authCode);
    }
    
    @ApiOperation("修改真实姓名")
    @RequestMapping(value = "/updateRealName", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<String> updateRealName(@RequestParam String realName) {
        return memberService.updateRealName(realName);
    }
    
    @ApiOperation("修改联系地址")
    @RequestMapping(value = "/updateAddress", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<String> updateAddress(@RequestParam String address) {
        return memberService.updateAddress(address);
    }
    
    /*
    @ApiOperation("微信分享填写邀请人")
    @RequestMapping(value = "/addInviter", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult addInviter(@RequestParam Long memberId) {
    	return memberService.addInviter(memberId);
    }
    */
    
    @ApiOperation("获取森态积分明细")
    @RequestMapping(value = "/getAccountDetail", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getAccountDetail(@RequestParam Integer pageSize, @RequestParam Integer pageNum) {
    	return CommonResult.success(memberService.getAccountDetail(pageSize, pageNum));
    }
    
    /*
    @ApiOperation("获取冻结积分明细")
    @RequestMapping(value = "/getFreezeAccountDetail", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getFreezeAccountDetail(@RequestParam Integer pageSize, @RequestParam Integer pageNum) {
    	return CommonResult.success(memberService.getAccountDetail(AccountType.Freeze, pageSize, pageNum));
    }
    */
    
    @ApiOperation("获取团队成员列表")
    @RequestMapping(value = "/getTeamList", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getTeamList(@RequestParam Long memberId, @RequestParam Integer pageSize, @RequestParam Integer pageNum) {
    	return CommonResult.success(memberService.getTeamList(memberId, pageSize, pageNum));
    }
    
    @ApiOperation("获取会员提现记录")
    @RequestMapping(value = "/getWithdrawApply", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getWithdrawApply(@RequestParam Integer pageSize, @RequestParam Integer pageNum) {
    	return CommonResult.success(memberService.getWithdrawApply(pageSize, pageNum));
    }
    
    @ApiOperation("会员提现申请")
    @RequestMapping(value = "/withdrawApply", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult withdrawApply(@RequestBody WithdrawApplyParam applyParam) {
    	return memberService.withdrawApply(applyParam);
    }
    
    @ApiOperation("会员转账")
    @RequestMapping(value = "/transfer", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult transfer(@RequestBody MemberTransferParam transferParam) {
    	return memberService.transfer(transferParam);
    }
    
    @ApiOperation("获取会员转账记录")
    @RequestMapping(value = "/getTransferList", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getTransferList(@RequestParam Integer pageSize, @RequestParam Integer pageNum) {
    	return CommonResult.success(memberService.getTransferList(pageSize, pageNum));
    }
    
    @ApiOperation("注册完善个人资料")
    @RequestMapping(value = "/firstSaveMemberInfo", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<String> firstSaveMemberInfo(@RequestBody FirstMemberInfoParam memberInfoParam) {
    	return memberService.firstSaveMemberInfo(memberInfoParam);
    }
    
    @ApiOperation("根据手机号获取用户真实姓名")
    @RequestMapping(value = "/getRealName", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Map<String, String>> getRealName(@RequestParam String phone) {
    	return memberService.getRealName(phone);
    }
    
}
