/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.biz.modular.user.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;
import vip.xiaonuo.biz.modular.user.entity.UserWxOpen;
import vip.xiaonuo.biz.modular.user.mapper.UserWxOpenMapper;
import vip.xiaonuo.common.util.DatesUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 用户微信关注Service
 *
 * @author xuyuxiang
 * @date 2022/4/21 18:35
 **/
@Service
public class UserWxOpenService extends ServiceImpl<UserWxOpenMapper, UserWxOpen> {

    /**
     * 获取订阅
     *
     * @author xuyuxiang
     * @date 2022/9/6 17:39
     */
    public List<UserWxOpen> list(List<String> userIds) {
        if (CollUtil.isEmpty(userIds)) return new ArrayList<>();
        LambdaQueryWrapper<UserWxOpen> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(UserWxOpen::getUserId, userIds);
        wrapper.eq(UserWxOpen::getEvent, "subscribe");
        return list(wrapper);
    }

    /**
     * 获取用户
     *
     * @author xuyuxiang
     * @date 2022/9/6 17:39
     */
    public UserWxOpen getOne(String userId, String unionId) {
        LambdaQueryWrapper<UserWxOpen> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Strings.isNotBlank(userId), UserWxOpen::getUserId, userId);
        wrapper.eq(Strings.isNotBlank(unionId), UserWxOpen::getUnionId, unionId);
        List<UserWxOpen> list = list(wrapper);
        return list.isEmpty() ? new UserWxOpen() : list.get(0);
    }

    /**
     * 保存用户
     *
     * @author xuyuxiang
     * @date 2022/9/6 17:39
     */
    public void save(String userId, String unionId, String miniOpenId) {
        UserWxOpen one = getOne(userId, null);

        one.setUserId(userId);
        one.setUnionId(unionId);
        one.setMiniOpenId(miniOpenId);

        //旧数据解绑
        LambdaUpdateWrapper<UserWxOpen> wrapper = new LambdaUpdateWrapper<>();
        wrapper.and(w ->
                w.eq(UserWxOpen::getUnionId, unionId)
                        .or().eq(UserWxOpen::getMiniOpenId, miniOpenId));
        wrapper.set(UserWxOpen::getUnionId, null);
        wrapper.set(UserWxOpen::getMiniOpenId, null);
        wrapper.set(UserWxOpen::getEvent, null);
        update(wrapper);

        saveOrUpdate(one);
    }

    /**
     * 订阅
     *
     * @author xuyuxiang
     * @date 2022/9/6 17:39
     */
    public void subscribe(String unionId, String openId, String event) {
        UserWxOpen eventOne = null;
        //旧数据解绑
        LambdaQueryWrapper<UserWxOpen> wrapper = new LambdaQueryWrapper<>();
        wrapper.and(w ->
                w.eq(UserWxOpen::getUnionId, unionId)
                        .or().eq(UserWxOpen::getOpenId, openId));
        List<UserWxOpen> upList = list(wrapper);
        List<String> deleteIdLst = new ArrayList<>();
        for (UserWxOpen one : upList) {
            if (eventOne == null && Strings.isNotBlank(one.getUserId()) && Objects.equals(unionId, one.getUnionId()))
                eventOne = new UserWxOpen(one.getId(), openId, event, DatesUtil.forDateTime());
            else
                deleteIdLst.add(one.getId());
        }
        if (eventOne != null)
            updateById(eventOne);
        if (!deleteIdLst.isEmpty())
            removeBatchByIds(deleteIdLst);
    }

    /**
     * 取消订阅
     *
     * @author xuyuxiang
     * @date 2022/9/6 17:39
     */
    public void unsubscribe(String openId) {
        LambdaUpdateWrapper<UserWxOpen> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(UserWxOpen::getOpenId, openId);
        wrapper.set(UserWxOpen::getEvent, "unsubscribe");
        wrapper.set(UserWxOpen::getEventTime, DatesUtil.forDateTime());
        update(wrapper);
    }

}
