package org.jeecg.modules.shop.user.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import lombok.RequiredArgsConstructor;
import org.jeecg.modules.sequence.service.IMiddleSequenceService;
import org.jeecg.config.shiro.util.ShopTokenUtil;
import org.jeecg.modules.shop.captcha.enums.CaptchaKeyEnum;
import org.jeecg.modules.shop.captcha.enums.CaptchaTypeEnum;
import org.jeecg.modules.shop.captcha.service.ICaptchaService;
import org.jeecg.modules.shop.user.entity.PartnerUser;
import org.jeecg.modules.shop.user.entity.dto.UserInfoDTO;
import org.jeecg.modules.shop.user.entity.dto.UserLoginDTO;
import org.jeecg.modules.shop.user.entity.vo.UserInfoVO;
import org.jeecg.modules.shop.user.entity.vo.UserLoginVO;
import org.jeecg.modules.shop.user.mapper.PartnerUserMapper;
import org.jeecg.modules.shop.user.service.IPartnerUserService;
import org.jeecg.modules.shop.user.util.partnerUserHolder;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.concurrent.TimeUnit;

/**
 * @Description: partner_user
 * @Author: jeecg-boot
 * @Date:   2025-07-09
 * @Version: V1.0
 */
@Service
@RequiredArgsConstructor
public class PartnerUserServiceImpl extends ServiceImpl<PartnerUserMapper, PartnerUser> implements IPartnerUserService {

    private final ICaptchaService captchaService;

    private final IMiddleSequenceService middleSequenceService;

    private final ShopTokenUtil shopTokenUtil;

    private final RedisTemplate<String, String> redisTemplate;
    @Override
    public UserLoginVO login(UserLoginDTO loginDTO) {
        PartnerUser user = null;
        String email = loginDTO.getEmail();
        String captchaKey = CaptchaKeyEnum.generateEmailCaptchaKey(
                CaptchaTypeEnum.SUPPLIER_LOGIN.getType(), email);
        String captcha = captchaService.getCaptcha(captchaKey, 5);
        Assert.isTrue(StrUtil.equals(loginDTO.getCaptcha(), captcha), "验证码无效");
        captchaService.clearCaptcha(captchaKey);
        // 查询用户
        LambdaQueryWrapper<PartnerUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PartnerUser::getEmail, email);
        user = this.getOne(queryWrapper);
        if (user == null) {
            // 创建新用户
            user = new PartnerUser();
            user.setId(String.valueOf(IdWorker.getId()));
            user.setEmail(email);
            user.setRegisterTime(DateUtil.date());
            user.setState(1);
            user.setLoginCount(1);
            user.setLoginTime(DateUtil.date());
            user.setBillNo(middleSequenceService.generate("HZ", 6));
            user.setInfoState(0);
            user.setAuditState(0);
            this.save(user);
        } else {
            // 更新登录信息
            user.setLoginCount(user.getLoginCount() + 1);
            user.setLoginTime(DateUtil.date());
            this.updateById(user);
        }
        UserLoginVO vo = new UserLoginVO();
        if (user.getState()==1) {
            vo.setToken(shopTokenUtil.createPartnerCustomerUserToken(user.getId()));
        }
        UserInfoVO userInfoVO = new UserInfoVO();
        BeanUtils.copyProperties(user, userInfoVO);
        vo.setUserInfo(userInfoVO);
        return vo;
    }

    @Override
    public void logout(String token) {
        if (StrUtil.isNotBlank(token)) {
            long remainingSeconds = shopTokenUtil.getRemainingSeconds(token);
            if (remainingSeconds > 0) {
                redisTemplate.opsForValue().set("shop:user:invalid-token:" + token, "1", remainingSeconds, TimeUnit.SECONDS);
            }
        }
    }

    @Override
    public UserInfoVO getUserInfo() {
        PartnerUser customerUser = getCurrentLoginCustomerUser();
        if (null == customerUser) {
            throw new RuntimeException("获取用户信息失败");
        }
        UserInfoVO userInfoVO = new UserInfoVO();
        BeanUtils.copyProperties(customerUser, userInfoVO);
        return userInfoVO;
    }

    @Override
    public void updateUserInfo(UserInfoDTO dto) {
        PartnerUser updateCustomerUser = new PartnerUser();
        BeanUtils.copyProperties(dto, updateCustomerUser);
        updateCustomerUser.setId(partnerUserHolder.currentLoginUserId());
        baseMapper.updateById(updateCustomerUser);
    }

    @Override
    public void submitInfo(UserInfoDTO dto) {
        PartnerUser updateCustomerUser = new PartnerUser();
        BeanUtils.copyProperties(dto, updateCustomerUser);
        updateCustomerUser.setId(partnerUserHolder.currentLoginUserId());
        updateCustomerUser.setInfoSubmitTime(DateUtil.date());
        baseMapper.updateById(updateCustomerUser);
    }

    private PartnerUser getCurrentLoginCustomerUser() {
        try {
            String userId = partnerUserHolder.currentLoginUserId(); // 此方法也提供给其他模块调用，所以加一下登录和类型判断
            PartnerUser customerUser = getById(userId);
            return customerUser;
        } catch (Exception e) {
            return null;
        }
    }
}
