package com.zcloud.util;

import com.zcloud.entity.PageData;
import com.zcloud.service.bus.CorpInfoService;
import com.zcloud.service.bus.CorpTimeService;
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.util.*;

@Component
public class CorpTimeRemindScheduled {

    @Autowired
    private CorpInfoService corpInfoService;
    @Autowired
    private ServiceNoticeService noticeService;
    @Autowired
    private NoticeRemindLogService noticeRemindLogService;

    @Scheduled(cron ="0 30 0 * * ?")//每天12点30
    public void scheduled () throws Exception {
        System.out.println("============定时提醒企业到期情况==========");
        PageData pd = new PageData();
        List<PageData> corpList = corpInfoService.findAllUsedCorp(pd);
        if (corpList != null && corpList.size() > 0) {
            for (PageData corp : corpList) {
                String content = "";
                if(corp.get("REMAININGTIME").toString().equals("90") || corp.get("REMAININGTIME").toString().equals("30") || corp.get("REMAININGTIME").toString().equals("15") ){
                    content = "尊敬的一体化数字平台客户,截止"+DateUtil.date2Str(new Date(),"yyyy年MM月dd日")+
                            ",您的平台服务期还剩余"+corp.get("REMAININGTIME")+"日,为了不影响您的平台正常使用,请联系我们客户服务人员进行后期服务续费," +
                            "客服热线:13303358557";
                }else if(corp.get("REMAININGTIME").equals("0") ){
                    content = "尊敬的一体化数字平台客户,您的平台服务期已过期,如需恢复平台使用功能,请联系我们客户服务人员," +
                            "客服热线:13303358557";
                }else{
                    continue;
                }
                PageData notice = new PageData();
                notice.put("NOTICE_CORP_ID", UuidUtil.get32UUID());
                notice.put("NOTICE_ID", UuidUtil.get32UUID());                            //主键
                notice.put("ISDELETE", "0");    //删除标识（0：有效   1：删除）
                notice.put("ISTOP", "1");    //是否置顶（0否 1是）
                notice.put("STATUS", "1");    //状态（0：未发布   1：发布）
                notice.put("USERNAME", "admin");                    //用户名
                notice.put("CREATOR", "admin");                        //发布人
                notice.put("CREATTIME", DateUtil.date2Str(new Date()));                    //发布时间
                notice.put("ISALL", "no");
                notice.put("TYPE", "2");
                notice.put("SYNOPSIS", "服务到期提醒");
                notice.put("CONTENT", content);
                noticeService.save(notice);

                PageData notCorpPd = new PageData();
                List<Map<String, Object>> list = new ArrayList<>();
                Map<String, Object> map2 = new HashMap<>();
                map2.put("NOTICE_CORP_ID", UuidUtil.get32UUID());
                map2.put("noticeId", notice.get("NOTICE_ID"));
                map2.put("corpId", corp.getString("CORPINFO_ID"));
                map2.put("TYPE", "0");
                list.add(map2);
                notCorpPd.put("list", list);
                noticeService.saveNotAndCorp(notCorpPd);

                PageData log = new PageData();
                log.put("NOTICE_REMIND_LOG_ID", UuidUtil.get32UUID());
                log.put("CORPINFO_ID", corp.getString("CORPINFO_ID"));
                log.put("TYPE", "1");
                log.put("CREATOR", "admin");
                log.put("CREATTIME", DateUtil.date2Str(new Date()));
                log.put("REMAININGTIME", corp.get("REMAININGTIME"));
                noticeRemindLogService.save(log);
            }
        }
    }

}
