package com.yijia.station.task;
import com.yijia.common.utils.DateUtils;

import com.yijia.station.domain.StationOilGun;
import com.yijia.station.domain.StationOilPrice;
import com.yijia.station.domain.StationOilPriceAdjust;
import com.yijia.station.service.IStationOilGunService;
import com.yijia.station.service.IStationOilPriceAdjustService;
import com.yijia.station.service.IStationOilPriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

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

@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class SaticScheduleTask {
    @Autowired
    private IStationOilPriceAdjustService stationOilPriceAdjustService;
    @Autowired
    private IStationOilPriceService stationOilPriceService;
    @Autowired
    private IStationOilGunService stationOilGunService;
    //3.添加定时任务
   // @Scheduled(cron = "0/5 * * * * ?")
    //或直接指定时间间隔，例如：5秒 5000
    @Scheduled(fixedRate=5000)
    private void configureTasks() throws ParseException {
       // 获取当前时间 yyyy-MM-dd HH:mm:ss
        String newdate = DateUtils.getTime();
        System.err.println("执行静态定时任务时间: " + newdate);
        //查询油品调价列表 查询出未调价的信息
        StationOilPriceAdjust stationOilPriceAdjust =new StationOilPriceAdjust();
        stationOilPriceAdjust.setStatus("1");
        stationOilPriceAdjust.setTakeEffectStatus("2");
        stationOilPriceAdjust.setEndTime(newdate);
        List<StationOilPriceAdjust>  oilPriceAdjustList=stationOilPriceAdjustService.selectStationOilPriceAdjustList(stationOilPriceAdjust);
        //循环查看 调价的生效时间是否和当前时间相符
        for(StationOilPriceAdjust adjust: oilPriceAdjustList ){
            //修改油品价格根据油品价格id
            StationOilPrice oilPrice=new StationOilPrice();
            oilPrice.setOilPriceId(adjust.getOilPriceId());
            oilPrice.setOilPrice(adjust.getOilAdjustPrice());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date sdate = sdf.parse(newdate);
            oilPrice.setDate(sdate);
            stationOilPriceService.updateStationOilPrice(oilPrice);
            //修改油枪信息中的价格
            StationOilGun gun =new StationOilGun();
            gun.setStationId(adjust.getStationId());
            gun.setOilName(adjust.getOilName());
            List<StationOilGun> listGun = stationOilGunService.selectStationOilGunList(gun);
            for(StationOilGun oilGun: listGun ){
                StationOilGun stationOilGun =new StationOilGun();
                stationOilGun.setOilPrice(adjust.getOilAdjustPrice());
                stationOilGun.setOilGunId(oilGun.getOilGunId());
                stationOilGunService.updateStationOilGun(stationOilGun);
            }
            StationOilPriceAdjust priceAdjust=new StationOilPriceAdjust();
            priceAdjust.setStatus("2");
            priceAdjust.setAdjustPriceId(adjust.getAdjustPriceId());
            stationOilPriceAdjustService.updateStationOilPriceAdjust(priceAdjust);
        }
    }
}
