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

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import com.gic.haoban.manage.service.entity.chat.TabGroupChatUser;
import com.gic.haoban.manage.service.pojo.bo.chat.GroupChatUserBO;
import com.gic.haoban.manage.service.pojo.qo.chat.GroupChatUserQO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gic.api.base.commons.BasePageInfo;
import com.gic.api.base.commons.Page;
import com.gic.commons.util.PageHelperUtils;
import com.gic.haoban.manage.api.dto.chat.GroupChatUserDTO;
import com.gic.haoban.manage.api.dto.qdto.chat.GroupChatUserSearchQDTO;
import com.gic.haoban.manage.service.dao.mapper.chat.GroupChatUserMapper;
import com.gic.haoban.manage.service.service.chat.GroupChatUserService;
import com.github.pagehelper.PageHelper;

@Service
public class GroupChatUserServiceImpl implements GroupChatUserService {

    @Autowired
    private GroupChatUserMapper groupChatUserMapper;

    @Override
    public Page<GroupChatUserDTO> listPage(GroupChatUserSearchQDTO qdto, BasePageInfo basePageInfo) {
        PageHelper.startPage(basePageInfo);
        if (StringUtils.isNotBlank(qdto.getSearchParams())) {
            String params = qdto.getSearchParams();
            qdto.setWxChatId(params + "%");
            qdto.setSearchParams("%" + params + "%");
        }
        List<GroupChatUserDTO> list = this.groupChatUserMapper.listPage(qdto);
        Page<GroupChatUserDTO> retPage = PageHelperUtils.changePageHelperToCurrentPage(list, GroupChatUserDTO.class);
        return retPage;
    }

    /**
     * 查询群组内群成员
     *
     * @param enterpriseId
     * @param wxEnterpriseId
     * @param groupChatId
     */
    @Override
    public List<GroupChatUserBO> queryGroupMemberWithChatId(String enterpriseId, String wxEnterpriseId, Long groupChatId) {
        GroupChatUserQO search = new GroupChatUserQO();
        search.setChatId(groupChatId);
        search.setEnterpriseId(enterpriseId);
        search.setWxEnterpriseId(wxEnterpriseId);
        List<TabGroupChatUser> tabGroupChatUsers = groupChatUserMapper.queryGroupChatUserList(search);
        if (CollectionUtils.isEmpty(tabGroupChatUsers)) {
            return Collections.emptyList();
        }
        return tabGroupChatUsers
                .stream()
                .map(item -> {
                    GroupChatUserBO temp = new GroupChatUserBO();
                    BeanUtils.copyProperties(item, temp);
                    return temp;
                })
                .collect(Collectors.toList());
    }

    @Override
    public  List<TabGroupChatUser> getByMember(String enterpriseId, String memberId) {
        return this.groupChatUserMapper.getByMember(enterpriseId,memberId);
    }

    @Override
    public int isAddGroupChat(String enterpriseId, String memberId) {
        return this.groupChatUserMapper.isAddGroupChat(enterpriseId,memberId) ;
    }
}