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

import com.alibaba.fastjson.JSONObject;
import com.gic.api.base.commons.Page;
import com.gic.commons.util.GICMQClientUtil;
import com.gic.haoban.base.api.common.BasePageInfo;
import com.gic.haoban.common.utils.PageUtil;
import com.gic.haoban.manage.api.dto.StaffClerkBindLogDetailDTO;
import com.gic.haoban.manage.api.dto.StaffClerkBindLogInfoDTO;
import com.gic.haoban.manage.service.dao.mapper.StaffClerkBindLogMapper;
import com.gic.haoban.manage.service.entity.TabHaobanStaffClerkBindLog;
import com.gic.haoban.manage.service.service.StaffClerkBindLogService;
import com.gic.mq.sdk.GicMQClient;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * Created 2020/6/24.
 *
 * @author hua
 */
@Service
public class StaffClerkBindLogServiceImpl implements StaffClerkBindLogService {

    private static final Logger logger = LoggerFactory.getLogger(StaffClerkBindLogServiceImpl.class);

    @Autowired
    private StaffClerkBindLogMapper staffClerkBindLogMapper;

    private static final String STAFF_LOG_MQ = "haobanStaffClerkBindLogMq";

    @Override
    public void insert(TabHaobanStaffClerkBindLog bindLog) {
        bindLog.setCreateTime(new Date());
        staffClerkBindLogMapper.insert(bindLog);
    }

    @Override
    public void pushToMq(String staffId, String optStaffId, int optType, int channelCode, String relationId) {

        StaffClerkBindLogInfoDTO infoDTO = new StaffClerkBindLogInfoDTO();
        infoDTO.setOptStaffId(optStaffId);
        infoDTO.setChannelCode(channelCode);
        infoDTO.setOptType(optType);
        infoDTO.setRelationId(relationId);

        GicMQClient clientInstance = GICMQClientUtil.getClientInstance();
        try {
            String ret = JSONObject.toJSONString(infoDTO);
            logger.info("绑定的mq日志：{}", ret);
            clientInstance.sendMessage(STAFF_LOG_MQ, ret);
        } catch (Exception e) {
            logger.info("异常",e);

        }
    }

    @Override
    public void pushToMqBatch(String optStaffId, int optType, int channelCode, List<String> relationIds) {
        logger.info("pushToMqBatch: {},{}", optStaffId, channelCode);
        List<String> ret = relationIds.stream().map(rid -> {
            StaffClerkBindLogInfoDTO infoDTO = new StaffClerkBindLogInfoDTO();
            infoDTO.setChannelCode(channelCode);
            infoDTO.setOptStaffId(optStaffId);
            infoDTO.setOptType(optType);
            infoDTO.setRelationId(rid);
            return JSONObject.toJSONString(infoDTO);
        }).collect(Collectors.toList());

        GicMQClient clientInstance = GICMQClientUtil.getClientInstance();
        try {
            logger.info("绑定的mq日志：{},{}", optStaffId, relationIds);
            clientInstance.sendBatchMessages(STAFF_LOG_MQ, ret);
        } catch (Exception e) {
            logger.info("异常",e);

        }
    }

    @Override
    public Page<StaffClerkBindLogDetailDTO> pageBindLog(String wxEnterpriseId, String enterpriseId, int optType, String search, BasePageInfo pageInfo) {
        PageHelper.startPage(pageInfo);
        List<TabHaobanStaffClerkBindLog> clerkBindLogList = staffClerkBindLogMapper.listStaffClerkBindLog(wxEnterpriseId, enterpriseId, optType, search);
        Page<StaffClerkBindLogDetailDTO> retPage = PageUtil.changePageHelperToCurrentPage(new PageInfo<>(clerkBindLogList), StaffClerkBindLogDetailDTO.class);
        return retPage;
    }

    @Override
    public Page<StaffClerkBindLogDetailDTO> pageUnBindLog(String wxEnterpriseId, String clerkId, String enterpriseId, int optType, BasePageInfo pageInfo) {
        PageHelper.startPage(pageInfo);
        List<TabHaobanStaffClerkBindLog> clerkBindLogList = staffClerkBindLogMapper.staffClerkUnBindLog(wxEnterpriseId, clerkId, enterpriseId, optType);
        Page<StaffClerkBindLogDetailDTO> retPage = PageUtil.changePageHelperToCurrentPage(new PageInfo<>(clerkBindLogList), StaffClerkBindLogDetailDTO.class);
        return retPage;
    }
}
