package com.bohuikeji.delivery.backstage.listener.order;

import com.bohuikeji.delivery.backstage.entity.TOrder;
import com.bohuikeji.delivery.backstage.entity.TOrderCircu;
import com.bohuikeji.delivery.backstage.entity.socket.AdminNotice;
import com.bohuikeji.delivery.backstage.service.ISocketService;
import com.bohuikeji.delivery.backstage.service.ITNewsService;
import com.bohuikeji.delivery.backstage.service.ITOrderService;
import com.bohuikeji.delivery.backstage.service.third.base.ThirdBase;
import com.bohuikeji.delivery.backstage.util.OrderUtils;
import com.bohuikeji.frame.module.admin.base.MqListener;
import com.bohuikeji.frame.module.admin.component.MqTool;
import com.bohuikeji.frame.module.admin.entity.TFranchiseeInformation;
import com.bohuikeji.frame.module.admin.service.ITFranchiseeInformationService;
import com.bohuikeji.frame.module.admin.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.time.Duration;
import java.time.LocalDateTime;

/**
 * 订单创建完成监听处理
 * @author Admin
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class OrderCreateListener extends MqListener<String> {

    final ITOrderService orderService;
    final ISocketService socketService;
    final ITNewsService newsService;
    final MqTool mqTool;
    final ITFranchiseeInformationService franService;

    /**
     * 获取当前配置环境
     */
    @Value("${spring.profiles.active}")
    private String active;

    @Override
    public boolean onReceive(String orderNum) {
        TOrder order = orderService.getByNum(orderNum);
        if (order == null) {
            log.error("【消息队列监听错误】订单完创建发送通知消息失败，无法找到订单：[{}]", orderNum);
            return true;
        }
        /*if("邮小哥".equals(order.getOrderChannel())){
            log.error("商户后台禁止推送：[{}]", orderNum);
            return true;
        }*/
        /**
         * 先发送语音消息给后台
         */
        AdminNotice notice = new AdminNotice(StringUtils.formatStr("您好，您有来自{0}的订单，请及时处理！", order.getMailingAddress().getProvince() + order.getMailingAddress().getCity()));
        socketService.sendAdminMsg(notice.setFranId(order.getFranId()));
        /**
         * 再发送文字消息给客户
         */
        newsService.pushCustomerMsg1(order.getPlayerId(), "下单成功通知", "尊敬的客户您好,您已下单成功，订单号:{0}!", order.getOrderNum());
        //跟新订单流转记录
        TOrderCircu.insertCircu(order);
        if(order.getTest()){
            log.error("发现测试订单:{}，取消推送!",orderNum);
            return true;
        }
        /*if("麦芽田".equals(order.getOrderChannel())){
            log.error("发现麦芽田订单:{}，取消推送!",orderNum);
            return true;
        }*/
        /**
         * 第三方配送推送订单
         * 只有生产换推送 prod
         */
        if("prod".equals(active)){
            Duration time=Duration.ofSeconds(20);
            for (int i = 0; i < ThirdBase.frans.length; i++) {
                if(order.getFranId().equals(ThirdBase.frans[i])){
                    time=Duration.ofMinutes(1);
                    break;
                    //return true;
                }
            }
            TFranchiseeInformation franchiseeInformation = franService.getById(order.getFranId());
            if(franchiseeInformation !=null){
                if("关闭".equals(franchiseeInformation.getFranPush())){
                    return true;
                }else {
                    time=Duration.ofMinutes(Integer.parseInt(franchiseeInformation.getFranPush()));
                }
            }
            LocalDateTime endTime= OrderUtils.getSurplusTime(order);
            Duration duration = Duration.between(LocalDateTime.now(),endTime);
            //取件时间大于俩小时记为预约订单
            if(duration.toMinutes()>=30&&duration.toMinutes()<120){
                log.error("识别到预约订单[{}]，预约取件时间：[{}],时间差为：[{}]",order.getOrderNum(),order.getPickTime(),duration.toMinutes());
                //提前30分钟推送
                time=duration.minusMinutes(30);
            }
            log.error("################################到这了吗:{}",time);
            mqTool.sendDelayMsg(order.getOrderId(),OrderSendThirdListener.class,time);
        }
        return true;
    }

    @Override
    public void onError(String msg, Exception e) {

    }
}
