package com.hb.proj.car.controller; import java.text.DecimalFormat; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.hb.proj.car.service.CarOilMonitorService; import com.hb.proj.car.service.CarRptService; import com.hb.proj.car.service.CarRptUtils; import com.hb.proj.car.service.CarService; import com.hb.proj.car.service.EnergySumRptService; import com.hb.proj.input.service.CarConsumeService; import com.hb.proj.model.CarConsume; import com.hb.proj.model.CarConsumeRptVO; import com.hb.proj.model.ERP; import com.hb.proj.model.OtherConsume; import com.hb.proj.utils.JsonOutUtils; import com.hb.proj.utils.MySessionUser; import com.hb.proj.utils.RptMonthUtil; import com.hb.proj.utils.RptOuter; import com.hb.xframework.util.DateUtil; import com.hb.xframework.util.SessionThreadLocal; @Controller @RequestMapping("/**/rpt") public class CarRptController { @Autowired private CarService carService; @Autowired private CarRptService service; @Autowired private CarOilMonitorService monitorService; @Autowired private CarConsumeService consumeService; @Autowired private EnergySumRptService energyService; /** * 单车油耗明细(有两个报表:项目部用、安全科用) * @param request * @param response */ @RequestMapping("/loadCarConsumeDtl") public void loadCarConsumeDtl(HttpServletRequest request,HttpServletResponse response){ try{ MySessionUser su=(MySessionUser)SessionThreadLocal.getSessionUser(); if(su==null){ JsonOutUtils.returnError(response, "缺少登录信息"); return; } String orgAssistCode=request.getParameter("orgAssistCode"); String orgName=request.getParameter("orgName"); String startMonth=request.getParameter("startMonth"); String endMonth=request.getParameter("endMonth"); //String useType=request.getParameter("useType"); //1:安全科;其它:项目部 String deviceKey=request.getParameter("deviceKey"); String sortField=request.getParameter("sortField"); String sortType=request.getParameter("sortType"); boolean rptForMgr="1".equals(request.getParameter("useType")); if(StringUtils.isEmpty(orgAssistCode)){ orgAssistCode=su.isSysAdmin()?null:su.getOrgAssistCode(); } Map rootMap=rptCarConsume(rptForMgr,orgName,orgAssistCode,startMonth,endMonth,deviceKey,sortField,sortType); RptOuter.clearTemplateCache(); RptOuter.write(request,response,rootMap,rptForMgr?"carOilCostDtlSafeDpt.ftl":"carOilCostDtl.ftl"); } catch(Exception e){ e.printStackTrace(); JsonOutUtils.returnError(response, "服务出现错误"); } } /** * 为单车油耗统计、按车型统计油耗提供核心方法(包括相关导出) * @param mgrRpt 管理用统计(安全科用) * @param orgName * @param orgAssistCode * @param startMonth * @param endMonth * @param deviceKey 设备名称或型号,可以同时输入 * @param sortField 排序字段 * @param sortType 排序方式 desc、asc */ private Map rptCarConsume(boolean mgrRpt,String orgName,String orgAssistCode,String startMonth,String endMonth,String deviceKey,String sortField,String sortType){ if(StringUtils.isEmpty(startMonth)&&StringUtils.isEmpty(endMonth)){ //默认当月 startMonth=DateUtil.format((new Date()), "yyyy-MM"); endMonth=startMonth; } else if(StringUtils.isEmpty(endMonth)){ //只输入开始月份的 endMonth=startMonth; } else if(StringUtils.isEmpty(startMonth)){ startMonth=endMonth; } List carConsumes=service.loadCarConsumeDtl(startMonth, endMonth, orgAssistCode,deviceKey,sortField,sortType); Map rootMap=new HashMap(); //安全科用,采用新逻辑计算实际单耗:上报加油量+上期余油-本期余油 if(mgrRpt){ Set carIds=new HashSet(carConsumes.size()); String crtMonth=DateUtil.format(new Date(), "yyyy-MM-dd"); for(CarConsumeRptVO car : carConsumes){ carIds.add(car.getCarId()); car.setRealAge(RptMonthUtil.getWorkAge(DateUtil.format(car.getWorkStart(),"yyyy-MM"),crtMonth)); } Map> volMapping=monitorService.loadCarMonitorRpt(carIds,startMonth, endMonth); boolean hadMonitor=volMapping!=null&&volMapping.size()>0; Map mthVol=null; for(CarConsumeRptVO car : carConsumes){ mthVol=hadMonitor?volMapping.get(car.getCarId()):null; if(mthVol!=null&&mthVol.get("startVolume")!=null&&mthVol.get("endVolume")!=null){ car.init(((Number)mthVol.get("startVolume")).doubleValue(),((Number)mthVol.get("endVolume")).doubleValue()); } else{ car.init(null,null); } } } else{ //项目用报表需要:计算汽油、柴油平均单价 2021.1.6 if(!startMonth.equals(endMonth)){ //跨月查询,实际单耗要重新计算 CarRptUtils.calculateRealCostPer100km(carConsumes); } ERP erp=energyService.erpMonthSum(startMonth,endMonth); OtherConsume kl=energyService.otherMonthSum(startMonth, endMonth); erp=ERPKlSumUtil.erpPlusKlMonth(kl,erp); if(erp==null){ return rootMap; } rootMap.put("oilPrice", getPrice(erp.getOilMoney(),erp.getOilCount())); //元/千克 rootMap.put("coilPrice", getPrice(erp.getCoilMoney(),erp.getCoilCount())); //元/千克 } String rptMonthTitle=startMonth.replace("-", "年")+"月"; String rptMonthCell=(startMonth.split("-"))[1]+"月"; if(!startMonth.equals(endMonth)){ rptMonthTitle+="——"+endMonth.replace("-", "年")+"月"; rptMonthCell+="-"+(endMonth.split("-"))[1]+"月"; } rootMap.put("orgName", orgName!=null?orgName:""); rootMap.put("rptMonthTitle", rptMonthTitle); rootMap.put("rptMonthCell", rptMonthCell); rootMap.put("datas", carConsumes); Map otherConsume=consumeService.loadOtherConsume(startMonth, endMonth, orgAssistCode); rootMap.put("otherConsume", otherConsume); return rootMap; } private String getPrice(Double money,Double count){ if(money==null||count==null){ return null; } DecimalFormat df=new DecimalFormat("#0.000"); return df.format((money*10)/count); } /** * 各车型(其实是设备名称)油耗汇总表,需要在loadCarConsumeDtl方法的结果集上,在程序内部进行分组统计 * (如果直接在sql中按设备名分组,丢失设备型号,行驶油耗无法计算) * @param orgName * @param orgAssistCode * @param year * @param request * @param response */ @RequestMapping("/loadCarTypeConsume") public void loadCarTypeConsume(String orgName,String orgAssistCode,String startMonth,String endMonth,HttpServletRequest request,HttpServletResponse response){ try{ MySessionUser su=(MySessionUser)SessionThreadLocal.getSessionUser(); if(su==null){ //测试暂时不用 JsonOutUtils.returnError(response, "缺少登录信息"); return; } if(StringUtils.isEmpty(orgAssistCode)){ orgAssistCode=su.isSysAdmin()?null:su.getOrgAssistCode(); } if(StringUtils.isEmpty(startMonth)&&StringUtils.isEmpty(endMonth)){ //默认当月 startMonth=DateUtil.format((new Date()), "yyyy-MM"); endMonth=startMonth; } else if(StringUtils.isEmpty(startMonth)){ startMonth=endMonth; } else if(StringUtils.isEmpty(endMonth)){ endMonth=startMonth; } List carConsumes=service.loadCarTypeConsume(startMonth, endMonth, orgAssistCode); String rptMonthTitle=startMonth.replace("-", "年")+"月"; if(!startMonth.equals(endMonth)){ rptMonthTitle+="——"+endMonth.replace("-", "年")+"月"; } Map rootMap=new HashMap(); rootMap.put("orgName", orgName!=null?orgName:""); rootMap.put("rptMonthTitle", rptMonthTitle); rootMap.put("datas", carConsumes); Map otherConsume=consumeService.loadOtherConsume(startMonth, endMonth, orgAssistCode); rootMap.put("otherConsume", otherConsume); RptOuter.clearTemplateCache(); RptOuter.write(request,response,rootMap,"carTypeOilCostRpt.ftl"); } catch(Exception e){ e.printStackTrace(); JsonOutUtils.returnError(response, "服务出现错误"); } } }