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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Constant;
import com.zhongzheng.common.utils.DateUtils;
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.bo.ExamArrangementTimeAddBo;
import com.zhongzheng.modules.exam.bo.ExamArrangementTimeQueryBo;
import com.zhongzheng.modules.exam.bo.ExamArrangementTimeEditBo;
import com.zhongzheng.modules.exam.domain.ExamArrangementTime;
import com.zhongzheng.modules.exam.mapper.ExamArrangementTimeMapper;
import com.zhongzheng.modules.exam.vo.ExamArrangementTimeVo;
import com.zhongzheng.modules.exam.service.IExamArrangementTimeService;

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

/**
 * 考试时间段安排Service业务层处理
 *
 * @author change
 * @date 2021-05-25
 */
@Service
public class ExamArrangementTimeServiceImpl extends ServiceImpl<ExamArrangementTimeMapper, ExamArrangementTime> implements IExamArrangementTimeService {

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

    @Override
    public List<ExamArrangementTimeVo> queryList(ExamArrangementTimeQueryBo bo) {
        LambdaQueryWrapper<ExamArrangementTime> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getArrangement_id() != null, ExamArrangementTime::getArrangementId, bo.getArrangement_id());
        lqw.eq(bo.getStartTime() != null, ExamArrangementTime::getStartTime, bo.getStartTime());
        lqw.eq(bo.getEndTime() != null, ExamArrangementTime::getEndTime, bo.getEndTime());
        return entity2Vo(this.list(lqw));
    }

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

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

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

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

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

    @Override
    public void insertExamArrangementTime(ExamArrangementTime examArrangementTime) {
        baseMapper.insertExamTime(examArrangementTime);
    }

    @Override
    public void inserTime(List<ExamArrangementTime> entity2Time,Long id) {
        entity2Time.stream().forEach(a -> baseMapper.insertExamTime(a.setArrangementId(id)));
    }

    @Override
    public void removeId(Long id) {
        baseMapper.removeId(id);
    }
}
