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.goods.vo.GoodsUserVo;
import com.zhongzheng.modules.goods.vo.GoodsVo;
import com.zhongzheng.modules.user.bo.SubjectStudyRecordQueryBo;
import com.zhongzheng.modules.user.service.IUserStudyRecordService;
import com.zhongzheng.modules.user.vo.SubjectStudyRecordVo;
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.user.bo.UserPlanGoodsAddBo;
import com.zhongzheng.modules.user.bo.UserPlanGoodsQueryBo;
import com.zhongzheng.modules.user.bo.UserPlanGoodsEditBo;
import com.zhongzheng.modules.user.domain.UserPlanGoods;
import com.zhongzheng.modules.user.mapper.UserPlanGoodsMapper;
import com.zhongzheng.modules.user.vo.UserPlanGoodsVo;
import com.zhongzheng.modules.user.service.IUserPlanGoodsService;

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

/**
 * 学习计划绑定课程Service业务层处理
 *
 * @author ruoyi
 * @date 2021-12-15
 */
@Service
public class UserPlanGoodsServiceImpl extends ServiceImpl<UserPlanGoodsMapper, UserPlanGoods> implements IUserPlanGoodsService {

    @Autowired
    private IUserStudyRecordService iUserStudyRecordService;

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

    @Override
    public List<UserPlanGoodsVo> queryList(UserPlanGoodsQueryBo bo) {
        LambdaQueryWrapper<UserPlanGoods> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPlanId() != null, UserPlanGoods::getPlanId, bo.getPlanId());
        lqw.eq(bo.getSourceId() != null, UserPlanGoods::getSourceId, bo.getSourceId());
        return entity2Vo(this.list(lqw));
    }

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

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

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

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

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

    @Override
    public List<GoodsUserVo> selectGoods(Long planId,Long UserId) {
        List<GoodsUserVo> goodsVos = baseMapper.selectGoods(planId);
        for (GoodsUserVo goodsUserVo : goodsVos) {
            Long secLong = 0L;
            Long studyLong = 0L;
            SubjectStudyRecordQueryBo subjectStudyRecordQueryBo = new SubjectStudyRecordQueryBo();
            subjectStudyRecordQueryBo.setGoodsId(goodsUserVo.getGoodsId());
            subjectStudyRecordQueryBo.setUserId(UserId);
            subjectStudyRecordQueryBo.setGradeId(goodsUserVo.getGradeId());
            List<SubjectStudyRecordVo> subjectStudyRecordVos = iUserStudyRecordService.listSubject(subjectStudyRecordQueryBo);
            for (SubjectStudyRecordVo subjectStudyRecordVo : subjectStudyRecordVos) {
                secLong = new BigDecimal(secLong.toString()).add(new BigDecimal(subjectStudyRecordVo.getSectionNum().toString())).longValue();
                studyLong = new BigDecimal(studyLong.toString()).add(new BigDecimal(subjectStudyRecordVo.getRecordNum().toString())).longValue();
            }
            goodsUserVo.setSecAllNum(secLong);
            goodsUserVo.setStuAllNum(studyLong);
        }
        return goodsVos;
    }
}
