package com.hfkj.schedule;

import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.DateUtil;
import com.hfkj.entity.*;
import com.hfkj.service.goods.BsOrderGoodsService;
import com.hfkj.service.goods.BsOrderStarbucksService;
import com.hfkj.service.goods.GoodsUserAddressService;
import com.hfkj.service.order.BsOrderChildService;
import com.hfkj.service.order.BsOrderService;
import com.hfkj.sysenum.order.OrderLogisticsStatusEnum;
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 java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
  * @ClassName OrderGoodsSchedule
  * @Author Sum1Dream
  * @Description 实物订单定时任务
  * @Date 2024/8/12 下午3:00
 **/
@Configuration
public class OrderGoodsSchedule {

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

    @Resource
    private BsOrderGoodsService orderGoodsService;


    @Resource
    private GoodsUserAddressService addressService;

    @Resource
    private BsOrderStarbucksService orderStarbucksService;

    @Resource
    private BsOrderChildService orderChildService;

    @Resource
    private BsOrderService orderService;

    //每日凌晨12点5秒执行一次
    @Scheduled(cron = "5 0 0 * * ?")
    public void orderGoodsConfirmReceipt() {
        Map<String , Object> map = new HashMap<>();

        map.put("logisticsStatus" , "SIGN");
        map.put("status" , 2);

        List<BsOrderGoods> orderGoodsList = orderGoodsService.getList(map);

        for (BsOrderGoods goods : orderGoodsList) {

            // 判断时间是否是十天前

            if (DateUtil.differentDays(goods.getCreateTime() , new Date()) > 10) {
                orderGoodsService.confirmReceipt(goods.getId());
            }
        }
    }

    @Scheduled(cron = "5 0 0 * * ?")
    public void getLogisticsMsg() {
        Map<String , Object> objectMap = new HashMap<>();

        objectMap.put("status" , 2);
        objectMap.put("createTime" , "createTime");

        List<BsOrderGoods> orderGoodsList = orderGoodsService.getList(objectMap);

        orderGoodsList = orderGoodsList.stream().filter(order -> order.getLogisticsNo() != null).collect(Collectors.toList());

        for (BsOrderGoods goods : orderGoodsList) {

            if (goods.getLogisticsNo() != null) {
                GoodsLogistics logistics = (GoodsLogistics) orderGoodsService.getLogisticsMsgSchedule(goods.getLogisticsNo() , goods.getUserPhone());
                if (logistics.getLogisticsStatus() != null) {
                    goods.setLogisticsStatus(logistics.getLogisticsStatus());
                    goods.setLogisticsStatusDesc(logistics.getLogisticsStatusDesc());

                } else {
                    goods.setLogisticsStatus(OrderLogisticsStatusEnum.statusWAIT_ACCEPT.getCode());
                    goods.setLogisticsStatusDesc(OrderLogisticsStatusEnum.statusWAIT_ACCEPT.getName());
                }

                goods.setUpdateTime(new Date());

                orderGoodsService.update(goods);
            }

        }
    }

    @Scheduled(cron = "5 0 0 * * ?")
    public void getStarbucks() {
        Map<String , Object> objectMap = new HashMap<>();

        objectMap.put("status" , 15);

        List<BsOrderStarbucks> orderStarbucks = orderStarbucksService.getList(objectMap);

        for (BsOrderStarbucks starbucks : orderStarbucks) {

            if (starbucks != null) {
                // 查询星巴克订单详情
                BsOrderChild child = orderChildService.getDetail(starbucks.getChildOrderNo());
                Map<String , Object> orderMap = new HashMap<>();
                orderMap.put("orderNo" , child.getChannelOrderNo());

                // 子订单完成
                orderService.childOrderComplete(starbucks.getChildOrderNo());

            }


        }
    }
}
