package com.ruoyi.cai.service.impl;

import cn.dev33.satoken.secure.BCrypt;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.cai.domain.Adolescent;
import com.ruoyi.cai.dto.app.query.user.AdolescentSettingReq;
import com.ruoyi.cai.dto.app.query.user.AdolescentSmsCheckReq;
import com.ruoyi.cai.dto.app.query.user.AdolescentSwitchReq;
import com.ruoyi.cai.enums.CodeEnum;
import com.ruoyi.cai.mapper.AdolescentMapper;
import com.ruoyi.cai.service.AdolescentService;
import com.ruoyi.cai.service.SmsVerifyService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.helper.LoginHelper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 青少年模式Service业务层处理
 *
 * @author 77
 * @date 2024-01-23
 */
@RequiredArgsConstructor
@Service
public class AdolescentServiceImpl extends ServiceImpl<AdolescentMapper,Adolescent> implements AdolescentService {

    @Autowired
    private SmsVerifyService smsVerifyService;


    @Override
    public void setAdolescent(AdolescentSettingReq setting) {
        if(StringUtils.isEmpty(setting.getPassword())){
            throw new ServiceException("密码不能为空");
        }
        Long userId = LoginHelper.getUserId();
        ReentrantLock lock = new ReentrantLock();
        try {
            lock.lock();
            Adolescent adolescent = this.getByUserId(userId);
            if(adolescent == null){
                adolescent = new Adolescent();
                adolescent.setUserId(userId);
            }
            adolescent.setOpen(1);
            adolescent.setPassword(BCrypt.hashpw(setting.getPassword()));
            this.saveOrUpdate(adolescent);
        }finally {
            lock.unlock();
        }
    }
    @Override
    public Adolescent getByUserId(Long userId){
        return this.getOne(Wrappers.lambdaQuery(Adolescent.class).eq(Adolescent::getUserId,userId).last("limit 1"));
    }

    @Override
    public void switchAdolescent(AdolescentSwitchReq adolescentSwitchReq) {
        if(adolescentSwitchReq.getIsOpen() == null) {
            throw new ServiceException("参数不正确");
        }else if(adolescentSwitchReq.getIsOpen() != 0 && adolescentSwitchReq.getIsOpen() != 1){
            throw new ServiceException("参数不正确");
        }
        Adolescent adolescent = this.getByUserId(LoginHelper.getUserId());
        if(adolescent == null || !BCrypt.checkpw(adolescentSwitchReq.getPassword(), adolescent.getPassword())){
            throw new ServiceException("密码错误");
        }
        if(adolescentSwitchReq.getIsOpen() == 0){
            this.removeById(adolescent);
        }else {
            adolescent.setOpen(1);
            this.updateById(adolescent);
        }
    }

    @Override
    public void verifyMobileCode(AdolescentSmsCheckReq req) {
        boolean check = smsVerifyService.check(CodeEnum.RESET_ADOLESCENT, req.getMobile(), req.getCode());
        if(!check){
            throw new ServiceException("验证码错误");
        }
        Long userId = LoginHelper.getUserId();
        this.remove(Wrappers.lambdaQuery(Adolescent.class).eq(Adolescent::getUserId,userId));
    }
}
