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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.PropertyNamingStrategy;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.hologres.client.HoloClient;
import com.alibaba.hologres.client.Put;
import com.alibaba.hologres.client.model.TableSchema;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigService;
import com.gic.haoban.common.utils.DateUtil;
import com.gic.haoban.manage.service.dao.mapper.content.TabHaobanTriggerCustomerDetailMapper;
import com.gic.haoban.manage.service.dao.mapper.content.holo.HoloDataSource;
import com.gic.haoban.manage.service.entity.content.TabHaobanTriggerCustomerDetail;
import com.gic.haoban.manage.service.pojo.bo.content.TriggerCustomerDetailBO;
import com.gic.haoban.manage.service.pojo.qo.content.TriggerCustomerDetailQO;
import com.gic.haoban.manage.service.service.content.TriggerCustomerDetailService;
import org.apache.commons.collections.CollectionUtils;
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.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author MUSI
 * @Date 2023/3/19 9:38 PM
 * @Description
 * @Version
 **/
@Component
public class TriggerCustomerDetailServiceImpl implements TriggerCustomerDetailService {

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

    @Autowired
    private TabHaobanTriggerCustomerDetailMapper triggerCustomerDetailMapper;

    @Autowired
    private HoloDataSource holoDataSource;

    @Override
    public void save(TriggerCustomerDetailBO triggerCustomerDetailBO) {

    }

    /**
     * 批量保存
     *
     * @param triggerCustomerDetailBos
     */
    @Override
    public void batchSave(List<TriggerCustomerDetailBO> triggerCustomerDetailBos) {
        if (CollectionUtils.isEmpty(triggerCustomerDetailBos)) {
            log.info("无客户触达日志 >>>> ");
            return;
        }
        Config appConfig = ConfigService.getAppConfig();
        // 当前阶段 0双写 1 只写数据表
        String triggerProcess = appConfig.getProperty("trigger.write", "1");
        if (StringUtils.equals(triggerProcess, "0")) {
            List<TabHaobanTriggerCustomerDetail> customerDetails = triggerCustomerDetailBos
                    .stream()
                    .map(item -> {
                        TabHaobanTriggerCustomerDetail temp = new TabHaobanTriggerCustomerDetail();
                        BeanUtils.copyProperties(item, temp);
                        return temp;
                    })
                    .collect(Collectors.toList());
            triggerCustomerDetailMapper.insertBatch(customerDetails);
        }
        try {
            HoloClient client = holoDataSource.getClient();
            if (client == null) {
                log.error("数仓数据源链接异常, 请检查！！！！{}", JSON.toJSONString(triggerCustomerDetailBos));
                return;
            }
            TableSchema tableSchema = client.getTableSchema("tab_haoban_trigger_customer_detail");

            SerializeConfig serializeConfig = new SerializeConfig();
            serializeConfig.setPropertyNamingStrategy(PropertyNamingStrategy.SnakeCase);
            // id, enterprise_id, wx_enterprise_id, biz_id, biz_type, clerk_id, staff_id,store_id, channel_type, customer_id, create_time, update_time, delete_flag
            List<Put> puts = triggerCustomerDetailBos
                    .stream()
                    .map(item -> {
                        Put temp = new Put(tableSchema);
                        JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(item, serializeConfig));
                        for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
                            temp.setObject(entry.getKey(), entry.getValue());
                        }
                        // 时间戳格式不行
                        temp.setObject("create_time", DateUtil.dateToStr(item.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
                        temp.setObject("update_time", DateUtil.dateToStr(item.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
                        return temp;
                    })
                    .collect(Collectors.toList());
            client.put(puts);
            log.info("客户触达写数据组完成 >>>>>");
        }catch (Exception ex) {
            log.info("数据组写入异常 {}", JSON.toJSONString(triggerCustomerDetailBos), ex);
        }
    }

    /**
     * 查询客户触达日志
     *
     * @param triggerCustomerDetailQO
     * @return
     */
    @Override
    public List<TriggerCustomerDetailBO> queryTriggerCustomerDetailList(TriggerCustomerDetailQO triggerCustomerDetailQO) {
        return null;
    }
}
