package com.yc.ship.module.trade.job;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.yc.ship.framework.quartz.core.handler.JobHandler;
import com.yc.ship.framework.security.core.LoginUser;
import com.yc.ship.framework.tenant.core.aop.TenantIgnore;
import com.yc.ship.framework.tenant.core.job.TenantJob;
import com.yc.ship.module.trade.controller.admin.order.vo.order.PayOrderReqVO;
import com.yc.ship.module.trade.dal.dataobject.order.TradeOrderDO;
import com.yc.ship.module.trade.dal.dataobject.shipSaleRule.ShipSaleRuleDO;
import com.yc.ship.module.trade.service.order.TradeOrderService;
import com.yc.ship.module.trade.service.otc.OtcTradeOrderService;
import com.yc.ship.module.trade.service.shipSaleRule.ShipSaleRuleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class OrderStausJob implements JobHandler {
    @Resource
    private ShipSaleRuleService shipSaleRuleService;

    @Resource
    private TradeOrderService tradeOrderService;

    @Resource
    private OtcTradeOrderService otcTradeOrderService;

    /**
     * 执行订单状态相关操作-初定自动确认
     *
     * 当前实现中，该方法会获取一条船舶销售规则，但未使用传入的参数和获取的规则，
     * 直接返回固定字符串"成功"。实际业务逻辑可能需要进一步完善。
     *
     * @param param 传入的参数（当前未使用）
     * @return 固定返回字符串"成功"
     */
    @Override
    @TenantJob
    public String execute(String param) {
        ShipSaleRuleDO shipSaleRuleOne = shipSaleRuleService.getShipSaleRuleOne();
        int  qdday = shipSaleRuleOne.getQdDay();
        int qdhour = shipSaleRuleOne.getQdHour();
        Date n = DateUtil.offsetDay(new DateTime(), qdday);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(n);
        calendar.set(Calendar.HOUR_OF_DAY, qdhour);
        Date time = calendar.getTime();
        List<TradeOrderDO> tradeOrderByExpire = tradeOrderService.getTradeOrderByExpire(DateUtil.format(time, "yyyy-MM-dd HH:mm:ss"));
        log.error("自动确认订单时间:{},tradeOrderByExpire={}", DateUtil.format(time, "yyyy-MM-dd HH:mm:ss"),tradeOrderByExpire.size());
        tradeOrderByExpire.forEach(tradeOrderDO -> {
            try {
                LoginUser agencyUser = new LoginUser();
                agencyUser.setStoreId(tradeOrderDO.getStoreId());
                agencyUser.setDistributorId(Long.parseLong(tradeOrderDO.getSourceId()));
                PayOrderReqVO payOrderReqVO = new PayOrderReqVO();
                payOrderReqVO.setOrderId(tradeOrderDO.getId());
                payOrderReqVO.setPayAmount(tradeOrderDO.getPayAmount());
                payOrderReqVO.setPayType(5);
                payOrderReqVO.setStoreId(tradeOrderDO.getStoreId());
                payOrderReqVO.setAccountId("9999");
                otcTradeOrderService.windowPayOrder(agencyUser, payOrderReqVO);
                log.error("自动确认订单成功:{}", tradeOrderDO.getOrderNo());
            }catch (Exception e){
                log.error(tradeOrderDO.getOrderNo()+";自动确认订单失败", e);
            }
        });
        return "成功";
    }

}
