package com.playlet.web.service.app.impl;

import cn.hutool.core.util.ObjectUtil;
import com.playlet.common.constant.PlayletConstants;
import com.playlet.common.constant.RedisConstants;
import com.playlet.system.domain.PlayletUser;
import com.playlet.system.service.IPlayletUserService;
import com.playlet.web.req.PlayUserReq;
import com.playlet.web.service.app.PlayletUserAppService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PlayletUserAppServiceImpl implements PlayletUserAppService {

    private final IPlayletUserService iPlayletUserService;

    private final StringRedisTemplate stringRedisTemplate;

    @Override
    public PlayletUser getByOpenId(String openid) {
        PlayletUser playletUser = iPlayletUserService.lambdaQuery().eq(PlayletUser::getOpenId, openid).one();
        if(ObjectUtil.isNull(playletUser)){
            playletUser = new PlayletUser();
            playletUser.setOpenId(openid);
            playletUser.setCreateBy(PlayletConstants.DEFAULT_CREATE);
            playletUser.setCreateTime(new Date());
            iPlayletUserService.save(playletUser);
        }
        return playletUser;
    }

    @Override
    public PlayletUser getByPhone(PlayUserReq playUserReq) throws Exception{
        String alreadyCode = stringRedisTemplate.opsForValue().get(RedisConstants.SMS_CODE_PREFIX + playUserReq.getPhone());
        if(StringUtils.isEmpty(alreadyCode)){
            throw new Exception("验证码已过期!");
        }
        if(!alreadyCode.equals(playUserReq.getCode())){
            throw new Exception("短信验证码错误!");
        }
        return iPlayletUserService.lambdaQuery()
                .eq(PlayletUser::getPhone, playUserReq.getPhone()).one();
    }

    @Override
    public PlayletUser getByPassword(PlayUserReq playUserReq) throws Exception {
        PlayletUser playletUser = iPlayletUserService.lambdaQuery()
                .eq(PlayletUser::getPhone, playUserReq.getPhone())
                .eq(PlayletUser::getPassword, playUserReq.getPassword()).one();
        if(playletUser == null){
            throw new Exception("账号或密码错误!");
        }else {
            return playletUser;
        }
    }

    @Override
    public void updatePlayletUser(PlayletUser playletUser) {
        iPlayletUserService.updateById(playletUser);
    }

    @Override
    public void updatePasswordByCode(PlayUserReq playUserReq) throws Exception{
        String alreadyCode = stringRedisTemplate.opsForValue().get(RedisConstants.SMS_CODE_PREFIX + playUserReq.getPhone());
        if(StringUtils.isEmpty(alreadyCode)){
            throw new Exception("验证码已过期!");
        }
        if(!alreadyCode.equals(playUserReq.getCode())){
            throw new Exception("短信验证码错误!");
        }
        PlayletUser user = iPlayletUserService.lambdaQuery()
                .eq(PlayletUser::getPhone, playUserReq.getPhone()).one();
        if(user == null){
            throw new Exception("用户尚未注册!");
        }else {
            user.setPassword(playUserReq.getPassword());
            iPlayletUserService.updateById(user);
        }

    }
}
