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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.modules.course.bo.CourseBusinessQueryBo;
import com.zhongzheng.modules.course.bo.CourseEducationTypeQueryBo;
import com.zhongzheng.modules.course.bo.CourseProjectTypeQueryBo;
import com.zhongzheng.modules.course.service.ICourseBusinessService;
import com.zhongzheng.modules.course.service.ICourseEducationTypeService;
import com.zhongzheng.modules.course.service.ICourseProjectTypeService;
import com.zhongzheng.modules.course.vo.*;
import com.zhongzheng.modules.goods.service.IGoodsService;
import com.zhongzheng.modules.grade.bo.ClassGradeQueryBo;
import com.zhongzheng.modules.grade.service.IClassGradeGoodsService;
import com.zhongzheng.modules.grade.service.IClassGradeSysService;
import com.zhongzheng.modules.grade.service.IClassGradeUserService;
import com.zhongzheng.modules.grade.vo.ClassGradeStudentVo;
import com.zhongzheng.modules.inform.bo.InformRemindBusinessAddBo;
import com.zhongzheng.modules.inform.domain.InformRemindBusiness;
import com.zhongzheng.modules.inform.service.IInformRemindBusinessService;
import com.zhongzheng.modules.inform.vo.InformRemindBusinessVo;
import com.zhongzheng.modules.user.service.IUserStudyRecordService;
import com.zhongzheng.modules.user.service.IUserUpdateService;
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.inform.bo.InformRemindAddBo;
import com.zhongzheng.modules.inform.bo.InformRemindQueryBo;
import com.zhongzheng.modules.inform.bo.InformRemindEditBo;
import com.zhongzheng.modules.inform.domain.InformRemind;
import com.zhongzheng.modules.inform.mapper.InformRemindMapper;
import com.zhongzheng.modules.inform.vo.InformRemindVo;
import com.zhongzheng.modules.inform.service.IInformRemindService;

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

/**
 * 提醒设置Service业务层处理
 *
 * @author ruoyi
 * @date 2021-11-25
 */
@Service
public class InformRemindServiceImpl extends ServiceImpl<InformRemindMapper, InformRemind> implements IInformRemindService {


    @Autowired
    private IGoodsService iGoodsService;

    @Autowired
    private ICourseEducationTypeService courseEducationTypeService;

    @Autowired
    private ICourseProjectTypeService courseProjectTypeService;

    @Autowired
    private ICourseBusinessService courseBusinessService;

    @Autowired
    private IClassGradeSysService classGradeSysService;

    @Autowired
    private IClassGradeGoodsService classGradeGoodsService;

    @Autowired
    private IUserStudyRecordService iUserStudyRecordService;

    @Autowired
    private IClassGradeUserService iClassGradeUserService;

    @Autowired
    private IUserUpdateService iUserUpdateService;

    @Autowired
    private IInformRemindBusinessService iInformRemindBusinessService;

    @Override
    public InformRemindVo queryById(Long id){
        InformRemind db = this.baseMapper.selectById(id);
        InformRemindVo informRemindVo = BeanUtil.toBean(db, InformRemindVo.class);
        List<InformRemindBusinessVo> informRemindBusinessVos = iInformRemindBusinessService.selectBusiness(informRemindVo.getId());
        informRemindVo.setInformRemindBusiness(informRemindBusinessVos);
        return informRemindVo;
    }

    @Override
    public List<InformRemindVo> queryList(InformRemindQueryBo bo) {
        LambdaQueryWrapper<InformRemind> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSort() != null, InformRemind::getSort, bo.getSort());
        lqw.eq(StrUtil.isNotBlank(bo.getRemind()), InformRemind::getRemind, bo.getRemind());
        lqw.eq(StrUtil.isNotBlank(bo.getLogic()), InformRemind::getLogic, bo.getLogic());
        lqw.eq(bo.getNoteStatus() != null, InformRemind::getNoteStatus, bo.getNoteStatus());
        lqw.eq(bo.getWayStatus() != null, InformRemind::getWayStatus, bo.getWayStatus());
        List<InformRemindVo> informRemindVos = entity2Vo(this.list(lqw));
        for (InformRemindVo informRemindVo : informRemindVos) {
            List<InformRemindBusinessVo> informRemindBusinessVos = iInformRemindBusinessService.selectBusiness(informRemindVo.getId());
            informRemindVo.setInformRemindBusiness(informRemindBusinessVos);
        }
        return informRemindVos;
    }

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

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

    @Override
    public Boolean updateByEditBo(InformRemindEditBo bo) {
        InformRemind update = BeanUtil.toBean(bo, InformRemind.class);
        validEntityBeforeSave(update);
        update.setUpdateTime(DateUtils.getNowTime());
        if (bo.getBusinessId() != null) {
            LambdaQueryWrapper<InformRemindBusiness> lqw = Wrappers.lambdaQuery();
            lqw.eq(InformRemindBusiness::getRemindId, bo.getId());
            iInformRemindBusinessService.remove(lqw);
            for (Long aLong : bo.getBusinessId()) {
                InformRemindBusinessAddBo informRemindBusinessAddBo = new InformRemindBusinessAddBo();
                informRemindBusinessAddBo.setRemindId(bo.getId());
                informRemindBusinessAddBo.setBusinessId(aLong);
                informRemindBusinessAddBo.setCreateTime(DateUtils.getNowTime());
                informRemindBusinessAddBo.setUpdateTime(DateUtils.getNowTime());
                iInformRemindBusinessService.insertByAddBo(informRemindBusinessAddBo);
            }
        }
        return this.updateById(update);
    }

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

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

    @Override
    public List<CourseUserEducationTypeVo> listGradeService(ClassGradeQueryBo bo) {
        CourseEducationTypeQueryBo courseEducationTypeQueryBo = new CourseEducationTypeQueryBo();
        courseEducationTypeQueryBo.setStatus(bo.getStatus());
        List<CourseUserEducationTypeVo> courseUserEducationTypeVos = entity2EducationVo(courseEducationTypeService.queryList(courseEducationTypeQueryBo));
        for (CourseUserEducationTypeVo courseUserEducationTypeVo : courseUserEducationTypeVos) {
            CourseProjectTypeQueryBo courseProjectTypeQueryBo = new CourseProjectTypeQueryBo();
            courseProjectTypeQueryBo.setStatus(bo.getStatus());
            courseProjectTypeQueryBo.setEducationId(Convert.toInt(courseUserEducationTypeVo.getId()));
            List<CourseUserProjectTypeVo> courseUserProjectTypeVos = entity2ServiceProjectVo(courseProjectTypeService.queryList(courseProjectTypeQueryBo));
            if (CollectionUtils.isNotEmpty(courseUserProjectTypeVos)) {
                for (CourseUserProjectTypeVo courseUserProjectTypeVo : courseUserProjectTypeVos) {
                    CourseBusinessQueryBo courseBusinessQueryBo = new CourseBusinessQueryBo();
                    courseBusinessQueryBo.setStatus(bo.getStatus());
                    courseBusinessQueryBo.setProjectId(Convert.toInt(courseUserProjectTypeVo.getId()));
                    List<CourseUserBusinessVo> courseUserBusinessVos = entity2ServiceBusinessVo(courseBusinessService.queryList(courseBusinessQueryBo));
                    courseUserProjectTypeVo.setCourseUserBusiness(courseUserBusinessVos);
                }
            }
            courseUserEducationTypeVo.setCourseUserProjectTypeVo(courseUserProjectTypeVos);
        }
        return courseUserEducationTypeVos;
    }

    private List<CourseUserEducationTypeVo> entity2EducationVo(Collection<CourseEducationTypeVo> collection) {
        List<CourseUserEducationTypeVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, CourseUserEducationTypeVo.class))
                .collect(Collectors.toList());
        return voList;
    }

    private List<CourseUserProjectTypeVo> entity2ServiceProjectVo(Collection<CourseProjectTypeVo> collection) {
        List<CourseUserProjectTypeVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, CourseUserProjectTypeVo.class))
                .collect(Collectors.toList());
        return voList;
    }

    private List<CourseUserBusinessVo> entity2ServiceBusinessVo(Collection<CourseBusinessVo> collection) {
        List<CourseUserBusinessVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, CourseUserBusinessVo.class))
                .collect(Collectors.toList());
        return voList;
    }
}
