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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.common.utils.ServletUtils;
import com.zhongzheng.modules.exam.bo.ExamNumberGoodsQueryBo;
import com.zhongzheng.modules.exam.vo.ExamApplySiteTimeVo;
import com.zhongzheng.modules.exam.vo.ExamApplySiteVo;
import com.zhongzheng.modules.exam.vo.ExamNumberGoodsVo;
import io.swagger.annotations.ApiModelProperty;
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.ExamApplyAddBo;
import com.zhongzheng.modules.exam.bo.ExamApplyQueryBo;
import com.zhongzheng.modules.exam.bo.ExamApplyEditBo;
import com.zhongzheng.modules.exam.domain.ExamApply;
import com.zhongzheng.modules.exam.mapper.ExamApplyMapper;
import com.zhongzheng.modules.exam.vo.ExamApplyVo;
import com.zhongzheng.modules.exam.service.IExamApplyService;

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 ExamApplyServiceImpl extends ServiceImpl<ExamApplyMapper, ExamApply> implements IExamApplyService {

    @Override
    public ExamApplyVo queryById(Long applyId){
        ExamApplyVo examApplyVo = baseMapper.selectByApply(applyId);
        List<ExamNumberGoodsVo> examNumberGoodsVos = baseMapper.examNumberGoodsVos(applyId);
        List<ExamApplySiteVo> examApplySite = baseMapper.addressExam(applyId,1L);
        for (ExamApplySiteVo examApplySiteVo : examApplySite) {
            List<ExamApplySiteTimeVo> examApplySiteTime = baseMapper.addressTimeExam(examApplySiteVo.getId());
            examApplySiteVo.setExamApplySiteTime(examApplySiteTime);
        }
        List<ExamApplySiteVo> examApplySiteTrain = baseMapper.addressExam(applyId,2L);
        for (ExamApplySiteVo examApplySiteVo : examApplySiteTrain) {
            List<ExamApplySiteTimeVo> examApplySiteTime = baseMapper.addressTimeExam(examApplySiteVo.getId());
            examApplySiteVo.setExamApplySiteTime(examApplySiteTime);
        }
        examApplyVo.setExamNumberGoods(examNumberGoodsVos);
        examApplyVo.setExamApplySite(examApplySite);
        examApplyVo.setExamApplySiteTrain(examApplySiteTrain);
        return examApplyVo;
    }

    @Override
    public List<ExamApplyVo> queryList(ExamApplyQueryBo bo) {

        return baseMapper.queryExam(bo);
    }

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

    @Override
    public Boolean insertByAddBo(ExamApplyAddBo bo) {
        ExamApply add = BeanUtil.toBean(bo, ExamApply.class);
        validEntityBeforeSave(add);
        add.setCode(ServletUtils.getEncoded("KSAP"));
        add.setCreateTime(DateUtils.getNowTime());
        add.setUpdateTime(DateUtils.getNowTime());
        return this.save(add);
    }

    @Override
    public Boolean updateByEditBo(ExamApplyEditBo bo) {
        ExamApply update = BeanUtil.toBean(bo, ExamApply.class);
        if (bo.getStatus() == 1){
            List<Long> contGoodsId = baseMapper.countGoods(bo.getApplyId());
            List<ExamApplySiteVo> examApplySite = baseMapper.addressExam(bo.getApplyId(),1L);
            if (CollectionUtils.isEmpty(examApplySite)){
                throw new RuntimeException("请先设置考试地点和时间");
            }
            if (CollectionUtils.isNotEmpty(contGoodsId)){
                String goodsName ="";
                for (Long goodsId : contGoodsId) {
                    ExamNumberGoodsQueryBo examNumberGoodsQueryBo = new ExamNumberGoodsQueryBo();
                    examNumberGoodsQueryBo.setGoodsId(goodsId);
                    List<ExamNumberGoodsVo> examNumberGoodsVos = this.listGoods(examNumberGoodsQueryBo);
                    goodsName.concat(examNumberGoodsVos.get(0).getGoodsName()+",");
                }
                throw new RuntimeException(goodsName+"已被其他进行中的考试计划使用，请修改，再启用");
            }else {
                throw new RuntimeException("请先设置适用商品");
            }
        }
        validEntityBeforeSave(update);
        update.setUpdateTime(DateUtils.getNowTime());
        return this.updateById(update);
    }

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

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

    @Override
    public List<ExamNumberGoodsVo> listGoods(ExamNumberGoodsQueryBo bo) {
        return baseMapper.listGoods(bo);
    }

    @Override
    public List<ExamApplySiteVo> getSiteInfo(ExamApplyQueryBo bo) {
        List<ExamApplySiteVo> examApplySite = baseMapper.addressExam(bo.getApplyId(),bo.getAddressStatus());
        for (ExamApplySiteVo examApplySiteVo : examApplySite) {
            List<ExamApplySiteTimeVo> examApplySiteTime = baseMapper.addressTimeExam(examApplySiteVo.getId());
            examApplySiteVo.setExamApplySiteTime(examApplySiteTime);
        }
        return examApplySite;
    }

    @Override
    public ExamApplyVo getGoodsInfo(ExamApplyQueryBo bo) {
        ExamApply db = this.baseMapper.selectById(bo.getApplyId());
        ExamApplyVo examApplyVo = BeanUtil.toBean(db, ExamApplyVo.class);
        List<ExamNumberGoodsVo> examNumberGoodsVos = baseMapper.examNumberGoodsVos(bo.getApplyId());
        //初始化需要得到的数组
        Long[] array = new Long[examNumberGoodsVos.size()];
        //使用for循环得到数组
        for(int i = 0; i < examNumberGoodsVos.size();i++){
            array[i] = examNumberGoodsVos.get(i).getGoodsId();
        }
        examApplyVo.setExamNumberGoods(examNumberGoodsVos);
        examApplyVo.setGoodsId(array);
        return examApplyVo;
    }
}
