package com.ssj.sys.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ssj.bean.common.framework.core.domain.Response;
import com.ssj.bean.sys.train.bean.ExamInfo;
import com.ssj.bean.sys.train.bean.VideoFile;
import com.ssj.bean.sys.train.bean.VideoInfo;
import com.ssj.framework.basic.common.bean.SplitPage;
import com.ssj.framework.core.common.controller.BaseController;
import com.ssj.framework.core.util.ResponseEntity;
import com.ssj.service.sys.train.service.ExamInfoService;
import com.ssj.service.sys.train.service.VideoFileService;
import com.ssj.service.sys.train.service.VideoInfoService;
import com.ssj.service.sys.train.service.VideoUserService;

@RequestMapping("sys/train")
@Controller
public class TrainController extends BaseController {

	@Autowired
	private ExamInfoService examInfoService;

	@Autowired
	private VideoFileService videoFileService;
	
	@Autowired
	private VideoInfoService videoInfoService;
	
	@Autowired
	private VideoUserService videoUserService;
	
	@RequestMapping("/video/list")
	public String videoList(Model model,
			@RequestParam(required = false, defaultValue = "10") int pageSize,
			@RequestParam(required = false, defaultValue = "1") int pageNo)
			throws Exception {
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("rolType", request.getParameter("rolType")==null?"":request.getParameter("rolType"));
		params.put("module", request.getParameter("module")==null?"":request.getParameter("module"));
		params.put("title", request.getParameter("title")==null?"":request.getParameter("title"));
		params.put("index", request.getParameter("index")==null?"":request.getParameter("index"));
		
		SplitPage sp = new SplitPage();
        sp.setAction(request.getRequestURI());
        sp.setPageNo(pageNo);
        sp.setPageSize(pageSize);
        sp.setParams(params);
        Page<Map<String,Object>> page = videoInfoService.findTrainVideoInfoByPage(params, initPage(pageNo, pageSize));
        List<Map<String,Object>> list=page.getContent();
       
        sp.setRowCnt((int) page.getTotalElements());
        model.addAttribute("list",list);
        model.addAttribute("listNavigatHtml", sp.getSysPaginHtml());
        model.addAttribute("page", sp);
        model.addAttribute("search", params);
		return "sys/train/video_list";
	}
	
	
	@RequestMapping("/video/edit")
	public String videoEdit(Model model,String id)
			throws Exception {
		VideoInfo videoInfo=new VideoInfo();
		if(StringUtils.isNotEmpty(id)) {
			videoInfo=videoInfoService.getById(id);
		}
		model.addAttribute("videoInfo",videoInfo);
		return "sys/train/video_edit";
	}
	

	 @RequestMapping("/video/save")
	 @ResponseBody
	 public Response videoSave(VideoInfo param) throws Exception{
    	Response response = new  Response();
    	if(StringUtils.isNotEmpty(param.getId())) {
	    	VideoInfo videoInfo=videoInfoService.getById(param.getId());
	    	videoInfo.setDescription(param.getDescription());
	    	videoInfo.setIndes(param.getIndes());
	    	videoInfo.setModule(param.getModule());
	    	videoInfo.setRoleType(param.getRoleType());
	    	videoInfo.setStatus(param.getStatus());
	    	videoInfo.setTitle(param.getTitle());
	    	videoInfo.setUpdateTime(new Date());
	    	videoInfoService.save(videoInfo);
    	}else {
    		param.setCreateTime(new Date());
    		videoInfoService.save(param);
    	}
    	response.success();
    	return response;
	 }
	
	
	

	
	@RequestMapping("/pace/list")
	public String paceList(Model model,
			@RequestParam(required = false, defaultValue = "10") int pageSize,
			@RequestParam(required = false, defaultValue = "1") int pageNo)
			throws Exception { 
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("unitName", request.getParameter("unitName")==null?"":request.getParameter("unitName"));
		
		SplitPage sp = new SplitPage();
        sp.setAction(request.getRequestURI());
        sp.setPageNo(pageNo);
        sp.setPageSize(pageSize);
        sp.setParams(params);
        Page<Map<String,Object>> page = videoInfoService.findMerchantTeaNumByPage(params, initPage(pageNo, pageSize));
        List<Map<String,Object>> list=page.getContent();
        for (Map<String, Object> pageMap : list) {
        	Map<String, Object> map=videoUserService.findTrainVideoUserByPage(pageMap);
        	pageMap.putAll(map);
		}
        
        sp.setRowCnt((int) page.getTotalElements());
        model.addAttribute("list",list);
        model.addAttribute("listNavigatHtml", sp.getSysPaginHtml());
        model.addAttribute("page", sp);
        model.addAttribute("search", params);

		return "sys/train/pace_list";
	}
	
	
	
	@RequestMapping("/pace/list/info")
	public String paceListInfo(Model model,
			@RequestParam(required = false, defaultValue = "10") int pageSize,
			@RequestParam(required = false, defaultValue = "1") int pageNo)
			throws Exception { 
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("name", request.getParameter("name")==null?"":request.getParameter("name"));
		params.put("type", request.getParameter("type")==null?"":request.getParameter("type"));
		params.put("libId", request.getParameter("libId")==null?"":request.getParameter("libId"));
		
		SplitPage sp = new SplitPage();
        sp.setAction(request.getRequestURI());
        sp.setPageNo(pageNo);
        sp.setPageSize(pageSize);
        sp.setParams(params);
        Page<Map<String,Object>> page = videoUserService.findTeaInfoNumByPage(params, initPage(pageNo, pageSize));
        List<Map<String,Object>> list=page.getContent();

        sp.setRowCnt((int) page.getTotalElements());
        model.addAttribute("list",list);
        model.addAttribute("listNavigatHtml", sp.getSysPaginHtml());
        model.addAttribute("page", sp);
        model.addAttribute("search", params);

		return "sys/train/pace_list_info";
	}
	
	
	
	@RequestMapping("/import/exam/info")
	@ResponseBody
    public ResponseEntity importDeviceData(HttpServletRequest request, HttpServletResponse response,
    		@RequestParam("file") MultipartFile file,String videoId)
    				throws Exception {
		ResponseEntity responseEntity = new  ResponseEntity();
		try {
			VideoInfo videoInfo=videoInfoService.getById(videoId);
			List<Map<String,String>> datas=ImLibUserController.analysisExFile(file);
			if(datas!=null && datas.size()>0){
				List<ExamInfo> examInfos=new ArrayList<ExamInfo>();
				ExamInfo examInfo=null;
				for (Map<String,String> map : datas) {
					examInfo=new ExamInfo();
					examInfo.setRoleType(videoInfo.getRoleType());
					examInfo.setVideoId(videoInfo.getId());
					examInfo.setExamNum(Integer.valueOf(map.get("data_0")));
					examInfo.setExamType(Integer.valueOf(map.get("data_1")));
					examInfo.setTitle(map.get("data_2"));
					examInfo.setOptions(map.get("data_3"));
					examInfo.setAnswer(map.get("data_4"));
					examInfo.setScore(Integer.valueOf(map.get("data_5")));
					if(map.containsKey("data_6") && StringUtils.isNotEmpty(map.get("data_6"))) {
						examInfo.setId(map.get("data_6"));
					}
					examInfo.setCreateTime(new Date());
					examInfos.add(examInfo);
				}
				examInfoService.save(examInfos);
			}
			
		} catch (Exception e) {
			logger.error("异常", e);
			responseEntity.failure("导入异常："+e.getMessage());
		}
		return responseEntity;
    }
	
	
	
	@RequestMapping("/file/list")
	public String fileList(Model model,
			@RequestParam(required = false, defaultValue = "10") int pageSize,
			@RequestParam(required = false, defaultValue = "1") int pageNo)
			throws Exception {
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("type", request.getParameter("type")==null?"":request.getParameter("type"));
		params.put("rolType", request.getParameter("rolType")==null?"":request.getParameter("rolType"));
		params.put("module", request.getParameter("module")==null?"":request.getParameter("module"));
		params.put("title", request.getParameter("title")==null?"":request.getParameter("title"));
		
		SplitPage sp = new SplitPage();
        sp.setAction(request.getRequestURI());
        sp.setPageNo(pageNo);
        sp.setPageSize(pageSize);
        sp.setParams(params);
        Page<Map<String,Object>> page = videoFileService.findVideoFileByPage(params, initPage(pageNo, pageSize));
        List<Map<String,Object>> list=page.getContent();
       
        sp.setRowCnt((int) page.getTotalElements());
        model.addAttribute("list",list);
        model.addAttribute("listNavigatHtml", sp.getSysPaginHtml());
        model.addAttribute("page", sp);
        model.addAttribute("search", params);
		return "sys/train/file_list";
	}
	
	
	@RequestMapping("/file/edit")
	public String fileEdit(Model model,String id)
			throws Exception {
		VideoFile videoFile=new VideoFile();
		if(StringUtils.isNotEmpty(id)) {
			videoFile=videoFileService.getById(id);
		}
		model.addAttribute("videoFile",videoFile);
		return "sys/train/file_edit";
	}
	

	 @RequestMapping("/file/save")
	 @ResponseBody
	 public Response fileSave(VideoFile param) throws Exception{
    	Response response = new  Response();
    	if(StringUtils.isNotEmpty(param.getId())) {
    		VideoFile videoFile=videoFileService.getById(param.getId());
    		videoFile.setModule(param.getModule());
    		videoFile.setRoleType(param.getRoleType());
    		videoFile.setStatus(param.getStatus());
    		videoFile.setTitle(param.getTitle());
    		videoFile.setFormat(param.getFormat());
    		videoFile.setUpdateTime(new Date());
    		videoFileService.save(videoFile);
    	}else {
    		param.setCreateTime(new Date());
    		videoFileService.save(param);
    	}
    	response.success();
    	return response;
	 }
	
	
	
	
	
}
