package com.xmomen.module.schedule;


import com.xmomen.framework.support.SpringContextUtil;
import com.xmomen.module.order.model.CreatePurchase;
import com.xmomen.module.order.service.PurchaseService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Jeng on 2016/2/23.
 */
@Component(value = "itemPurchaseJob")
public class ItemPurchaseJob implements Job {

    // 注入PurchaseService
    @Autowired
    PurchaseService purchaseService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        // 如果purchaseService为空，则从Spring上下文中获取
        if(purchaseService == null){
            purchaseService = (PurchaseService) SpringContextUtil.getApplicationContext().getBean(PurchaseService.class);
            // 创建一个CreatePurchase对象
            CreatePurchase createPurchase = new CreatePurchase();
            // 设置订单日期为当前日期
            createPurchase.setOrderDate(new Date());
            // 调用PurchaseService的createPurchase方法创建采购订单
            purchaseService.createPurchase(createPurchase);
        }
    }
}
