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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.common.utils.SecurityUtils;
import com.zhongzheng.common.utils.ServletUtils;
import com.zhongzheng.modules.goods.service.IGoodsService;
import com.zhongzheng.modules.goods.vo.GoodsVo;
import com.zhongzheng.modules.grade.bo.*;
import com.zhongzheng.modules.grade.domain.ClassGradeGoods;
import com.zhongzheng.modules.grade.domain.ClassGradeSys;
import com.zhongzheng.modules.grade.service.IClassGradeGoodsService;
import com.zhongzheng.modules.grade.service.IClassGradeSysService;
import com.zhongzheng.modules.grade.vo.ClassGradeStudentVo;
import com.zhongzheng.modules.system.domain.SysConfig;
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.grade.domain.ClassGrade;
import com.zhongzheng.modules.grade.mapper.ClassGradeMapper;
import com.zhongzheng.modules.grade.vo.ClassGradeVo;
import com.zhongzheng.modules.grade.service.IClassGradeService;

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-11-10
 */
@Service
public class ClassGradeServiceImpl extends ServiceImpl<ClassGradeMapper, ClassGrade> implements IClassGradeService {

    @Autowired
    private  IGoodsService iGoodsService;

    @Autowired
    private IClassGradeSysService classGradeSysService;

    @Autowired
    private IClassGradeGoodsService classGradeGoodsService;

    @Autowired
    private IUserStudyRecordService iUserStudyRecordService;

    @Override
    public ClassGradeVo queryById(Long gradeId){
        ClassGradeQueryBo classGradeQueryBo = new ClassGradeQueryBo();
        classGradeQueryBo.setGradeId(gradeId);
        return entity2Vo(baseMapper.queryList(classGradeQueryBo)).get(0);
    }

    @Override
    public List<ClassGradeVo> queryList(ClassGradeQueryBo bo) {
        return entity2Vo(baseMapper.queryList(bo));
    }

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

    @Override
    public Boolean insertByAddBo(ClassGradeAddBo bo) {
        ClassGrade add = BeanUtil.toBean(bo, ClassGrade.class);
        validEntityBeforeSave(add);
        add.setCreateTime(DateUtils.getNowTime());
        add.setUpdateTime(DateUtils.getNowTime());
        add.setGradeCode(ServletUtils.getEncoded("BJ"));
        boolean save = this.save(add);
        if (bo.getClassGradeGoodsAddBos() != null){
            for (Long classGradeGoodsAddBo : bo.getClassGradeGoodsAddBos()) {
                ClassGradeGoods classGradeGoods = new ClassGradeGoods();
                classGradeGoods.setGradeId(add.getGradeId());
                classGradeGoods.setGoodsId(classGradeGoodsAddBo);
                classGradeGoods.setCreateTime(DateUtils.getNowTime());
                classGradeGoods.setUpdateTime(DateUtils.getNowTime());
                classGradeGoodsService.save(classGradeGoods);
            }
        }
        if (bo.getSysUserId() != null){
            ClassGradeSys classGradeSys = new ClassGradeSys();
            classGradeSys.setGradeId(add.getGradeId());
            classGradeSys.setCreateBy(SecurityUtils.getUsername());
            classGradeSys.setStartTime(DateUtils.getNowTime());
            classGradeSys.setSysUserId(bo.getSysUserId());
            classGradeSys.setStatus(1);

            classGradeSys.setCreateTime(DateUtils.getNowTime());
            classGradeSys.setUpdateTime(DateUtils.getNowTime());
            classGradeSysService.save(classGradeSys);
        }

        return save;
    }

    @Override
    public Boolean updateByEditBo(ClassGradeEditBo bo) {
        ClassGrade update = BeanUtil.toBean(bo, ClassGrade.class);
        validEntityBeforeSave(update);
        update.setUpdateTime(DateUtils.getNowTime());
        if (bo.getSysUserId() != null){
            LambdaQueryWrapper<ClassGradeSys> lqw = new LambdaQueryWrapper<>();
            lqw.like(ClassGradeSys::getGradeId,bo.getGradeId());
            lqw.like(ClassGradeSys::getSysUserId,bo.getSysUserId());
            lqw.like(ClassGradeSys::getStatus,1);
            if (CollectionUtils.isEmpty(classGradeSysService.list(lqw))){
                lqw.clear();
                lqw.like(ClassGradeSys::getGradeId,bo.getGradeId());
                lqw.like(ClassGradeSys::getStatus,1);
                ClassGradeSys classGradeSys1 = new ClassGradeSys();
                classGradeSys1.setStatus(0);
                classGradeSys1.setEndTime(DateUtils.getNowTime());
                classGradeSysService.update(classGradeSys1,lqw);

                ClassGradeSys classGradeSys = new ClassGradeSys();
                classGradeSys.setGradeId(bo.getGradeId());
                classGradeSys.setCreateBy(SecurityUtils.getUsername());
                classGradeSys.setStartTime(DateUtils.getNowTime());
                classGradeSys.setSysUserId(bo.getSysUserId());
                classGradeSys.setStatus(1);
                classGradeSys.setCreateTime(DateUtils.getNowTime());
                classGradeSys.setUpdateTime(DateUtils.getNowTime());
                classGradeSysService.save(classGradeSys);
            }
        }
        return this.updateById(update);
    }

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

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

    @Override
    public List<ClassGradeStudentVo> listGrade(ClassGradeUserQueryBo bo) {
        List<ClassGradeStudentVo> classGradeStudentVos = baseMapper.listGrade(bo);
        for (ClassGradeStudentVo classGradeStudentVo : classGradeStudentVos) {
            Long secLong = 0L;
            Long studyLong = 0L;

            SubjectStudyRecordQueryBo subjectStudyRecordQueryBo = new SubjectStudyRecordQueryBo();
            subjectStudyRecordQueryBo.setGoodsId(classGradeStudentVo.getGoodsId());
            subjectStudyRecordQueryBo.setUserId(bo.getUserId());
            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();

            }
            //BigDecimal divide = new BigDecimal(studyLong.toString()).divide(new BigDecimal(secLong.toString()),2,BigDecimal.ROUND_HALF_UP);
            //classGradeStudentVo.setStudyNum(divide.multiply(new BigDecimal("100")).longValue());
            classGradeStudentVo.setSecAllNum(secLong);
            classGradeStudentVo.setStuAllNum(studyLong);
        }
        return classGradeStudentVos;
    }
}
