package com.hfkj.schedule;

import com.alibaba.fastjson.JSONArray;
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.BsOrderGoods;
import com.hfkj.entity.GoodsLogistics;
import com.hfkj.entity.GoodsUserAddress;
import com.hfkj.service.goods.BsOrderGoodsService;
import com.hfkj.service.goods.GoodsUserAddressService;
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;

/**
  * @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 BsOrderGoodsService bsOrderGoodsService;

    @Resource
    private GoodsUserAddressService addressService;

    //每日凌晨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) {
                bsOrderGoodsService.confirmReceipt(goods.getId());
            }
        }
    }

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

        objectMap.put("status" , 2);

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

        for (BsOrderGoods goods : orderGoodsList) {

            if (goods == null) {
                throw ErrorHelp.genException(SysCode.System , ErrorCode.COMMON_ERROR , "暂无物流信息！");
            }

            GoodsUserAddress userAddress = addressService.queryDetail(goods.getDeliveryAddressId());

            if (userAddress == null) {
                throw ErrorHelp.genException(SysCode.System , ErrorCode.COMMON_ERROR , "暂无物流信息！");
            }

            if (goods.getLogisticsNo() != null) {
                GoodsLogistics logistics = (GoodsLogistics) bsOrderGoodsService.getLogisticsMsg(goods.getLogisticsNo() , userAddress.getPhone());
                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());

                bsOrderGoodsService.update(goods);
            }

        }
    }
}
