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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.modules.exam.vo.ExamUserApplyVo;
import com.zhongzheng.modules.grade.bo.ClassGradeUserQueryBo;
import com.zhongzheng.modules.grade.mapper.ClassGradeUserMapper;
import com.zhongzheng.modules.grade.vo.ClassGradeUserGoodsVo;
import com.zhongzheng.modules.inform.bo.InformQueryBo;
import com.zhongzheng.modules.inform.domain.Inform;
import com.zhongzheng.modules.inform.mapper.InformMapper;
import com.zhongzheng.modules.inform.service.IInformCourseService;
import com.zhongzheng.modules.inform.service.IInformExamService;
import com.zhongzheng.modules.inform.service.IInformService;
import com.zhongzheng.modules.inform.vo.InformVo;
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.InformUserAddBo;
import com.zhongzheng.modules.inform.bo.InformUserQueryBo;
import com.zhongzheng.modules.inform.bo.InformUserEditBo;
import com.zhongzheng.modules.inform.domain.InformUser;
import com.zhongzheng.modules.inform.mapper.InformUserMapper;
import com.zhongzheng.modules.inform.vo.InformUserVo;
import com.zhongzheng.modules.inform.service.IInformUserService;

import javax.swing.plaf.nimbus.State;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 通知绑定学员Service业务层处理
 *
 * @author ruoyi
 * @date 2021-11-23
 */
@Service
public class InformUserServiceImpl extends ServiceImpl<InformUserMapper, InformUser> implements IInformUserService {

    @Autowired
    private ClassGradeUserMapper classGradeUserMapper;

    @Autowired
    private InformMapper informMapper;

    @Autowired
    private IInformService informService;

    @Autowired
    private IInformExamService iInformExamService;

 @Autowired
    private IInformCourseService iInformCourseService;

    @Override
    public InformUserVo queryById(Long id){
        InformUser db = this.baseMapper.selectById(id);
        InformUserVo informUserVo = BeanUtil.toBean(db, InformUserVo.class);
        if (informUserVo.getSystemStatus() == 2) {
            InformVo informVo = informService.queryById(informUserVo.getInformId());
            informVo.setInformExamVo(iInformExamService.queryExam(informVo.getInformId()));
            informVo.setInformCourseVo(iInformCourseService.queryCourse(informVo.getInformId()));
            informUserVo.setInformVo(informVo);
        }
        return informUserVo;
    }

    @Override
    public List<InformUserVo> queryList(InformUserQueryBo bo) {
        LambdaQueryWrapper<InformUser> lqw = Wrappers.lambdaQuery();
        List<Long> status = new ArrayList<>();
        status.add(1L);
        status.add(2L);
        lqw.eq(bo.getUserId() != null, InformUser::getUserId, bo.getUserId());
        lqw.eq(bo.getSendTime() != null, InformUser::getSendTime, bo.getSendTime());
        lqw.eq(bo.getSendStatus() != null, InformUser::getSendStatus, bo.getSendStatus());
        lqw.eq(bo.getReceiptStatus() != null, InformUser::getReceiptStatus, bo.getReceiptStatus());
        lqw.eq( InformUser::getSendStatus, 1);
        lqw.in( InformUser::getSystemStatus, status);
        lqw.orderByDesc(InformUser::getSendTime);
        List<InformUserVo> informUserVos = entity2Vo(this.list(lqw));
        for (InformUserVo informUserVo : informUserVos) {
            if (informUserVo.getSystemStatus() == 2) {
                InformVo informVo = informService.queryById(informUserVo.getInformId());
                informVo.setInformExamVo(iInformExamService.queryExam(informVo.getInformId()));
                informVo.setInformCourseVo(iInformCourseService.queryCourse(informVo.getInformId()));
                informUserVo.setInformVo(informVo);
            }
        }
        return informUserVos;
    }

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

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

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

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

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

    @Override
    public List<InformUserVo> queryUserList(InformUserQueryBo bo) {
        List<InformUserVo> informUserVos = baseMapper.queryUserList(bo);
        for (InformUserVo informUserVo : informUserVos) {
            ClassGradeUserQueryBo classGradeUserQueryBo = new ClassGradeUserQueryBo();
            classGradeUserQueryBo.setUserId(informUserVo.getUserId());
            List<Integer> status = new ArrayList<>();
            status.add(1);
            classGradeUserQueryBo.setStatus(status);
            List<ClassGradeUserGoodsVo> classGradeUserGoodsVos = classGradeUserMapper.listUser(classGradeUserQueryBo);
            List<String> className = new ArrayList<>();
            for (ClassGradeUserGoodsVo classGradeUserGoodsVo : classGradeUserGoodsVos) {

                className.add(classGradeUserGoodsVo.getGradeName());
            }
            informUserVo.setClassName(className);
        }
        return informUserVos;
    }

    @Override
    public List<ExamUserApplyVo> listUserApply() {
        return baseMapper.listUserApply();
    }
}
