package com.ruoyi.demo.utils.statistics;

import com.ruoyi.demo.entity.StoreWdOpclose;
import com.ruoyi.demo.mapper.StoreWdOpcloseDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
public class StoreWdOpcloseStat {
    @Autowired
    StoreWdOpcloseDao storeWdOpcloseDao;

    public void continueInsertAll(){
        // 已分析的门店
        List<String> ins = storeWdOpcloseDao.selectIns();
        System.out.println("已处理：" + ins.size() + "个");
        // 未分析的门店
        List<StoreWdOpclose> storeWdOpcloses = storeWdOpcloseDao.selectStatistics(ins);
        int total = storeWdOpcloses.size();
        int i = 0;
        for (StoreWdOpclose storeWd : storeWdOpcloses) {
            if (storeWd.getBusinessTime().equals("24小时营业")) {
                storeWd.setStartTime("00:00");
                storeWd.setEndTime("24:00");
            } else {
//            String str = "周一至周日:07:00-17:00 18:00-23:00";
                Pattern pattern = Pattern.compile("\\d{2}:\\d{2}");
//            Matcher matcher = pattern.matcher(str);
                Matcher matcher = pattern.matcher(storeWd.getBusinessTime());

                String minTime = null, maxTime = null;
                while (matcher.find()) {
                    String time = matcher.group();
                    if (minTime == null || time.compareTo(minTime) < 0) {
                        // 如果当前时间小于最小时间，则更新最小时间
                        minTime = time;
                    }
                    if (maxTime == null || time.compareTo(maxTime) > 0) {
                        // 如果当前时间大于最大时间，则更新最大时间
                        maxTime = time;
                    }
                }

//            System.out.println("最小时间：" + minTime);
//            System.out.println("最大时间：" + maxTime);
                storeWd.setStartTime(minTime);
                storeWd.setEndTime(maxTime);
                i += storeWdOpcloseDao.insert(storeWd);
                System.out.println(i + "/" + total);
            }
        }

    }
}
