package com.ssj.sys.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import com.ssj.bean.common.framework.core.domain.Response;
import com.ssj.bean.conch.conch.domain.ConchDerver;
import com.ssj.bean.sys.homework.domain.HomeworkPicture;
import com.ssj.bean.sys.usercenter.domain.CuratorCenter;
import com.ssj.bean.sys.usercenter.domain.ParentCenter;
import com.ssj.bean.sys.usercenter.domain.SchoolmasterCenter;
import com.ssj.bean.sys.usercenter.domain.TeacherCenter;
import com.ssj.bean.weixin.libmy.domain.LibVip;
import com.ssj.bean.weixin.libmy.domain.TbLibManager;
import com.ssj.bean.weixin.sales.domain.LibVipService;
import com.ssj.bean.weixin.user.domain.User;
import com.ssj.framework.basic.common.bean.SplitPage;
import com.ssj.framework.core.common.controller.BaseController;
import com.ssj.framework.core.util.ResponseConstant;
import com.ssj.framework.core.util.ResponseEntity;
import com.ssj.framework.core.util.StringUtil;
import com.ssj.framework.weixin.util.Collections3;
import com.ssj.framework.weixin.util.DateUtil;
import com.ssj.service.conch.conch.service.ConchDerverService;
import com.ssj.service.sys.homework.service.HomeworkPictureService;
import com.ssj.service.sys.homework.service.HomeworkService;
import com.ssj.service.sys.teacher.OnlineTeacherWeightService;
import com.ssj.service.sys.usercenter.service.UserCenterService;
import com.ssj.service.weixin.library.service.IBookManagerService;
import com.ssj.service.weixin.library.service.ILibVipService;
import com.ssj.service.weixin.sales.service.LibVipServiceService;
import com.ssj.service.weixin.user.service.UserService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * @author Mount Liu
 * @date 2018/9/11
 */
@Controller
@RequestMapping("/sys/usercenter")
public class UserCenterController extends BaseController {

	@Autowired
	private UserCenterService userCenterService;

	@Autowired
	private UserService userService;

	@Autowired
	private HomeworkPictureService homeworkPictureService;

	@Autowired
	private HomeworkService homeworkService;

	@Autowired
	private IBookManagerService managerService;
	
	@Autowired
	private OnlineTeacherWeightService onlineTeacherWeightService;

	@Autowired
	private ConchDerverService conchDerverService;

	@Autowired
	private ILibVipService vipService;

	@Autowired
	private LibVipServiceService libVipServiceService;

	@RequestMapping("/userList")
	public String userList(Model model,
							  @RequestParam(required = false, defaultValue = "10") int pageSize,
							  @RequestParam(required = false, defaultValue = "1") int pageNo) {
		Map<String, Object> params = new HashMap<>(16);
		params.put("LIKE_userName", request.getParameter("LIKE_userName") == null ? "" : request.getParameter("LIKE_userName"));
		SplitPage sp = new SplitPage();
		sp.setAction(request.getRequestURI());
		sp.setPageNo(pageNo);
		sp.setPageSize(pageSize);
		sp.setParams(params);
		try{
			Page<User> page = userService.findByPage(params,initPage(pageNo,pageSize,Sort.Direction.DESC,"userName"));
			sp.setRowCnt((int) page.getTotalElements());
			model.addAttribute("list", page.getContent());
			model.addAttribute("listNavigatHtml", sp.getSysPaginHtml());
			model.addAttribute("page", sp);
			model.addAttribute("search", params);
		}catch (Exception e){
			e.printStackTrace();
		}
		return "sys/usercenter/userList";
	}

	@RequestMapping("/studentList")
	public String studentList(Model model,
							  @RequestParam(required = false, defaultValue = "10") int pageSize,
							  @RequestParam(required = false, defaultValue = "1") int pageNo) {
		Map<String, Object> params = new HashMap<>(16);
		params.put("LIKE_value", request.getParameter("LIKE_value") == null ? "" : request.getParameter("LIKE_value"));
		params.put("child_type", request.getParameter("child_type") == null ? "" : request.getParameter("child_type"));
		params.put("beginDate", request.getParameter("beginDate") == null ? "" : request.getParameter("beginDate"));
		params.put("endDate", request.getParameter("endDate") == null ? "" : request.getParameter("endDate"));
        SplitPage sp = new SplitPage();
		sp.setAction(request.getRequestURI());
		sp.setPageNo(pageNo);
		sp.setPageSize(pageSize);
		sp.setParams(params);
		Page<Map<String, Object>> page = userCenterService.findStudentListByQueryPage(params, initPage(pageNo, pageSize));
		//Page<LibVip> page = vipService.findByPage(params, initPage(pageNo, pageSize, Sort.Direction.DESC, "childName"));
		sp.setRowCnt((int) page.getTotalElements());
		model.addAttribute("list", page.getContent());
		model.addAttribute("listNavigatHtml", sp.getSysPaginHtml());
		model.addAttribute("page", sp);
		model.addAttribute("search", params);
		return "sys/usercenter/studentList";
	}

	@RequestMapping("/studentDetail")
	public String studentDetail(Model model, @RequestParam(defaultValue = "0") String id) {
		Map<String, Object> map = userCenterService.studentDetail(id);
		model.addAttribute("map", map);
		return "sys/usercenter/studentDetail";
	}

	@RequestMapping("/studentEdit")
	public String studentEdit(Model model, String id) throws Exception {
		LibVip libVip = vipService.getById(id);
		model.addAttribute("entity", libVip);
		return "sys/usercenter/studentEdit";
	}

	@RequestMapping("/studentSave")
	@ResponseBody
	public Response studentSave(Model model, LibVip entity) throws Exception {
		Response response = new Response();
		LibVip libVip = vipService.getById(entity.getId());
		LibVipService libVipService = libVipServiceService.findTopByVipIdAndSpeciesOrderByEndTimeDesc(entity.getId(), "S999");
		//设置为永久小孩
		if (entity.getChildType() == 1) {
			LibVipService zybService = libVipServiceService.findTopByVipIdAndSpeciesOrderByEndTimeDesc(entity.getId(), "S003");
			if (libVipService == null) {
				libVipService = new LibVipService();
				libVipService.setStartTime(new Date());
				libVipService.setEndTime(DateUtil.parse_yyyyMMdd("9999-01-01"));
				libVipService.setStat(1);
				libVipService.setVipId(entity.getId());
				libVipService.setSpecies("S999");
				libVipService.setOrderDetailsId("zyb");
				libVipService.setCreateTime(new Date());
				libVipService.setIsJia(0);
				libVipService.setServiceSort(5);
			}
			//在成为永久用户之前，如果之前的作业吧服务已经绑定了版需要将那个馆转移到这里来
			if(zybService != null && StringUtil.isNotBlank(zybService.getLibId())){
				libVipService.setLibId(zybService.getLibId());
				zybService.setLibId(null);
				libVipServiceService.save(zybService);
			}
			libVipServiceService.save(libVipService);
		} else {
			if (libVipService != null) {
				LibVipService zybService = libVipServiceService.findTopByVipIdAndSpeciesOrderByEndTimeDesc(entity.getId(), "S003");
				//如果有原来作业记录，则把原来的图书馆id插入
				if (zybService != null && StringUtil.isNotBlank(libVipService.getLibId())) {
					zybService.setLibId(libVipService.getLibId());
					libVipServiceService.save(zybService);
				}
				libVipServiceService.delete(libVipService);
			}
		}
		libVip.setChildType(entity.getChildType());
		vipService.save(libVip);
		response.success("成功");
		return response;
	}

	@RequestMapping("/schoolmasterList")
	public String schoolmasterList(Model model,
							  @RequestParam(required = false, defaultValue = "10") int pageSize,
							  @RequestParam(required = false, defaultValue = "1") int pageNo) {
		Map<String, Object> params = new HashMap<>(16);
		params.put("LIKE_name", request.getParameter("LIKE_name") == null ? "" : request.getParameter("LIKE_name"));
		SplitPage sp = new SplitPage();
		sp.setAction(request.getRequestURI());
		sp.setPageNo(pageNo);
		sp.setPageSize(pageSize);
		sp.setParams(params);
		Page<SchoolmasterCenter> page = userCenterService.findSchoolmasterListByPage(params, initPage(pageNo, pageSize,Sort.Direction.DESC,"name"));
		sp.setRowCnt((int) page.getTotalElements());
		model.addAttribute("list", page.getContent());
		model.addAttribute("listNavigatHtml", sp.getSysPaginHtml());
		model.addAttribute("page", sp);
		model.addAttribute("search", params);
		return "sys/usercenter/schoolmasterList";
	}

    /**
     * 后台系统-用户管理中心-校长管理   校长详情
     * @param schoolmasterUserId 校长的userId
     */
	@RequestMapping("schoolmasterDetail")
	public String schoolmasterDetail(Model model, @RequestParam String schoolmasterUserId) {
	    userCenterService.findSchoolmasterDetail(model, schoolmasterUserId);
		return "sys/usercenter/schoolmasterDetail";
	}

	/**
	 * 后台系统-用户管理中心-家长管理   家长的分页列表展示
	 * @param pageNo 页数
	 * @param pageSize 每页展示的数量
	 */
    @RequestMapping("parentList")
    public String parentList(Model model,
                                @RequestParam(defaultValue = "1") Integer pageNo,
                                @RequestParam(defaultValue = "10") Integer pageSize) throws Exception {

        HashMap<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("LIKE_userName", request.getParameter("LIKE_userName"));

        Page<ParentCenter> userPage = userCenterService.findParentListByPage(paramsMap, initPage(pageNo, pageSize, Sort.Direction.DESC, "createTime"));

        SplitPage splitPage = new SplitPage();
        splitPage.setPageSize(pageSize);
        splitPage.setPageNo(pageNo);
        splitPage.setAction(request.getRequestURI());
        splitPage.setRowCnt((int) userPage.getTotalElements());

        model.addAttribute("list", userPage.getContent());
        model.addAttribute("page", splitPage);
        model.addAttribute("search", paramsMap);
        model.addAttribute("listNavigatHtml", splitPage.getSysPaginHtml());

        return "sys/usercenter/parentList";
    }

    /**
     * 后台系统-用户管理中心-家长管理   家长详情
     * @param parentUserId 家长的userId
     */
    @RequestMapping("parentDetail")
    public String parentDetail(Model model, @RequestParam String parentUserId) {
        userCenterService.findParentDetail(model, parentUserId);
        return "sys/usercenter/parentDetail";
    }

	/**
	 * 后台系统-用户管理中心-老师管理   老师的分页列表展示
	 * @param pageNo 页数
	 * @param pageSize 每页展示的数量
	 */
    @RequestMapping("teacherList")
	public String teacherList(Model model,
							  @RequestParam(defaultValue = "1") Integer pageNo,
							  @RequestParam(defaultValue = "10") Integer pageSize) {

		HashMap<String, Object> paramsMap = new HashMap<>();
		paramsMap.put("LIKE_name", request.getParameter("LIKE_name"));

		Page<TeacherCenter> userPage = userCenterService.findTeacherListByPage(paramsMap, initPage(pageNo, pageSize, Sort.Direction.DESC, "createTime"));

		SplitPage splitPage = new SplitPage();
		splitPage.setPageSize(pageSize);
		splitPage.setPageNo(pageNo);
		splitPage.setAction(request.getRequestURI());
		splitPage.setRowCnt((int) userPage.getTotalElements());

		model.addAttribute("list", userPage.getContent());
		model.addAttribute("page", splitPage);
		model.addAttribute("search", paramsMap);
		model.addAttribute("listNavigatHtml", splitPage.getSysPaginHtml());

		return "sys/usercenter/teacherList";
	}
	@RequestMapping("teacherDetail")
	public String teacherDetail(Model model, @RequestParam String id) {
		userCenterService.findTeacherDetail(model, id);
		return "sys/usercenter/teacherDetail";
	}
	/**
	 * 后台系统-用户管理中心-馆长管理   馆长的分页列表展示
	 * @param pageNo 页数
	 * @param pageSize 每页展示的数量
	 */
    @RequestMapping("curatorList")
	public String curatorList(Model model,
							  @RequestParam(defaultValue = "1") Integer pageNo,
							  @RequestParam(defaultValue = "10") Integer pageSize) {

		HashMap<String, Object> paramsMap = new HashMap<>();
		paramsMap.put("LIKE_name", request.getParameter("LIKE_name"));

		Page<CuratorCenter> userPage = userCenterService.findCuratorListByPage(paramsMap, initPage(pageNo, pageSize, Sort.Direction.DESC, "createTime"));

		SplitPage splitPage = new SplitPage();
		splitPage.setPageSize(pageSize);
		splitPage.setPageNo(pageNo);
		splitPage.setAction(request.getRequestURI());
		splitPage.setRowCnt((int) userPage.getTotalElements());

		model.addAttribute("list", userPage.getContent());
		model.addAttribute("page", splitPage);
		model.addAttribute("search", paramsMap);
		model.addAttribute("listNavigatHtml", splitPage.getSysPaginHtml());

		return "sys/usercenter/curatorList";
	}

	/**
	 * 后台系统-用户管理中心-贝壳管理   贝壳的分页列表展示
	 * @param pageNo 页数
	 * @param pageSize 每页展示的数量
	 */
	@RequestMapping("conchList")
	public String conchList(Model model,
							  @RequestParam(defaultValue = "1") Integer pageNo,
							  @RequestParam(defaultValue = "10") Integer pageSize) {

		HashMap<String, Object> paramsMap = new HashMap<>(16);
		paramsMap.put("LIKE_value", request.getParameter("LIKE_value"));
		paramsMap.put("use_type", request.getParameter("use_type") == null ? "" : request.getParameter("use_type"));
		Page<Map<String, Object>> userPage = userCenterService.findConchListByQueryPage(paramsMap, initPage(pageNo, pageSize));
		SplitPage splitPage = new SplitPage();
		splitPage.setPageSize(pageSize);
		splitPage.setPageNo(pageNo);
		splitPage.setAction(request.getRequestURI());
		splitPage.setRowCnt((int) userPage.getTotalElements());

		model.addAttribute("list", userPage.getContent());
		model.addAttribute("page", splitPage);
		model.addAttribute("search", paramsMap);
		model.addAttribute("listNavigatHtml", splitPage.getSysPaginHtml());

		return "sys/usercenter/conchList";
	}

	@RequestMapping("/conchEdit")
	public String merchantAdd(Model model,String id)throws Exception {
		ConchDerver conchDerver = conchDerverService.getById(id);
		model.addAttribute("entity", conchDerver);
		return "sys/usercenter/conchEdit";
	}

	@RequestMapping("/conchSave")
	@ResponseBody
	public Response conchSave(Model model, ConchDerver entity) throws Exception {
		Response response = new Response();
		ConchDerver conchDerver = conchDerverService.getById(entity.getId());
		conchDerver.setUseType(entity.getUseType());
		conchDerver.setUseDays(entity.getUseDays());
		conchDerver.setIsGiveInvalid(entity.getIsGiveInvalid());
		conchDerverService.save(conchDerver);
		response.success("成功");
		return response;
	}

	@RequestMapping("/role")
	public String role(Model model,@RequestParam String id){
		model.addAttribute("id", id);
		List<Map<String, Object>> list = userCenterService.findRoleList(id);
		model.addAttribute("list", list);
		return "sys/usercenter/role";
	}

	@RequestMapping("/onlineTeacherAllot")
	public String onlineTeacherAllot(Model model,
						   @RequestParam(required = false, defaultValue = "10") int pageSize,
						   @RequestParam(required = false, defaultValue = "1") int pageNo) {
		Map<String, Object> params = new HashMap<>(16);
		params.put("LIKE_childName", request.getParameter("LIKE_childName") == null ? "" : request.getParameter("LIKE_childName"));
		SplitPage sp = new SplitPage();
		sp.setAction(request.getRequestURI());
		sp.setPageNo(pageNo);
		sp.setPageSize(pageSize);
		sp.setParams(params);
		try {
			Page<Map<String, Object>> page = homeworkService.findHomeworkByPage(params, initPage(pageNo, pageSize));
			sp.setRowCnt((int) page.getTotalElements());
			model.addAttribute("list", page.getContent());
			model.addAttribute("listNavigatHtml", sp.getSysPaginHtml());
			model.addAttribute("page", sp);
			model.addAttribute("search", params);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "sys/usercenter/onlineTeacherAllot";
	}

	/**
	 * 在线老师列表
	 * @param model
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("/onlineTeacherList")
	public String onlineTeacherList(Model model,
									@RequestParam(defaultValue = "1")String id,
									@RequestParam(defaultValue = "1") Integer pageNo,
									@RequestParam(defaultValue = "10") Integer pageSize) {

		HashMap<String, Object> params = new HashMap<>();
		params.put("name", request.getParameter("name") !=null ? request.getParameter("name"):"");
		params.put("id", request.getParameter("id") !=null ? request.getParameter("id") :id);
		Page<Map<String, Object>> userPage = managerService.findKmtTeacherByPage(params, initPage(pageNo, pageSize));
		SplitPage splitPage = new SplitPage();
		splitPage.setPageSize(pageSize);
		splitPage.setPageNo(pageNo);
		splitPage.setAction(request.getRequestURI());
		splitPage.setRowCnt((int) userPage.getTotalElements());
		model.addAttribute("list", userPage.getContent());
		model.addAttribute("page", splitPage);
		model.addAttribute("search", params);
		model.addAttribute("listNavigatHtml", splitPage.getSysPaginHtml());
		return "sys/usercenter/onlineTeacherList";
	}

	@RequestMapping(value = "/saveOnlineTeacher", method = RequestMethod.POST)
	@ResponseBody
	public Response saveOnlineTeacher(String id, @RequestParam(value = "teacherIds[]") String[] teacherIds) {
		Response response = new Response();
		if (teacherIds.length == 1) {
			List<HomeworkPicture> homeworkPictures = homeworkPictureService.findNoFeedbackByHomeworkId(id);
			if(CollectionUtils.isNotEmpty(homeworkPictures)) {
				//清除分配前老师的待批改数量
				TbLibManager correctTeacher = managerService.findByUserIdAndType(homeworkPictures.get(0).getTeacherId(), 8);
				if(correctTeacher != null) {
					//更新原批改老师的实时权重和批改图片总数
					onlineTeacherWeightService.updateRealTimeWeightAndHomeworkSum(homeworkPictures.get(0).getVipId(), correctTeacher, homeworkPictures.size() * -1);
				}
				correctTeacher = managerService.findByUserIdAndType(teacherIds[0], 8);
				if(correctTeacher != null) {
					//更新选定批改老师的实时权重和批改图片总数
					onlineTeacherWeightService.updateRealTimeWeightAndHomeworkSum(homeworkPictures.get(0).getVipId(), correctTeacher, homeworkPictures.size());
				}
				for (HomeworkPicture homeworkPicture : homeworkPictures) {
					homeworkPicture.setTeacherId(teacherIds[0]);
				}
				homeworkPictureService.save(homeworkPictures);
			}
			response.success();
		} else {
			response.failure("不能选多个老师");
		}
		return response;
	}

	@RequestMapping(value = "/getHomeworkOnlineTeacher", method = RequestMethod.POST)
	@ResponseBody
	public String getHomeworkOnlineTeacher(String id) {
		String teacherId = "";
		List<HomeworkPicture> homeworkPictures = homeworkPictureService.findAllByHomeworkId(id);
		if (homeworkPictures.size() > 0) {
			teacherId = homeworkPictures.get(0).getTeacherId();
		}
		return teacherId;
	}


	@RequestMapping(value = "/untieDerver", method = RequestMethod.POST)
	@ResponseBody
	public ResponseEntity untieDerver(@RequestParam(name = "derver") String derver) {
		ResponseEntity responseEntity = new ResponseEntity();
		try {
			responseEntity =conchDerverService.untyingParents(derver);
			responseEntity.success();
		} catch (Exception e) {
			logger.error("解绑小塾异常", e);
			responseEntity.failure(ResponseConstant.CODE_500, "解绑小塾异常");
		}
		return responseEntity;
	}

	/**
	 * 解除质检员角色
	 * @param userId
	 * @return
	 */

	@RequestMapping(value = "/untieRole", method = RequestMethod.POST)
	@ResponseBody
	public ResponseEntity untieRole(@RequestParam(name = "userId") String userId) {
		ResponseEntity responseEntity = new ResponseEntity();
		try {
			responseEntity =userCenterService.untieRole(userId);
		} catch (Exception e) {
			logger.error("解除质检员角色异常", e);
			responseEntity.failure(ResponseConstant.CODE_500, "解除质检员角色异常");
		}
		return responseEntity;
	}
	@RequestMapping("/addChecker")
	public String addChecker(Model model,String id)throws Exception {
		List<TbLibManager> managers = managerService.findAllByUserIdAndTypeIn(id, new int[]{8, 11, 12, 13, 14});
		if (managers.size() == 1) {
			TbLibManager manager = managers.get(0);
			model.addAttribute("entity", manager);
		}
		return "sys/usercenter/addChecker";
	}
	/**
	 * 添加质检员角色
	 * @param userId
	 * @return
	 */

	@RequestMapping(value = "/addCheckerSave", method = RequestMethod.POST)
	@ResponseBody
	public ResponseEntity addCheckerSave(@RequestParam(name = "userId") String userId, @RequestParam(name = "name") String name, @RequestParam(name = "subject") String subject,
									 @RequestParam(name = "phone") String phone) {
		ResponseEntity responseEntity = new ResponseEntity();
		try {
			responseEntity = userCenterService.addChecker(userId, name, subject, phone);
		} catch (Exception e) {
			logger.error("添加质检员角色异常", e);
			responseEntity.failure(ResponseConstant.CODE_500, "添加质检员角色异常");
		}
		return responseEntity;
	}

	@RequestMapping("/correctUserList")
	public String correctUserList(Model model,
							  @RequestParam(required = false, defaultValue = "10") int pageSize,
							  @RequestParam(required = false, defaultValue = "1") int pageNo) {
		Map<String, Object> params = new HashMap<>(16);
		params.put("LIKE_value", request.getParameter("LIKE_value") == null ? "" : request.getParameter("LIKE_value"));
		params.put("type", request.getParameter("type") == null ? "" : request.getParameter("type"));
		params.put("isOnline", request.getParameter("isOnline") == null ? "" : request.getParameter("isOnline"));
		params.put("isBanned", request.getParameter("isBanned") == null ? "" : request.getParameter("isBanned"));
		params.put("isCancelled", request.getParameter("isCancelled") == null ? "" : request.getParameter("isCancelled"));
		params.put("types", Arrays.asList(8, 11, 12, 13, 14));
		params.put("state", 1);
		SplitPage sp = new SplitPage();
		sp.setAction(request.getRequestURI());
		sp.setPageNo(pageNo);
		sp.setPageSize(pageSize);
		sp.setParams(params);
		Map<String, Object> data = userCenterService.findUserRoleByPage(params, pageNo, pageSize);
		sp.setRowCnt((int) data.get("total"));
		model.addAttribute("list", data.get("list"));
		model.addAttribute("listNavigatHtml", sp.getSysPaginHtml());
		model.addAttribute("page", sp);
		model.addAttribute("search", params);
		return "sys/usercenter/correctUserList";
	}

	@RequestMapping("/vipStudentList")
	public String vipStudentList(Model model,
							  @RequestParam(required = false, defaultValue = "10") int pageSize,
							  @RequestParam(required = false, defaultValue = "1") int pageNo) {
		Map<String, Object> params = new HashMap<>(16);
		params.put("LIKE_value", request.getParameter("LIKE_value") == null ? "" : request.getParameter("LIKE_value"));
		params.put("LIKE_value2", request.getParameter("LIKE_value2") == null ? "" : request.getParameter("LIKE_value2"));
		params.put("child_type", request.getParameter("child_type") == null ? "" : request.getParameter("child_type"));
		params.put("beginDate", request.getParameter("beginDate") == null ? "" : request.getParameter("beginDate"));
		params.put("endDate", request.getParameter("endDate") == null ? "" : request.getParameter("endDate"));
		SplitPage sp = new SplitPage();
		sp.setAction(request.getRequestURI());
		sp.setPageNo(pageNo);
		sp.setPageSize(pageSize);
		sp.setParams(params);
		Page<Map<String, Object>> page = userCenterService.findVipStudentList(params, initPage(pageNo, pageSize));
		List<Map<String, Object>> list = page.getContent();
		if(Collections3.isNotEmpty(list)){
			for (Map<String,Object> map:list){
				params.put("userId",map.get("id"));
				map.put("childList",userCenterService.findVipListByUserId(params));
			}
		}
		sp.setRowCnt((int) page.getTotalElements());
		model.addAttribute("list", list);
		model.addAttribute("listNavigatHtml", sp.getSysPaginHtml());
		model.addAttribute("page", sp);
		model.addAttribute("search", params);
		return "sys/usercenter/vipStudentList";
	}

	@RequestMapping(value = "/exportVipData", method = RequestMethod.POST)
	@ResponseBody
	public void exportVipData(Model model, HttpServletRequest request, HttpServletResponse response) {
		String sysVersion=model.asMap().getOrDefault("sys_version_val","1").toString();

		ServletOutputStream os = null;
		try {
			String libName="用户信息";
			os = response.getOutputStream(); //获得输出流
			response.reset();   //清空输出流
			String fileName = new String(libName.getBytes("gb2312"), "ISO8859-1") +".xlsx";
			response.setHeader("Content-disposition", "attachment; filename="+ fileName); //设定输出文件头
			response.setContentType("application/vnd.ms-excel;charset=UTF-8");
			response.setCharacterEncoding("utf-8");
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("LIKE_value", request.getParameter("LIKE_value") == null ? "" : request.getParameter("LIKE_value"));
			params.put("LIKE_value2", request.getParameter("LIKE_value2") == null ? "" : request.getParameter("LIKE_value2"));
			params.put("child_type", request.getParameter("child_type") == null ? "" : request.getParameter("child_type"));
			params.put("beginDate", request.getParameter("beginDate") == null ? "" : request.getParameter("beginDate"));
			params.put("endDate", request.getParameter("endDate") == null ? "" : request.getParameter("endDate"));

			Page<Map<String, Object>> page = userCenterService.findVipStudentList(params, initPage(1, 100000));
			List<Map<String, Object>> list = page.getContent();
			if(Collections3.isNotEmpty(list)){
				for (Map<String,Object> map:list){
					params.put("userId",map.get("id"));
					map.put("childList",userCenterService.findVipListByUserId(params));
				}
			}
			List<ExcelExportEntity> entity = new ArrayList<ExcelExportEntity>();
			//构造对象等同于@Excel
			ExcelExportEntity excelentity = new ExcelExportEntity("用户昵称", "user_name");
			excelentity.setNeedMerge(true);
			entity.add(excelentity);
			ExcelExportEntity excelentity2 = new ExcelExportEntity("电话号码", "mobile");
			excelentity2.setNeedMerge(true);
			entity.add(excelentity2);
			ExcelExportEntity excelentity3 = new ExcelExportEntity("创建时间", "create_time");
			excelentity3.setNeedMerge(true);
			entity.add(excelentity3);
			ExcelExportEntity excelentity5 = new ExcelExportEntity(null, "childList");
			List<ExcelExportEntity> temp = new ArrayList<ExcelExportEntity>();
			temp.add(new ExcelExportEntity("名字", "child_name"));
			temp.add(new ExcelExportEntity("性别", "sex"));
			temp.add(new ExcelExportEntity("电话", "telephone"));
			temp.add(new ExcelExportEntity("学校", "school"));
			temp.add(new ExcelExportEntity("校区", "league_name"));
			temp.add(new ExcelExportEntity("年级", "grade"));
			temp.add(new ExcelExportEntity("家长", "parent_name"));
			temp.add(new ExcelExportEntity("激活时间", "activate_time"));
			//构造List等同于@ExcelCollection
			excelentity5.setList(temp);
			entity.add(excelentity5);
			//把我们构造好的bean对象放到params就可以了
			List<Map<String, Object>> list2 = new ArrayList<>();
			list2.addAll(list);
			Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(libName, libName, ExcelType.XSSF), entity,
					list2);
			workbook.write(os);
		}catch (IOException e) {
			logger.info("下载出现异常--->"+e);
		}catch (Exception e1){
			logger.info("下载出现异常--->"+e1);
		}finally{
			if(os != null){
				try {
					os.flush();
					os.close();
				} catch (IOException e) {
					logger.info("下载出现异常--->"+e);
				}
			}
		}
	}
}
