package com.zcloud.util;

import com.zcloud.entity.PageData;
import com.zcloud.service.bus.CorpInfoService;
import com.zcloud.service.bus.CorpVisitService;
import com.zcloud.service.bus.MaintenanceUserInfoService;
import com.zcloud.service.bus.ServiceNoticeService;
import com.zcloud.service.notice.NoticeRemindLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.*;

@Component
public class CorpVisitScheduled {
    @Autowired
    private CorpVisitService corpvisitService;

    @Scheduled(cron ="0 30 0 * * ?")//每天12点30
    public void scheduled () throws Exception {
        System.out.println("============定时修改周期未回访==========");
        PageData pd = new PageData();

        pd.put("EXPIRED",1);
        List<PageData> records = corpvisitService.listAll(pd); //获取企业是否有周期包含当前时间的回访记录

        for (int i = 0; i < records.size(); i++) {
            pd.put("STATUS",2);
            pd.put("CORPVISIT_ID",records.get(i).getString("CORPVISIT_ID"));
            corpvisitService.edit(pd);
            PageData record =records.get(i);
            String PERIOD_END_TIME = record.getString("PERIOD_END_TIME");
            SimpleDateFormat sdf =   new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
            Date periodEndTime = sdf.parse(PERIOD_END_TIME);
            PageData visit = new PageData();
            visit.put("CORPVISIT_ID", UuidUtil.get32UUID());    //主键
            visit.put("CORPINFO_ID", pd.getString("CORPINFO_ID"));
            visit.put("PERIOD_START_TIME", DateUtil.date2Str(periodEndTime));
            visit.put("PERIOD_END_TIME", DateUtil.date2Str(DateUtil.TimePlus(periodEndTime, Integer.parseInt(record.get("VISIT_PERIOD").toString()))));
            visit.put("STATUS", -1);
            corpvisitService.save(visit);
        }
    }

}
