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

import com.google.common.collect.Lists;
import com.jeeplus.common.config.Global;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.common.web.BaseController;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.workattendance.entity.WorkAttendanceCount;
import com.jeeplus.modules.workattendance.service.WorkAttendanceCountService;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;
import java.util.List;

/**
 * 考勤统计Controller
 * @author 杨帆
 * @version 2018-09-14
 */
@Controller
@RequestMapping(value = "${adminPath}/workattendance/workAttendanceCount")
public class WorkAttendanceCountController extends BaseController {

	@Autowired
	private WorkAttendanceCountService workAttendanceCountService;
	@ModelAttribute
	public WorkAttendanceCount get(@RequestParam(required=false) String id) {
		WorkAttendanceCount entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = workAttendanceCountService.get(id);
		}
		if (entity == null){
			entity = new WorkAttendanceCount();
		}
		return entity;
	}
	
	/**
	 * 考勤统计列表页面
	 */
	@RequiresPermissions("workattendance:workAttendanceCount:list")
	@RequestMapping(value = {"list", ""})
	public String list(WorkAttendanceCount workAttendanceCount, HttpServletRequest request, HttpServletResponse response, Model model) {
		String branchOffice = UserUtils.getSelectBranchOffice();
		workAttendanceCount.setBranchOffice(branchOffice);
		Page<WorkAttendanceCount> page = workAttendanceCountService.findPage(new Page<WorkAttendanceCount>(request, response), workAttendanceCount); 
		model.addAttribute("page", page);
		return "modules/workattendance/workAttendanceCountList";
	}

	/**
	 * 查看，增加，编辑考勤统计表单页面
	 */
	//@RequiresPermissions(value={"workattendance:workAttendanceCount:view","workattendance:workAttendanceCount:add","workattendance:workAttendanceCount:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(WorkAttendanceCount workAttendanceCount, Model model) {
		model.addAttribute("workAttendanceCount", workAttendanceCount);
		return "modules/workattendance/workAttendanceCountForm";
	}

	/**
	 * 保存考勤统计
	 */
	//@RequiresPermissions(value={"workattendance:workAttendanceCount:add","workattendance:workAttendanceCount:edit"},logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(WorkAttendanceCount workAttendanceCount, Model model, RedirectAttributes redirectAttributes) throws Exception{
		if (!beanValidator(model, workAttendanceCount)){
			return form(workAttendanceCount, model);
		}
		workAttendanceCountService.save(workAttendanceCount);
		addMessage(redirectAttributes, "审批部门考勤成功");
		return "redirect:" + Global.getAdminPath() + "/home/?repage";
	}
	
	@RequestMapping(value = "store")
	@ResponseBody
	public AjaxJson store(WorkAttendanceCount workAttendanceCount, Model model) throws Exception{
		AjaxJson ajaxJson = new AjaxJson();
		try {
			workAttendanceCountService.store(workAttendanceCount);
			ajaxJson.setMsg("考勤数据重算成功");
		}catch (Exception e){
			ajaxJson.setSuccess(false);
			ajaxJson.setMsg("考勤数据重算失败");
		}

		return ajaxJson;
	}

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

	/**
	 * 导入Excel数据

	 */
	@RequiresPermissions("workattendance:workAttendanceCount: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<WorkAttendanceCount> list = ei.getDataList(WorkAttendanceCount.class);
			for (WorkAttendanceCount workAttendanceCount : list){
				try{
					workAttendanceCountService.save(workAttendanceCount);
					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()+"/workattendance/workAttendanceCount/?repage";
    }
	
	/**
	 * 下载导入考勤统计数据模板
	 */
	@RequiresPermissions("workattendance:workAttendanceCount:import")
    @RequestMapping(value = "import/template")
    public String importFileTemplate(HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "考勤统计数据导入模板.xlsx";
    		List<WorkAttendanceCount> list = Lists.newArrayList(); 
    		new ExportExcel("考勤统计数据", WorkAttendanceCount.class, 1).setDataList(list).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入模板下载失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/workattendance/workAttendanceCount/?repage";
    }
	
	
	

}