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.vo.UserDateNoteVo;
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.user.bo.UserNoteAddBo;
import com.zhongzheng.modules.user.bo.UserNoteQueryBo;
import com.zhongzheng.modules.user.bo.UserNoteEditBo;
import com.zhongzheng.modules.user.domain.UserNote;
import com.zhongzheng.modules.user.mapper.UserNoteMapper;
import com.zhongzheng.modules.user.vo.UserNoteVo;
import com.zhongzheng.modules.user.service.IUserNoteService;

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

/**
 * 用户笔记记录Service业务层处理
 *
 * @author ruoyi
 * @date 2021-12-14
 */
@Service
public class UserNoteServiceImpl extends ServiceImpl<UserNoteMapper, UserNote> implements IUserNoteService {

    @Override
    public UserNoteVo queryById(Long noteId){
        UserNote db = this.baseMapper.selectById(noteId);
        return BeanUtil.toBean(db, UserNoteVo.class);
    }

    @Override
    public List<UserNoteVo> queryList(UserNoteQueryBo bo) {
        LambdaQueryWrapper<UserNote> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getNoteText()), UserNote::getNoteText, bo.getNoteText());
        lqw.eq(bo.getNoteDate() != null, UserNote::getNoteDate, bo.getNoteDate());
        lqw.eq(bo.getCourseId() != null, UserNote::getCourseId, bo.getCourseId());
        lqw.eq(bo.getSectionId() != null, UserNote::getSectionId, bo.getSectionId());
        lqw.eq(bo.getGoodsId() != null, UserNote::getGoodsId, bo.getGoodsId());
        lqw.eq(bo.getModuleId() != null, UserNote::getModuleId, bo.getModuleId());
        lqw.eq(bo.getChapterId() != null, UserNote::getChapterId, bo.getChapterId());
        lqw.eq(bo.getGradeId() != null, UserNote::getGradeId, bo.getGradeId());
        return entity2Vo(this.list(lqw));
    }

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

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

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

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

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

    @Override
    public List<UserDateNoteVo> listDate(UserNoteQueryBo bo) {
        List<UserDateNoteVo> userDateVos = baseMapper.listDate(bo);
        for (UserDateNoteVo userDateVo : userDateVos) {
            bo.setNoteDate(userDateVo.getDateNote());
            List<UserNoteVo> userNoteVos = baseMapper.listDateSon(bo);
            userDateVo.setUserNotes(userNoteVos);
        }
        return userDateVos;
    }
}
