package com.cyl.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cyl.h5.service.H5OrderService;
import com.cyl.manager.oms.domain.OmsOrderService;
import com.cyl.manager.oms.domain.Order;
import com.cyl.manager.oms.mapper.OmsOrderServiceMapper;
import com.cyl.manager.oms.mapper.OrderMapper;
import com.ruoyi.common.config.properties.SmsProperties;
import com.ruoyi.common.core.sms.AliyunSmsUtils;
import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class OrderNotifyJob {

    @Autowired
    private H5OrderService h5OrderService;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private SmsProperties smsProperties;

    @Autowired
    private OmsOrderServiceMapper omsOrderServiceMapper;
    /**
     * H5订单查询状态
     * -1->全部 0->待付款；1->待发货；2->待收货；3->已完成；4->已关闭；5->无效订单 -2->售后单
     */
    @Async
    @Scheduled(cron = "0 0 9 * * ?")
    public void cancelOrder(){
        log.info("【订单服务定时提醒任务开始】");

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        LocalDateTime now = LocalDateTime.now();
        String todayStr = sdf.format(now.toLocalDate());
        String tomorrowStr = sdf.format(now.plusDays(1).toLocalDate());


        //服务即将开始提醒通知：提前1天的上午9点
        log.info("【订单服务定时提醒任务开始】服务即将开始提醒通知：提前1天的上午9点:" + todayStr + "|" + tomorrowStr);
        QueryWrapper<OmsOrderService> qw1 = new QueryWrapper<>();
        qw1.eq("service_date", tomorrowStr);
        List<OmsOrderService> orderServiceList = omsOrderServiceMapper.selectList(qw1);

        if (orderServiceList != null) {
            orderServiceList.forEach( omsOrderService -> {
                log.info("【服务即将开始提醒通知：提前1天的上午9点】" + omsOrderService.getOrderId());
                QueryWrapper<OmsOrderService> qw3 = new QueryWrapper<>();
                qw3.eq("order_id", omsOrderService.getOrderId());
                List<OmsOrderService> orderServiceList2 = omsOrderServiceMapper.selectList(qw3);

                if (orderServiceList2 == null)
                    return;

                for (OmsOrderService service: orderServiceList2) {
                    String serviceDate = service.getServiceDate();
                    if (StringUtils.isNotEmpty(serviceDate)) {
                        try {
                            Date serviceDateDate = sdf.parse(serviceDate);
                            if (serviceDateDate.before(sdf.parse(tomorrowStr)) ){
                                return;
                            }
                        } catch (ParseException e) {
                            log.error("时间解析失败：" + serviceDate);
                        }
                    }
                }

                if (orderServiceList2.size() == 1) {
                    Order order = orderMapper.selectById(omsOrderService.getOrderId());
                    Integer status = order.getStatus();
                    if (status == 1 || status == 2) {
                        log.info("【服务即将开始提醒通知点】发送提前1天的上午9点短信，手机号" + order.getReceiverPhone() + " | 手机号：" + order.getId());
                        AliyunSmsUtils util = new AliyunSmsUtils(smsProperties);
                        AliyunSmsUtils.sendPoStartServiceSMS(order.getReceiverPhone());
                    }
                }
            });
        }

        //服务开始提醒通知：服务期间每日9点
        log.info("【订单服务定时提醒任务开始】服务开始提醒通知：服务期间每日9点");
        QueryWrapper<OmsOrderService> qw2 = new QueryWrapper<>();
        qw2.eq("service_date", todayStr);
        List<OmsOrderService> orderServiceList2 = omsOrderServiceMapper.selectList(qw2);

        if (orderServiceList2 != null) {
            orderServiceList2.forEach( omsOrderService -> {
                log.info("【服务即将开始提醒通知：提前1天的上午9点】" + omsOrderService.getOrderId());
                Order order = orderMapper.selectById(omsOrderService.getOrderId());
                Integer status = order.getStatus();
                if (status == 1 || status == 2) {
                    log.info("【服务即将开始提醒通知点】发送提前1天的上午9点短信，手机号" + order.getReceiverPhone() + " | 手机号：" + order.getId());
                    AliyunSmsUtils util = new AliyunSmsUtils(smsProperties);
                    AliyunSmsUtils.sendPoTodayStartServiceSMS(order.getReceiverPhone());
                }
            });
        }

        log.info("【订单服务定时提醒任务开始】");
    }

}
