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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bohuikeji.delivery.backstage.entity.AliPayConfig;
import com.bohuikeji.delivery.backstage.entity.AliPayV2Config;
import com.bohuikeji.delivery.backstage.entity.TOrder;
import com.bohuikeji.delivery.backstage.entity.TPay;
import com.bohuikeji.delivery.backstage.entity.wx.TWxUser;
import com.bohuikeji.delivery.backstage.pay.AliPayV2;
import com.bohuikeji.delivery.backstage.service.*;
import com.bohuikeji.delivery.backstage.util.AliUtils;
import com.bohuikeji.frame.module.admin.base.MqListener;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 订单完成业务监听
 * @author Admin
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class OrderAliCenterListener extends MqListener<String> {
    final ITOrderService orderService;
    final ITPayService payService;
    final IWxUserService wxUserService;
    final ITNewsService newsService;
    @Override
    public boolean onReceive(String orderNum) {
        TOrder order = orderService.getByNum(orderNum);
        if (order == null) {
            log.error("【消息队列监听错误】订单完成后发送通知消息失败，无法找到订单：[{}]", orderNum);
            return true;
        }
        String[] status={"支付宝小程序","同城急送"};
        if (!Arrays.asList(status).contains(order.getOrderChannel())) {
            return true;
        }
        TPay pay = payService.getOne(new QueryWrapper<TPay>().eq("merchant_num",order.getOrderNum()).eq("pay_scenario","业务支付"));
        TWxUser wxUser = wxUserService.getOne(new QueryWrapper<TWxUser>().eq("wx_app_name",order.getOrderChannel()).eq("user_id",order.getPlayerId()));
        if("取件中".equals(order.getOrderStatus()) || "异常".equals(order.getOrderStatus())){
            return true;
        }
       /* if("待接单".equals(order.getOrderStatus())){
            log.error("####################################################");
            *//*if(!"同城急送".equals(order.getOrderChannel())){
                this.subscribemsg(order,pay,wxUser);
            }*//*
            this.ordermsg(order,pay,wxUser);
        }
        if("待取件".equals(order.getOrderStatus())){
            this.ordermsg(order,pay,wxUser);
        }
        if("待派送".equals(order.getOrderStatus())){
            *//*if(!"同城急送".equals(order.getOrderChannel())){
                this.subscribemsg(order,pay,wxUser);
            }*//*
            this.ordermsg(order,pay,wxUser);
        }
        if("派送中".equals(order.getOrderStatus()) || "已完成".equals(order.getOrderStatus())){
            this.ordermsg(order,pay,wxUser);
        }
        if("已取消".equals(order.getOrderStatus()) || "已退款".equals(order.getOrderStatus())){
            *//*if(!"同城急送".equals(order.getOrderChannel())){
                this.cancelmsg(order,pay,wxUser);
            }*//*
            this.ordermsg(order,pay,wxUser);
        }*/
        this.ordermsg(order,pay,wxUser);
        return true;
    }

    /**
     * 订单中心状态同步消息
     * @param order
     * @param pay
     * @param wxUser
     */
    private void ordermsg(TOrder order,TPay pay,TWxUser wxUser) {
        log.error("##########################################渠道：{}，pay：{},wxUser:{}",order.getOrderChannel(),pay,wxUser);
        String state = AliUtils.getOrderStatus(order.getOrderStatus());//根据订单状态获取支付宝订单中心状态
        Map<String, Object> params = new HashMap<>(10);
        params.put("price",order.getOrderFactPrice());
        params.put("modTime", LocalDateTime.now().toString());
        params.put("createTime",order.getOrderCreateTime().toString());
        params.put("itemName","文件资料/日常用品/其他");
        params.put("materialId","2022031500502200000066862472");
        params.put("storeAddress",order.getMailingAddress());
        params.put("orderType",order.getWayName());
        params.put("unitPrice",order.getOrderFactPrice());
        params.put("tradeNo",pay.getPayChannelNum());
        params.put("bizType","KX_TAKE_OUT");
        params.put("orderStatus",state);
        if("同城急送".equals(order.getOrderChannel())){
            log.error("##########################################");
            params.put("appId", AliPayV2Config.AliID);
        }else {
            params.put("appId", AliPayConfig.AliClientID);
        }
        params.put("bizNo",order.getOrderNum());
        params.put("uid",wxUser.getWxUserOpenid());
        AliUtils.orderCcenter(params,order.getOrderChannel());
    }

    /**
     * 订阅消息模板
     * @param order
     * @param pay
     * @param wxUser
     */
    private void subscribemsg(TOrder order,TPay pay,TWxUser wxUser) {
        Map<String, Object> params = new HashMap<>(10);
        params.put("price",order.getOrderFactPrice());
        params.put("placeTime",order.getOrderCreateTime().toString());
        params.put("address",order.getMailingAddress().getDetail());
        params.put("corName", "同城急送");
        params.put("orderNum",order.getOrderNum());
        params.put("uid",wxUser.getWxUserOpenid());
        params.put("fromId",pay.getPayChannelNum());
        AliUtils.orderPlace(params);
    }
    /**
     * 订阅消息模板
     * @param order
     * @param pay
     * @param wxUser
     */
    private void cancelmsg(TOrder order,TPay pay,TWxUser wxUser) {
        Map<String, Object> params = new HashMap<>(10);
        params.put("mailingName",order.getMailingName());
        params.put("receiptName",order.getReceiptName());
        params.put("price",pay.getPayFactPrice());
        params.put("orderNum", order.getOrderNum());
        params.put("cancelTime",order.getOrderEndTime());
        params.put("uid",wxUser.getWxUserOpenid());
        params.put("fromId",pay.getPayChannelNum());
        AliUtils.orderCancel(params);
    }

    /**
     * 订阅消息模板
     * 订单状态流转
     * @param order
     * @param pay
     * @param wxUser
     */
    /*private void orderStatus(TOrder order,TPay pay,TWxUser wxUser) {
        Map<String, Object> params = new HashMap<>(10);
        params.put("mailingName",order.getMailingName());
        params.put("receiptName",order.getReceiptName());
        params.put("price",pay.getPayFactPrice());
        params.put("orderNum", order.getOrderNum());
        params.put("cancelTime",order.getOrderEndTime());
        params.put("uid",wxUser.getWxUserOpenid());
        params.put("fromId",pay.getPayChannelNum());
        AliUtils.orderStatus(params);
    }*/


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

    }
}
