package com.ruoyi.web.controller.api;


import com.ruoyi.common.utils.ApiResult;
import com.ruoyi.mall.service.IMallMemberCmsService;
import com.ruoyi.mall.service.IMallMemberRegisterService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.MapUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 用户注册
 * @author chang
 * @date 2021-11-25
 */
@RestController
@RequestMapping("/api/memberRegister")
public class ApiMemberRegisterController {

    @Resource
    private IMallMemberRegisterService mallMemberRegisterService;

    @Resource
    private IMallMemberCmsService mallMemberCmsService;

    /**
     * 用户提交注册信息
     * @return
     */
    @PostMapping("/submitMemberRegister")
    @ApiOperation("用户提交注册信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "registerPhone", value = "手机号码", dataType = "String"),
            @ApiImplicitParam(name = "shopName", value ="门店名称", dataType = "String"),
            @ApiImplicitParam(name = "personName", value ="店主姓名", dataType = "String"),
            @ApiImplicitParam(name = "province", value ="省份", dataType = "String"),
            @ApiImplicitParam(name = "city", value ="城市", dataType = "String"),
            @ApiImplicitParam(name = "area", value ="区域", dataType = "String"),
            @ApiImplicitParam(name = "address", value ="详细地址", dataType = "String"),
            @ApiImplicitParam(name = "password", value ="密码", dataType = "String"),
            @ApiImplicitParam(name = "cmsCode", value ="验证码", dataType="String")
    })
    public ApiResult submitMemberRegister(@RequestBody Map<String,Object> param){
        if (!param.containsKey("personName") || MapUtils.getString(param,"personName") == null){
            return ApiResult.error("缺少personName");
        }
        if (!param.containsKey("registerPhone") || MapUtils.getString(param,"registerPhone") == null){
            return ApiResult.error("缺少registerPhone");
        }
        if (!param.containsKey("shopName") || MapUtils.getString(param,"shopName") == null){
            return ApiResult.error("缺少shopName");
        }
        if (!param.containsKey("province") || MapUtils.getString(param,"province") == null){
            return ApiResult.error("缺少province");
        }
        if (!param.containsKey("city") || MapUtils.getString(param,"city") == null){
            return ApiResult.error("缺少city");
        }
        if (!param.containsKey("area") || MapUtils.getString(param,"area") == null){
            return ApiResult.error("缺少area");
        }
        if (!param.containsKey("address") || MapUtils.getString(param,"address") == null){
            return ApiResult.error("缺少address");
        }
        if (!param.containsKey("password") || MapUtils.getString(param,"password") == null){
            return ApiResult.error("缺少password");
        }
        if (!param.containsKey("cmsCode") || MapUtils.getString(param,"cmsCode") == null){
            return ApiResult.error("缺少cmsCode");
        }
        try {
            //比对验证码
            int i = mallMemberCmsService.checkCmsCode(MapUtils.getString(param, "registerPhone"), MapUtils.getString(param, "cmsCode"));
            if(i == 1){
                return ApiResult.error("验证码已过期");
            }else if( i == 2){
                return ApiResult.error("验证码错误 ");
            }
            return mallMemberRegisterService.submitMemberRegister(param);
        }catch (Exception e){
            e.printStackTrace();
            return ApiResult.error("服务器异常");
        }
    }

}
