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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.zhongzheng.modules.exam.bo.*;
import com.zhongzheng.modules.exam.service.IExamArrangementTimeService;
import com.zhongzheng.modules.exam.vo.ExamUserVo;
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.ExamArrangementStudent;
import com.zhongzheng.modules.exam.mapper.ExamArrangementStudentMapper;
import com.zhongzheng.modules.exam.vo.ExamArrangementStudentVo;
import com.zhongzheng.modules.exam.service.IExamArrangementStudentService;

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

/**
 * 报名考试得学生关联Service业务层处理
 *
 * @author change
 * @date 2021-05-20
 */
@Service
public class ExamArrangementStudentServiceImpl extends ServiceImpl<ExamArrangementStudentMapper, ExamArrangementStudent> implements IExamArrangementStudentService {

    @Autowired
    private IExamArrangementTimeService examArrangementTimeService;

    @Override
    public ExamArrangementStudentVo queryById(Long arrangementId){
        ExamArrangementStudent db = this.baseMapper.selectById(arrangementId);
        return BeanUtil.toBean(db, ExamArrangementStudentVo.class);
    }

    @Override
    public List<ExamArrangementStudentVo> queryList(ExamArrangementStudentQueryBo bo) {
        LambdaQueryWrapper<ExamArrangementStudent> lqw = Wrappers.lambdaQuery();
        return entity2Vo(this.list(lqw));
    }

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

    @Override
    public Boolean insertByAddBo(ExamArrangementStudentAddBo bo) {
        ExamArrangementStudent add = BeanUtil.toBean(bo, ExamArrangementStudent.class);
        validEntityBeforeSave(add);
        return this.save(add);
    }

    @Override
    public Boolean updateByEditBo(ExamArrangementStudentEditBo bo) {
        ExamArrangementStudent update = BeanUtil.toBean(bo, ExamArrangementStudent.class);
        validEntityBeforeSave(update);
        return this.updateById(update);
    }

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

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

    @Override
    public List<ExamUserVo> queryUserList(ExamArrangementQueryBo bo) {
        List<ExamUserVo> examUserVos = baseMapper.selectExamUserList(bo);
        examUserVos.stream().forEach(e -> {
                    ExamArrangementTimeQueryBo examArrangementTimeQueryBo = new ExamArrangementTimeQueryBo();
                    examArrangementTimeQueryBo.setArrangement_id(e.getExamId());
                    e.setExamArrangementTimeVo(examArrangementTimeService.queryList(examArrangementTimeQueryBo));
        }
        );
        return entity2UserVo(examUserVos);
    }

    @Override
    public List<ExamUserVo> querySubList(ExamArrangementQueryBo bo) {
        List<ExamUserVo> examUserVos = baseMapper.selectSubList(bo);
        examUserVos.stream().forEach(e -> {
                    ExamArrangementTimeQueryBo examArrangementTimeQueryBo = new ExamArrangementTimeQueryBo();
                    examArrangementTimeQueryBo.setArrangement_id(e.getExamId());
                    e.setExamArrangementTimeVo(examArrangementTimeService.queryList(examArrangementTimeQueryBo));
                }
        );
        return entity2UserVo(examUserVos);
    }

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