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

import com.alibaba.fastjson.JSON;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.util.GICMQClientUtil;
import com.gic.haoban.manage.api.dto.content.log.ClerkShareMaterialLogDTO;
import com.gic.haoban.manage.api.dto.content.log.TriggerCustomerDetailLogDTO;
import com.gic.haoban.manage.api.enums.content.ClerkShareMaterialType;
import com.gic.haoban.manage.api.service.content.MaterialShareLogApiService;
import com.gic.haoban.manage.service.pojo.bo.content.ClerkShareLogBO;
import com.gic.haoban.manage.service.service.content.ClerkShareLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author MUSI
 * @Date 2023/3/18 8:34 PM
 * @Description
 * @Version
 **/
@Component(value = "materialShareLogApiServiceImpl")
public class MaterialShareLogApiServiceImpl implements MaterialShareLogApiService {

    private static final Logger log = LoggerFactory.getLogger(MaterialShareLogApiServiceImpl.class);

    /**
     * 导购分享素材 客户触达日志
     * @see com.gic.haoban.manage.api.service.content.task.QywxGroupMsgTaskApiService#handlerTriggerCustomerDetailLog(String)
     */
    private static final String SHARE_MATERIAL_DETAIL_LOG = "shareMaterialDetailLog";
    @Autowired
    private ClerkShareLogService clerkShareLogService;

    @Override
    public ServiceResponse<Long> saveClerkShareMaterialLog(ClerkShareMaterialLogDTO clerkShareMaterialLogDTO) {
        log.info("saveClerkShareMaterialLog 保存导购分享日志 clerkId:{}, bizId:{}", clerkShareMaterialLogDTO.getClerkId(), clerkShareMaterialLogDTO.getBizId());
        ClerkShareLogBO clerkShareLogBO = new ClerkShareLogBO();
        BeanUtils.copyProperties(clerkShareMaterialLogDTO, clerkShareLogBO);
        Long logId = clerkShareLogService.saveClerkShareLog(clerkShareLogBO);
        if (logId != null && ClerkShareMaterialType.SHARE_PYQ.getCode().equals(clerkShareLogBO.getShareType())){
            // MQ 通知处理导购分享日志
            TriggerCustomerDetailLogDTO triggerCustomerDetailLog = new TriggerCustomerDetailLogDTO();
            triggerCustomerDetailLog.setEnterpriseId(clerkShareLogBO.getEnterpriseId());
            triggerCustomerDetailLog.setClerkId(clerkShareLogBO.getClerkId());
            triggerCustomerDetailLog.setShareType(ClerkShareMaterialType.SHARE_PYQ.getCode());
            TriggerCustomerDetailLogDTO.SharePYQ sharePYQ = new TriggerCustomerDetailLogDTO.SharePYQ();
            sharePYQ.setShareLogId(logId);
            triggerCustomerDetailLog.setSharePYQ(sharePYQ);
            try{
                log.info("分享朋友圈日志 MQ {}", JSON.toJSONString(triggerCustomerDetailLog));
                GICMQClientUtil.getClientInstance().sendMessage(SHARE_MATERIAL_DETAIL_LOG, JSON.toJSONString(triggerCustomerDetailLog));
            }catch (Exception ex){
                log.warn("客户分享素材 触达日志MQ异常 {}", JSON.toJSONString(triggerCustomerDetailLog), ex);
            }
        }
        return ServiceResponse.success(logId);
    }
}
