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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.modules.grade.domain.UserPeriodStatus;
import com.zhongzheng.modules.grade.service.IUserPeriodStatusService;
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 com.zhongzheng.modules.grade.bo.UserPeriodAddBo;
import com.zhongzheng.modules.grade.bo.UserPeriodQueryBo;
import com.zhongzheng.modules.grade.bo.UserPeriodEditBo;
import com.zhongzheng.modules.grade.domain.UserPeriod;
import com.zhongzheng.modules.grade.mapper.UserPeriodMapper;
import com.zhongzheng.modules.grade.vo.UserPeriodVo;
import com.zhongzheng.modules.grade.service.IUserPeriodService;
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-11-22
 */
@Service
public class UserPeriodServiceImpl extends ServiceImpl<UserPeriodMapper, UserPeriod> implements IUserPeriodService {

    @Autowired
    private IUserPeriodStatusService userPeriodStatusService;


    @Override
    public UserPeriodVo queryById(Long id){
        UserPeriod db = this.baseMapper.selectById(id);
        return BeanUtil.toBean(db, UserPeriodVo.class);
    }

    @Override
    public List<UserPeriodVo> queryList(UserPeriodQueryBo bo) {
        LambdaQueryWrapper<UserPeriod> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, UserPeriod::getUserId, bo.getUserId());
        lqw.eq(bo.getCourseId() != null, UserPeriod::getCourseId, bo.getCourseId());
        lqw.eq(bo.getSectionId() != null, UserPeriod::getSectionId, bo.getSectionId());
        lqw.eq(bo.getGoodsId() != null, UserPeriod::getGoodsId, bo.getGoodsId());
        lqw.eq(bo.getType() != null, UserPeriod::getType, bo.getType());
        lqw.eq(bo.getExamId() != null, UserPeriod::getExamId, bo.getExamId());
        lqw.eq(bo.getModuleId() != null, UserPeriod::getModuleId, bo.getModuleId());
        lqw.eq(bo.getChapterId() != null, UserPeriod::getChapterId, bo.getChapterId());
        return entity2Vo(this.list(lqw));
    }

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

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByEditBo(UserPeriodEditBo bo) {
            for (Long aLong : bo.getId()) {
                UserPeriodStatus userPeriodStatus = new UserPeriodStatus();
                userPeriodStatus.setPeriodStatus(1);
                userPeriodStatus.setStatus(bo.getStatus());
                userPeriodStatus.setUpdateTime(DateUtils.getNowTime());
                LambdaQueryWrapper<UserPeriodStatus> lqw = Wrappers.lambdaQuery();
                lqw.eq( UserPeriodStatus::getPeriodId, aLong);
                lqw.eq( UserPeriodStatus::getPeriodStatus, 1);
                userPeriodStatusService.update(userPeriodStatus,lqw);
            }
        return true;
    }

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

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean editPeriodAll(UserPeriodEditBo bo) {
            LambdaQueryWrapper<UserPeriod> lqw = Wrappers.lambdaQuery();
            lqw.eq(bo.getUserId() != null, UserPeriod::getUserId, bo.getUserId());
            lqw.eq(bo.getGoodsId() != null, UserPeriod::getGoodsId, bo.getGoodsId());
            List<UserPeriod> list = this.list(lqw);
            for (UserPeriod userPeriod : list) {
                UserPeriodStatus userPeriodStatus = new UserPeriodStatus();
                userPeriodStatus.setPeriodStatus(1);
                userPeriodStatus.setStatus(bo.getStatus());
                userPeriodStatus.setUpdateTime(DateUtils.getNowTime());
                LambdaQueryWrapper<UserPeriodStatus> userPeriods = Wrappers.lambdaQuery();
                userPeriods.eq( UserPeriodStatus::getPeriodId, userPeriod.getId());
                userPeriods.eq( UserPeriodStatus::getPeriodStatus, 1);
                userPeriods.eq( UserPeriodStatus::getStatus, 2);
                userPeriodStatusService.update(userPeriodStatus,userPeriods);
            }
        return true;
    }
}
