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

import java.util.Arrays;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
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 com.alibaba.fastjson.JSON;
import com.gic.api.base.commons.BasePageInfo;
import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.util.DateUtil;
import com.gic.commons.util.EntityUtil;
import com.gic.commons.util.PageHelperUtils;
import com.gic.haoban.manage.api.dto.chat.GroupChatHmDTO;
import com.gic.haoban.manage.api.dto.hm.HmLinkChatDTO;
import com.gic.haoban.manage.api.dto.qdto.chat.ChatHmSearchQDTO;
import com.gic.haoban.manage.api.dto.qdto.hm.HmLinkChatQDTO;
import com.gic.haoban.manage.api.service.chat.GroupChatHmApiService;
import com.gic.haoban.manage.service.pojo.bo.chat.GroupChatHmBO;
import com.gic.haoban.manage.service.service.chat.GroupChatHmService;
import com.gic.haoban.manage.service.service.hm.HmLinkChatService;

/**
 * 
* @ClassName: GroupChatHmApiService
* @Description: 群活码
* @author xugh
* @date 2022年12月1日 下午7:44:51
*
 */
@Service("groupChatHmApiService")
public class GroupChatHmApiServiceImpl implements GroupChatHmApiService {

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

    @Autowired
    private GroupChatHmService groupChatHmService;
    @Autowired
    private HmLinkChatService hmLinkChatService;

    @Override
    public ServiceResponse<Page<GroupChatHmDTO>> listPage(ChatHmSearchQDTO qdto, BasePageInfo basePageInfo) {
        if (null != qdto.getEndDate()) {
            qdto.setEndDate(DateUtil.getEndTimeOfDay(qdto.getEndDate()));
        }
        Page<GroupChatHmBO> page = this.groupChatHmService.listPage(qdto, basePageInfo);
        logger.info("PAGE={}", JSON.toJSONString(page));
        if (CollectionUtils.isEmpty(page.getResult())) {
            return ServiceResponse.success(new Page<GroupChatHmDTO>());
        }
        Page<GroupChatHmDTO> resultPage = PageHelperUtils.changePageToCurrentPage(page, GroupChatHmDTO.class);
        return ServiceResponse.success(resultPage);
    }

    @Override
    public ServiceResponse<GroupChatHmDTO> detail(Long chatHmId) {
        GroupChatHmBO bo = this.groupChatHmService.getById(chatHmId);
        GroupChatHmDTO dto = EntityUtil.changeEntityByJSON(GroupChatHmDTO.class, bo);
        return ServiceResponse.success(dto);
    }

    @Override
    public ServiceResponse<GroupChatHmDTO> save(GroupChatHmDTO dto) {
        return this.groupChatHmService.saveHm(dto);
    }

    @Override
    public ServiceResponse<Void> discard(Long chatHmId, String wxEnterpriseId, String clerkId, String clerkName) {
        List<HmLinkChatDTO> linkCount = this.hmLinkChatService.getLinkCountForHm(wxEnterpriseId,
                Arrays.asList(chatHmId));
        if (CollectionUtils.isNotEmpty(linkCount) && linkCount.get(0).getLinkCount() > 0) {
            return ServiceResponse.failure("9999", "当前活码已关联" + linkCount.get(0).getLinkCount() + "个链接，请确认链接已投放结束");
        }
        this.groupChatHmService.discard(chatHmId, wxEnterpriseId, clerkId, clerkName);
        return ServiceResponse.success();
    }

    @Override
    public ServiceResponse<Page<HmLinkChatDTO>> listPageChatLink(HmLinkChatQDTO qdto, BasePageInfo basePageInfo) {
        Page<HmLinkChatDTO> page = this.hmLinkChatService.listPage(qdto, basePageInfo);
        return ServiceResponse.success(page);
    }
    
    @Override
    public ServiceResponse<List<Long>> listHmGroupChatId(String wxEnterpriseId, Long chatHmId) {
        return ServiceResponse.success(this.groupChatHmService.listHmGroupChatId(wxEnterpriseId, chatHmId));
    }

}
