package cn.pluss.platform.controller.phonecode;

import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultCode;
import cn.pluss.platform.constants.Constant;
import cn.pluss.platform.entity.MerchantMenber;
import cn.pluss.platform.entity.PhoneValidateCode;
import cn.pluss.platform.entity.UserInfo;
import cn.pluss.platform.merchantMenber.MerchantMenberService;
import cn.pluss.platform.phoneValidateCode.PhoneValidateCodeService;
import cn.pluss.platform.userInfo.UserInfoService;
import cn.pluss.platform.util.StringUtil;
import cn.pluss.platform.util.ValidateCodeUtil;
import com.alibaba.fastjson.JSON;
import com.aliyuncs.exceptions.ClientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yuchen
 * @Description 验证码
 */
@Controller
public class PhoneCodeController {

    @Autowired
    private PhoneValidateCodeService phoneValidateCodeService;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private MerchantMenberService merchantMenberService;

    @Autowired
    private ValidateCodeUtil validateCodeUtil;

    public Object sevice(String param) throws ClientException {
        UserInfo userInfo = new UserInfo();
        userInfo.setLoginName(param);
        Map<String, Object> result = new HashMap<>(16);
        List<UserInfo> queryUserInfoList = userInfoService.queryUserInfoList(userInfo);
        if (queryUserInfoList != null && queryUserInfoList.size() > 0) {
            result.put(Constant.RESULT_CODE_FAIL, "手机号已注册");
            return JSON.toJSONString(result);
        }

        String checkCode = StringUtil.random(4);
        Result<Object> objectResult = validateCodeUtil.requestValidateCodeALi(param, checkCode);
        if (objectResult != null && objectResult.getCode() == ResultCode.FAIL.code()) {
            result.put(Constant.RESULT_CODE_FAIL, objectResult.getMessage());
            return JSON.toJSONString(result);
        }

        PhoneValidateCode phoneValidateCode = new PhoneValidateCode();
        phoneValidateCode.setPhone(param);
        phoneValidateCode.setSendDt(new Date());
        result.put(Constant.RESULT_CODE_SUCCESS, "发送成功");
        phoneValidateCode.setCode(checkCode);
        phoneValidateCode.setStatus("1");
        phoneValidateCodeService.savePhoneValidateCode(phoneValidateCode);
        String res = JSON.toJSONString(result);
        System.out.println(res);
        return res;
    }

    public Object seviceVIP(String phone, String storeId) throws ClientException {
        MerchantMenber merchantMenber = new MerchantMenber();
        merchantMenber.setPhone(phone);
        merchantMenber.setStoreId(storeId);
        Map<String, Object> result = new HashMap<>(16);
        List<MerchantMenber> queryMerchantMenberList = merchantMenberService.queryMerchantMenberList(merchantMenber);
        if (queryMerchantMenberList != null && queryMerchantMenberList.size() > 0) {
            result.put(Constant.RESULT_CODE_FAIL, "该门店已注册会员");
            return JSON.toJSONString(result);
        }

        String checkCode = StringUtil.random(4);

        Result<Object> objectResult = validateCodeUtil.requestValidateCodeALi(phone, checkCode);
        if (objectResult != null && objectResult.getCode() == ResultCode.FAIL.code()) {
            result.put(Constant.RESULT_CODE_FAIL, objectResult.getMessage());
            return JSON.toJSONString(result);
        }

        PhoneValidateCode phoneValidateCode = new PhoneValidateCode();
        phoneValidateCode.setPhone(phone);
        phoneValidateCode.setSendDt(new Date());
        result.put(Constant.RESULT_CODE_SUCCESS, "发送成功");
        phoneValidateCode.setCode(checkCode);
        phoneValidateCode.setStatus("1");
        phoneValidateCodeService.savePhoneValidateCode(phoneValidateCode);
        return JSON.toJSONString(result);
    }

}

