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.commons.util.UniqueIdUtils;
import com.gic.haoban.manage.api.dto.anaylsis.ClerkShareGoodsLogDTO;
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.anaylsis.ProTriggerCustomerDetailBO;
import com.gic.haoban.manage.service.pojo.bo.content.ClerkShareLogBO;
import com.gic.haoban.manage.service.pojo.bo.content.TriggerCustomerDetailBO;
import com.gic.haoban.manage.service.service.content.ClerkShareLogService;
import com.gic.haoban.manage.service.service.content.TriggerCustomerDetailService;
import org.apache.commons.lang3.StringUtils;
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;

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

/**
 * @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 = "handlerTriggerCustomerDetailLog";

    @Autowired
    private ClerkShareLogService clerkShareLogService;

    @Autowired
    TriggerCustomerDetailService triggerCustomerDetailService;


    @Override
    public ServiceResponse<Long> saveClerkShareMaterialLog(ClerkShareMaterialLogDTO clerkShareMaterialLogDTO) {
        log.info("saveClerkShareMaterialLog 保存导购分享日志 clerkId:{}, bizId:{} {}", clerkShareMaterialLogDTO.getClerkId(),
                clerkShareMaterialLogDTO.getBizId(), JSON.toJSONString(clerkShareMaterialLogDTO));

        if (StringUtils.isNotBlank(clerkShareMaterialLogDTO.getExternalUserId())) {
            // 通过对话框发送
            TriggerCustomerDetailBO temp = new TriggerCustomerDetailBO();
            temp.setId(UniqueIdUtils.uniqueLong());
            temp.setEnterpriseId(clerkShareMaterialLogDTO.getEnterpriseId());
            temp.setWxEnterpriseId(clerkShareMaterialLogDTO.getWxEnterpriseId());
            temp.setBizId(clerkShareMaterialLogDTO.getBizId());
            temp.setBizType(clerkShareMaterialLogDTO.getBizType());
            temp.setClerkId(clerkShareMaterialLogDTO.getClerkId());
            temp.setStaffId(clerkShareMaterialLogDTO.getStaffId());
            temp.setStoreId(clerkShareMaterialLogDTO.getStoreId());
            temp.setChannelType(clerkShareMaterialLogDTO.getShareType());
            temp.setCustomerId(clerkShareMaterialLogDTO.getExternalUserId());
            temp.setCreateTime(new Date());
            temp.setUpdateTime(new Date());
            temp.setDeleteFlag(0);
            triggerCustomerDetailService.batchSave(Collections.singletonList(temp));
            return ServiceResponse.success(temp.getId());
        }

        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), 60);
            } catch (Exception ex) {
                log.warn("客户分享素材 触达日志MQ异常 {}", JSON.toJSONString(triggerCustomerDetailLog), ex);
            }
        }
        return ServiceResponse.success(logId);
    }

    @Override
    public ServiceResponse<Long> saveClerkShareRecommendLog(List<ClerkShareGoodsLogDTO> clerkShareGoodsLogDTOList) {
        List<ProTriggerCustomerDetailBO> list = clerkShareGoodsLogDTOList.stream().map(clerkShareGoodsLogDTO -> {
            ProTriggerCustomerDetailBO temp = new ProTriggerCustomerDetailBO();
            temp.setId(UniqueIdUtils.uniqueLong());
            temp.setEnterpriseId(clerkShareGoodsLogDTO.getEnterpriseId());
            temp.setWxEnterpriseId(clerkShareGoodsLogDTO.getWxEnterpriseId());
            temp.setBizId(clerkShareGoodsLogDTO.getBizId());
            temp.setBizType(clerkShareGoodsLogDTO.getBizType());
            temp.setClerkId(clerkShareGoodsLogDTO.getClerkId());
            temp.setStaffId(clerkShareGoodsLogDTO.getStaffId());
            temp.setStoreId(clerkShareGoodsLogDTO.getStoreId());
            temp.setChannelType(clerkShareGoodsLogDTO.getShareType());
            temp.setCustomerId(clerkShareGoodsLogDTO.getExternalUserId());
            temp.setMemberId(clerkShareGoodsLogDTO.getMemberId());
            temp.setWxaLinkId(clerkShareGoodsLogDTO.getWxaLinkId());
            temp.setCreateTime(new Date());
            temp.setUpdateTime(new Date());
            temp.setDeleteFlag(0);
            return temp;
        }).collect(Collectors.toList());
        triggerCustomerDetailService.batchSaveGoodsShare(list);


        return ServiceResponse.success();
    }
}
