package com.reptileclient.jlifeapp.schedule;

import cn.hutool.core.date.DateUtil;
import com.reptileclient.jlifeapp.entity.hm.HmBookOrder;
import com.reptileclient.jlifeapp.service.HmBookOrderService;
import com.reptileclient.jlifeapp.service.HmPaymentOrderService;
import com.reptileclient.jlifeapp.service.JzBookOrderService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author fuxubing
 */
@Async
@Component
public class HmBookOrderTask {
    private static final Logger logger = LoggerFactory.getLogger(HmBookOrderTask.class);
    @Resource
    private HmBookOrderService hmBookOrderService;
    @Resource
    private HmPaymentOrderService hmPaymentOrderService;
    @Resource
    private JzBookOrderService jzBookOrderService;
    @Resource
    private JzOrderTask jzOrderTask;

    /**
     * 每天1点执行一次 更新和增量合成一个job 如果没有一条盒马数据，则全量跑 如有有盒马数据，只更新最近一个月的
     */
    // 最慢的
//    @Scheduled(cron = "0 0 1 * * ?")
    @Scheduled(cron = "0 0 4 * * ?")
    public void runTask() {
        logger.info("开始执行同步盒马服务单数据");
        HmBookOrder hmBookOrder = hmBookOrderService.selectNewOne(null);

        try {
            int row = hmBookOrderService.addBookOrderList();
            logger.info("执行完成盒马服务单总条数：{}", row);
            // 数据去重
            hmBookOrderService.deleteDistinctData(null != hmBookOrder ? DateUtil.lastMonth() : null);
            jzBookOrderService.deleteDistinctData(null != hmBookOrder ? DateUtil.lastMonth() : null);
            // 同步盒马付款单数据
            logger.info("开始执行同步盒马付款单数据");
            hmPaymentOrderService.addPaymentOrderList();

            // 非盒马订单 只有服务单的同步补充订单和客户信息
            jzBookOrderService.updateCustomerIdIsNull();

            // 同步数据到customer_client---by leo 停用
            // hmPaymentOrderService.addPaymentOrderList中有新增客户的方法
            // hmBookOrderService.insertClientByBatch(null != hmBookOrder ?
            // hmBookOrder.getCreateTime() : null);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //--------------有依赖关系
        try {
            logger.info("开始执行盒马服务单payment_no为空的数据转为jz_order订单");// 其他渠道，非盒马订单 只有服务单，没有付款单
            jzOrderTask.runTask();
        } catch (Exception e) {
            // TODO: handle exception
        }
    }


    /**
     * 每天8点执行一次 将作业单匹配到的预约单、支付单进行状态同步
     */
    @Scheduled(cron = "0 0 8 * * ?")
    public void runUpdateTask() {
        logger.info("开始执行将作业单匹配到的预约单、支付单进行状态同步");
        List<Map<String, Object>> match = hmBookOrderService.match();

        match.forEach(item -> {
            String bookOrderNo = String.valueOf(item.get("book_order_no"));
            String stationCode = String.valueOf(item.get("station_code"));
            String paymentNo = String.valueOf(item.get("payment_no"));

            //更新服务单
            hmBookOrderService.updateByBookOrderNo(bookOrderNo, stationCode);
            //更新支付单
            if (StringUtils.isNotBlank(paymentNo)) {
                hmPaymentOrderService.updateByPaymentNo(paymentNo, stationCode);
            }
        });
    }


//    /**
//     * 每天8点执行一次  不用了
//     */
//    @Scheduled(cron = "0 0 8 * * ?")
//    public void runUpdateTask() {
//        logger.info("开始执行更新盒马服务单数据");
//        HmBookOrder hmBookOrder = hmBookOrderService.selectNewOne(null);
//
//        try {
//            int row = hmBookOrderService.updateBookOrder();
//            logger.info("执行完成盒马服务单总条数：{}", row);
//            //数据去重
//            hmBookOrderService.deleteDistinctData(null != hmBookOrder ? DateUtil.lastMonth() : null);
//            //同步数据到customer_client
//           hmBookOrderService.insertClientByBatch(null != hmBookOrder ? hmBookOrder.getCreateTime() : null);
//            //更新盒马付款单数据
//            logger.info("开始执行更新盒马付款单数据");
//            hmPaymentOrderService.updatePaymentOrderList();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }


}
