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.modules.bank.bo.QuestionDetailAddBo;
import com.zhongzheng.modules.bank.bo.QuestionDetailEditBo;
import com.zhongzheng.modules.bank.bo.QuestionDetailQueryBo;
import com.zhongzheng.modules.bank.domain.QuestionDetail;
import com.zhongzheng.modules.bank.mapper.QuestionDetailMapper;
import com.zhongzheng.modules.bank.service.IQuestionDetailService;
import com.zhongzheng.modules.bank.vo.QuestionDetailVo;
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 java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 题目详情Service业务层处理
 *
 * @author hjl
 * @date 2021-05-20
 */
@Service
public class QuestionDetailServiceImpl extends ServiceImpl<QuestionDetailMapper, QuestionDetail> implements IQuestionDetailService {

    @Override
    public QuestionDetailVo queryById(Long questionDetailId){
        QuestionDetail db = this.baseMapper.selectById(questionDetailId);
        return BeanUtil.toBean(db, QuestionDetailVo.class);
    }

    @Override
    public List<QuestionDetailVo> queryList(QuestionDetailQueryBo bo) {
        LambdaQueryWrapper<QuestionDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getQuestionId() != null, QuestionDetail::getQuestionId, bo.getQuestionId());
        lqw.eq(StrUtil.isNotBlank(bo.getContent()), QuestionDetail::getContent, bo.getContent());
        lqw.eq(StrUtil.isNotBlank(bo.getImgUrl()), QuestionDetail::getImgUrl, bo.getImgUrl());
        lqw.eq(bo.getOptionAnswer() != null, QuestionDetail::getOptionAnswer, bo.getOptionAnswer());
        lqw.eq(StrUtil.isNotBlank(bo.getAnalysisContent()), QuestionDetail::getAnalysisContent, bo.getAnalysisContent());
        lqw.eq(bo.getPid() != null, QuestionDetail::getPid, bo.getPid());
        lqw.eq(bo.getType()!= null, QuestionDetail::getType, bo.getType());
        lqw.eq(bo.getItemAnswerQuestion()!= null, QuestionDetail::getItemAnswerQuestion, bo.getItemAnswerQuestion());
        lqw.eq(bo.getStatus() != null, QuestionDetail::getStatus, bo.getStatus());
        return entity2Vo(this.list(lqw));
    }

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

    @Override
    public Boolean insertByAddBo(QuestionDetailAddBo bo) {
        QuestionDetail add = BeanUtil.toBean(bo, QuestionDetail.class);
        validEntityBeforeSave(add);
        add.setCreateTime(DateUtils.getNowTime());
        add.setUpdateTime(DateUtils.getNowTime());
        return this.save(add);
    }

    @Override
    public Boolean updateByEditBo(QuestionDetailEditBo bo) {
        QuestionDetail update = BeanUtil.toBean(bo, QuestionDetail.class);
        validEntityBeforeSave(update);
        update.setUpdateTime(DateUtils.getNowTime());
        return this.updateById(update);
    }

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

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