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

import com.jeeplus.common.config.Global;
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.web.BaseController;
import com.jeeplus.modules.projectcontentinfo.entity.ProjectReportDataMonth;
import com.jeeplus.modules.sys.dao.OfficeDao;
import com.jeeplus.modules.sys.entity.Office;
import com.jeeplus.modules.sys.service.OfficeService;
import com.jeeplus.modules.sys.utils.UserUtils;

import com.jeeplus.modules.workreimbursementcount.entity.WorkReimbursementCompany;
import com.jeeplus.modules.workreimbursementcount.entity.WorkReimbursementInfo;
import com.jeeplus.modules.workreimbursementcount.entity.WorkReimbursementMonth;
import com.jeeplus.modules.workreimbursementcount.entity.WorkReimbursementOffice;
import com.jeeplus.modules.workreimbursementcount.service.WorkReimbursementCountService;
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.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 报销统计
 * @author 陈玉平
 * @version 2018-07-03
 */
@Controller
@RequestMapping(value = "${adminPath}/workreimbursementcount/workReimbursementCount")
public class WorkReimbursementCountController extends BaseController {
	private List<String> xAxisData;
	private Map<String,List<Double>> yAxisData;
	private Map<String,Integer> yAxisIndex;
	private Map<String,Object> orientData;
	@Autowired
	private OfficeDao officeDao;
	@Autowired
	private WorkReimbursementCountService workReimbursemenCountService;
	@Autowired
	private OfficeService officeService;

	/**
	 * 报销统计(公司级)
	 */
	@RequiresPermissions("workreimbursementcount:workReimbursementCount:list")
	@RequestMapping(value = {"list", ""})
	public String list(WorkReimbursementCompany workReimbursementCompany, HttpServletRequest request,
					   HttpServletResponse response, Model model,RedirectAttributes redirectAttributes) {
		int roleMax=UserUtils.getSelectRoleMax();
		model.addAttribute("roleMax", roleMax);
		switch(roleMax){
			case 1:
				model.addAttribute("list", new ArrayList<>());
				request.setAttribute("xAxisData",new ArrayList<>());
				request.setAttribute("yAxisData",new HashMap<String,List<Double>>());
				request.setAttribute("orientData",new HashMap<String,Object>());
				break;
			case 2:
				return "redirect:"+Global.getAdminPath()+"/workreimbursementcount/workReimbursementCount/infolist?repage";
			case 3:
				if(isOffice()){//如果所在分公司下没有子公司
					return "redirect:"+Global.getAdminPath()+"/workreimbursementcount/workReimbursementCount/officelist?repage";
				}
				if (StringUtils.isBlank(workReimbursementCompany.getDate())){
					workReimbursementCompany.setDate(getYear(new Date()));
				}
				if (StringUtils.isNotBlank(workReimbursementCompany.getCheck()) && workReimbursementCompany.getCheck().equals("1")){
					WorkReimbursementMonth workReimbursementMonth = new WorkReimbursementMonth();
					workReimbursementMonth.setCompanyId(workReimbursementCompany.getCompanyId());
					workReimbursementMonth.setOfficeId(workReimbursementCompany.getOfficeId());
					workReimbursementMonth.setDate(workReimbursementCompany.getDate());
					workReimbursementMonth.setExt(workReimbursementCompany.getExt());
					List<WorkReimbursementMonth> list = new ArrayList<>();
					list = workReimbursemenCountService.findMonthCompanyList(workReimbursementMonth);
					model.addAttribute("list", list);
					model.addAttribute("showName", "月份");
					request.setAttribute("xAxisData", getxAxisData(list, 3));
					request.setAttribute("yAxisData", getyAxisData(list, 3));
					request.setAttribute("orientData", getorientData(list, 3));
				} else{
					if(StringUtils.isNotBlank(workReimbursementCompany.getCompanyId())){
						request.setAttribute("backState",1);
					}
					List<WorkReimbursementCompany> list = workReimbursemenCountService.findBranchList(workReimbursementCompany);
					model.addAttribute("list", list);
					model.addAttribute("showName", "分公司");
					request.setAttribute("xAxisData", getxAxisData(list,1));//柱状图与折线图//展现样式
					request.setAttribute("yAxisData", getyAxisData(list,1));
					request.setAttribute("orientData", getorientData(list,1));//饼图
				}
				return "modules/workreimbursementcount/workReimbursementCompany";
			case 4:
				if (StringUtils.isBlank(workReimbursementCompany.getDate())){
					workReimbursementCompany.setDate(getYear(new Date()));
				}
				if (StringUtils.isNotBlank(workReimbursementCompany.getCheck()) && workReimbursementCompany.getCheck().equals("1")){
					WorkReimbursementMonth workReimbursementMonth = new WorkReimbursementMonth();
					workReimbursementMonth.setCompanyId(workReimbursementCompany.getCompanyId());
					workReimbursementMonth.setOfficeId(workReimbursementCompany.getOfficeId());
					workReimbursementMonth.setDate(workReimbursementCompany.getDate());
					workReimbursementMonth.setExt(workReimbursementCompany.getExt());
					List<WorkReimbursementMonth> list = new ArrayList<>();
					list = workReimbursemenCountService.findMonthCompanyList(workReimbursementMonth);
					model.addAttribute("list", list);
					model.addAttribute("showName", "月份");
					request.setAttribute("xAxisData", getxAxisData(list, 3));
					request.setAttribute("yAxisData", getyAxisData(list, 3));
					request.setAttribute("orientData", getorientData(list, 3));
				} else{
					if(StringUtils.isNotBlank(workReimbursementCompany.getCompanyId())){
						request.setAttribute("backState",1);
					}
					List<WorkReimbursementCompany> list = workReimbursemenCountService.findCompanyList(workReimbursementCompany);
				    model.addAttribute("list", list);
					model.addAttribute("showName", "分公司");
					request.setAttribute("xAxisData", getxAxisData(list,1));//柱状图与折线图//展现样式
					request.setAttribute("yAxisData", getyAxisData(list,1));
					request.setAttribute("orientData", getorientData(list,1));//饼图
						 }
				break;
			default:
				model.addAttribute("list", new ArrayList<>());
				request.setAttribute("xAxisData",new ArrayList<>());
				request.setAttribute("yAxisData",new HashMap<String,List<Double>>());
				request.setAttribute("orientData",new HashMap<String,Object>());
				break;
		}
		return "modules/workreimbursementcount/workReimbursementCompany";
	}

	/**
	 * 报销统计(部门级)
	 */
	@RequiresPermissions("workreimbursementcount:workReimbursementCount:list")
	@RequestMapping(value = {"officelist"})
	public String officelist(WorkReimbursementOffice workReimbursementOffice, HttpServletRequest request,
							 HttpServletResponse response, Model model,RedirectAttributes redirectAttributes) {
		int roleMax=UserUtils.getSelectRoleMax();
		model.addAttribute("roleMax", roleMax);
		if (StringUtils.isBlank(workReimbursementOffice.getDate())){
			workReimbursementOffice.setDate(getYear(new Date()));
		}
		if (StringUtils.isNotBlank(workReimbursementOffice.getCheck()) && workReimbursementOffice.getCheck().equals("1")){
			WorkReimbursementMonth workReimbursementMonth = new WorkReimbursementMonth();
			workReimbursementMonth.setCompanyId(workReimbursementOffice.getCompanyId());
			workReimbursementMonth.setOfficeId(workReimbursementOffice.getOfficeId());
			workReimbursementMonth.setDate(workReimbursementOffice.getDate());
			workReimbursementMonth.setExt(workReimbursementOffice.getExt());
			List<WorkReimbursementMonth> list = workReimbursemenCountService.findMonthOfficeList(workReimbursementMonth);
			model.addAttribute("list", list);
			model.addAttribute("showName", "月份");
			request.setAttribute("xAxisData", getxAxisData(list, 3));
			request.setAttribute("yAxisData", getyAxisData(list, 3));
			request.setAttribute("orientData", getorientData(list, 3));
		}else{
			if(StringUtils.isNotBlank(workReimbursementOffice.getCompanyId())){
				request.setAttribute("backState",1);
			}
			List<WorkReimbursementOffice>list = workReimbursemenCountService.findOfficeList(workReimbursementOffice);
			model.addAttribute("list", list);
			model.addAttribute("showName", "部门");
			request.setAttribute("xAxisData", getxAxisData(list,2));//展现样式//柱状图与折线图
			request.setAttribute("yAxisData", getyAxisData(list,2));
			request.setAttribute("orientData", getorientData(list,2));//饼图
		}
		return "modules/workreimbursementcount/workReimbursementOffice";
	}

	/**
	 * 报销统计(明细)
	 */
	@RequiresPermissions("workreimbursementcount:workReimbursementCount:list")
	@RequestMapping(value = {"infolist"})
	public String infolist(WorkReimbursementInfo workReimbursementInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		int roleMax=UserUtils.getSelectRoleMax();
		model.addAttribute("roleMax", roleMax);
		if(StringUtils.isNotBlank(workReimbursementInfo.getOfficeId())){
			request.setAttribute("backState",1);
		}
		if (StringUtils.isBlank(workReimbursementInfo.getDate())){
			workReimbursementInfo.setDate(getYear(new Date()));
		}
		Page<WorkReimbursementInfo> page = workReimbursemenCountService.findInfoPage(new Page<WorkReimbursementInfo>(request, response), workReimbursementInfo);
		model.addAttribute("page", page);
		return "modules/workreimbursementcount/workReimbursementInfo";
	}


	/**
	 * 导出excel文件
	 */
	@RequiresPermissions("workreimbursementcount:workReimbursementCount:export")
	@RequestMapping(value = "exportCompany", method=RequestMethod.POST)
	public String exportCompanyFile(WorkReimbursementCompany workReimbursementCompany, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
			if (StringUtils.isBlank(workReimbursementCompany.getCheck())) {
				String fileName = "报销统计" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
				List<WorkReimbursementCompany> list = workReimbursemenCountService.findCompanyList(workReimbursementCompany);
				new ExportExcel("报销统计", WorkReimbursementCompany.class).setDataList(list).write(response, fileName).dispose();
				return null;
			}else{
				String fileName = "报销统计" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
				WorkReimbursementMonth workReimbursementMonth = new WorkReimbursementMonth();
				workReimbursementMonth.setCompanyId(workReimbursementCompany.getCompanyId());
				workReimbursementMonth.setOfficeId(workReimbursementCompany.getOfficeId());
				workReimbursementMonth.setDate(workReimbursementCompany.getDate());
				workReimbursementMonth.setExt(workReimbursementCompany.getExt());
				List<WorkReimbursementMonth> list = new ArrayList<>();
				if (StringUtils.isBlank(workReimbursementCompany.getCompanyId()) || StringUtils.isBlank(workReimbursementCompany.getOfficeName())){
					list = workReimbursemenCountService.findMonthCompanyList(workReimbursementMonth);
				}else {
					list = workReimbursemenCountService.findMonthCompanyList(workReimbursementMonth);
				}
				new ExportExcel("报销统计", WorkReimbursementMonth.class).setDataList(list).write(response, fileName).dispose();
			}
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出报销统计记录失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/workreimbursementcount/workReimbursementCount/?repage";
	}

	/**
	 * 导出excel文件
	 */
	@RequiresPermissions("workreimbursementcount:workReimbursementCount:export")
	@RequestMapping(value = "exportOffice", method=RequestMethod.POST)
	public String exportOfficeFile(WorkReimbursementOffice workReimbursementOffice, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
			if (StringUtils.isBlank(workReimbursementOffice.getCheck())) {
				String fileName = "报销统计" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
				List<WorkReimbursementOffice> list = workReimbursemenCountService.findOfficeList(workReimbursementOffice);
				new ExportExcel("报销统计", WorkReimbursementOffice.class).setDataList(list).write(response, fileName).dispose();
				return null;
			}else{
				String fileName = "报销统计" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
				WorkReimbursementMonth workReimbursementMonth = new WorkReimbursementMonth();
				workReimbursementMonth.setCompanyId(workReimbursementOffice.getCompanyId());
				workReimbursementMonth.setOfficeId(workReimbursementOffice.getOfficeId());
				workReimbursementMonth.setDate(workReimbursementOffice.getDate());
				workReimbursementMonth.setExt(workReimbursementOffice.getExt());
				List<WorkReimbursementMonth> list = workReimbursemenCountService.findMonthOfficeList(workReimbursementMonth);
				new ExportExcel("报销统计", WorkReimbursementMonth.class).setDataList(list).write(response, fileName).dispose();
			}
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出报销统计记录失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/workreimbursementcount/workReimbursementCount/?repage";
	}

	/**
	 * 导出excel文件
	 */
	@RequiresPermissions("workreimbursementcount:workReimbursementCount:export")
	@RequestMapping(value = "exportInfo", method=RequestMethod.POST)
	public String exportInfoFile(WorkReimbursementInfo workReimbursementInfo, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
			String fileName = "报销统计"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
			List<WorkReimbursementInfo> list = workReimbursemenCountService.findInfoList(workReimbursementInfo);
			new ExportExcel("报销统计", WorkReimbursementInfo.class).setDataList(list).write(response, fileName).dispose();
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出报销统计记录失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/workreimbursementcount/workReimbursementCount/?repage";
	}

//柱图折线图X轴
	public List<String> getxAxisData(List list,int type){
		xAxisData = new ArrayList<String>();
		if(type==1){
			for(WorkReimbursementCompany w:(List<WorkReimbursementCompany>) list){
				if(!"合计".equals(w.getOfficeName())){
					xAxisData.add(w.getOfficeName());
				}
			}
		}else if(type==2){
			for(WorkReimbursementOffice w:(List<WorkReimbursementOffice>) list){
				if(!"合计".equals(w.getOfficeName())){
					xAxisData.add(w.getOfficeName());
				}
			}
		}else if(type==3){
			for(WorkReimbursementMonth w:(List<WorkReimbursementMonth>) list){
				if(!"合计".equals(w.getOfficeName())){
					xAxisData.add(w.getOfficeName());
				}
			}
		}

		return xAxisData;
	}
	//柱图折线图Y轴
	public Map<String,List<Double>> getyAxisData(List list,int type){
		Random random = new Random();
		yAxisData = new HashMap<String,List<Double>>();
		List<Double> data = new ArrayList<Double>();
		if(type==1){
			for(WorkReimbursementCompany w:(List<WorkReimbursementCompany>) list){
				if(!"合计".equals(w.getOfficeName())){
					data.add(w.getMoney().doubleValue());
				}
			}
		}else if(type==2){
			for(WorkReimbursementOffice w:(List<WorkReimbursementOffice>) list){
				if(!"合计".equals(w.getOfficeName())){
					data.add(w.getMoney().doubleValue());
				}
			}
		}else if(type==3){
			for(WorkReimbursementMonth w:(List<WorkReimbursementMonth>) list){
				if(!"合计".equals(w.getOfficeName())){
					data.add(w.getMoney().doubleValue());
				}
			}
		}
		yAxisData.put("报销金额", data);
		return yAxisData;
	}
	//饼图
	public Map<String,Object> getorientData(List list,int type){
		orientData = new HashMap<String,Object>();
		if (type==1){
			for(WorkReimbursementCompany w:(List<WorkReimbursementCompany>) list){
				if(!"合计".equals(w.getOfficeName())){
					orientData.put(w.getOfficeName(),w.getMoney());
				}
			}
		}else if(type==2){
			for(WorkReimbursementOffice w:(List<WorkReimbursementOffice>) list){
				if(!"合计".equals(w.getOfficeName())){
					orientData.put(w.getOfficeName(),w.getMoney());
				}
			}
		}else if(type==3){
			for(WorkReimbursementMonth w:(List<WorkReimbursementMonth>) list){
				if(!"合计".equals(w.getOfficeName())){
					orientData.put(w.getOfficeName(),w.getMoney());
				}
			}
		}
		return orientData;
	}

	public static String getYear(Date date){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String str = format.format(date);
		return str.substring(0,4);
	}

	//分公司下是否存在子公司
	public Boolean isOffice(){
		String companyId= UserUtils.getSelectBranchOffice();
		List<Office>office=	officeDao.getAllBranch(companyId);
		if(office!=null&&office.size()>0){
			return false;
		}else{
			return true;
		}

	}
}