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

import com.alibaba.fastjson.JSONObject;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.enterprise.api.constant.EnterpriseServiceEnum;
import com.gic.haoban.manage.api.dto.send.DealQwSendDTO;
import com.gic.haoban.manage.api.service.send.QwSendApiService;
import com.gic.haoban.manage.service.service.content.adaptor.EnterpriseAdaptor;
import com.gic.haoban.manage.service.service.send.QwSendMsgService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * Created by wangzubo on 2022/11/1.
 */
@Service("qwSendApiService")
@Slf4j
public class QwSendApiServiceImpl implements QwSendApiService {

    @Autowired
    private QwSendMsgService qwSendMsgService;
    @Autowired
    private EnterpriseAdaptor enterpriseAdaptor;

    @Override
    public ServiceResponse<Void> qwSendRefreshByMQ(String message) {
        if (StringUtils.isBlank(message)) {
            return ServiceResponse.success();
        }
        DealQwSendDTO dealQwSendDTO = JSONObject.parseObject(message, DealQwSendDTO.class);
        boolean hasRight = enterpriseAdaptor.checkEnterpriseHasRight(dealQwSendDTO.getEnterpriseId(), EnterpriseServiceEnum.POTENTIAL.getRightMenuCode());
        if (!hasRight) {
            return ServiceResponse.success();
        }
        Integer refreshType = dealQwSendDTO.getRefreshType();
        log.info("刷新企微发送记录:{}", message);

        if (Objects.equals(refreshType,1)) {
            qwSendMsgService.finishQwSend(dealQwSendDTO.getFinishQwSend(),dealQwSendDTO.getTryCount());
        } else if (Objects.equals(refreshType, 2)) {
            qwSendMsgService.refreshQwSendLog(dealQwSendDTO);
        }
        return ServiceResponse.success();
    }
}
