package com.example.repurchase.task;

import com.example.repurchase.getTags.CalculateHousePriceTags;
import com.example.repurchase.getTags.CalculateRFMTags;
import com.example.repurchase.getTags.CalculateRepurchaseUserTags;
import com.example.repurchase.utils.DateUtils;
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.SimpleDateFormat;
import java.util.Date;

@Component
@Configuration
@EnableScheduling
public class Task01 {


    @Autowired
    private CalculateRepurchaseUserTags calculateRepurchaseUserTags;

    @Autowired
    private CalculateHousePriceTags calculateHousePriceTags;

    @Autowired
    private CalculateRFMTags calculateRFMTags;


    @Scheduled(cron = "0 15 3 * * ?")
    public void calculateRepurchaseOrRFMOrMemberbaseInfo() throws Exception {

        for (int i = 0; i < 20; i++) {
            calculateHousePriceTags.runGetHouseTagsMultiThread(15, 100);
        }


        String yearAndMonth = new SimpleDateFormat("yyyy-MM").format(new Date());

        int day = Integer.parseInt(new SimpleDateFormat("yyyy-MM-dd").format(new Date()).split("-")[2]);

        if (day < 15) {
            try {
                calculateRepurchaseUserTags.getRepurchaseUserByMonth(DateUtils.getOffsetMonth(yearAndMonth, -1));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        try {
            calculateRepurchaseUserTags.getRepurchaseUserByMonth(yearAndMonth);
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            calculateRFMTags.calculateRFMTags();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
