/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.workpartetag.web;

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

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

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.MyBeanUtils;
import com.jeeplus.common.config.Global;
import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.modules.workpartetag.entity.WorkPartEtag;
import com.jeeplus.modules.workpartetag.service.WorkPartEtagService;

/**
 * 断点续传Controller
 * @author 杨帆
 * @version 2017-12-18
 */
@Controller
@RequestMapping(value = "${adminPath}/workpartetag/workPartEtag")
public class WorkPartEtagController extends BaseController {

	@Autowired
	private WorkPartEtagService workPartEtagService;
	
	@ModelAttribute
	public WorkPartEtag get(@RequestParam(required=false) String id) {
		WorkPartEtag entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = workPartEtagService.get(id);
		}
		if (entity == null){
			entity = new WorkPartEtag();
		}
		return entity;
	}
	
	/**
	 * 断点续传成功列表页面
	 */
	@RequiresPermissions("workpartetag:workPartEtag:list")
	@RequestMapping(value = {"list", ""})
	public String list(WorkPartEtag workPartEtag, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<WorkPartEtag> page = workPartEtagService.findPage(new Page<WorkPartEtag>(request, response), workPartEtag); 
		model.addAttribute("page", page);
		return "modules/workpartetag/workPartEtagList";
	}

	/**
	 * 查看，增加，编辑断点续传成功表单页面
	 */
	@RequiresPermissions(value={"workpartetag:workPartEtag:view","workpartetag:workPartEtag:add","workpartetag:workPartEtag:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(WorkPartEtag workPartEtag, Model model) {
		model.addAttribute("workPartEtag", workPartEtag);
		return "modules/workpartetag/workPartEtagForm";
	}

	/**
	 * 保存断点续传成功
	 */
	@RequiresPermissions(value={"workpartetag:workPartEtag:add","workpartetag:workPartEtag:edit"},logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(WorkPartEtag workPartEtag, Model model, RedirectAttributes redirectAttributes) throws Exception{
		if (!beanValidator(model, workPartEtag)){
			return form(workPartEtag, model);
		}
		if(!workPartEtag.getIsNewRecord()){//编辑表单保存
			WorkPartEtag t = workPartEtagService.get(workPartEtag.getId());//从数据库取出记录的值
			MyBeanUtils.copyBeanNotNull2Bean(workPartEtag, t);//将编辑表单中的非NULL值覆盖数据库记录中的值
			workPartEtagService.save(t);//保存
		}else{//新增表单保存
			workPartEtagService.save(workPartEtag);//保存
		}
		addMessage(redirectAttributes, "保存断点续传成功成功");
		return "redirect:"+Global.getAdminPath()+"/workpartetag/workPartEtag/?repage";
	}
	/**
	 * 保存断点续传成功
	 */
	@RequestMapping(value = "setCheckpoint")
	@ResponseBody
	public Map<String,Object> setCheckpoint(String percent,String nextPart,String fileSize,
											String type,String name,String partSize,String uploadId) throws Exception{
		WorkPartEtag workPartEtag = new WorkPartEtag();
		workPartEtag.setSize(fileSize);
		workPartEtag.setUploadId(uploadId);
		workPartEtag.setPartNumber(nextPart);
		Map map = new HashMap();
		workPartEtagService.save(workPartEtag);//保存
		map.put("str","success");
		map.put("workPartEtag",workPartEtag);
		return map;
	}

	/**
	 * 断点续传信息
	 */
	@RequestMapping(value = "findByuploadId")
	@ResponseBody
	public Map<String,Object> findByuploadId(WorkPartEtag workPartEtag, HttpServletRequest request, HttpServletResponse response, Model model) {
		Map map = new HashMap();
		List<WorkPartEtag> list = workPartEtagService.findList(workPartEtag);
		WorkPartEtag etag = new WorkPartEtag();
		if (list!=null && list.size()!=0){
			etag = list.get(0);
		}
		map.put("str","success");
		map.put("workPartEtag",etag);
		map.put("uploadId",etag.getUploadId());
		return map;
	}

	/**
	 * 删除断点续传成功
	 */
	@RequiresPermissions("workpartetag:workPartEtag:del")
	@RequestMapping(value = "delete")
	public String delete(WorkPartEtag workPartEtag, RedirectAttributes redirectAttributes) {
		workPartEtagService.delete(workPartEtag);
		addMessage(redirectAttributes, "删除断点续传成功成功");
		return "redirect:"+Global.getAdminPath()+"/workpartetag/workPartEtag/?repage";
	}
	
	/**
	 * 批量删除断点续传成功
	 */
	@RequiresPermissions("workpartetag:workPartEtag:del")
	@RequestMapping(value = "deleteAll")
	public String deleteAll(String ids, RedirectAttributes redirectAttributes) {
		String idArray[] =ids.split(",");
		for(String id : idArray){
			workPartEtagService.delete(workPartEtagService.get(id));
		}
		addMessage(redirectAttributes, "删除断点续传成功成功");
		return "redirect:"+Global.getAdminPath()+"/workpartetag/workPartEtag/?repage";
	}
	
	/**
	 * 导出excel文件
	 */
	@RequiresPermissions("workpartetag:workPartEtag:export")
    @RequestMapping(value = "export", method=RequestMethod.POST)
    public String exportFile(WorkPartEtag workPartEtag, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "断点续传成功"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
            Page<WorkPartEtag> page = workPartEtagService.findPage(new Page<WorkPartEtag>(request, response, -1), workPartEtag);
    		new ExportExcel("断点续传成功", WorkPartEtag.class).setDataList(page.getList()).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出断点续传成功记录失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/workpartetag/workPartEtag/?repage";
    }

	/**
	 * 导入Excel数据

	 */
	@RequiresPermissions("workpartetag:workPartEtag:import")
    @RequestMapping(value = "import", method=RequestMethod.POST)
    public String importFile(MultipartFile file, RedirectAttributes redirectAttributes) {
		try {
			int successNum = 0;
			int failureNum = 0;
			StringBuilder failureMsg = new StringBuilder();
			ImportExcel ei = new ImportExcel(file, 1, 0);
			List<WorkPartEtag> list = ei.getDataList(WorkPartEtag.class);
			for (WorkPartEtag workPartEtag : list){
				try{
					workPartEtagService.save(workPartEtag);
					successNum++;
				}catch(ConstraintViolationException ex){
					failureNum++;
				}catch (Exception ex) {
					failureNum++;
				}
			}
			if (failureNum>0){
				failureMsg.insert(0, "，失败 "+failureNum+" 条断点续传成功记录。");
			}
			addMessage(redirectAttributes, "已成功导入 "+successNum+" 条断点续传成功记录"+failureMsg);
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入断点续传成功失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/workpartetag/workPartEtag/?repage";
    }
	
	/**
	 * 下载导入断点续传成功数据模板
	 */
	@RequiresPermissions("workpartetag:workPartEtag:import")
    @RequestMapping(value = "import/template")
    public String importFileTemplate(HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "断点续传成功数据导入模板.xlsx";
    		List<WorkPartEtag> list = Lists.newArrayList(); 
    		new ExportExcel("断点续传成功数据", WorkPartEtag.class, 1).setDataList(list).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入模板下载失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/workpartetag/workPartEtag/?repage";
    }
	
	
	

}