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

import com.alibaba.fastjson.JSONObject;
import com.gic.api.base.commons.BusinessException;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.util.EntityUtil;
import com.gic.haoban.manage.api.dto.chat.GroupChatActivityHmDTO;
import com.gic.haoban.manage.api.dto.chat.GroupChatHmDTO;
import com.gic.haoban.manage.api.qdto.chat.GroupChatActivityHmQDTO;
import com.gic.haoban.manage.api.qdto.chat.GroupChatActivityShareBindQDTO;
import com.gic.haoban.manage.api.service.chat.GroupChatActivityShareApiService;
import com.gic.haoban.manage.service.entity.chat.TabGroupChatActivityShareBind;
//import com.gic.haoban.manage.service.pojo.bo.chat.GroupChatActivityBaseContext;
import com.gic.haoban.manage.service.pojo.bo.chat.GroupChatActivityBaseContext;
import com.gic.haoban.manage.service.pojo.bo.chat.GroupChatActivityHmBO;
import com.gic.haoban.manage.service.pojo.bo.chat.GroupChatHmBO;
import com.gic.haoban.manage.service.service.chat.GroupChatActivityInviteLogService;
import com.gic.haoban.manage.service.service.chat.GroupChatActivityService;
import com.gic.haoban.manage.service.service.chat.GroupChatActivityShareBindService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service("groupChatActivityShareApiService")
public class GroupChatActivityShareApiServiceImpl implements GroupChatActivityShareApiService {

    @Autowired
    private GroupChatActivityShareBindService activityShareBindService;
    @Autowired
    private GroupChatActivityService groupChatActivityService;
    @Autowired
    private GroupChatActivityInviteLogService groupChatActivityInviteLogService;

    @Override
    public ServiceResponse<Void> shareBind(GroupChatActivityShareBindQDTO shareBind) {
        String enterpriseId = shareBind.getEnterpriseId();
        Long chatActivityId = shareBind.getChatActivityId();
        String inviterMemberId = shareBind.getInviterMemberId();
        String inviteeUnionId = shareBind.getInviteeUnionId();
        String inviteeMemberId = shareBind.getInviteeMemberId();
        if (chatActivityId == null || StringUtils.isAnyBlank(enterpriseId,inviteeUnionId, inviterMemberId)) {
            return ServiceResponse.failure("500","参数异常");
        }
        if (Objects.equals(inviterMemberId, inviteeMemberId)) {
            return ServiceResponse.success();
        }
        TabGroupChatActivityShareBind bind = EntityUtil.changeEntityNew(TabGroupChatActivityShareBind.class, shareBind);
        activityShareBindService.shareBind(bind);
        return ServiceResponse.success();
    }

    @Override
    public ServiceResponse<List<GroupChatActivityHmDTO>> listGroupChatHm(GroupChatActivityHmQDTO activityHmQDTO) {
        if (StringUtils.isAnyBlank(activityHmQDTO.getEnterpriseId(),
                activityHmQDTO.getInviterMemberId()) || activityHmQDTO.getChatActivityId() == null) {
            return ServiceResponse.failure("500", "参数异常");
        }
        try {
            List<GroupChatActivityHmBO> hmBOList = groupChatActivityService.listGroupChatHm(activityHmQDTO);
            return ServiceResponse.success(EntityUtil.changeEntityListNew(GroupChatActivityHmDTO.class,hmBOList));
        } catch (BusinessException e) {
            return ServiceResponse.failure(e.getCode(), e.getMessage());
        }
    }

    @Override
    public ServiceResponse<Void> shareGroupChatInviteMQ(String param) {
        GroupChatActivityBaseContext chatActivityContext = JSONObject.parseObject(param, GroupChatActivityBaseContext.class);
        groupChatActivityInviteLogService.groupChatInvite(chatActivityContext);
        return ServiceResponse.success();
    }
}
