package com.hai.schedule;

import com.alibaba.fastjson.JSONObject;
import com.hai.common.utils.HttpsUtils;
import com.hai.config.QianZhuConfig;
import com.hai.config.WxOrderConfig;
import com.hai.entity.HighChildOrder;
import com.hai.entity.HighOrder;
import com.hai.model.OrderRefundModel;
import com.hai.service.HighOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import javax.xml.ws.RespectBinding;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: 胡锐
 * @Description: 订单定时任务
 * @Date: 2021/3/27 15:41
 */
@Configuration
public class HighOrderSchedule {

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

    @Resource
    private HighOrderService highOrderService;

    /**
     * @Author 胡锐
     * @Description 取消订单  15分钟
     * @Date 2021/4/4 22:45
     **/
    @Scheduled(cron="0 0/1 * * * ?")   //每1分钟执行一次
    public void cancelOrder() {
        List<HighOrder> orderList = highOrderService.getCloseOrder();
        if (orderList != null && orderList.size() > 0) {
            for (HighOrder order : orderList) {
                try {
                    highOrderService.cancelOrder(order.getId());
                } catch (Exception e) {
                    log.error("HighCouponSchedule --> expiredCoupon() error!", e);
                }
            }
        }
    }

    /**
     * @Author 胡锐
     * @Description 处理话费充值订单
     * @Date 2021/4/4 22:45
     **/
    @Scheduled(cron="0 0/1 * * * ?")   //每1分钟执行一次
    public void handleMobileOrder() {
        List<HighOrder> orderList = highOrderService.getAlreadyPaidMobileOrder();
        if (orderList != null && orderList.size() > 0) {
            for (HighOrder order : orderList) {
                try {
                    HighOrder highOrder = highOrderService.getOrderById(order.getId());
                    if (highOrder != null) {
                        JSONObject mobileOrderJson = QianZhuConfig.getMobileOrderByOrderNo(highOrder.getOrderNo());
                        if (mobileOrderJson != null && mobileOrderJson.getBoolean("success") == true) {
                            JSONObject data = mobileOrderJson.getJSONObject("data");

                            // 订单状态 0:待付款 5:已支付 10:充值中 15:交易成功 -5:已取消 -10:充值失败
                            if (data.getInteger("status")  == 15) {
                                 for (HighChildOrder childOrder : highOrder.getHighChildOrderList()) {
                                     childOrder.setChildOrdeStatus(3);
                                 }
                                 highOrder.setOrderStatus(3);
                                 highOrder.setFinishTime(new Date());
                                 highOrderService.updateOrder(highOrder);
                            }
                            if (data.getInteger("status") == -10 || data.getInteger("status")  == -5) {
                                if(highOrder.getPaySerialNo() != null && highOrder.getPayRealPrice() != null) {
                                    OrderRefundModel orderRefundModel = WxOrderConfig.orderToRefund(highOrder.getPaySerialNo(), highOrder.getPayRealPrice(), highOrder.getPayRealPrice());
                                    if(orderRefundModel.getResult_code().equals("SUCCESS")) {
                                        for (HighChildOrder childOrder : highOrder.getHighChildOrderList()) {
                                            childOrder.setChildOrdeStatus(4);
                                        }
                                        highOrder.setOrderStatus(4);
                                        highOrder.setRefundTime(new Date());
                                        highOrder.setRefundPrice(highOrder.getPayRealPrice());
                                        highOrderService.updateOrder(highOrder);
                                    }
                                } else {
                                    for (HighChildOrder childOrder : highOrder.getHighChildOrderList()) {
                                        childOrder.setChildOrdeStatus(5);
                                    }
                                    highOrder.setCancelTime(new Date());
                                    highOrder.setOrderStatus(5);
                                    highOrderService.updateOrder(highOrder);
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    log.error("HighCouponSchedule --> handleMobileOrder() error!", e);
                }
            }
        }
    }

    /**
     * @Author 胡锐
     * @Description 处理话KFC订单
     * @Date 2021/4/4 22:45
     **/
    @Scheduled(cron="0 0/1 * * * ?")   //每1分钟执行一次
    public void handleKfcOrder() {
        List<HighOrder> orderList = highOrderService.getAlreadyPaidKfcOrder();
        if (orderList != null && orderList.size() > 0) {
            for (HighOrder order : orderList) {
                try {
                    HighOrder highOrder = highOrderService.getOrderById(order.getId());
                    if (highOrder != null) {
                        JSONObject mobileOrderJson = QianZhuConfig.getKfcOrderByOrderNo(highOrder.getOrderNo());
                        if (mobileOrderJson != null && mobileOrderJson.getBoolean("success") == true) {
                            JSONObject data = mobileOrderJson.getJSONObject("data");

                            // 订单状态 0:待付款 5:排队中 15:TRAN_SUCCESS:交易成功 -5:已取消
                            if (data.getInteger("status")  == 15) {
                                for (HighChildOrder childOrder : highOrder.getHighChildOrderList()) {
                                    childOrder.setChildOrdeStatus(3);
                                }
                                highOrder.setOrderStatus(3);
                                highOrder.setFinishTime(new Date());
                                highOrderService.updateOrder(highOrder);
                            }

                            if (data.getInteger("status")  == -5) {
                                if(highOrder.getPaySerialNo() != null && highOrder.getPayRealPrice() != null) {
                                    OrderRefundModel orderRefundModel = WxOrderConfig.orderToRefund(highOrder.getPaySerialNo(), highOrder.getPayRealPrice(), highOrder.getPayRealPrice());
                                    if(orderRefundModel.getResult_code().equals("SUCCESS")) {
                                        for (HighChildOrder childOrder : highOrder.getHighChildOrderList()) {
                                            childOrder.setChildOrdeStatus(4);
                                        }
                                        highOrder.setOrderStatus(4);
                                        highOrder.setRefundTime(new Date());
                                        highOrder.setRefundPrice(highOrder.getPayRealPrice());
                                        highOrderService.updateOrder(highOrder);
                                    }
                                } else {
                                    for (HighChildOrder childOrder : highOrder.getHighChildOrderList()) {
                                        childOrder.setChildOrdeStatus(5);
                                    }
                                    highOrder.setCancelTime(new Date());
                                    highOrder.setOrderStatus(5);
                                    highOrderService.updateOrder(highOrder);
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    log.error("HighCouponSchedule --> handleMobileOrder() error!", e);
                }
            }
        }
    }

    /**
     * @Author 胡锐
     * @Description 处理电影票订单
     * @Date 2021/4/4 22:45
     **/
    @Scheduled(cron="0 0/1 * * * ?")   //每1分钟执行一次
    public void handleCinemaOrder() {
        List<HighOrder> orderList = highOrderService.getAlreadyPaidCinemaOrder();
        if (orderList != null && orderList.size() > 0) {
            for (HighOrder order : orderList) {
                try {
                    HighOrder highOrder = highOrderService.getOrderById(order.getId());
                    if (highOrder != null) {
                        JSONObject mobileOrderJson = QianZhuConfig.getCinemaOrderByOrderNo(highOrder.getOrderNo());
                        if (mobileOrderJson != null && mobileOrderJson.getBoolean("success") == true) {
                            JSONObject data = mobileOrderJson.getJSONObject("data");

                            // 	订单状态 0:待付款 5:待出票 10:已出票 15:交易成功 -5:已取消
                            if (data.getInteger("status") == 10 || data.getInteger("status") == 15) {
                                for (HighChildOrder childOrder : highOrder.getHighChildOrderList()) {
                                    childOrder.setChildOrdeStatus(3);
                                }
                                highOrder.setOrderStatus(3);
                                highOrder.setFinishTime(new Date());
                                highOrderService.updateOrder(highOrder);
                            }

                            if (data.getInteger("status")  == -5) {
                                if(highOrder.getPaySerialNo() != null && highOrder.getPayRealPrice() != null) {
                                    OrderRefundModel orderRefundModel = WxOrderConfig.orderToRefund(highOrder.getPaySerialNo(), highOrder.getPayRealPrice(), highOrder.getPayRealPrice());
                                    if(orderRefundModel.getResult_code().equals("SUCCESS")) {
                                        for (HighChildOrder childOrder : highOrder.getHighChildOrderList()) {
                                            childOrder.setChildOrdeStatus(4);
                                        }
                                        highOrder.setOrderStatus(4);
                                        highOrder.setRefundTime(new Date());
                                        highOrder.setRefundPrice(highOrder.getPayRealPrice());
                                        highOrderService.updateOrder(highOrder);
                                    }
                                } else {
                                    for (HighChildOrder childOrder : highOrder.getHighChildOrderList()) {
                                        childOrder.setChildOrdeStatus(5);
                                    }
                                    highOrder.setCancelTime(new Date());
                                    highOrder.setOrderStatus(5);
                                    highOrderService.updateOrder(highOrder);
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    log.error("HighCouponSchedule --> handleMobileOrder() error!", e);
                }
            }
        }
    }

    /**
     * @Author 胡锐
     * @Description 完成团油订单  超过支付时间24小时订单自动完成
     * @Date 2021/4/4 22:45
     **/
    @Scheduled(cron="0 0/1 * * * ?")   //每1分钟执行一次
    public void finishGasOrder() {
        List<Map<String, Object>> mapList = highOrderService.getFinishGasOrder();
        if (mapList != null && mapList.size() > 0) {
            for (Map<String,Object> map : mapList) {
                HighOrder order = highOrderService.getOrderById(Long.parseLong(map.get("orderId").toString()));
                if (order != null) {
                    order.setOrderStatus(3);
                    order.setFinishTime(new Date());
                    highOrderService.updateOrderDetail(order);
                }
            }
        }
    }

}
