package com.gic.haoban.manage.service.service.chat.impl;

import com.alibaba.fastjson.JSON;
import com.gic.commons.util.UniqueIdUtils;
import com.gic.haoban.manage.api.enums.chat.ChatActivityStatusEnum;
import com.gic.haoban.manage.service.dao.mapper.chat.GroupChatActivityMapper;
import com.gic.haoban.manage.service.dao.mapper.chat.GroupChatActivityShareBindMapper;
import com.gic.haoban.manage.service.entity.chat.TabGroupChatActivity;
import com.gic.haoban.manage.service.entity.chat.TabGroupChatActivityShareBind;
import com.gic.haoban.manage.service.service.chat.GroupChatActivityShareBindService;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 社群邀请有礼邀请人和受邀人绑定表(TabHaobanGroupChatActivityShareBind)表服务实现类
 *
 * @author makejava
 * @since 2024-09-27 16:09:58
 */
@Service("groupChatActivityShareBindService")
public class GroupChatActivityShareBindServiceImpl implements GroupChatActivityShareBindService {

    private static final Logger logger = LogManager.getLogger(GroupChatActivityShareBindService.class);

    @Autowired
    private GroupChatActivityShareBindMapper groupChatActivityShareBindMapper;
    @Autowired
    private GroupChatActivityMapper groupChatActivityMapper;

    @Override
    public void shareBing(TabGroupChatActivityShareBind shareBind) {
        logger.info("绑定分享关系参数:{}", JSON.toJSONString(shareBind));
        Long chatActivityId = shareBind.getChatActivityId();
        String inviteeUnionId = shareBind.getInviteeUnionId();
        String inviterMemberId = shareBind.getInviterMemberId();
        if (chatActivityId == null || StringUtils.isAnyBlank(inviteeUnionId, inviterMemberId)) {
            logger.info("绑定分享关系参数异常");
            return;
        }
        TabGroupChatActivity chatActivity = groupChatActivityMapper.queryById(chatActivityId);
        if (chatActivity == null) {
            return;
        }
        Integer status = chatActivity.getStatus();
        if (Objects.equals(status, ChatActivityStatusEnum.END.value) || Objects.equals(status, ChatActivityStatusEnum.STOP.value)) {
            logger.info("活动已结束");
            return;
        }
        shareBind.setEnterpriseId(chatActivity.getEnterpriseId());
        shareBind.setWxEnterpriseId(chatActivity.getWxEnterpriseId());
        shareBind.setChatActivityBindId(UniqueIdUtils.uniqueLong());
        groupChatActivityShareBindMapper.insert(shareBind);
    }

    @Override
    public List<TabGroupChatActivityShareBind> getLast(String inviteeUnionId) {
        return groupChatActivityShareBindMapper.query24Record(inviteeUnionId);
    }
}
