package com.template.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.template.config.MySecurity;
import com.template.model.pojo.SmsCode;
import com.template.mapper.SmsCodeMapper;
import com.template.model.pojo.SystemUser;
import com.template.model.result.CommonResult;
import com.template.services.SmsCodeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.template.services.SystemUserService;
import com.template.common.utils.EncryptionUtil;
import com.template.common.utils.RSAUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ceshi
 * @since 2023-04-20
 */
@Service
public class SmsCodeServiceImpl extends ServiceImpl<SmsCodeMapper, SmsCode> implements SmsCodeService {

    @Autowired
    SystemUserService systemUserService;

    @Autowired
    MySecurity mySecurity;

    private static final int MAX_JSON_STRING = 40;

    @Override
    public CommonResult logIn(String phone, String code) {
        if (ObjectUtils.isEmpty(phone) || ObjectUtils.isEmpty(code)) {
            return CommonResult.fail("500", "参数为空");
        }
//        获取私钥
        String privateKey = mySecurity.getPrivateKey();
//        postman传参会将+转换成空格
        phone = phone.replace(" ", "+");
        code = code.replace(" ", "+");
        String decryptPhone;
        String decryptCode;
        try {
            decryptPhone = RSAUtils.decrypt(phone, RSAUtils.getPrivateKey(privateKey));
            decryptCode = RSAUtils.decrypt(code, RSAUtils.getPrivateKey(privateKey));
        } catch (Exception e) {
            return CommonResult.fail("500", "解密失败");
        }
        phone = decryptPhone;
        code = decryptCode;

        LambdaQueryWrapper<SmsCode> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SmsCode::getPhoneNumber, phone);
        wrapper.ge(SmsCode::getExpirationTime, new Date());
        wrapper.eq(SmsCode::getIsVerify, "0");
        SmsCode one = this.getOne(wrapper);
        if (ObjectUtils.isEmpty(one)) {
            return CommonResult.fail("500", "验证码已过期");
        }
        String code1 = one.getCode();
//            验证
        if (!(code1.equals(code))) {
            return CommonResult.fail("500", "验证码错误");
        }

        // 登录成功，生成token
        LambdaQueryWrapper<SystemUser> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(SystemUser::getPhoneNumber, phone);
        SystemUser one1 = systemUserService.getOne(wrapper2);
//        String token = JWTUtil.getToken(one1);
//        LoginVO loginVO = new LoginVO();
//        loginVO.setToken(token);
//        loginVO.setTokenTtl(JWTUtil.getExpired());
//        loginVO.setUserName(one1.getAccountName());
//        loginVO.setTelPhone(phone);

//        将返回值转成json字符串并加密
        String s = "";//JSON.toJSONString(loginVO);

        Map<Object, Object> encryption = new HashMap<>();
        try {
//        返回的加密字符串太长，无法解析，每次返回加密40长度的字符串
            EncryptionUtil encryptionUtil = new EncryptionUtil();
            encryption = encryptionUtil.encryption(s,mySecurity.getPublicKey());
        } catch (Exception e) {
            return CommonResult.fail("500", "加密失败");
        }
//        将未验证更改为已验证
//        SmsCode smsCode = new SmsCode();
//        BeanUtils.copyProperties(one, smsCode);
//        smsCode.setIsVerify("1");
//        this.updateById(smsCode);

        return CommonResult.ok(encryption);
    }




}
