package com.koobietech.eas.service.impl;

import com.koobietech.eas.common.result.PageDataResult;
import com.koobietech.eas.common.utils.SecurityManager;
import com.koobietech.eas.dao.dto.GroupStudentsDto;
import com.koobietech.eas.dao.mapper.GroupStudentsMapper;
import com.koobietech.eas.dao.pojo.GroupStudentsPojo;
import com.koobietech.eas.mbg.mapper.EasEduClassMapper;
import com.koobietech.eas.mbg.mapper.EasEduCltRelationMapper;
import com.koobietech.eas.mbg.model.EasEduClass;
import com.koobietech.eas.mbg.model.EasEduClassExample;
import com.koobietech.eas.mbg.model.EasEduCltRelation;
import com.koobietech.eas.mbg.model.EasEduCltRelationExample;
import com.koobietech.eas.service.EasEduClassService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Stream;

@Service
public class EasEduClassServiceImpl implements EasEduClassService {

    @Resource
    EasEduClassMapper easEduClassMapper;

    @Resource
    GroupStudentsMapper  groupStudentsMapper;

    @Resource
    EasEduCltRelationMapper easEduCltRelationMapper;

    @Override
    public int addClass(EasEduClass easEduClass) {
        easEduClass.setCreateTime(new Date());
        easEduClass.setModifyTime(new Date());
        return easEduClassMapper.insert(easEduClass);
    }

    @Override
    public int updateClass(EasEduClass easEduClass) {
        return easEduClassMapper.updateByPrimaryKeySelective( easEduClass );
    }

    @Override
    public int deleteClass(Long id) {
        return  easEduClassMapper.deleteByPrimaryKey( id );
    }

    @Override
    public EasEduClass selectId(Long id) {
        return easEduClassMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<EasEduClass> selectTotal() {
        return easEduClassMapper.selectByExample(null);
    }

    @Override
    public PageDataResult selectCondition(EasEduClass easEduClass) {
        EasEduClassExample easEduClassExample = new EasEduClassExample();
        EasEduClassExample.Criteria criteria = easEduClassExample.createCriteria();
        if ( Objects.nonNull(easEduClass) ) {
            if (Objects.nonNull(easEduClass.getId())) {
                criteria.andIdEqualTo(easEduClass.getId());
            }
            if (StringUtils.hasText(easEduClass.getName())) {
                criteria.andNameLike("%" + easEduClass.getName() + "%");
            }
            if (Objects.nonNull(easEduClass.getManageId()) && easEduClass.getManageId() > 0){
                criteria.andManageIdEqualTo(easEduClass.getManageId());
            }
            if (Objects.nonNull(easEduClass.getAssistantId()) && easEduClass.getAssistantId() > 0){
                criteria.andAssistantIdEqualTo(easEduClass.getAssistantId());
            }
            if (Objects.nonNull(easEduClass.getCreateTime())) {
                criteria.andCreateTimeGreaterThan(easEduClass.getCreateTime());
            }
            if (Objects.nonNull(easEduClass.getModifyTime())) {
                criteria.andCreateTimeGreaterThan(easEduClass.getModifyTime());
            }
            if (Objects.nonNull(easEduClass.getCreateUid()) && easEduClass.getCreateUid() > 0) {
                criteria.andCreateUidEqualTo(easEduClass.getCreateUid());
            }
            if (StringUtils.hasText(easEduClass.getDisabled())) {
                criteria.andDisabledEqualTo(easEduClass.getDisabled());
            }
        }
        List<EasEduClass> easEduClasses = easEduClassMapper.selectByExample(easEduClassExample);
        long l = easEduClassMapper.countByExample(easEduClassExample);
        return PageDataResult.init(easEduClasses,l);
    }

    @Override
    public List<EasEduCltRelation> queryClassStudentById(int classId) {
        EasEduCltRelationExample easEduCltRelationExample = new EasEduCltRelationExample();
        easEduCltRelationExample.createCriteria().andClassIdEqualTo(Long.valueOf(classId));
        return easEduCltRelationMapper.selectByExample(easEduCltRelationExample);
    }

    @Override
    public List<GroupStudentsPojo> queryClassStudent(GroupStudentsDto dto) {
        List<GroupStudentsPojo> students = groupStudentsMapper.getStudents(dto);
        return students;
    }

    @Override
    public boolean updateGroupStudentForm(Integer groupId, Integer[] studentIds) {
        EasEduCltRelationExample easEduCltRelationExample = new EasEduCltRelationExample();
        EasEduCltRelationExample.Criteria criteria = easEduCltRelationExample.createCriteria();
        criteria.andClassIdEqualTo(groupId.longValue());
        List<EasEduCltRelation> easEduCltRelations = easEduCltRelationMapper.selectByExample(easEduCltRelationExample);
        ArrayList<Integer> oldArray = new ArrayList<>();
        for (EasEduCltRelation existingStudent : easEduCltRelations) {
            oldArray.add(existingStudent.getStudentId());
        }
        //insert 从set2中删除在set1中出现的元素
        Set<Integer> set1 = new HashSet<>(oldArray);
        Set<Integer> set2 = new HashSet<>(Arrays.asList(studentIds));
        set2.removeAll(set1);
        Integer[] studentList = set2.toArray(new Integer[0]);
        //delete
        Set<Integer> set3 = new HashSet<>(oldArray);
        Set<Integer> set4 = new HashSet<>(Arrays.asList(studentIds));
        set3.removeAll(set4);
        Integer[] studentDel = set3.toArray(new Integer[0]);
        //update
        Set<Integer> set6 = new HashSet<>(oldArray);
        Set<Integer> set7 = new HashSet<>(Arrays.asList(studentIds));
        set1.removeAll(set2);
        Integer[] studentUpdate = set1.toArray(new Integer[0]);
        //更新 实际没啥用, 放这里看着好看
        for (Integer i : studentUpdate) {
            Stream<EasEduCltRelation> easEduCltRelationStream = easEduCltRelations.stream().filter(easEduCltRelation -> {
                return easEduCltRelation.getStudentId().equals(i);
            });
            easEduCltRelationMapper.updateByPrimaryKeySelective(easEduCltRelationStream.findFirst().get());
        }
        //删除
        if (studentDel.length > 0) {
            EasEduCltRelationExample delExample = new EasEduCltRelationExample();
            EasEduCltRelationExample.Criteria criteriaDel = delExample.createCriteria();
            criteriaDel.andClassIdEqualTo(groupId.longValue());
            criteriaDel.andStudentIdIn(Arrays.asList(studentDel));
            easEduCltRelationMapper.deleteByExample(delExample);
        }
        //新增
        for (Integer studentId: studentList) {
            EasEduCltRelation easEduCltRelation = new EasEduCltRelation();
            easEduCltRelation.setClassId(groupId.longValue());
            easEduCltRelation.setStudentId(studentId);
            easEduCltRelation.setCreateTime(new Date());
            easEduCltRelation.setCreateUid( SecurityManager.getLoginUid().intValue() );
            easEduCltRelation.setType("正式");
            easEduCltRelationMapper.insert(easEduCltRelation);
        }
        return true;
    }

    @Override
    public boolean groupStudentDelete(Integer rowId) {
        return easEduCltRelationMapper.deleteByPrimaryKey(rowId) == 1;
    }

    @Override
    public boolean groupStudentUpdate(EasEduCltRelation easEduCltRelation) {
        EasEduCltRelation updateEduCltRelation = new EasEduCltRelation();
        updateEduCltRelation.setId(easEduCltRelation.getId());
        updateEduCltRelation.setType(easEduCltRelation.getType());
        return easEduCltRelationMapper.updateByPrimaryKeySelective(updateEduCltRelation) == 1;
    }
}
