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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.modules.alisms.service.IAliSmsService;
import com.zhongzheng.modules.exam.mapper.ExamApplyMapper;
import com.zhongzheng.modules.exam.vo.ExamApplySiteTimeVo;
import com.zhongzheng.modules.exam.vo.ExamApplySiteVo;
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.inform.bo.InformUserAddBo;
import com.zhongzheng.modules.inform.service.IInformRemindService;
import com.zhongzheng.modules.inform.service.IInformUserService;
import com.zhongzheng.modules.inform.vo.InformRemindBusinessVo;
import com.zhongzheng.modules.inform.vo.InformRemindVo;
import com.zhongzheng.modules.user.bo.*;
import com.zhongzheng.modules.user.service.IUserService;
import com.zhongzheng.modules.user.vo.UserSubscribeImport;
import com.zhongzheng.modules.user.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.zhongzheng.modules.user.domain.UserSubscribe;
import com.zhongzheng.modules.user.mapper.UserSubscribeMapper;
import com.zhongzheng.modules.user.vo.UserSubscribeVo;
import com.zhongzheng.modules.user.service.IUserSubscribeService;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;

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

/**
 * 用户预约考试Service业务层处理
 *
 * @author ruoyi
 * @date 2021-12-07
 */
@Service
public class UserSubscribeServiceImpl extends ServiceImpl<UserSubscribeMapper, UserSubscribe> implements IUserSubscribeService {

    @Autowired
    private ExamApplyMapper examApplyMapper;

    @Autowired
    private IInformRemindService informRemindService;

    @Autowired
    private IGoodsService iGoodsService;

    @Autowired
    private IInformUserService iInformUserService;

    @Autowired
    private IUserService iUserService;

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

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

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

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

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

    @Override
    public UserSubscribeVo queryById(Long subscribeId) {
        UserSubscribe db = this.baseMapper.selectById(subscribeId);
        return BeanUtil.toBean(db, UserSubscribeVo.class);
    }

    @Override
    public List<UserSubscribeVo> queryList(UserSubscribeQueryBo bo) {
        LambdaQueryWrapper<UserSubscribe> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getGoodsId() != null, UserSubscribe::getGoodsId, bo.getGoodsId());
        lqw.eq(bo.getUserId() != null, UserSubscribe::getUserId, bo.getUserId());
        lqw.eq(bo.getSubscribeStatus() != null, UserSubscribe::getSubscribeStatus, bo.getSubscribeStatus());
        lqw.eq(bo.getBeforeStatus() != null, UserSubscribe::getBeforeStatus, bo.getBeforeStatus());
        lqw.eq(bo.getExamStatus() != null, UserSubscribe::getExamStatus, bo.getExamStatus());
        lqw.eq(bo.getPerformance() != null, UserSubscribe::getPerformance, bo.getPerformance());
        lqw.eq(bo.getResult() != null, UserSubscribe::getResult, bo.getResult());
        lqw.eq(StrUtil.isNotBlank(bo.getCertificateCode()), UserSubscribe::getCertificateCode, bo.getCertificateCode());
        lqw.eq(bo.getApplyId() != null, UserSubscribe::getApplyId, bo.getApplyId());
        lqw.eq(bo.getStudentType() != null, UserSubscribe::getStudentType, bo.getStudentType());
        lqw.eq(bo.getExamExpend() != null, UserSubscribe::getExamExpend, bo.getExamExpend());
        lqw.eq(bo.getBeforeExpend() != null, UserSubscribe::getBeforeExpend, bo.getBeforeExpend());
        return entity2Vo(this.list(lqw));
    }

    /**
     * 实体类转化成视图对象
     *
     * @param collection 实体类集合
     * @return
     */
    private List<UserSubscribeVo> entity2Vo(Collection<UserSubscribe> collection) {
        List<UserSubscribeVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, UserSubscribeVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<UserSubscribe> page = (Page<UserSubscribe>) collection;
            Page<UserSubscribeVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page, pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }

    @Override
    public Long insertByAddBo(UserSubscribeAddBo bo) {
        UserSubscribe add = BeanUtil.toBean(bo, UserSubscribe.class);
        validEntityBeforeSave(add);
        add.setSubscribeStatus(1);
        add.setExamStatus(0);
        add.setCreateTime(DateUtils.getNowTime());
        add.setUpdateTime(DateUtils.getNowTime());
        this.save(add);
        sendExamSucceed(bo);
        return add.getSubscribeId();
    }

    private void sendExamSucceed(UserSubscribeAddBo bo) {
        InformRemindVo informRemindVo = informRemindService.queryById(15L);
        Long count = baseMapper.sendExamSucceed(bo.getGoodsId());
        if (count > 0) {
            if (informRemindVo.getWayStatus().equals(1)) {
                InformUserAddBo informUserAddBo = new InformUserAddBo();
                informUserAddBo.setUserId(bo.getUserId());
                informUserAddBo.setSendStatus(1);
                informUserAddBo.setSendTime(DateUtils.getNowTime());
                informUserAddBo.setCreateTime(DateUtils.getNowTime());
                informUserAddBo.setUpdateTime(DateUtils.getNowTime());
                informUserAddBo.setRemindId(15L);
                informUserAddBo.setSystemStatus(1);
                informUserAddBo.setRemind("考试预约成功提醒");
                informUserAddBo.setGoodsId(bo.getGoodsId());
                GoodsVo goodsVo1 = iGoodsService.queryById(bo.getGoodsId());
                informUserAddBo.setText("尊敬的用户：已成功预约施工现场专业人员" + goodsVo1.getGoodsName() + "考试");
                iInformUserService.insertByAddBo(informUserAddBo);
            }
            if (informRemindVo.getNoteStatus().equals(1)) {
                InformUserAddBo informUserAddBo = new InformUserAddBo();
                informUserAddBo.setUserId(bo.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(bo.getGoodsId());
                GoodsVo goodsVo1 = iGoodsService.queryById(bo.getGoodsId());
                informUserAddBo.setText("尊敬的用户：已成功预约施工现场专业人员" + goodsVo1.getGoodsName() + "考试");
                iInformUserService.insertByAddBo(informUserAddBo);
                UserVo userVo = iUserService.queryById(bo.getUserId());
                Map<String, Object> param = new HashMap<>();
                param.put("goods", goodsVo1.getGoodsName());
                IAliSmsService.sendInformSms(userVo.getTelphone(), JSON.toJSONString(param), reservationSuccess);
            }
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByEditBo(UserSubscribeEditBo bo) {
        for (Long subscribe : bo.getSubscribeId()) {
            UserSubscribe update = new UserSubscribe();
            update.setSubscribeId(subscribe);
            update.setSubscribeStatus(bo.getSubscribeStatus());
            if (bo.getSubscribeStatus() != null && bo.getSubscribeStatus().equals(2)) {
                sendExamcancel(bo);
            }
            update.setExamStatus(bo.getExamStatus());
            update.setRemark(bo.getRemark());
            update.setBeforeId(bo.getBeforeId());
            update.setBeforeStatus(bo.getBeforeStatus());
            update.setUpdateTime(DateUtils.getNowTime());
            boolean b = this.updateById(update);
        }
        return true;
    }

    //取消预约
    private void sendExamcancel(UserSubscribeEditBo bo) {
        for (Long subscribe : bo.getSubscribeId()) {
            UserSubscribeVo userSubscribeVo = this.queryById(subscribe);
            InformRemindVo informRemindVo = informRemindService.queryById(14L);
            if (!CollectionUtils.isEmpty(informRemindVo.getInformRemindBusiness())) {
                Long businessId = 0L;
                for (InformRemindBusinessVo informRemindBusiness : informRemindVo.getInformRemindBusiness()) {
                    businessId += baseMapper.queryBusinessId(subscribe, informRemindBusiness.getBusinessId());
                }
                if (businessId > 0) {
                    if (informRemindVo.getWayStatus().equals(1)) {
                        InformUserAddBo informUserAddBo = new InformUserAddBo();
                        informUserAddBo.setUserId(userSubscribeVo.getUserId());
                        informUserAddBo.setSendStatus(1);
                        informUserAddBo.setSendTime(DateUtils.getNowTime());
                        informUserAddBo.setCreateTime(DateUtils.getNowTime());
                        informUserAddBo.setUpdateTime(DateUtils.getNowTime());
                        informUserAddBo.setRemindId(14L);
                        informUserAddBo.setSystemStatus(1);
                        informUserAddBo.setRemind("考试预约取消提醒");
                        informUserAddBo.setGoodsId(userSubscribeVo.getGoodsId());
                        GoodsVo goodsVo1 = iGoodsService.queryById(userSubscribeVo.getGoodsId());
                        informUserAddBo.setText("尊敬的用户：已取消预约施工现场专业人员" + goodsVo1.getGoodsName() + "考试。");
                        iInformUserService.insertByAddBo(informUserAddBo);
                    }
                    if (informRemindVo.getNoteStatus().equals(1)) {
                        InformUserAddBo informUserAddBo = new InformUserAddBo();
                        informUserAddBo.setUserId(userSubscribeVo.getUserId());
                        informUserAddBo.setSendTime(DateUtils.getNowTime());
                        informUserAddBo.setSendStatus(1);
                        informUserAddBo.setCreateTime(DateUtils.getNowTime());
                        informUserAddBo.setUpdateTime(DateUtils.getNowTime());
                        informUserAddBo.setRemindId(14L);
                        informUserAddBo.setSystemStatus(3);
                        informUserAddBo.setRemind("考试预约取消提醒");
                        informUserAddBo.setGoodsId(userSubscribeVo.getGoodsId());
                        GoodsVo goodsVo1 = iGoodsService.queryById(userSubscribeVo.getGoodsId());
                        informUserAddBo.setText("尊敬的用户：已取消预约施工现场专业人员" + goodsVo1.getGoodsName() + "考试。");
                        iInformUserService.insertByAddBo(informUserAddBo);
                        UserVo userVo = iUserService.queryById(userSubscribeVo.getUserId());
                        Map<String, Object> param = new HashMap<>();
                        param.put("goods", goodsVo1.getGoodsName());
                        IAliSmsService.sendInformSms(userVo.getTelphone(), JSON.toJSONString(param), cancellationReminder);
                    }
                }
            }
        }

    }

    @Override
    public Boolean updateByEditSingleBo(UserAppSubscribeEditBo bo) {
        UserSubscribe update = BeanUtil.toBean(bo, UserSubscribe.class);
        validEntityBeforeSave(update);
        if (bo.getSubscribeStatus() != null && bo.getSubscribeStatus().equals(2)) {
            UserSubscribeEditBo userSubscribeEditBo = new UserSubscribeEditBo();
            Long[] s = new Long[]{
                    bo.getSubscribeId()
            };
            userSubscribeEditBo.setSubscribeId(s);
            sendExamcancel(userSubscribeEditBo);
        }
        update.setUpdateTime(DateUtils.getNowTime());
        return this.updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(UserSubscribe entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return this.removeByIds(ids);
    }

    @Override
    public List<UserSubscribeVo> listSubscribe(UserSubscribeQueryBo bo) {
        List<UserSubscribeVo> userSubscribeVos = baseMapper.listSubscribe(bo);
        return userSubscribeVos;
    }

    @Override
    public boolean editCertificate(List<UserSubscribeCertificateEditBo> bo) {
        for (UserSubscribeCertificateEditBo userSubscribeCertificateEditBo : bo) {
            UserSubscribe update = new UserSubscribe();
            update.setSubscribeId(userSubscribeCertificateEditBo.getSubscribeId());
            update.setPerformance(userSubscribeCertificateEditBo.getPerformance());
            update.setResult(userSubscribeCertificateEditBo.getResult());
            update.setCertificateCode(userSubscribeCertificateEditBo.getCertificateCode());
            update.setRemark(userSubscribeCertificateEditBo.getRemark());
            update.setUpdateTime(DateUtils.getNowTime());
            boolean b = this.updateById(update);
            UserSubscribeVo userSubscribeVo = this.queryById(userSubscribeCertificateEditBo.getSubscribeId());
            UserSubscribe userSubscribe = BeanUtil.toBean(userSubscribeVo, UserSubscribe.class);
            sendExamPass(userSubscribe);
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<UserSubscribeImport> importData(List<UserSubscribeImport> userSubscribeImportList) {
        if (CollectionUtils.isEmpty(userSubscribeImportList)) {
            throw new IllegalArgumentException("请勿导入空表格");
        }
        List<UserSubscribeImport> userSubscribeImports = new ArrayList<>();
        for (UserSubscribeImport userSubscribeImport : userSubscribeImportList) {
            UserSubscribeAddBo userSubscribeAddBo = new UserSubscribeAddBo();
            if (StringUtils.isEmpty(userSubscribeImport.getUserName()) && StringUtils.isEmpty(userSubscribeImport.getApplyName()) && StringUtils.isEmpty(userSubscribeImport.getUserName())){
                continue;
            }
            //查询商品
            if (StringUtils.isEmpty(userSubscribeImport.getCode())) {
                userSubscribeImport.setCause("未填商品编码");
                userSubscribeImports.add(userSubscribeImport);
                continue;
            }
            Long goodsId = baseMapper.selectGoodsId(userSubscribeImport.getCode());
            if (goodsId == null) {
                userSubscribeImport.setCause("商品编码错误");
                userSubscribeImports.add(userSubscribeImport);
                continue;
            }
            userSubscribeAddBo.setGoodsId(goodsId);
            //查询学员Id
            if (StringUtils.isEmpty(userSubscribeImport.getUserName()) || userSubscribeImport.getIdCard() == null) {
                userSubscribeImport.setCause("学员信息未填");
                userSubscribeImports.add(userSubscribeImport);
                continue;
            }
            Long userId = baseMapper.selectUserId(userSubscribeImport.getUserName(), userSubscribeImport.getIdCard());
            if (userId == null) {
                userSubscribeImport.setCause("用户信息错误");
                userSubscribeImports.add(userSubscribeImport);
                continue;
            }
            userSubscribeAddBo.setUserId(userId);

            if (StringUtils.isEmpty(userSubscribeImport.getSubscribeStatus())) {
                userSubscribeImport.setCause("预约状态为空");
                userSubscribeImports.add(userSubscribeImport);
                continue;
            }
            if (userSubscribeImport.getSubscribeStatus().equals("正常")) {
                userSubscribeAddBo.setSubscribeStatus(1);
            }
            if (userSubscribeImport.getSubscribeStatus().equals("取消")) {
                userSubscribeImport.setCause("预约状态为取消");
                userSubscribeImports.add(userSubscribeImport);
                continue;
            }

            if (!StringUtils.isEmpty(userSubscribeImport.getBeforeStatus()) && !StringUtils.isEmpty(userSubscribeImport.getBeforeName())) {
                Long beforeId = baseMapper.selectBeforeId(userSubscribeImport.getBeforeName());
                if (beforeId == null) {
                    userSubscribeImport.setCause("前培标题有误");
                    userSubscribeImports.add(userSubscribeImport);
                    continue;
                }
                userSubscribeAddBo.setBeforeStatus(1);
                userSubscribeAddBo.setBeforeId(beforeId);
                userSubscribeAddBo.setBeforeExpend(1);
            }

            if (!StringUtils.isEmpty(userSubscribeImport.getExamStatus())) {
                if (userSubscribeImport.getExamStatus().equals("待登记")) {
                    userSubscribeAddBo.setExamStatus(0);
                } else if (userSubscribeImport.getExamStatus().equals("正常")) {
                    userSubscribeAddBo.setExamStatus(1);
                } else if (userSubscribeImport.getExamStatus().equals("缺考")) {
                    userSubscribeAddBo.setExamStatus(2);
                } else if (userSubscribeImport.getExamStatus().equals("作弊")) {
                    userSubscribeAddBo.setExamStatus(3);
                } else if (userSubscribeImport.getExamStatus().equals("替考")) {
                    userSubscribeAddBo.setExamStatus(4);
                }
            }

            if (!StringUtils.isEmpty(userSubscribeImport.getResult())) {
                if (userSubscribeImport.getResult().equals("不通过")) {
                    userSubscribeAddBo.setResult(0);
                } else if (userSubscribeImport.getResult().equals("通过")) {
                    userSubscribeAddBo.setResult(1);
                }
            }

            if (!StringUtils.isEmpty(userSubscribeImport.getCertificateCode())) {
                userSubscribeAddBo.setCertificateCode(userSubscribeImport.getCertificateCode());
            }
            if (StringUtils.isEmpty(userSubscribeImport.getApplyName())) {
                userSubscribeImport.setCause("考试标题有误");
                userSubscribeImports.add(userSubscribeImport);
                continue;
            }
            Long applyId = baseMapper.selectApplyId(userSubscribeImport.getApplyName());
            if (applyId == null) {
                userSubscribeImport.setCause("考试标题有误");
                userSubscribeImports.add(userSubscribeImport);
                continue;
            }
            userSubscribeAddBo.setApplyId(applyId);

            Long subscribeId = baseMapper.selectSubscribeId(applyId, goodsId, userId);
            if (subscribeId != null) {
                userSubscribeImport.setCause("该学员在考试计划中有预约考试数据，请确认该学员预约考试状况");
                userSubscribeImports.add(userSubscribeImport);
                continue;
            }

            if (!StringUtils.isEmpty(userSubscribeImport.getStudentTypeName())) {
                if (userSubscribeImport.getStudentTypeName().equals("非补考学员")) {
                    userSubscribeAddBo.setStudentType(1);
                } else {
                    userSubscribeAddBo.setStudentType(2);
                }
            } else {
                userSubscribeImport.setCause("学员报名类型有误");
                userSubscribeImports.add(userSubscribeImport);
                continue;
            }

            if (!StringUtils.isEmpty(userSubscribeImport.getApplySiteAddress())) {
                userSubscribeAddBo.setApplySiteAddress(userSubscribeImport.getApplySiteAddress());
            } else {
                userSubscribeImport.setCause("地址未填");
                userSubscribeImports.add(userSubscribeImport);
                continue;
            }

            if (userSubscribeImport.getApplySiteExamTime() != null) {
                userSubscribeAddBo.setApplySiteExamTime(Convert.toLong(userSubscribeImport.getApplySiteExamTime().getTime())/1000);
            } else {
                userSubscribeImport.setCause("考试时间未填");
                userSubscribeImports.add(userSubscribeImport);
                continue;
            }

            if (!StringUtils.isEmpty(userSubscribeImport.getApplySiteTime())) {
                String[] split = userSubscribeImport.getApplySiteTime().split("-");
                if (split.length < 2) {
                    userSubscribeImport.setCause("考试时间段错误");
                    userSubscribeImports.add(userSubscribeImport);
                    continue;
                }
                String startTime = split[0];
                String endTime = split[1];
                startTime = startTime.replace(":", "-");
                endTime = endTime.replace(":", "-");

                userSubscribeAddBo.setApplySiteStartTime(startTime);
                userSubscribeAddBo.setApplySiteEndTime(endTime);
            } else {
                userSubscribeImport.setCause("考试时间段未填");
                userSubscribeImports.add(userSubscribeImport);
                continue;
            }


            if (!StringUtils.isEmpty(userSubscribeImport.getApplySiteAddressTrain())) {
                userSubscribeAddBo.setApplySiteAddressTrain(userSubscribeImport.getApplySiteAddressTrain());
            }

            if (userSubscribeImport.getApplySiteExamTrainTime() != null) {
                userSubscribeAddBo.setApplySiteExamTrainTime(Convert.toLong(userSubscribeImport.getApplySiteExamTrainTime().getDay()));
            }

            if (!StringUtils.isEmpty(userSubscribeImport.getApplySiteTrainTime())) {
                String[] split = userSubscribeImport.getApplySiteTrainTime().split("-");
                if (split.length < 2) {
                    userSubscribeImport.setCause("考试时间段错误");
                    userSubscribeImports.add(userSubscribeImport);
                    continue;
                }
                String startTime = split[0];
                String endTime = split[1];
                startTime = startTime.replace(":", "-");
                endTime = endTime.replace(":", "-");

                userSubscribeAddBo.setApplySiteStartTrainTime(startTime);
                userSubscribeAddBo.setApplySiteEndTrainTime(endTime);
            }

            userSubscribeAddBo.setCreateTime(DateUtils.getNowTime());
            userSubscribeAddBo.setUpdateTime(DateUtils.getNowTime());
            this.insertByAddBo(userSubscribeAddBo);
        }
        return userSubscribeImports;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<UserSubscribeImport> importUpdateData(List<UserSubscribeImport> userSubscribeImportList) {
        if (CollectionUtils.isEmpty(userSubscribeImportList)) {
            throw new IllegalArgumentException("请勿导入空表格");
        }
        List<UserSubscribeImport> userSubscribeImports = new ArrayList<>();
        for (UserSubscribeImport userSubscribeImport : userSubscribeImportList) {
            UserSubscribe userSbusciEditBo = new UserSubscribe();
            if (StringUtils.isEmpty(userSubscribeImport.getUserName()) && StringUtils.isEmpty(userSubscribeImport.getApplyName()) && StringUtils.isEmpty(userSubscribeImport.getUserName())){
                continue;
            }
            //查询商品
            if (StringUtils.isEmpty(userSubscribeImport.getCode())) {
                userSubscribeImport.setCause("未填商品编码");
                userSubscribeImports.add(userSubscribeImport);
                continue;
            }
            Long goodsId = baseMapper.selectGoodsId(userSubscribeImport.getCode());
            if (goodsId == null) {
                userSubscribeImport.setCause("商品编码错误");
                userSubscribeImports.add(userSubscribeImport);
                continue;
            }
            userSbusciEditBo.setGoodsId(goodsId);
            //查询学员Id
            if (StringUtils.isEmpty(userSubscribeImport.getUserName()) || userSubscribeImport.getIdCard() == null) {
                userSubscribeImport.setCause("学员信息未填");
                userSubscribeImports.add(userSubscribeImport);
                continue;
            }
            Long userId = baseMapper.selectUserId(userSubscribeImport.getUserName(), userSubscribeImport.getIdCard());
            if (userId == null) {
                userSubscribeImport.setCause("用户信息错误");
                userSubscribeImports.add(userSubscribeImport);
                continue;
            }
            userSbusciEditBo.setUserId(userId);

            if (StringUtils.isEmpty(userSubscribeImport.getSubscribeStatus())) {
                userSubscribeImport.setCause("预约状态为空");
                userSubscribeImports.add(userSubscribeImport);
                continue;
            }
            if (userSubscribeImport.getSubscribeStatus().equals("正常")) {
                userSbusciEditBo.setSubscribeStatus(1);
            }
            if (userSubscribeImport.getSubscribeStatus().equals("取消")) {
                userSbusciEditBo.setSubscribeStatus(2);
            }

            if (!StringUtils.isEmpty(userSubscribeImport.getBeforeStatus())) {
                if (userSubscribeImport.getBeforeStatus().equals("是")) {
                    if (StringUtils.isEmpty(userSubscribeImport.getBeforeName())) {
                        userSubscribeImport.setCause("前培标题有误");
                        userSubscribeImports.add(userSubscribeImport);
                        continue;
                    }
                    Long beforeId = baseMapper.selectBeforeId(userSubscribeImport.getBeforeName());
                    if (beforeId == null) {
                        userSubscribeImport.setCause("前培标题有误");
                        userSubscribeImports.add(userSubscribeImport);
                        continue;
                    }
                    userSbusciEditBo.setBeforeStatus(1);
                    userSbusciEditBo.setBeforeId(beforeId);
                    userSbusciEditBo.setBeforeExpend(1);
                } else if (userSubscribeImport.getBeforeStatus().equals("否")) {
                    userSbusciEditBo.setBeforeStatus(0);
                }
            }

            if (!StringUtils.isEmpty(userSubscribeImport.getExamStatus())) {
                if (userSubscribeImport.getExamStatus().equals("待登记")) {
                    userSbusciEditBo.setExamStatus(0);
                } else if (userSubscribeImport.getExamStatus().equals("正常")) {
                    userSbusciEditBo.setExamStatus(1);
                } else if (userSubscribeImport.getExamStatus().equals("缺考")) {
                    userSbusciEditBo.setExamStatus(2);
                } else if (userSubscribeImport.getExamStatus().equals("作弊")) {
                    userSbusciEditBo.setExamStatus(3);
                } else if (userSubscribeImport.getExamStatus().equals("替考")) {
                    userSbusciEditBo.setExamStatus(4);
                }
            }

            if (!StringUtils.isEmpty(userSubscribeImport.getResult())) {
                if (userSubscribeImport.getResult().equals("不通过")) {
                    userSbusciEditBo.setResult(0);
                } else if (userSubscribeImport.getResult().equals("通过")) {
                    userSbusciEditBo.setResult(1);
                }
            }

            if (!StringUtils.isEmpty(userSubscribeImport.getCertificateCode())) {
                userSbusciEditBo.setCertificateCode(userSubscribeImport.getCertificateCode());
            }

            if (StringUtils.isEmpty(userSubscribeImport.getApplyName())) {
                userSubscribeImport.setCause("考试标题有误");
                userSubscribeImports.add(userSubscribeImport);
                continue;
            }
            Long applyId = baseMapper.selectApplyId(userSubscribeImport.getApplyName());
            if (applyId == null) {
                userSubscribeImport.setCause("考试标题有误");
                userSubscribeImports.add(userSubscribeImport);
                continue;
            }
            userSbusciEditBo.setApplyId(applyId);
            Long subscribeId = baseMapper.selectSubscribeId(applyId, goodsId, userId);
            if (subscribeId == null) {
                userSubscribeImport.setCause("该学员在考试计划中无预约数据，请确认该学员已预约考试");
                userSubscribeImports.add(userSubscribeImport);
                continue;
            }
            userSbusciEditBo.setSubscribeId(subscribeId);

            if (!StringUtils.isEmpty(userSubscribeImport.getStudentTypeName())) {
                if (userSubscribeImport.getStudentTypeName().equals("非补考学员")) {
                    userSbusciEditBo.setStudentType(1);
                } else {
                    userSbusciEditBo.setStudentType(2);
                }
            } else {
                userSubscribeImport.setCause("学员报名类型有误");
                userSubscribeImports.add(userSubscribeImport);
                continue;
            }

            if (!StringUtils.isEmpty(userSubscribeImport.getApplySiteAddress())) {
                userSbusciEditBo.setApplySiteAddress(userSubscribeImport.getApplySiteAddress());
            } else {
                userSubscribeImport.setCause("地址未填");
                userSubscribeImports.add(userSubscribeImport);
                continue;
            }

            if (userSubscribeImport.getApplySiteExamTime() != null) {
                userSbusciEditBo.setApplySiteExamTime(Convert.toLong(userSubscribeImport.getApplySiteExamTime().getDay()));
            } else {
                userSubscribeImport.setCause("考试时间未填");
                userSubscribeImports.add(userSubscribeImport);
                continue;
            }

            if (!StringUtils.isEmpty(userSubscribeImport.getApplySiteTime())) {
                String[] split = userSubscribeImport.getApplySiteTime().split("-");
                if (split.length < 2) {
                    userSubscribeImport.setCause("考试时间段错误");
                    userSubscribeImports.add(userSubscribeImport);
                    continue;
                }
                String startTime = split[0];
                String endTime = split[1];
                startTime = startTime.replace(":", "-");
                endTime = endTime.replace(":", "-");

                userSbusciEditBo.setApplySiteStartTime(startTime);
                userSbusciEditBo.setApplySiteEndTime(endTime);
            } else {
                userSubscribeImport.setCause("考试时间段未填");
                userSubscribeImports.add(userSubscribeImport);
                continue;
            }


            if (!StringUtils.isEmpty(userSubscribeImport.getApplySiteAddressTrain())) {
                userSbusciEditBo.setApplySiteAddressTrain(userSubscribeImport.getApplySiteAddressTrain());
            }

            if (userSubscribeImport.getApplySiteExamTrainTime() != null) {
                userSbusciEditBo.setApplySiteExamTrainTime(Convert.toLong(userSubscribeImport.getApplySiteExamTrainTime().getDay()));
            }

            if (!StringUtils.isEmpty(userSubscribeImport.getApplySiteTrainTime())) {
                String[] split = userSubscribeImport.getApplySiteTrainTime().split("-");
                if (split.length < 2) {
                    userSubscribeImport.setCause("考试时间段错误");
                    userSubscribeImports.add(userSubscribeImport);
                    continue;
                }
                String startTime = split[0];
                String endTime = split[1];
                startTime = startTime.replace(":", "-");
                endTime = endTime.replace(":", "-");

                userSbusciEditBo.setApplySiteStartTrainTime(startTime);
                userSbusciEditBo.setApplySiteEndTrainTime(endTime);
            }

            userSbusciEditBo.setUpdateTime(DateUtils.getNowTime());
            this.updateById(userSbusciEditBo);

            if (userSubscribeImport.getSubscribeStatus().equals("取消")) {
                userSbusciEditBo.setSubscribeStatus(0);
                UserSubscribeEditBo userSubscribeEditBo = new UserSubscribeEditBo();
                Long[] s = new Long[]{
                        userSbusciEditBo.getSubscribeId()
                };
                userSubscribeEditBo.setSubscribeId(s);
                sendExamcancel(userSubscribeEditBo);
            }
            if (!StringUtils.isEmpty(userSubscribeImport.getResult())) {
                sendExamPass(userSbusciEditBo);
            }
        }
        return userSubscribeImports;
    }

    private void sendExamPass(UserSubscribe bo) {
        UserSubscribeVo userSubscribeVo = this.queryById(bo.getSubscribeId());
        InformRemindVo informRemindVoPass = informRemindService.queryById(17L);
        InformRemindVo informRemindVo = informRemindService.queryById(18L);
        if (!CollectionUtils.isEmpty(informRemindVo.getInformRemindBusiness())) {
            Long businessId = 0L;
            for (InformRemindBusinessVo informRemindBusiness : informRemindVo.getInformRemindBusiness()) {
                businessId += baseMapper.queryBusinessId(bo.getSubscribeId(), informRemindBusiness.getBusinessId());
            }
            if (businessId > 0) {
                if (userSubscribeVo.getResult().equals(1)) {
                    if (informRemindVoPass.getWayStatus().equals(1)) {
                        InformUserAddBo informUserAddBo = new InformUserAddBo();
                        informUserAddBo.setUserId(userSubscribeVo.getUserId());
                        informUserAddBo.setSendStatus(1);
                        informUserAddBo.setSendTime(DateUtils.getNowTime());
                        informUserAddBo.setCreateTime(DateUtils.getNowTime());
                        informUserAddBo.setUpdateTime(DateUtils.getNowTime());
                        informUserAddBo.setRemindId(17L);
                        informUserAddBo.setSystemStatus(1);
                        informUserAddBo.setRemind("七大员新考考试通过提醒");
                        informUserAddBo.setGoodsId(userSubscribeVo.getGoodsId());
                        GoodsVo goodsVo1 = iGoodsService.queryById(userSubscribeVo.getGoodsId());
                        informUserAddBo.setText("尊敬的用户：您已通过施工现场专业人员"+goodsVo1.getGoodsName()+"岗位考核，分数：" + bo.getPerformance() + "，证书编号："+bo.getCertificateCode());
                        iInformUserService.insertByAddBo(informUserAddBo);
                    }
                    if (informRemindVoPass.getNoteStatus().equals(1)) {
                        InformUserAddBo informUserAddBo = new InformUserAddBo();
                        informUserAddBo.setUserId(userSubscribeVo.getUserId());
                        informUserAddBo.setSendTime(DateUtils.getNowTime());
                        informUserAddBo.setSendStatus(1);
                        informUserAddBo.setCreateTime(DateUtils.getNowTime());
                        informUserAddBo.setUpdateTime(DateUtils.getNowTime());
                        informUserAddBo.setRemindId(17L);
                        informUserAddBo.setSystemStatus(3);
                        informUserAddBo.setRemind("七大员新考考试通过提醒");
                        informUserAddBo.setGoodsId(userSubscribeVo.getGoodsId());
                        GoodsVo goodsVo1 = iGoodsService.queryById(userSubscribeVo.getGoodsId());
                        informUserAddBo.setText("尊敬的用户：您已通过施工现场专业人员"+goodsVo1.getGoodsName()+"岗位考核，分数：" + bo.getPerformance() + "，证书编号："+bo.getCertificateCode());
                        iInformUserService.insertByAddBo(informUserAddBo);
                        UserVo userVo = iUserService.queryById(userSubscribeVo.getUserId());
                        Map<String, Object> param = new HashMap<>();
                        param.put("goods", goodsVo1.getGoodsName());
                        param.put("score", bo.getPerformance());
                        param.put("number", bo.getCertificateCode());
                        IAliSmsService.sendInformSms(userVo.getTelphone(), JSON.toJSONString(param), newExamPass);
                    }
                }
                if (userSubscribeVo.getResult().equals(0)) {
                    if (informRemindVo.getWayStatus().equals(1)) {
                        InformUserAddBo informUserAddBo = new InformUserAddBo();
                        informUserAddBo.setUserId(userSubscribeVo.getUserId());
                        informUserAddBo.setSendStatus(1);
                        informUserAddBo.setSendTime(DateUtils.getNowTime());
                        informUserAddBo.setCreateTime(DateUtils.getNowTime());
                        informUserAddBo.setUpdateTime(DateUtils.getNowTime());
                        informUserAddBo.setRemindId(18L);
                        informUserAddBo.setSystemStatus(1);
                        informUserAddBo.setRemind("七大员新考考试不通过提醒");
                        informUserAddBo.setGoodsId(userSubscribeVo.getGoodsId());
                        GoodsVo goodsVo1 = iGoodsService.queryById(userSubscribeVo.getGoodsId());
                        informUserAddBo.setText("尊敬的用户：您缺席或未通过施工现场专业人员"+goodsVo1.getGoodsName()+"岗位考核，分数：" + bo.getPerformance() + "，请马重新预约考试.");
                        iInformUserService.insertByAddBo(informUserAddBo);
                    }
                    if (informRemindVo.getNoteStatus().equals(1)) {
                        InformUserAddBo informUserAddBo = new InformUserAddBo();
                        informUserAddBo.setUserId(userSubscribeVo.getUserId());
                        informUserAddBo.setSendTime(DateUtils.getNowTime());
                        informUserAddBo.setSendStatus(1);
                        informUserAddBo.setCreateTime(DateUtils.getNowTime());
                        informUserAddBo.setUpdateTime(DateUtils.getNowTime());
                        informUserAddBo.setRemindId(18L);
                        informUserAddBo.setSystemStatus(3);
                        informUserAddBo.setRemind("七大员新考考试不通过提醒");
                        informUserAddBo.setGoodsId(userSubscribeVo.getGoodsId());
                        GoodsVo goodsVo1 = iGoodsService.queryById(userSubscribeVo.getGoodsId());
                        informUserAddBo.setText("尊敬的用户：您缺席或未通过施工现场专业人员"+goodsVo1.getGoodsName()+"岗位考核，分数：" + bo.getPerformance() + "，请马重新预约考试.");
                        iInformUserService.insertByAddBo(informUserAddBo);
                        UserVo userVo = iUserService.queryById(userSubscribeVo.getUserId());
                        Map<String, Object> param = new HashMap<>();
                        param.put("goods", goodsVo1.getGoodsName());
                        param.put("score", bo.getPerformance());
                        IAliSmsService.sendInformSms(userVo.getTelphone(), JSON.toJSONString(param), newExamFail);
                    }
                }
            }
        }
    }

    @Override
    public List<UserSubscribeVo> timeSend() {
        return baseMapper.timeSend();
    }
}
