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

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.gic.clerk.api.service.ClerkService;
import com.gic.enterprise.api.enums.PlatformChannelEnum;
import com.gic.enterprise.api.service.StoreService;
import com.gic.haoban.manage.service.dao.mapper.contact.TabContactLogMapper;
import com.gic.haoban.manage.service.dao.mapper.contact.TabContactOrderMapper;
import com.gic.haoban.manage.service.entity.contact.TabContactLog;
import com.gic.haoban.manage.service.entity.contact.TabContactOrder;
import com.gic.haoban.manage.service.service.contact.ContactFollowService;
import com.gic.haoban.manage.service.service.contact.ContactLogService;
import com.gic.haoban.manage.service.service.contact.ContactOrderService;
import com.gic.member.api.service.MemberService;
import com.gic.order.api.dto.resp.OrderInfoResp;
import com.gic.store.goods.service.StoreGoodsService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

@Service("contactOrderService")
public class ContactOrderServiceImpl implements ContactOrderService {

    private static final Logger logger = LogManager.getLogger(ContactOrderService.class);

    @Autowired
    private TabContactOrderMapper contactOrderMapper;
    @Autowired
    private ContactLogService contactLogService;

    @Override
    public void saveContactOrder(OrderInfoResp orderInfoResp) {
        Integer orderType = orderInfoResp.getOrderType();
        PlatformChannelEnum platformChannelEnum = PlatformChannelEnum.getEnumByOldOrderType(orderType);
        if (platformChannelEnum == null) {
            return;
        }
        Date receiptsDate = orderInfoResp.getReceiptsDate();
        Date contactBeginTime = null;
        String channelCode = platformChannelEnum.getChannelCode();
        // 1 线上; 2 线下
        Integer channelType = null;
        if (Objects.equals(PlatformChannelEnum.C_POS.getChannelCode(), channelCode)) {
            channelType = 2;
            contactBeginTime = DateUtil.offsetDay(receiptsDate, -7);
        }else {
            channelType = 1;
            contactBeginTime = DateUtil.offsetDay(receiptsDate, -1);
        }

        TabContactLog lastContactLog = contactLogService.getLastByTime(orderInfoResp.getMemberId(), contactBeginTime);
        if (lastContactLog == null) {
            return;
        }
        logger.info("建联转化订单建联信息:{}", JSON.toJSONString(lastContactLog));
        TabContactOrder contactOrder = new TabContactOrder();
        contactOrder.setOrderId(orderInfoResp.getOrderId());
        contactOrder.setMemberId(orderInfoResp.getMemberId());
        contactOrder.setOrderNumber(orderInfoResp.getOrderNo());
        contactOrder.setPaidAmount(orderInfoResp.getPayAmount());
        contactOrder.setProductCount(orderInfoResp.getGoodsCount());
        contactOrder.setPayTime(orderInfoResp.getReceiptsDate());
        contactOrder.setReceiptsDate(orderInfoResp.getReceiptsDate());
        contactOrder.setChannelCode(channelCode);
        contactOrder.setChannelType(channelType);
        contactOrder.setClerkId(lastContactLog.getClerkId());
        contactOrder.setClerkCode(lastContactLog.getClerkCode());
        contactOrder.setStoreId(lastContactLog.getStoreId());
        contactOrder.setContactLogId(lastContactLog.getLogId());
        contactOrder.setContactTime(lastContactLog.getContactTime());
        contactOrder.setPotentialTime(lastContactLog.getPotentialTime());
        contactOrder.setEnterpriseId(lastContactLog.getEnterpriseId());
        contactOrderMapper.insert(contactOrder);

    }
}
