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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.zhongzheng.common.utils.DateUtils;
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.user.bo.UserSubscribeCertificateEditBo;
import org.springframework.beans.factory.annotation.Autowired;
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.bo.UserSubscribeAddBo;
import com.zhongzheng.modules.user.bo.UserSubscribeQueryBo;
import com.zhongzheng.modules.user.bo.UserSubscribeEditBo;
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 java.util.Collection;
import java.util.Collections;
import java.util.List;
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;

    @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 Boolean insertByAddBo(UserSubscribeAddBo bo) {
        UserSubscribe add = BeanUtil.toBean(bo, UserSubscribe.class);
        validEntityBeforeSave(add);
        add.setCreateTime(DateUtils.getNowTime());
        add.setUpdateTime(DateUtils.getNowTime());
        return this.save(add);
    }

    @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());
            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;
    }

    /**
     * 保存前的数据校验
     *
     * @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);
        for (UserSubscribeVo userSubscribeVo : userSubscribeVos) {
            List<ExamApplySiteVo> examApplySite = examApplyMapper.addressExam(userSubscribeVo.getApplyId(),1L);
            for (ExamApplySiteVo examApplySiteVo : examApplySite) {
                List<ExamApplySiteTimeVo> examApplySiteTime = examApplyMapper.addressTimeExam(examApplySiteVo.getId());
                examApplySiteVo.setExamApplySiteTime(examApplySiteTime);
            }
            List<ExamApplySiteVo> examApplySiteTrain = examApplyMapper.addressExam(userSubscribeVo.getApplyId(),2L);
            for (ExamApplySiteVo examApplySiteVo : examApplySiteTrain) {
                List<ExamApplySiteTimeVo> examApplySiteTime = examApplyMapper.addressTimeExam(examApplySiteVo.getId());
                examApplySiteVo.setExamApplySiteTime(examApplySiteTime);
            }
            userSubscribeVo.setExamApplySite(examApplySite);
            userSubscribeVo.setExamApplySiteTrain(examApplySiteTrain);
        }
        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);
        }
        return true;
    }
}
