package com.ssj.service.sys.fx.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ssj.bean.conch.curator.dto.CuratorTeacherListDto;
import com.ssj.framework.core.util.Constant;
import com.ssj.framework.weixin.util.Collections3;
import com.ssj.service.mapper.CuratorMapper;
import com.ssj.service.sys.fx.service.SysCuratorService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author sh
 * @className SysCuratorServiceImpl
 * @description TODO
 * @date 2020/6/17
 */
@Service
public class SysCuratorServiceImpl implements SysCuratorService {

    @Autowired
    private CuratorMapper curatorMapper;

    @Override
    public Map<String, Object> findIndexData(String libId) {
        Map<String, Object> data = new HashMap<>(4);
        //总学生数
        com.baomidou.mybatisplus.extension.plugins.pagination.Page   page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page(1, 1);
        Map<String, Object> params = new HashMap<>(2);
        params.put("libId",libId);
        curatorMapper.findPageStudentList(page, params);
        data.put("studentCount",page.getTotal());
        //老师总数
        Integer[] types = {Constant.TEACHER_TYPE, Constant.LIB_TEACHER_TYPE, Constant.ORG_TEACHER_TYPE};
        List<CuratorTeacherListDto> teacherList = curatorMapper.findCuratorTeacherList(libId ,Arrays.asList(types), null);
        data.put("teacherCount",teacherList.size());
        //上传作业学生人数
        data.put("uploadHomeworkSum",curatorMapper.getUploadHomeworkSumByLibId(libId));
        //班级总数
        List<Map<String, Object>> list = curatorMapper.findClassListBySys(page,libId,"","");
        data.put("classCount",page.getTotal());
        return data;
    }

    @Override
    public Map<String, Object> studentIndex(String libId,  Map<String, Object> params) {
        Map<String, Object> data = new HashMap<>(2);
        int pageNo = Integer.parseInt(params.get("pageNo").toString());
        int pageSize = Integer.parseInt(params.get("pageSize").toString());
        com.baomidou.mybatisplus.extension.plugins.pagination.Page   page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page(pageNo, pageSize);
        List<Map<String, Object>> studentList =  curatorMapper.findPageStudentList(page,params);
        if(Collections3.isNotEmpty(studentList)){
            for(Map<String, Object> map:studentList){
                //查询所有的服务('S003', 'S999')
                params.put("vipId",map.get("vipId"));
                map.put("serviceList",curatorMapper.findServiceListByVipId(params));
            }
        }
        data.put("list",studentList);
        data.put("total",page.getTotal());
        return data;
    }

    @Override
    public Map<String, Object> teacherIndex(String libId, int pageSize, int pageNo, String searchValue,String classId) {
        Map<String, Object> data = new HashMap<>(2);
        com.baomidou.mybatisplus.extension.plugins.pagination.Page   page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page(pageNo, pageSize);
        Integer[] types = {Constant.TEACHER_TYPE, Constant.LIB_TEACHER_TYPE, Constant.ORG_TEACHER_TYPE};
        List<CuratorTeacherListDto> teacherList = curatorMapper.findCuratorTeacherListBySys(page,libId ,Arrays.asList(types), searchValue,classId);
        data.put("list",teacherList);
        data.put("total",page.getTotal());
        return data;
    }

    @Override
    public Map<String, Object> classIndex(String libId, int pageSize, int pageNo, String searchValue,String teacherId) {
        Map<String, Object> data = new HashMap<>(2);
        com.baomidou.mybatisplus.extension.plugins.pagination.Page   page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page(pageNo, pageSize);
        List<Map<String, Object>> list = curatorMapper.findClassListBySys(page,libId,searchValue,teacherId);
        data.put("list",list);
        data.put("total",page.getTotal());
        return data;
    }

    @Override
    public Map<String, Object> homeworkIndex(String libId, int pageSize, int pageNo, Map<String, Object> params) {
        Map<String, Object> data = new HashMap<>(2);
        com.baomidou.mybatisplus.extension.plugins.pagination.Page   page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page(pageNo, pageSize);
        page.setSearchCount(false);

        List<Map<String, Object>> list = curatorMapper.homeworkIndex(page,params.get("libId").toString(),params.get("queryDate").toString(),params.get("searchValue").toString(), MapUtils.getString(params,"queryEndDate"));
        data.put("list",list);
        Page page2 = new Page(1,-1);
        List<Map<String, Object>> list2 = curatorMapper.homeworkIndex(page2,params.get("libId").toString(),params.get("queryDate").toString(),params.get("searchValue").toString(), MapUtils.getString(params,"queryEndDate"));
        page.setTotal(list2.size());
        data.put("total",page.getTotal());
        return data;
    }

    @Override
    public Map<String, Object> allLibIndex(String queryDate) {
        return  curatorMapper.allLibIndex(queryDate);
    }

    @Override
    public Map<String, Object> searchTop(String startDate, String endDate, int pageNo, int pageSize, String subject) {
        Map<String, Object> data = new HashMap<>(2);
        com.baomidou.mybatisplus.extension.plugins.pagination.Page   page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page(pageNo, pageSize);
        page.setSearchCount(false);
        List<Map<String, Object>> list = curatorMapper.searchHomeworkAllTop(page,startDate,endDate,subject);
        data.put("list",list);
        Page page1 = new Page(1,-1);
        page1.setSearchCount(false);
        List<Map<String, Object>> listTotal = curatorMapper.searchHomeworkAllTop(page1,startDate,endDate,subject);
        data.put("total",listTotal.size());
        return data;
    }

    @Override
    public Map<String, Object> searchActiveTop(String startDate, String endDate, int pageNo, int pageSize) {
        Map<String, Object> data = new HashMap<>(2);
        com.baomidou.mybatisplus.extension.plugins.pagination.Page   page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page(pageNo, pageSize);
        List<Map<String, Object>> list = curatorMapper.searchActiveTop(startDate,endDate);
        data.put("total",list.size());
        List<Map<String, Object>> lastList = new ArrayList<>();
        if(pageNo >1){
            lastList =list.subList((pageNo-1)*pageSize,pageNo*pageSize);
        }else{
            lastList.addAll(list);
        }
        data.put("list",lastList);
        return data;
    }

    private int objectToInt(Object o){

        return  Integer.parseInt(o.toString());
    }
}
