package com.zhongzheng.modules.user.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.vo.QuestionVo;
import com.zhongzheng.modules.collect.domain.CollectQuestion;
import com.zhongzheng.modules.user.bo.UserExamWrongRecordAddBo;
import com.zhongzheng.modules.user.bo.UserExamWrongRecordEditBo;
import com.zhongzheng.modules.user.bo.UserExamWrongRecordQueryBo;
import com.zhongzheng.modules.user.domain.UserExamWrongRecord;
import com.zhongzheng.modules.user.mapper.UserExamRecordMapper;
import com.zhongzheng.modules.user.mapper.UserExamWrongRecordMapper;
import com.zhongzheng.modules.user.service.IUserExamWrongRecordService;
import com.zhongzheng.modules.user.vo.UserExamWrongRecordVo;
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 java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户我的题库错题记录Service业务层处理
 *
 * @author hjl
 * @date 2021-12-15
 */
@Service
public class UserExamWrongRecordServiceImpl extends ServiceImpl<UserExamWrongRecordMapper, UserExamWrongRecord> implements IUserExamWrongRecordService {

    @Autowired
    private UserExamWrongRecordMapper userExamWrongRecordMapper;

    @Override
    public UserExamWrongRecordVo queryById(Long wrongId){
        UserExamWrongRecord db = this.baseMapper.selectById(wrongId);
        return BeanUtil.toBean(db, UserExamWrongRecordVo.class);
    }

    @Override
    public Long recordNum(UserExamWrongRecordQueryBo bo) {
        return this.baseMapper.recordNum(bo);
    }

    @Override
    public List<UserExamWrongRecordVo> queryList(UserExamWrongRecordQueryBo bo) {
        LambdaQueryWrapper<UserExamWrongRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getRecordId() != null, UserExamWrongRecord::getRecordId, bo.getRecordId());
        lqw.eq(bo.getQuestionId() != null, UserExamWrongRecord::getQuestionId, bo.getQuestionId());
        lqw.eq(bo.getUserId() != null, UserExamWrongRecord::getUserId, bo.getUserId());
        lqw.eq(bo.getGoodsId() != null, UserExamWrongRecord::getGoodsId, bo.getGoodsId());
        lqw.eq(bo.getExamId() != null, UserExamWrongRecord::getExamId, bo.getExamId());
        return entity2Vo(this.list(lqw));
    }

    @Override
    public List<UserExamWrongRecordVo> selectList(UserExamWrongRecordQueryBo bo) {
        return userExamWrongRecordMapper.selectList(bo);
    }

    @Override
    public List<UserExamWrongRecordVo> selectQuestionList(UserExamWrongRecordQueryBo bo) {
        return userExamWrongRecordMapper.selectQuestionList(bo);
    }

    @Override
    public List<UserExamWrongRecordVo> selectTypeNum(UserExamWrongRecordQueryBo bo) {
        return userExamWrongRecordMapper.selectTypeNum(bo);
    }

    @Override
    public List<UserExamWrongRecordVo> selectTypeQuestionList(UserExamWrongRecordQueryBo bo) {
        return userExamWrongRecordMapper.selectTypeQuestionList(bo);
    }

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

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

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

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

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

    @Override
    public Boolean remove_question(UserExamWrongRecordQueryBo bo) {
        return this.remove(new LambdaQueryWrapper<UserExamWrongRecord>()
                .eq(UserExamWrongRecord::getQuestionId,bo.getQuestionId())
                .eq(UserExamWrongRecord::getGoodsId,bo.getGoodsId())
                .eq(UserExamWrongRecord::getExamId,bo.getExamId())
                .eq(UserExamWrongRecord::getUserId,bo.getUserId()));
    }
}
