package com.crm.ib.task.service.impl;

import com.crm.ib.task.service.DailyTaskService;
import com.crm.ib.task.service.DailyTradeService;
import com.crm.rely.backend.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Calendar;
import java.util.Date;

@Service
public class DailyTaskServiceImpl implements DailyTaskService {

    private final Logger logger = LoggerFactory.getLogger(DailyTaskServiceImpl.class);

    @Autowired
    private DailyTradeService dailyTradeService;

    @Override
    @Scheduled(cron = "30 0 0 * * ?")
    @Transactional(rollbackFor = Exception.class)
    public void supplyTrade() throws Exception {

        try {
            Date endDate = DateUtil.getPureDate(new Date());
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(endDate);
            calendar.add(Calendar.DATE, -1);
            Date startDate = calendar.getTime();
            dailyTradeService.supply(startDate, endDate);
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw e;
        }
    }
}
