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

import com.alibaba.fastjson.JSON;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.util.DateUtil;
import com.gic.haoban.manage.api.dto.content.log.TriggerCustomerDetailLogDTO;
import com.gic.haoban.manage.api.enums.content.TriggerCustomerChannelType;
import com.gic.haoban.manage.api.service.content.task.QywxGroupMsgTaskApiService;
import com.gic.haoban.manage.service.pojo.bo.content.GroupMessageInfoBo;
import com.gic.haoban.manage.service.service.content.GroupMessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author MUSI
 * @Date 2023/3/19 8:33 PM
 * @Description
 * @Version
 **/
@Component("qywxGroupMsgTaskApiServiceImpl")
public class QywxGroupMsgTaskApiServiceImpl implements QywxGroupMsgTaskApiService {

    private static final Logger log = LoggerFactory.getLogger(QywxGroupMsgTaskApiServiceImpl.class);
    @Autowired
    private GroupMessageService groupMessageService;

    @Override
    public ServiceResponse<Void> groupMsgTaskJob(String params) {
        // 获取开启【内容】权限的企业id
        List<String> enterpriseIds = new ArrayList<>();
        // 获取企业 (当前时间 - 30分钟, 当前时间)范围内的 导购分享记录。按创建时间排序
        Date currentTime = new Date();
        for (String enterpriseId : enterpriseIds) {
            GroupMessageInfoBo groupMessageInfoBo = new GroupMessageInfoBo();
            groupMessageInfoBo.setEnterpriseId(enterpriseId);
            groupMessageInfoBo.setStartTime(DateUtil.addNumForMinute(currentTime, -30));
            groupMessageInfoBo.setEndTime(currentTime);
            groupMessageService.handlerGroupMessage(groupMessageInfoBo);
        }
        // 获取当前时间段内的数据
        return ServiceResponse.success();
    }

    /**
     * 处理客户触达日志明细
     *
     * @param params
     * @return
     */
    @Override
    public ServiceResponse<Void> handlerTriggerCustomerDetailLog(String params) {
        log.info("处理客户触达日志 {}", params);
        TriggerCustomerDetailLogDTO customerDetailLog = JSON.parseObject(params, TriggerCustomerDetailLogDTO.class);

        if (customerDetailLog == null) {
            return ServiceResponse.success();
        }

        GroupMessageInfoBo groupMessageInfoBo = new GroupMessageInfoBo();
        groupMessageInfoBo.setEnterpriseId(customerDetailLog.getEnterpriseId());
        if (TriggerCustomerChannelType.PYQ.getCode().equals(customerDetailLog.getShareType())) {
            if (customerDetailLog.getSharePYQ() == null) {
                log.info("handlerTriggerCustomerDetailLog 分享朋友圈类型 参数异常 {}", params);
                return ServiceResponse.success();
            }
            groupMessageInfoBo.setShareLogId(customerDetailLog.getSharePYQ().getShareLogId());
        }
        groupMessageService.handlerGroupMessage(groupMessageInfoBo);
        return ServiceResponse.success();
    }
}
