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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.modules.activity.bo.ActivityTaskUserAddBo;
import com.zhongzheng.modules.activity.domain.ActivityTaskUser;
import com.zhongzheng.modules.activity.service.IActivityTaskUserService;
import com.zhongzheng.modules.activity.vo.ActivityGradeVo;
import com.zhongzheng.modules.activity.vo.ActivityTaskVo;
import com.zhongzheng.modules.user.bo.UserStudyRecordAddBo;
import com.zhongzheng.modules.user.bo.UserStudyRecordEditBo;
import com.zhongzheng.modules.user.bo.UserStudyRecordQueryBo;
import com.zhongzheng.modules.user.domain.UserStudyRecord;
import com.zhongzheng.modules.user.mapper.UserStudyRecordMapper;
import com.zhongzheng.modules.user.service.IUserStudyRecordService;
import com.zhongzheng.modules.user.vo.UserStudyRecordVo;
import com.zhongzheng.modules.user.vo.UserVo;
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.List;
import java.util.stream.Collectors;

/**
 * 用户学习记录Service业务层处理
 *
 * @author ruoyi
 * @date 2021-06-25
 */
@Service
public class UserStudyRecordServiceImpl extends ServiceImpl<UserStudyRecordMapper, UserStudyRecord> implements IUserStudyRecordService {

    @Autowired
    private UserStudyRecordMapper userStudyRecordMapper;

    @Autowired
    private IActivityTaskUserService activityTaskUserServiceImpl;

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

    @Override
    public List<UserStudyRecordVo> queryList(UserStudyRecordQueryBo bo) {
        LambdaQueryWrapper<UserStudyRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, UserStudyRecord::getUserId, bo.getUserId());
        lqw.eq(bo.getCourseId() != null, UserStudyRecord::getCourseId, bo.getCourseId());
        lqw.eq(bo.getSectionId() != null, UserStudyRecord::getSectionId, bo.getSectionId());
        lqw.eq(bo.getStudyDuration() != null, UserStudyRecord::getStudyDuration, bo.getStudyDuration());
        lqw.eq(bo.getStartTime() != null, UserStudyRecord::getStartTime, bo.getStartTime());
        lqw.eq(bo.getEndTime() != null, UserStudyRecord::getEndTime, bo.getEndTime());
        return entity2Vo(this.list(lqw));
    }

    @Override
    public List<UserStudyRecordVo> selectList(UserStudyRecordQueryBo bo) {
        return userStudyRecordMapper.selectList(bo);
    }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer insertByAddBo(UserStudyRecordAddBo bo) {
        UserStudyRecord add = BeanUtil.toBean(bo, UserStudyRecord.class);
        validEntityBeforeSave(add);
        add.setCreateTime(DateUtils.getNowTime());
        add.setUpdateTime(DateUtils.getNowTime());
        add.setStartTime(DateUtils.getNowTime()-add.getStudyDuration());
        add.setEndTime(DateUtils.getNowTime());

        //学习时长达到规定时长即加分
        ActivityTaskUserAddBo activityTaskUserAddBo = new ActivityTaskUserAddBo();
        activityTaskUserAddBo.setUserId(bo.getUserId());
        activityTaskUserAddBo.setTaskType(4);
        activityTaskUserAddBo.setScore(Convert.toStr(bo.getStudyDuration()));
        Integer integer = activityTaskUserServiceImpl.learnTime(activityTaskUserAddBo);

        this.save(add);
        return integer;
    }

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

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

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

    @Override
    public List<UserStudyRecord> selectUserStudy(Long userId, Integer courseId) {
        return baseMapper.selectUserStudy(userId,courseId);
    }
}
