package com.zcloud.util;

import com.zcloud.entity.PageData;
import com.zcloud.service.bus.CorpInfoService;
import com.zcloud.service.bus.CorpTimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class CorpStopScheduled {
    @Autowired
    private CorpTimeService corpTimeService;
    @Autowired
    private CorpInfoService corpInfoService;

    @Scheduled(cron = "0 0 1 * * ?")
    public void scheduled() throws Exception {
        System.out.println("===========定时停用/启用企业==========");

        List<PageData> dataList = corpTimeService.getStopCorpScheduledList();
        List<PageData> stopList = new ArrayList<PageData>();
        List<PageData> enableList = new ArrayList<PageData>();
        if (dataList != null && dataList.size() > 0) {
            for (PageData pageData : dataList) {
                if (pageData.getString("STATUS").equals("stop")) {
                    stopList.add(pageData);
                }
                if (pageData.getString("STATUS").equals("enable")) {
                    enableList.add(pageData);
                }
            }
        }
        if (stopList.size() > 0) {
            stopCorp(stopList);   // 停用企业
        }
        if (enableList.size() > 0) {
            enableCorp(enableList);     // 启用企业
        }
    }

    /**
     * 停用企业
     * @throws Exception
     */
    private void stopCorp(List<PageData> stopList) throws Exception {
        List<String> stopCorpIds = new ArrayList<>();
        List<PageData> stopListData = new ArrayList<>();
        for (PageData pageData : stopList) {
            stopCorpIds.add(pageData.getString("CORPINFO_ID"));
            PageData stopPageData = new PageData();
            stopPageData.put("ID",UuidUtil.get32UUID());
            stopPageData.put("OPERATOR", "admin");	//操作人
            stopPageData.put("OPERATTIME", DateUtil.date2Str(new Date()));	//操作时间
            stopPageData.put("ISDELETE", '0');
            stopPageData.put("ISSTOP", '0');
            stopPageData.put("CORPINFO_ID",pageData.getString("CORPINFO_ID"));
            stopPageData.put("STOPREASON",pageData.getString("STOP_REASON"));
            stopListData.add(stopPageData);
        }
        corpInfoService.stopCorp(stopCorpIds);
        corpInfoService.stopCorpInfo(stopCorpIds);
        corpInfoService.batchGoStop(stopListData);
    }

    /**
     * 启用企业
     * @throws Exception
     */
    private void enableCorp(List<PageData> enableList) throws Exception {
        List<String> enableCorpIds = new ArrayList<String>();
        List<PageData> enableListData = new ArrayList<>();
        for (PageData pageData : enableList) {
            enableCorpIds.add(pageData.getString("CORPINFO_ID"));
            PageData enablePageData = new PageData();
            enablePageData.put("OPERATOR", "admin");	//操作人
            enablePageData.put("ID",UuidUtil.get32UUID());
            enablePageData.put("OPERATTIME", DateUtil.date2Str(new Date()));	//操作时间
            enablePageData.put("ISDELETE", '0');
            enablePageData.put("ISSTOP", '1');
            enablePageData.put("CORPINFO_ID",pageData.getString("CORPINFO_ID"));
            enablePageData.put("STOPREASON",pageData.getString("STOP_REASON"));
            enableListData.add(enablePageData);
        }
        corpInfoService.enableCorp(enableCorpIds);
        corpInfoService.enableCorpInfo(enableCorpIds);
        corpInfoService.batchGoStart(enableListData);
    }
}
