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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.common.utils.ServletUtils;
import com.zhongzheng.modules.bank.bo.*;
import com.zhongzheng.modules.bank.domain.QuestionBusiness;
import com.zhongzheng.modules.bank.domain.QuestionChapter;
import com.zhongzheng.modules.bank.domain.QuestionChapterExam;
import com.zhongzheng.modules.bank.mapper.QuestionChapterMapper;
import com.zhongzheng.modules.bank.service.IQuestionBusinessService;
import com.zhongzheng.modules.bank.service.IQuestionChapterExamService;
import com.zhongzheng.modules.bank.service.IQuestionChapterService;
import com.zhongzheng.modules.bank.vo.QuestionChapterVo;
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 org.springframework.transaction.annotation.Transactional;

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

/**
 * 章卷Service业务层处理
 *
 * @author hjl
 * @date 2021-10-25
 */
@Service
public class QuestionChapterServiceImpl extends ServiceImpl<QuestionChapterMapper, QuestionChapter> implements IQuestionChapterService {

    @Autowired
    private IQuestionBusinessService iQuestionBusinessService;

    @Autowired
    private IQuestionChapterExamService iQuestionChapterExamService;
    @Autowired
    private QuestionChapterMapper questionChapterMapper;

    @Override
    public QuestionChapterVo queryById(Long chapterExamId){
        QuestionChapter db = this.baseMapper.selectById(chapterExamId);
        return BeanUtil.toBean(db, QuestionChapterVo.class);
    }

    @Override
    public List<QuestionChapterVo> getList(QuestionChapterQueryBo bo) {
        return questionChapterMapper.getList(bo);
    }

    @Override
    public List<QuestionChapterVo> queryList(QuestionChapterQueryBo bo) {
        LambdaQueryWrapper<QuestionChapter> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getName()), QuestionChapter::getName, bo.getName());
        lqw.eq(bo.getSort() != null, QuestionChapter::getSort, bo.getSort());
        lqw.in(bo.getStatus() != null, QuestionChapter::getStatus, bo.getStatus());
        lqw.like(StrUtil.isNotBlank(bo.getPrefixName()), QuestionChapter::getPrefixName, bo.getPrefixName());
        lqw.eq(bo.getPublishStatus() != null, QuestionChapter::getPublishStatus, bo.getPublishStatus());
        lqw.eq(StrUtil.isNotBlank(bo.getCode()), QuestionChapter::getCode, bo.getCode());
        return entity2Vo(this.list(lqw));
    }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByAddBo(QuestionChapterAddBo bo) {
        QuestionChapter add = BeanUtil.toBean(bo, QuestionChapter.class);
        add.setCode(ServletUtils.getEncoded("ZJ"));
        validEntityBeforeSave(add);
        add.setCreateTime(DateUtils.getNowTime());
        add.setUpdateTime(DateUtils.getNowTime());
        boolean result = this.save(add);
        if(bo.getBusinessList()!=null){
            Collection<QuestionBusiness> coll = new HashSet<>();
            for(int i=0;i<bo.getBusinessList().size();i++){
                QuestionBusinessAddBo item = bo.getBusinessList().get(i);
                QuestionBusiness addItem = BeanUtil.toBean(item,  QuestionBusiness.class);
                addItem.setMajorId(add.getChapterExamId());
                addItem.setType(QuestionBusiness.TYPE_QUESTION_CHAPTER);
                coll.add(addItem);
            }
            iQuestionBusinessService.saveBatch(coll);
        }
        if(bo.getExamList()!=null){
            Collection<QuestionChapterExam> coll = new HashSet<>();
            for(int i=0;i<bo.getExamList().size();i++){
                QuestionChapterExamAddBo item = bo.getExamList().get(i);
                QuestionChapterExam addItem = BeanUtil.toBean(item,  QuestionChapterExam.class);
                addItem.setChapterExamId(add.getChapterExamId());
                coll.add(addItem);
            }
            iQuestionChapterExamService.saveBatch(coll);
        }
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByEditBo(QuestionChapterEditBo bo) {
        QuestionChapter update = BeanUtil.toBean(bo, QuestionChapter.class);
        validEntityBeforeSave(update);
        update.setUpdateTime(DateUtils.getNowTime());
        if(bo.getBusinessList()!=null){
            iQuestionBusinessService.remove(new LambdaQueryWrapper<QuestionBusiness>().eq(QuestionBusiness::getMajorId, bo.getChapterExamId())
                    .eq(QuestionBusiness::getType,QuestionBusiness.TYPE_QUESTION_CHAPTER));
            Collection<QuestionBusiness> coll = new HashSet<>();
            for(int i=0;i<bo.getBusinessList().size();i++){
                QuestionBusinessEditBo item = bo.getBusinessList().get(i);
                QuestionBusiness addItem = BeanUtil.toBean(item,  QuestionBusiness.class);
                addItem.setMajorId(update.getChapterExamId());
                addItem.setType(QuestionBusiness.TYPE_QUESTION_CHAPTER);
                coll.add(addItem);
            }
            iQuestionBusinessService.saveBatch(coll);
        }
        if(bo.getExamList()!=null){
            iQuestionChapterExamService.remove(new LambdaQueryWrapper<QuestionChapterExam>().eq(QuestionChapterExam::getChapterExamId, bo.getChapterExamId()));
            Collection<QuestionChapterExam> coll = new HashSet<>();
            for(int i=0;i<bo.getExamList().size();i++){
                QuestionChapterExamEditBo item = bo.getExamList().get(i);
                QuestionChapterExam addItem = BeanUtil.toBean(item,  QuestionChapterExam.class);
                addItem.setChapterExamId(update.getChapterExamId());
                coll.add(addItem);
            }
            if(coll.size()>0){
                iQuestionChapterExamService.saveBatch(coll);
            }

        }
        return this.updateById(update);
    }

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

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