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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.common.utils.ServletUtils;
import com.zhongzheng.modules.course.vo.CourseBusinessVo;
import com.zhongzheng.modules.exam.bo.*;
import com.zhongzheng.modules.exam.domain.ExamKnowledgeBusiness;
import com.zhongzheng.modules.exam.domain.ExamPaper;
import com.zhongzheng.modules.exam.service.IExamKnowledgeBusinessService;
import org.apache.commons.codec.digest.Md5Crypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
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.ExamKnowledge;
import com.zhongzheng.modules.exam.mapper.ExamKnowledgeMapper;
import com.zhongzheng.modules.exam.vo.ExamKnowledgeVo;
import com.zhongzheng.modules.exam.service.IExamKnowledgeService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

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

/**
 * 知识点Service业务层处理
 *
 * @author ruoyi
 * @date 2021-10-18
 */
@Service
public class ExamKnowledgeServiceImpl extends ServiceImpl<ExamKnowledgeMapper, ExamKnowledge> implements IExamKnowledgeService {

    @Autowired
    private IExamKnowledgeBusinessService examKnowledgeBusinessService;


    @Override
    public ExamKnowledgeVo queryById(Long knowledgeId){
        ExamKnowledge db = this.baseMapper.selectById(knowledgeId);
        return BeanUtil.toBean(db, ExamKnowledgeVo.class);
    }

    @Override
    public List<ExamKnowledgeVo> queryList(ExamKnowledgeQueryBo bo) {
        Collection<ExamKnowledgeVo> examKnowledgeVos = baseMapper.queryList(bo);
        for (ExamKnowledgeVo examKnowledgeVo : examKnowledgeVos) {
            List<CourseBusinessVo> courseBusinessVos = baseMapper.queryCourseBusinessList(examKnowledgeVo.getKnowledgeId());
            examKnowledgeVo.setCourseBusinessList(courseBusinessVos);
        }
        return entity2Vo(examKnowledgeVos);
    }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByAddBo(ExamKnowledgeAddBo bo) {
        for (String s : bo.getKnowledgeName()) {
            ExamKnowledge examKnowledge = BeanUtil.toBean(bo, ExamKnowledge.class);
            examKnowledge.setKnowledgeName(s);
            examKnowledge.setEncoder(ServletUtils.getEncoded("ZSD"));
            examKnowledge.setCreateTime(DateUtils.getNowTime());
            examKnowledge.setUpdateTime(DateUtils.getNowTime());
            boolean save = this.save(examKnowledge);
            for (ExamKnowledgeBusinessAddBo examKnowledgeBusinessAddBo : bo.getExamKnowledgeBusinessAddBoList()) {
                examKnowledgeBusinessAddBo.setKnowledgeId(examKnowledge.getKnowledgeId());
                examKnowledgeBusinessAddBo.setUpdateTime(DateUtils.getNowTime());
                examKnowledgeBusinessAddBo.setCreateTime(DateUtils.getNowTime());
                Boolean aBoolean = examKnowledgeBusinessService.insertByAddBo(examKnowledgeBusinessAddBo);
            }
        }
        return true;
    }

    @Override
    public Boolean updateByEditBo(ExamKnowledgeEditBo bo) {
        ExamKnowledge update = BeanUtil.toBean(bo, ExamKnowledge.class);
        validEntityBeforeSave(update);
        update.setUpdateTime(DateUtils.getNowTime());
        LambdaQueryWrapper<ExamKnowledgeBusiness> lqw = Wrappers.lambdaQuery();
        lqw.eq(ExamKnowledgeBusiness::getKnowledgeId,bo.getKnowledgeId());
        boolean remove = examKnowledgeBusinessService.remove(lqw);
        if (!CollectionUtils.isEmpty(bo.getExamKnowledgeBusinessAddBoList())) {
            for (ExamKnowledgeBusinessAddBo examKnowledgeBusinessAddBo : bo.getExamKnowledgeBusinessAddBoList()) {
                examKnowledgeBusinessAddBo.setKnowledgeId(bo.getKnowledgeId());
                examKnowledgeBusinessAddBo.setUpdateTime(DateUtils.getNowTime());
                examKnowledgeBusinessAddBo.setCreateTime(DateUtils.getNowTime());
                Boolean aBoolean = examKnowledgeBusinessService.insertByAddBo(examKnowledgeBusinessAddBo);
            }
        }
        return this.updateById(update);
    }

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

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

    @Override
    public long queryListTotal(ExamKnowledgeQueryBo bo) {
        LambdaQueryWrapper<ExamKnowledge> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getKnowledgeName()), ExamKnowledge::getKnowledgeName, bo.getKnowledgeName());
        lqw.in(bo.getStatus() != null, ExamKnowledge::getStatus, bo.getStatus());
        lqw.eq(StrUtil.isNotBlank(bo.getEncoder()), ExamKnowledge::getEncoder, bo.getEncoder());
        return baseMapper.selectCount(lqw);
    }
}
