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.user.bo.UserExamRecordAddBo;
import com.zhongzheng.modules.user.bo.UserExamRecordEditBo;
import com.zhongzheng.modules.user.bo.UserExamRecordQueryBo;
import com.zhongzheng.modules.user.domain.UserExamRecord;
import com.zhongzheng.modules.user.mapper.UserExamRecordMapper;
import com.zhongzheng.modules.user.mapper.UserSubscribeMapper;
import com.zhongzheng.modules.user.service.IUserExamRecordService;
import com.zhongzheng.modules.user.vo.UserExamRecordVo;
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 UserExamRecordServiceImpl extends ServiceImpl<UserExamRecordMapper, UserExamRecord> implements IUserExamRecordService {

    @Autowired
    private UserExamRecordMapper userExamRecordMapper;

    @Override
    public UserExamRecordVo queryById(Long recordId){
        UserExamRecord db = this.baseMapper.selectById(recordId);
        return BeanUtil.toBean(db, UserExamRecordVo.class);
    }

    @Override
    public UserExamRecordVo selectDetail(UserExamRecordQueryBo bo) {
        return this.baseMapper.selectDetail(bo);
    }

    @Override
    public List<UserExamRecordVo> queryList(UserExamRecordQueryBo bo) {
        LambdaQueryWrapper<UserExamRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExamId() != null, UserExamRecord::getExamId, bo.getExamId());
        lqw.eq(bo.getUserId() != null, UserExamRecord::getUserId, bo.getUserId());
        lqw.eq(bo.getStatus() != null, UserExamRecord::getStatus, bo.getStatus());
        lqw.eq(bo.getGoodsId() != null, UserExamRecord::getGoodsId, bo.getGoodsId());
        lqw.eq(bo.getReportStatus() != null, UserExamRecord::getReportStatus, bo.getReportStatus());
        lqw.eq(bo.getPerformance() != null, UserExamRecord::getPerformance, bo.getPerformance());
        return entity2Vo(this.list(lqw));
    }

    @Override
    public Integer recordNum(UserExamRecordQueryBo bo) {
        LambdaQueryWrapper<UserExamRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExamId() != null, UserExamRecord::getExamId, bo.getExamId());
        lqw.eq(bo.getUserId() != null, UserExamRecord::getUserId, bo.getUserId());
        lqw.eq(bo.getStatus() != null, UserExamRecord::getStatus, bo.getStatus());
        lqw.eq(bo.getGoodsId() != null, UserExamRecord::getGoodsId, bo.getGoodsId());
        return this.count(lqw);
    }

    @Override
    public Long selectDoNum(UserExamRecordQueryBo bo) {
        return userExamRecordMapper.selectDoNum(bo);
    }

    @Override
    public Long selectRightNum(UserExamRecordQueryBo bo) {
        return userExamRecordMapper.selectRightNum(bo);
    }

    @Override
    public List<UserExamRecordVo> selectList(UserExamRecordQueryBo bo) {
        return userExamRecordMapper.selectList(bo);
    }

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

    @Override
    public Long insertByAddBo(UserExamRecordAddBo bo) {
        UserExamRecord add = BeanUtil.toBean(bo, UserExamRecord.class);
        validEntityBeforeSave(add);
        add.setCreateTime(DateUtils.getNowTime());
        add.setUpdateTime(DateUtils.getNowTime());
        this.save(add);
        return add.getRecordId();
    }

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

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

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