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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.func.VoidFunc0;
import cn.hutool.core.util.StrUtil;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.modules.course.service.IMajorCategoryService;
import com.zhongzheng.modules.course.vo.MajorCategoryVo;
import com.zhongzheng.modules.exam.bo.*;
import com.zhongzheng.modules.exam.domain.ExamArrangementTime;
import com.zhongzheng.modules.exam.service.IExamArrangementStudentService;
import com.zhongzheng.modules.exam.service.IExamArrangementTimeService;
import com.zhongzheng.modules.exam.vo.ExamArrangementTimeVo;
import com.zhongzheng.modules.exam.vo.ExamUserVo;
import com.zhongzheng.modules.user.service.IUserService;
import com.zhongzheng.modules.user.vo.UserVo;
import org.springframework.beans.BeanUtils;
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.exam.domain.ExamArrangement;
import com.zhongzheng.modules.exam.mapper.ExamArrangementMapper;
import com.zhongzheng.modules.exam.vo.ExamArrangementVo;
import com.zhongzheng.modules.exam.service.IExamArrangementService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 考试安排，包含考试指引Service业务层处理
 *
 * @author change
 * @date 2021-05-19
 */
@Service
public class ExamArrangementServiceImpl extends ServiceImpl<ExamArrangementMapper, ExamArrangement> implements IExamArrangementService {

    @Autowired
    private IExamArrangementTimeService examArrangementTimeService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IExamArrangementStudentService examArrangementStudentService;

    @Autowired
    private IMajorCategoryService majorCategoryService;


    @Override
    public ExamArrangementVo queryById(Long id){
        ExamArrangement db = this.baseMapper.selectById(id);
        ExamArrangementVo examArrangementVo = BeanUtil.toBean(db, ExamArrangementVo.class);
        //獲得考試時間
        LambdaQueryWrapper<ExamArrangementTime> lqw = Wrappers.lambdaQuery();
        lqw.eq(true,ExamArrangementTime::getArrangementId,db.getId());
        List<ExamArrangementTime> list = examArrangementTimeService.list(lqw);
        List<ExamArrangementTimeVo> examArrangementTimeVos = entity2TimeVo(list);
        examArrangementVo.setExamArrangementTimeVo(examArrangementTimeVos);

        return examArrangementVo;
    }


    /**
     * 实体类转化成视图对象
     *
     * @param collection 实体类集合
     * @return
     */
    private List<ExamArrangementTimeVo> entity2TimeVo(Collection<ExamArrangementTime> collection) {
        List<ExamArrangementTimeVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, ExamArrangementTimeVo.class))
                .collect(Collectors.toList());
        return voList;
    }
    @Override
    public List<ExamArrangementVo> queryList(ExamArrangementQueryBo bo) {
        List<ExamArrangementVo> examArrangementVos1 = baseMapper.selectExamgement(bo);

        List<ExamArrangementVo> examArrangementVos = entity2Vo(examArrangementVos1);
        examArrangementVos.stream().forEach(e -> {
                    ExamArrangementTimeQueryBo examArrangementTimeQueryBo = new ExamArrangementTimeQueryBo();
                    examArrangementTimeQueryBo.setArrangement_id(e.getId());
                    e.setExamArrangementTimeVo(examArrangementTimeService.queryList(examArrangementTimeQueryBo));
                }
        );
        return examArrangementVos;
    }

    /**
    * 实体类转化成视图对象
    *
    * @param collection 实体类集合
    * @return
    */
    private List<ExamArrangementVo> entity2Vo(List<ExamArrangementVo> collection) {
        List<ExamArrangementVo> voList = collection;
        if (collection instanceof Page) {
            Page<ExamArrangementVo> page = (Page<ExamArrangementVo>)collection;
            Page<ExamArrangementVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page,pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByAddBo(ExamArrangementAddBo bo) {
        ExamArrangement add = BeanUtil.toBean(bo, ExamArrangement.class);
        validEntityBeforeSave(add);
        add.setCreateTime(DateUtils.getNowTime());
        add.setUpdateTime(DateUtils.getNowTime());
        MajorCategoryVo majorCategoryVo = majorCategoryService.queryById(bo.getExamTypeId());
        add.setExamType(majorCategoryVo.getCategoryName());
        if (bo.getStatus() == 3){
            bo.setReleaseTime(DateUtils.getNowTime());
        }
        boolean save = this.save(add);
        examArrangementTimeService.inserTime(entity2Time(bo.getExamArrangementTimeAddBo()),add.getId());
        return save;
    }

    /**
     * 实体类转化成视图对象
     *
     * @param collection 实体类集合
     * @return
     */
    private List<ExamArrangementTime> entity2Time(Collection<ExamArrangementTimeAddBo> collection) {
        List<ExamArrangementTime> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, ExamArrangementTime.class))
                .collect(Collectors.toList());
        return voList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByEditBo(ExamArrangementEditBo bo) {
        ExamArrangement update = BeanUtil.toBean(bo, ExamArrangement.class);
        validEntityBeforeSave(update);
        if (!CollectionUtils.isEmpty(bo.getExamArrangementTimeEditBoList())) {
            examArrangementTimeService.removeId(bo.getId());
            bo.getExamArrangementTimeEditBoList().stream().forEach(e -> {
                    ExamArrangementTime examArrangementTime = BeanUtil.toBean(e, ExamArrangementTime.class);
                    examArrangementTime.setArrangementId(bo.getId());
                    examArrangementTimeService.insertExamArrangementTime(examArrangementTime);
            });
        }
        MajorCategoryVo majorCategoryVo = majorCategoryService.queryById(bo.getExamTypeId());
        update.setExamType(majorCategoryVo.getCategoryName());
        update.setUpdateTime(DateUtils.getNowTime());
        if (bo.getStatus() == 3){
            bo.setReleaseTime(DateUtils.getNowTime());
        }
        return this.updateById(update);
    }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
       List<ExamArrangement> examArrangementList = new ArrayList<>();
       ids.stream().forEach(id -> examArrangementList.add(new ExamArrangement().setId(id).setStatus(0)));
        return this.updateBatchById(examArrangementList);
    }

    @Override
    public List<ExamUserVo> queryUserList(ExamArrangementQueryBo bo) {
        List<ExamUserVo> examUserVos = examArrangementStudentService.queryUserList(bo);
        return examUserVos;
    }

    @Override
    public List<ExamUserVo> queryUserSubList(ExamArrangementQueryBo bo) {
        List<ExamUserVo> examUserVos = examArrangementStudentService.querySubList(bo);
        return examUserVos;
    }
}
