package org.dromara.system.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.SpringUtils;
import org.dromara.system.domain.bo.TblDzbcPathSchedulingBo;
import org.dromara.system.domain.vo.TblDzbcPathSchedulingVo;
import org.dromara.system.service.ISysUserService;
import org.dromara.system.service.ITblDzbcPathSchedulingService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class ScheduleCarUtil {

    @Scheduled(cron = "0 10 0 ? * ? ")
    public void doSchedule() {
        //获取所有自动排班标记的路线
        TblDzbcPathSchedulingBo bo = new TblDzbcPathSchedulingBo();
        bo.setIsauto(1);
        List<TblDzbcPathSchedulingVo> list = SpringUtils.getBean(ITblDzbcPathSchedulingService.class).queryList(bo);
        Map<Long, TblDzbcPathSchedulingVo> map = new HashMap<>();
        for (TblDzbcPathSchedulingVo vo : list) {
            if(vo.getMdate()==null) continue;
            if(map.containsKey(vo.getPathId())){
                TblDzbcPathSchedulingVo vo1 = map.get(vo.getPathId());
                if(vo1.getMdate().getTime()<vo.getMdate().getTime()){
                    map.put(vo.getPathId(),vo);
                }
            }else{
                map.put(vo.getPathId(),vo);
            }
        }
        map.values().stream().forEach(vo -> {
            try {
                TblDzbcPathSchedulingBo bo1 = new TblDzbcPathSchedulingBo();
                // 取明天和 mdate+1 的最大值，避免暂停后恢复时一直生成过去日期
                Date nextDate = DateUtil.offsetDay(vo.getMdate(), 1);
                Date tomorrow = DateUtil.offsetDay(new Date(), 1);
                bo1.setMdate(nextDate.before(tomorrow) ? tomorrow : nextDate);
                bo1.setMtime(vo.getMtime());
                bo1.setPathId(vo.getPathId());
                bo1.setExt1(vo.getExt1());
                bo1.setExt2(vo.getExt2());
                bo1.setCarId(vo.getCarId());
                bo1.setPprice(vo.getPprice());
                bo1.setIsauto(1);
                // 从原记录 ext2 解析 type，确保 insertByBo 能正确识别路线类型
                if (vo.getExt2() != null) {
                    try {
                        JSONObject ext2 = JSONUtil.parseObj(vo.getExt2());
                        if (ext2.containsKey("type")) {
                            bo1.setType(ext2.getInt("type"));
                        }
                    } catch (Exception e) {
                        log.warn("解析排班类型失败, pathId={}", vo.getPathId());
                    }
                }
                SpringUtils.getBean(ITblDzbcPathSchedulingService.class).insertByBo(bo1);
            } catch (Exception e) {
                log.error("自动排班续排失败, pathId={}, mdate={}", vo.getPathId(), vo.getMdate(), e);
            }
        });


    }
}
