/**
 * 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.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.workmonthlyplan.entity.WorkMonthlyPlanThis;
import com.jeeplus.modules.workreimbursementcount.entity.*;
import com.jeeplus.modules.workreimbursementcount.service.WorkInvoiceCountService;
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/workInvoiceCount")
public class WorkInvoiceCountController extends BaseController {
	private List<String> xAxisData;
	private Map<String,List<Double>> yAxisData;
	private Map<String,Integer> yAxisIndex;
	private Map<String,Object> orientData;
	private Map<String,Object> orientData1;
	@Autowired
	private WorkInvoiceCountService workInvoiceCountService;
	@Autowired
	private OfficeService officeService;
	@Autowired
	private OfficeDao officeDao;

	/**
	 * 报销统计(公司级)
	 */
	@RequiresPermissions("workinvoicecount:workInvoiceCount:list")
	@RequestMapping(value = {"list", ""})
	public String list(WorkInvoiceCompany workInvoiceCompany, 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>());
				request.setAttribute("orientData1",new HashMap<String,Object>());
				break;
			case 2:
				return "redirect:"+Global.getAdminPath()+"/workreimbursementcount/workInvoiceCount/infolist?repage";
			case 3:
               if(isOffice()){//如果所在分公司下没有子公司
				   return "redirect:"+Global.getAdminPath()+"/workreimbursementcount/workInvoiceCount/officelist?repage";
			   }
				if (StringUtils.isBlank(workInvoiceCompany.getDate())){
					workInvoiceCompany.setDate(getYear(new Date()));
				}
				if (StringUtils.isNotBlank(workInvoiceCompany.getCheck()) && workInvoiceCompany.getCheck().equals("1")){
					WorkInvoiceMonth workInvoiceMonth = new WorkInvoiceMonth();
					workInvoiceMonth.setCompanyId(workInvoiceCompany.getCompanyId());
					workInvoiceMonth.setOfficeId(workInvoiceCompany.getOfficeId());
					workInvoiceMonth.setDate(workInvoiceCompany.getDate());
					List<WorkInvoiceMonth> list = new ArrayList<>();
					list = workInvoiceCountService.findMonthCompanyList(workInvoiceMonth);
					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));
					request.setAttribute("orientData1", getorientData1(list,3));
				}else{
					if(StringUtils.isNotBlank(workInvoiceCompany.getCompanyId())){
						request.setAttribute("backState",1);
					}
					List<WorkInvoiceCompany> list = workInvoiceCountService.findBranchList(workInvoiceCompany);
					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));
					request.setAttribute("orientData1", getorientData1(list,1));
				}
				return "modules/workreimbursementcount/workInvoiceCompany";
			case 4:
				if (StringUtils.isBlank(workInvoiceCompany.getDate())){
					workInvoiceCompany.setDate(getYear(new Date()));
				}
				if (StringUtils.isNotBlank(workInvoiceCompany.getCheck()) && workInvoiceCompany.getCheck().equals("1")){
					WorkInvoiceMonth workInvoiceMonth = new WorkInvoiceMonth();
					workInvoiceMonth.setCompanyId(workInvoiceCompany.getCompanyId());
					workInvoiceMonth.setOfficeId(workInvoiceCompany.getOfficeId());
					workInvoiceMonth.setDate(workInvoiceCompany.getDate());
					List<WorkInvoiceMonth> list = new ArrayList<>();
					list = workInvoiceCountService.findMonthCompanyList(workInvoiceMonth);
					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));
					request.setAttribute("orientData1", getorientData1(list,3));
				}else{
					if(StringUtils.isNotBlank(workInvoiceCompany.getCompanyId())){
						request.setAttribute("backState",1);
					}
				List<WorkInvoiceCompany> list = workInvoiceCountService.findCompanyList(workInvoiceCompany);
				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));
				request.setAttribute("orientData1", getorientData1(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>());
				request.setAttribute("orientData1",new HashMap<String,Object>());
				break;
		}
		return "modules/workreimbursementcount/workInvoiceCompany";
	}

	/**
	 * 报销统计(部门级)
	 */
	@RequiresPermissions("workinvoicecount:workInvoiceCount:list")
	@RequestMapping(value = {"officelist"})
	public String officelist(WorkInvoiceOffice workInvoiceOffice, HttpServletRequest request,
							 HttpServletResponse response, Model model,RedirectAttributes redirectAttributes) {
		int roleMax=UserUtils.getSelectRoleMax();
		model.addAttribute("roleMax", roleMax);
		if (StringUtils.isBlank(workInvoiceOffice.getDate())){
			workInvoiceOffice.setDate(getYear(new Date()));
		}
		if (StringUtils.isNotBlank(workInvoiceOffice.getCheck()) && workInvoiceOffice.getCheck().equals("1")){
			WorkInvoiceMonth workInvoiceMonth = new WorkInvoiceMonth();
			workInvoiceMonth.setCompanyId(workInvoiceOffice.getCompanyId());
			workInvoiceMonth.setOfficeId(workInvoiceOffice.getOfficeId());
			workInvoiceMonth.setDate(workInvoiceOffice.getDate());
			Office company = officeService.get(workInvoiceOffice.getCompanyId());
			List<WorkInvoiceMonth> list = workInvoiceCountService.findMonthOfficeList(workInvoiceMonth);
			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));
			request.setAttribute("orientData1", getorientData1(list,3));
		}else{
			if(StringUtils.isNotBlank(workInvoiceOffice.getCompanyId())){
				request.setAttribute("backState",1);
			}
		List<WorkInvoiceOffice>list = workInvoiceCountService.findOfficeList(workInvoiceOffice);
		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));//饼图
		request.setAttribute("orientData1", getorientData1(list,2));
		}
		return "modules/workreimbursementcount/workInvoiceOffice";
	}

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


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

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

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


	public List<String> getxAxisData(List list,int type){
		xAxisData = new ArrayList<String>();
		if(type==1){
			for(WorkInvoiceCompany w:(List<WorkInvoiceCompany>)list){
				if(!"合计".equals(w.getOfficeName())) {
					xAxisData.add(w.getOfficeName());
				}
			}
		}else if(type==2){
			for(WorkInvoiceOffice w:(List<WorkInvoiceOffice>)list){
				if(!"合计".equals(w.getOfficeName())) {
					xAxisData.add(w.getOfficeName());
				}
			}
		}else if(type==3){
			for(WorkInvoiceMonth w:(List<WorkInvoiceMonth>)list){
				if(!"合计".equals(w.getOfficeName())) {
					xAxisData.add(w.getOfficeName());
				}
			}
		}
		return xAxisData;
	}

	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>();
		List<Double> data1 = new ArrayList<Double>();
		if(type==1){
			for(WorkInvoiceCompany w:(List<WorkInvoiceCompany>)list){
				if(!"合计".equals(w.getOfficeName())){
					data.add(w.getIncomeMoneys().doubleValue());
					data1.add(w.getInvoiceMoneys().doubleValue());
				}
			}
		}else if (type==2){
			for(WorkInvoiceOffice w:(List<WorkInvoiceOffice>)list){
				if(!"合计".equals(w.getOfficeName())){
					data.add(w.getIncomeMoneys().doubleValue());
					data1.add(w.getInvoiceMoneys().doubleValue());
				}
			}
		}else if (type==3){
			for(WorkInvoiceMonth w:(List<WorkInvoiceMonth>)list){
				if(!"合计".equals(w.getOfficeName())){
					data.add(w.getIncomeMoneys().doubleValue());
					data1.add(w.getInvoiceMoneys().doubleValue());
				}
			}
		}
		yAxisData.put("开票金额", data1);
		yAxisData.put("收款金额", data);
		return yAxisData;
	}
	//饼图
	public Map<String,Object> getorientData(List list,int type){
		orientData = new HashMap<String,Object>();
		if (type==1){
			for(WorkInvoiceCompany w:(List<WorkInvoiceCompany>)list){
				if(!"合计".equals(w.getOfficeName())){
					orientData.put(w.getOfficeName(),w.getIncomeMoneys());
				}
			}
		}else if(type==2){
			for(WorkInvoiceOffice w:(List<WorkInvoiceOffice>)list){
				if(!"合计".equals(w.getOfficeName())){
					orientData.put(w.getOfficeName(),w.getIncomeMoneys());
				}
			}
		}else if(type==3){
			for(WorkInvoiceMonth w:(List<WorkInvoiceMonth>)list){
				if(!"合计".equals(w.getOfficeName())){
					orientData.put(w.getOfficeName(),w.getIncomeMoneys());
				}
			}
		}

		return orientData;
	}
	//饼图
	public Map<String,Object> getorientData1(List list,int type){
		orientData1 = new HashMap<String,Object>();
		if (type==1){
			for(WorkInvoiceCompany w:(List<WorkInvoiceCompany>)list){
				if(!"合计".equals(w.getOfficeName())){
					orientData1.put(w.getOfficeName(),w.getInvoiceMoneys());
				}
			}
		}else if (type==2){
			for(WorkInvoiceOffice w:(List<WorkInvoiceOffice>)list){
				if(!"合计".equals(w.getOfficeName())){
					orientData1.put(w.getOfficeName(),w.getInvoiceMoneys());
				}
			}
		}else if (type==3){
			for(WorkInvoiceMonth w:(List<WorkInvoiceMonth>)list){
				if(!"合计".equals(w.getOfficeName())){
					orientData1.put(w.getOfficeName(),w.getInvoiceMoneys());
				}
			}
		}

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


	//判断点击公司进部门页面还是公司页面
	@RequestMapping(value = {"signlist"})
	@ResponseBody
	public int signlist(String companyId,String sign){
		int count=0;
		if("1".equals(sign)){
			count=0;
		}else{
			List<Office>office=	officeDao.getAllBranch(companyId);
			if(office!=null&&office.size()>0){
				count=1;
			}else{
				count=0;
			}
		}
		return  count;
	}

	//判断点击公司进部门页面还是公司页面
	@RequestMapping(value = {"officeSignlist"})
	@ResponseBody
	public int officeSignlist(String officeId){
		int count=0;
		List<Office>office=	officeDao.getAllBranchOffice(officeId);
		if(office!=null&&office.size()>0){
			count=1;
		}else{
			count=0;
		}
		return  count;
	}

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