package com.zhongzheng.modules.schedule.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpStatus;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.zhongzheng.common.core.redis.RedisCache;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.common.utils.http.HttpUtils;
import com.zhongzheng.common.utils.polyv.PolyvUtils;
import com.zhongzheng.modules.base.service.IProfileTpService;
import com.zhongzheng.modules.base.vo.ProfileTpVo;
import com.zhongzheng.modules.base.vo.UserProfileVo;
import com.zhongzheng.modules.exam.domain.ExamNumber;
import com.zhongzheng.modules.exam.vo.ExamUserApplyVo;
import com.zhongzheng.modules.goods.service.IGoodsService;
import com.zhongzheng.modules.goods.vo.GoodsVo;
import com.zhongzheng.modules.grade.domain.ClassGradeUser;
import com.zhongzheng.modules.grade.domain.ClassGradeUserTemp;
import com.zhongzheng.modules.grade.service.IClassGradeGoodsService;
import com.zhongzheng.modules.grade.service.IClassGradeService;
import com.zhongzheng.modules.grade.service.IClassGradeUserService;
import com.zhongzheng.modules.grade.service.IClassGradeUserTempService;
import com.zhongzheng.modules.grade.vo.ClassGradeUserGoodsVo;
import com.zhongzheng.modules.grade.vo.ClassGradeUserVo;
import com.zhongzheng.modules.inform.bo.InformQueryBo;
import com.zhongzheng.modules.inform.bo.InformUserAddBo;
import com.zhongzheng.modules.inform.domain.Inform;
import com.zhongzheng.modules.inform.domain.InformUser;
import com.zhongzheng.modules.inform.service.IInformRemindService;
import com.zhongzheng.modules.inform.service.IInformService;
import com.zhongzheng.modules.inform.service.IInformUserService;
import com.zhongzheng.modules.inform.vo.InformRemindVo;
import com.zhongzheng.modules.inform.vo.InformVo;
import com.zhongzheng.modules.order.bo.OrderQueryBo;
import com.zhongzheng.modules.order.domain.Order;
import com.zhongzheng.modules.order.mapper.OrderMapper;
import com.zhongzheng.modules.order.service.IOrderBusinessService;
import com.zhongzheng.modules.order.service.IOrderGoodsService;
import com.zhongzheng.modules.order.service.IOrderService;
import com.zhongzheng.modules.order.vo.OrderGoodsVo;
import com.zhongzheng.modules.order.vo.OrderListVo;
import com.zhongzheng.modules.order.vo.OrderVo;
import com.zhongzheng.modules.polyv.bo.PolyvVideoAddBo;
import com.zhongzheng.modules.polyv.bo.PolyvVideoEditBo;
import com.zhongzheng.modules.polyv.bo.PolyvVideoQueryBo;
import com.zhongzheng.modules.polyv.domain.PolyvVideDo;
import com.zhongzheng.modules.polyv.domain.PolyvVideo;
import com.zhongzheng.modules.polyv.domain.TokenResponse;
import com.zhongzheng.modules.polyv.mapper.PolyvVideoMapper;
import com.zhongzheng.modules.polyv.vo.PolyvVideoQuerVo;
import com.zhongzheng.modules.polyv.vo.PolyvVideoVo;
import com.zhongzheng.modules.polyv.vo.PolyvVo;
import com.zhongzheng.modules.schedule.service.IScheduleService;
import com.zhongzheng.modules.user.bo.UserExamGoodsSupplementAddBo;
import com.zhongzheng.modules.user.bo.UserQueryBo;
import com.zhongzheng.modules.user.domain.UserExamGoods;
import com.zhongzheng.modules.user.domain.UserSubscribe;
import com.zhongzheng.modules.user.service.IUserExamGoodsService;
import com.zhongzheng.modules.user.service.IUserExamGoodsSupplementService;
import com.zhongzheng.modules.user.service.IUserService;
import com.zhongzheng.modules.user.service.IUserSubscribeService;
import com.zhongzheng.modules.user.vo.UserSubscribeVo;
import com.zhongzheng.modules.user.vo.UserVo;
import com.zhongzheng.modules.wx.service.IWxPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import static com.squareup.okhttp.internal.Util.md5Hex;

/**
 * 定时任务
 *
 * @author change
 * @date 2021-06-11
 */
@Service
public class ScheduleServiceImpl extends ServiceImpl<PolyvVideoMapper, PolyvVideo> implements IScheduleService {

    @Autowired
    private IInformService informService;

    @Autowired
    private IInformRemindService informRemindService;

    @Autowired
    private IProfileTpService iProfileTpService;


    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private IUserExamGoodsSupplementService userExamGoodsSupplementService;

    @Autowired
    private com.zhongzheng.modules.alisms.service.IAliSmsService IAliSmsService;

    @Autowired
    private IInformUserService iInformUserService;

    @Autowired
    private IOrderGoodsService iOrderGoodsService;

    @Autowired
    private IOrderBusinessService iOrderBusinessService;
    @Autowired
    private IGoodsService iGoodsService;
    @Autowired
    private IClassGradeService iClassGradeService;
    @Autowired
    private IClassGradeUserService iClassGradeUserService;
    @Autowired
    private IClassGradeGoodsService iClassGradeGoodsService;
    @Autowired
    private IOrderService iOrderService;

    @Autowired
    private IUserService iUserService;

    @Autowired
    private IUserExamGoodsService userExamGoodsService;

    @Autowired
    private IClassGradeUserTempService iClassGradeUserTempService;


    @Autowired
    private IUserSubscribeService iUserSubscribeService;

    @Value("${aliyun.sms.OpenTheGoodsCode}")
    private String OpenTheGoodsCode;

    @Value("${aliyun.sms.informationNotificationCode}")
    private String informationNotificationCode;

    @Value("${aliyun.sms.bookingReminder}")
    private String bookingReminder;

    @Value("${aliyun.sms.examinationRemind}")
    private String examinationRemind;

    @Value("${aliyun.sms.remainingDaysValidityCode}")
    private String remainingDaysValidityCode;

    @Override
    public String updateGoodsSend(UserQueryBo bo) {
        InformRemindVo informRemindVo = informRemindService.queryById(1L);
        List<OrderGoodsVo> orderGoodsVo = iOrderGoodsService.selectOrderGoods();

        InformRemindVo informRemindTwoVo = informRemindService.queryById(2L);
        if (!CollectionUtils.isEmpty(orderGoodsVo)) {
            for (OrderGoodsVo goodsVo : orderGoodsVo) {
                ProfileTpVo profileTpVo = iProfileTpService.queryByGoodsId(goodsVo.getGoodsId());
                if (informRemindVo.getWayStatus() == 1 && profileTpVo == null) {
                    InformUserAddBo informUserAddBo = new InformUserAddBo();
                    informUserAddBo.setUserId(goodsVo.getUserId());
                    informUserAddBo.setSendStatus(1);
                    informUserAddBo.setCreateTime(DateUtils.getNowTime());
                    informUserAddBo.setUpdateTime(DateUtils.getNowTime());
                    informUserAddBo.setSendTime(DateUtils.getNowTime());
                    informUserAddBo.setRemindId(1L);
                    informUserAddBo.setSystemStatus(1);
                    informUserAddBo.setRemind("商品开通提醒");
                    informUserAddBo.setGoodsId(goodsVo.getGoodsId());
                    GoodsVo goodsVo1 = iGoodsService.queryById(goodsVo.getGoodsId());
                    informUserAddBo.setText("尊敬的用户：您购买的" + goodsVo1.getGoodsName() + "已经开通，请尽快去学习吧。");
                    iInformUserService.insertByAddBo(informUserAddBo);
                }
                if (informRemindTwoVo.getWayStatus() == 1 && profileTpVo != null) {
                    InformUserAddBo informUserAddBo = new InformUserAddBo();
                    informUserAddBo.setUserId(goodsVo.getUserId());
                    informUserAddBo.setSendTime(DateUtils.getNowTime());
                    informUserAddBo.setSendStatus(1);
                    informUserAddBo.setCreateTime(DateUtils.getNowTime());
                    informUserAddBo.setUpdateTime(DateUtils.getNowTime());
                    informUserAddBo.setRemindId(2L);
                    informUserAddBo.setSystemStatus(1);
                    informUserAddBo.setRemind("完善报名资料通知");
                    informUserAddBo.setGoodsId(goodsVo.getGoodsId());
                    GoodsVo goodsVo1 = iGoodsService.queryById(goodsVo.getGoodsId());
                    informUserAddBo.setText("尊敬的用户：已为您开通了" + goodsVo1.getGoodsName() + "课程，本课程需完善信息方可学习，请尽快去完善资料吧");
                    iInformUserService.insertByAddBo(informUserAddBo);
                }
            }
        }
        List<OrderGoodsVo> orderGoodsTwoVo = iOrderGoodsService.selectOrderTwoGoods();
        if (!CollectionUtils.isEmpty(orderGoodsTwoVo)) {
            for (OrderGoodsVo goodsVo : orderGoodsTwoVo) {
                ProfileTpVo profileTpVo = iProfileTpService.queryByGoodsId(goodsVo.getGoodsId());
                if (informRemindVo.getNoteStatus() == 1 && profileTpVo == null) {
                    InformUserAddBo informUserAddBo = new InformUserAddBo();
                    informUserAddBo.setUserId(goodsVo.getUserId());
                    informUserAddBo.setSendTime(DateUtils.getNowTime());
                    informUserAddBo.setSendStatus(1);
                    informUserAddBo.setCreateTime(DateUtils.getNowTime());
                    informUserAddBo.setUpdateTime(DateUtils.getNowTime());
                    informUserAddBo.setSendTime(DateUtils.getNowTime());
                    informUserAddBo.setRemindId(1L);
                    informUserAddBo.setSystemStatus(3);
                    informUserAddBo.setRemind("商品开通提醒");
                    informUserAddBo.setGoodsId(goodsVo.getGoodsId());
                    GoodsVo goodsVo1 = iGoodsService.queryById(goodsVo.getGoodsId());
                    informUserAddBo.setText("尊敬的用户：您购买的" + goodsVo1.getGoodsName() + "已经开通，请尽快去学习吧。");
                    iInformUserService.insertByAddBo(informUserAddBo);
                    UserVo userVo = iUserService.queryById(goodsVo.getUserId());
                    Map<String, Object> param = new HashMap<>();
                    param.put("goods", goodsVo1.getGoodsName());
                    IAliSmsService.sendInformSms(userVo.getTelphone(), JSON.toJSONString(param), OpenTheGoodsCode);
                }
                if (informRemindTwoVo.getNoteStatus() == 1 && profileTpVo != null) {
                    InformUserAddBo informUserAddBo = new InformUserAddBo();
                    informUserAddBo.setUserId(goodsVo.getUserId());
                    informUserAddBo.setSendTime(DateUtils.getNowTime());
                    informUserAddBo.setSendStatus(1);
                    informUserAddBo.setCreateTime(DateUtils.getNowTime());
                    informUserAddBo.setUpdateTime(DateUtils.getNowTime());
                    informUserAddBo.setRemindId(1L);
                    informUserAddBo.setSystemStatus(3);
                    informUserAddBo.setRemind("商品开通提醒");
                    informUserAddBo.setGoodsId(goodsVo.getGoodsId());
                    GoodsVo goodsVo1 = iGoodsService.queryById(goodsVo.getGoodsId());
                    informUserAddBo.setText("尊敬的用户：您购买的" + goodsVo1.getGoodsName() + "已经开通，请尽快去学习吧。");
                    iInformUserService.insertByAddBo(informUserAddBo);
                    UserVo userVo = iUserService.queryById(goodsVo.getUserId());
                    Map<String, Object> param = new HashMap<>();
                    param.put("goods", goodsVo1.getGoodsName());
                    IAliSmsService.sendInformSms(userVo.getTelphone(), JSON.toJSONString(param), informationNotificationCode);
                }
            }
        }
        sendGoods();
        return "请求成功";
    }

    private void sendGoods() {
        List<OrderGoodsVo> orderGoodsVo = iOrderGoodsService.sendGoods();
        for (OrderGoodsVo goodsVo : orderGoodsVo) {
            ExamNumber examNumber = userExamGoodsService.queryExam(goodsVo.getGoodsId());
            if (examNumber != null) {
                UserExamGoods add = new UserExamGoods();
                add.setGoodsId(goodsVo.getGoodsId());
                add.setUserId(goodsVo.getUserId());
                add.setExamNumber(examNumber.getExamNumber());
                add.setDoNumber(examNumber.getDoNumber());
                add.setCreateTime(DateUtils.getNowTime());
                add.setUpdateTime(DateUtils.getNowTime());
                userExamGoodsService.save(add);
                UserExamGoodsSupplementAddBo userExamGoodsSupplementAddBo = new UserExamGoodsSupplementAddBo();
                userExamGoodsSupplementAddBo.setGoodsId(goodsVo.getGoodsId());
                userExamGoodsSupplementAddBo.setUserExamGoodsId(add.getUserExamGoodsId());
                userExamGoodsSupplementAddBo.setExamNumber(examNumber.getExamNumber());
                userExamGoodsSupplementAddBo.setDoNumber(examNumber.getDoNumber());
                userExamGoodsSupplementAddBo.setStatus(3);
                userExamGoodsSupplementService.insertByAddBo(userExamGoodsSupplementAddBo);
            }
        }
    }

    @Override
    public void updateExamSend(UserQueryBo bo) {
        //新考预约提醒
        sendExamSubscribe();

        sendClassEnd();
    }

    private void sendClassEnd() {
        List<ClassGradeUserGoodsVo> classTwentyGradeUserVos = iClassGradeUserService.sendTwentyClassGradeUser();
        List<ClassGradeUserGoodsVo> classTenGradeUserVos = iClassGradeUserService.sendTenClassGradeUser();
        List<ClassGradeUserGoodsVo> classFiveGradeUserVos = iClassGradeUserService.sendFiveClassGradeUser();
        InformRemindVo informTwentyRemindVo = informRemindService.queryById(5L);
        InformRemindVo informTenRemindVo = informRemindService.queryById(6L);
        InformRemindVo informFiveRemindVo = informRemindService.queryById(7L);
        for (ClassGradeUserGoodsVo classTwentyGradeUserVo : classTwentyGradeUserVos) {
            String res;
            String resEnd;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Long timeId = classTwentyGradeUserVo.getClassStartTime()*1000;
            Long timeEndId = classTwentyGradeUserVo.getClassStartTime()*1000;
            Date date = new Date(timeId);
            Date dateEnd = new Date(timeEndId);
            res = simpleDateFormat.format(date);
            resEnd = simpleDateFormat.format(dateEnd);

            if (informTwentyRemindVo.getWayStatus().equals(1)) {
                InformUserAddBo informUserAddBo = new InformUserAddBo();
                informUserAddBo.setUserId(classTwentyGradeUserVo.getUserId());
                informUserAddBo.setSendStatus(1);
                informUserAddBo.setSendTime(DateUtils.getNowTime());
                informUserAddBo.setCreateTime(DateUtils.getNowTime());
                informUserAddBo.setUpdateTime(DateUtils.getNowTime());
                informUserAddBo.setRemindId(5L);
                informUserAddBo.setSystemStatus(1);
                informUserAddBo.setRemind("您开通班级有效期还剩20天");
                informUserAddBo.setGradeId(classTwentyGradeUserVo.getGradeId());
                informUserAddBo.setGoodsId(classTwentyGradeUserVo.getGoodsId());
                GoodsVo goodsVo1 = iGoodsService.queryById(classTwentyGradeUserVo.getGoodsId());
                informUserAddBo.setText("尊敬的用户：您购买的课程班级有效期还剩20天，未在"+resEnd+"前完成全部学时，可能会影响审核进度，最终导致学时无效，请尽快完成学习");
                iInformUserService.insertByAddBo(informUserAddBo);
            }
            if (informTwentyRemindVo.getNoteStatus().equals(1)){
                InformUserAddBo informUserAddBo = new InformUserAddBo();
                informUserAddBo.setUserId(classTwentyGradeUserVo.getUserId());
                informUserAddBo.setSendTime(DateUtils.getNowTime());
                informUserAddBo.setSendStatus(1);
                informUserAddBo.setCreateTime(DateUtils.getNowTime());
                informUserAddBo.setUpdateTime(DateUtils.getNowTime());
                informUserAddBo.setRemindId(5L);
                informUserAddBo.setSystemStatus(3);
                informUserAddBo.setRemind("考试提醒");
                informUserAddBo.setGoodsId(classTwentyGradeUserVo.getGoodsId());
                informUserAddBo.setGradeId(classTwentyGradeUserVo.getGradeId());
                GoodsVo goodsVo1 = iGoodsService.queryById(classTwentyGradeUserVo.getGoodsId());
                informUserAddBo.setText("尊敬的用户：您购买的课程班级有效期还剩20天，未在"+resEnd+"前完成全部学时，可能会影响审核进度，最终导致学时无效，请尽快完成学习");
                iInformUserService.insertByAddBo(informUserAddBo);
                UserVo userVo = iUserService.queryById(classTwentyGradeUserVo.getUserId());
                Map<String, Object> param = new HashMap<>();
                param.put("time", resEnd);
                param.put("day", 20);
                IAliSmsService.sendInformSms(userVo.getTelphone(), JSON.toJSONString(param), remainingDaysValidityCode);
            }
        }

        for (ClassGradeUserGoodsVo classTenGradeUserVo : classTenGradeUserVos) {
            String res;
            String resEnd;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Long timeId = classTenGradeUserVo.getClassStartTime()*1000;
            Long timeEndId = classTenGradeUserVo.getClassStartTime()*1000;
            Date date = new Date(timeId);
            Date dateEnd = new Date(timeEndId);
            res = simpleDateFormat.format(date);
            resEnd = simpleDateFormat.format(dateEnd);

            if (informTenRemindVo.getWayStatus().equals(1)) {
                InformUserAddBo informUserAddBo = new InformUserAddBo();
                informUserAddBo.setUserId(classTenGradeUserVo.getUserId());
                informUserAddBo.setSendStatus(1);
                informUserAddBo.setSendTime(DateUtils.getNowTime());
                informUserAddBo.setCreateTime(DateUtils.getNowTime());
                informUserAddBo.setUpdateTime(DateUtils.getNowTime());
                informUserAddBo.setRemindId(6L);
                informUserAddBo.setSystemStatus(1);
                informUserAddBo.setRemind("您开通班级有效期还剩10天");
                informUserAddBo.setGradeId(classTenGradeUserVo.getGradeId());
                informUserAddBo.setGoodsId(classTenGradeUserVo.getGoodsId());
                GoodsVo goodsVo1 = iGoodsService.queryById(classTenGradeUserVo.getGoodsId());
                informUserAddBo.setText("尊敬的用户：您购买的课程班级有效期还剩10天，未在"+resEnd+"前完成全部学时，可能会影响审核进度，最终导致学时无效，请尽快完成学习");
                iInformUserService.insertByAddBo(informUserAddBo);
            }
            if (informTenRemindVo.getNoteStatus().equals(1)){
                InformUserAddBo informUserAddBo = new InformUserAddBo();
                informUserAddBo.setUserId(classTenGradeUserVo.getUserId());
                informUserAddBo.setSendTime(DateUtils.getNowTime());
                informUserAddBo.setSendStatus(1);
                informUserAddBo.setCreateTime(DateUtils.getNowTime());
                informUserAddBo.setUpdateTime(DateUtils.getNowTime());
                informUserAddBo.setRemindId(6L);
                informUserAddBo.setSystemStatus(3);
                informUserAddBo.setRemind("您开通班级有效期还剩10天");
                informUserAddBo.setGoodsId(classTenGradeUserVo.getGoodsId());
                informUserAddBo.setGradeId(classTenGradeUserVo.getGradeId());
                GoodsVo goodsVo1 = iGoodsService.queryById(classTenGradeUserVo.getGoodsId());
                informUserAddBo.setText("尊敬的用户：您购买的课程班级有效期还剩10天，未在"+resEnd+"前完成全部学时，可能会影响审核进度，最终导致学时无效，请尽快完成学习");
                iInformUserService.insertByAddBo(informUserAddBo);
                UserVo userVo = iUserService.queryById(classTenGradeUserVo.getUserId());
                Map<String, Object> param = new HashMap<>();
                param.put("time", resEnd);
                param.put("day", 10);
                IAliSmsService.sendInformSms(userVo.getTelphone(), JSON.toJSONString(param), remainingDaysValidityCode);
            }
        }

        for (ClassGradeUserGoodsVo classFiveGradeUserVo : classFiveGradeUserVos) {
            String res;
            String resEnd;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Long timeId = classFiveGradeUserVo.getClassStartTime()*1000;
            Long timeEndId = classFiveGradeUserVo.getClassStartTime()*1000;
            Date date = new Date(timeId);
            Date dateEnd = new Date(timeEndId);
            res = simpleDateFormat.format(date);
            resEnd = simpleDateFormat.format(dateEnd);

            if (informTenRemindVo.getWayStatus().equals(1)) {
                InformUserAddBo informUserAddBo = new InformUserAddBo();
                informUserAddBo.setUserId(classFiveGradeUserVo.getUserId());
                informUserAddBo.setSendStatus(1);
                informUserAddBo.setSendTime(DateUtils.getNowTime());
                informUserAddBo.setCreateTime(DateUtils.getNowTime());
                informUserAddBo.setUpdateTime(DateUtils.getNowTime());
                informUserAddBo.setRemindId(7L);
                informUserAddBo.setSystemStatus(1);
                informUserAddBo.setRemind("您开通班级有效期还剩10天");
                informUserAddBo.setGradeId(classFiveGradeUserVo.getGradeId());
                informUserAddBo.setGoodsId(classFiveGradeUserVo.getGoodsId());
                GoodsVo goodsVo1 = iGoodsService.queryById(classFiveGradeUserVo.getGoodsId());
                informUserAddBo.setText("尊敬的用户：您购买的课程班级有效期还剩10天，未在"+resEnd+"前完成全部学时，可能会影响审核进度，最终导致学时无效，请尽快完成学习");
                iInformUserService.insertByAddBo(informUserAddBo);
            }
            if (informTenRemindVo.getNoteStatus().equals(1)){
                InformUserAddBo informUserAddBo = new InformUserAddBo();
                informUserAddBo.setUserId(classFiveGradeUserVo.getUserId());
                informUserAddBo.setSendTime(DateUtils.getNowTime());
                informUserAddBo.setSendStatus(1);
                informUserAddBo.setCreateTime(DateUtils.getNowTime());
                informUserAddBo.setUpdateTime(DateUtils.getNowTime());
                informUserAddBo.setRemindId(7L);
                informUserAddBo.setSystemStatus(3);
                informUserAddBo.setRemind("您开通班级有效期还剩5天");
                informUserAddBo.setGoodsId(classFiveGradeUserVo.getGoodsId());
                informUserAddBo.setGradeId(classFiveGradeUserVo.getGradeId());
                GoodsVo goodsVo1 = iGoodsService.queryById(classFiveGradeUserVo.getGoodsId());
                informUserAddBo.setText("尊敬的用户：您购买的课程班级有效期还剩5天，未在"+resEnd+"前完成全部学时，可能会影响审核进度，最终导致学时无效，请尽快完成学习");
                iInformUserService.insertByAddBo(informUserAddBo);
                UserVo userVo = iUserService.queryById(classFiveGradeUserVo.getUserId());
                Map<String, Object> param = new HashMap<>();
                param.put("time", resEnd);
                param.put("day", 5);
                IAliSmsService.sendInformSms(userVo.getTelphone(), JSON.toJSONString(param), remainingDaysValidityCode);
            }
        }

    }

    @Override
    public void timeSend(UserQueryBo bo) {
        List<UserSubscribeVo> userSubscribeVo = iUserSubscribeService.timeSend();
        InformRemindVo informRemindVo = informRemindService.queryById(16L);
        for (UserSubscribeVo subscribeVo : userSubscribeVo) {
            String res;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Long timeId = subscribeVo.getApplySiteExamTime()*1000;
            Date date = new Date(timeId);
            res = simpleDateFormat.format(date);
            String startTime = subscribeVo.getApplySiteStartTime().replace("-", ":");
            String endTime = subscribeVo.getApplySiteEndTime().replace("-", ":");
            if (informRemindVo.getWayStatus().equals(1)) {
                InformUserAddBo informUserAddBo = new InformUserAddBo();
                informUserAddBo.setUserId(subscribeVo.getUserId());
                informUserAddBo.setSendStatus(1);
                informUserAddBo.setSendTime(DateUtils.getNowTime());
                informUserAddBo.setCreateTime(DateUtils.getNowTime());
                informUserAddBo.setUpdateTime(DateUtils.getNowTime());
                informUserAddBo.setRemindId(16L);
                informUserAddBo.setSystemStatus(1);
                informUserAddBo.setRemind("考试提醒");
                informUserAddBo.setGoodsId(subscribeVo.getGoodsId());
                GoodsVo goodsVo1 = iGoodsService.queryById(subscribeVo.getGoodsId());
                informUserAddBo.setText("尊敬的用户：您预约的施工现场专业人员"+goodsVo1.getGoodsName()+"考试将在"+res+"（"+startTime+"-"+endTime+"）进行，" +
                        "考试地点："+subscribeVo.getApplySiteAddress()+"");
                iInformUserService.insertByAddBo(informUserAddBo);
            }
            if (informRemindVo.getNoteStatus().equals(1)){
                InformUserAddBo informUserAddBo = new InformUserAddBo();
                informUserAddBo.setUserId(subscribeVo.getUserId());
                informUserAddBo.setSendTime(DateUtils.getNowTime());
                informUserAddBo.setSendStatus(1);
                informUserAddBo.setCreateTime(DateUtils.getNowTime());
                informUserAddBo.setUpdateTime(DateUtils.getNowTime());
                informUserAddBo.setRemindId(3L);
                informUserAddBo.setSystemStatus(3);
                informUserAddBo.setRemind("考试提醒");
                informUserAddBo.setGoodsId(subscribeVo.getGoodsId());
                GoodsVo goodsVo1 = iGoodsService.queryById(subscribeVo.getGoodsId());
                informUserAddBo.setText("尊敬的用户：您预约的施工现场专业人员"+goodsVo1.getGoodsName()+"考试将在"+res+"（"+startTime+"-"+endTime+"）进行，" +
                        "考试地点："+subscribeVo.getApplySiteAddress()+"");
                iInformUserService.insertByAddBo(informUserAddBo);
                UserVo userVo = iUserService.queryById(subscribeVo.getUserId());
                Map<String, Object> param = new HashMap<>();
                param.put("goods", goodsVo1.getGoodsName());
                param.put("time", startTime+"-"+endTime);
                param.put("address", subscribeVo.getApplySiteAddress());
                IAliSmsService.sendInformSms(userVo.getTelphone(), JSON.toJSONString(param), examinationRemind);
            }
        }
    }

    @Override
    public void closeTimeOutOrder() {
        OrderQueryBo bo = new OrderQueryBo();
        Integer[] status={0};
        bo.setOrderStatus(status);
        List<OrderListVo> list = iOrderService.selectListApp(bo);
        for (OrderListVo orderVo : list) {
            if(orderVo.getCreateTime().longValue()<(new Long(DateUtils.getNowTime()-(24*3600))).longValue()){
                //超过24小时
                Order order = new Order();
                order.setOrderId(orderVo.getOrderId());
                order.setOrderStatus(-2);
                order.setUpdateTime(DateUtils.getNowTime());
                order.setCancelTime(DateUtils.getNowTime());
                iOrderService.updateById(order);
                //关闭锁定班级
                LambdaQueryWrapper<ClassGradeUserTemp> lqwLock = new LambdaQueryWrapper<>();
                lqwLock.eq(ClassGradeUserTemp::getOrderSn,orderVo.getOrderSn());
                ClassGradeUserTemp classGradeUserTemp = iClassGradeUserTempService.getOne(lqwLock);
                if(classGradeUserTemp!=null&&classGradeUserTemp.getStatus()==ClassGradeUserTemp.STATUS_LOCK){
                    classGradeUserTemp.setStatus(ClassGradeUserTemp.STATUS_CLOSE); //
                    classGradeUserTemp.setUpdateTime(DateUtils.getNowTime());
                    iClassGradeUserTempService.updateById(classGradeUserTemp);//关闭锁定状态
                }
            }
        }
    }

    @Override
    public void updateIssue(UserQueryBo bo) {
        InformQueryBo informQueryBo = new InformQueryBo();
        informQueryBo.setIssue(1);
        informQueryBo.setIssueStatus(2);
        List<InformVo> informVos = informService.listInform(informQueryBo);
        for (InformVo informVo : informVos) {
            LambdaQueryWrapper<InformUser> lqw = Wrappers.lambdaQuery();
            lqw.eq(InformUser::getInformId, informVo.getInformId());
            InformUser informUser = new InformUser();
            informUser.setSendTime(DateUtils.getNowTime());
            informUser.setSendStatus(1);
            informUser.setUpdateTime(DateUtils.getNowTime());
            iInformUserService.update(informUser,lqw);
        }
    }


    private void sendExamSubscribe() {
        List<ExamUserApplyVo> examUserApplyVos = iInformUserService.listUserApply();
        for (ExamUserApplyVo examUserApplyVo : examUserApplyVos) {
            InformRemindVo informRemindVo = informRemindService.queryById(13L);
            String res;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Long timeId = examUserApplyVo.getApplyEndTime()*1000;
            Date date = new Date(timeId);
            res = simpleDateFormat.format(date);
            if (informRemindVo.getWayStatus().equals(1)) {
                InformUserAddBo informUserAddBo = new InformUserAddBo();
                informUserAddBo.setUserId(examUserApplyVo.getUserId());
                informUserAddBo.setSendStatus(1);
                informUserAddBo.setSendTime(DateUtils.getNowTime());
                informUserAddBo.setCreateTime(DateUtils.getNowTime());
                informUserAddBo.setUpdateTime(DateUtils.getNowTime());
                informUserAddBo.setRemindId(13L);
                informUserAddBo.setSystemStatus(1);
                informUserAddBo.setRemind("考试预约提醒");
                informUserAddBo.setGoodsId(examUserApplyVo.getGoodsId());
                informUserAddBo.setGradeId(examUserApplyVo.getGradeId());
                GoodsVo goodsVo1 = iGoodsService.queryById(examUserApplyVo.getGoodsId());
                informUserAddBo.setText("尊敬的用户：七大员新考考试预约已经开始，预约截至时间"+res+"，请马上点击进行预约考试。");
                iInformUserService.insertByAddBo(informUserAddBo);
            }
            if (informRemindVo.getNoteStatus().equals(1)) {
                InformUserAddBo informUserAddBo = new InformUserAddBo();
                informUserAddBo.setUserId(examUserApplyVo.getUserId());
                informUserAddBo.setSendTime(DateUtils.getNowTime());
                informUserAddBo.setSendStatus(1);
                informUserAddBo.setCreateTime(DateUtils.getNowTime());
                informUserAddBo.setUpdateTime(DateUtils.getNowTime());
                informUserAddBo.setRemindId(13L);
                informUserAddBo.setSystemStatus(3);
                informUserAddBo.setRemind("考试预约提醒");
                informUserAddBo.setGoodsId(examUserApplyVo.getGoodsId());
                GoodsVo goodsVo1 = iGoodsService.queryById(examUserApplyVo.getGoodsId());
                informUserAddBo.setGradeId(examUserApplyVo.getGradeId());
                informUserAddBo.setText("尊敬的用户：七大员新考考试预约已经开始，预约截至时间"+res+"，请马上点击进行预约考试。");
                iInformUserService.insertByAddBo(informUserAddBo);
                UserVo userVo = iUserService.queryById(examUserApplyVo.getUserId());
                Map<String, Object> param = new HashMap<>();
                param.put("time", res);
                IAliSmsService.sendInformSms(userVo.getTelphone(), JSON.toJSONString(param), bookingReminder);
            }
        }
    }
}
