TBCarUseInfoController.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. package cn.com.lzt.oilconsumption.controller;
  2. import cn.com.lzt.common.util.DateUtil;
  3. import cn.com.lzt.common.util.StringUtil;
  4. import cn.com.lzt.oilconsumption.entity.*;
  5. import cn.com.lzt.useractiviti.data.controller.ActivitiToolsController;
  6. import com.daju.mix.vendor.service.ECarVehicleService;
  7. import org.apache.poi.hssf.usermodel.HSSFCell;
  8. import org.apache.poi.hssf.usermodel.HSSFRow;
  9. import org.apache.poi.hssf.usermodel.HSSFSheet;
  10. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  11. import org.apache.poi.ss.util.CellRangeAddress;
  12. import org.jeecgframework.web.system.service.SystemService;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.stereotype.Controller;
  15. import org.springframework.web.bind.annotation.RequestMapping;
  16. import org.springframework.web.servlet.ModelAndView;
  17. import javax.annotation.Resource;
  18. import javax.servlet.ServletOutputStream;
  19. import javax.servlet.http.HttpServletRequest;
  20. import javax.servlet.http.HttpServletResponse;
  21. import java.io.IOException;
  22. import java.io.OutputStream;
  23. import java.net.URLEncoder;
  24. import java.text.ParseException;
  25. import java.text.SimpleDateFormat;
  26. import java.util.*;
  27. import java.util.concurrent.atomic.AtomicReference;
  28. import java.util.logging.Logger;
  29. import java.util.stream.Collectors;
  30. import static cn.com.lzt.sign.controller.StatisticsController.getDaysOfMonth;
  31. @Controller
  32. @RequestMapping("/tBCarUseInfoController")
  33. public class TBCarUseInfoController extends ActivitiToolsController {
  34. private static final Logger logger = Logger.getLogger(String.valueOf(TBCarUseInfoController.class));
  35. @Autowired
  36. private SystemService systemService;
  37. @Resource
  38. private ECarVehicleService eCarVehicleService;
  39. /**
  40. * 车辆耗用明细列表 页面跳转
  41. *
  42. * @return
  43. */
  44. @RequestMapping(params = "list")
  45. public ModelAndView list(HttpServletRequest request) throws ParseException {
  46. //基础数据准备
  47. String nf = request.getParameter("nf");
  48. String yf = request.getParameter("yf");
  49. String dept = request.getParameter("departId");
  50. String sql = "SELECT\n" +
  51. "\tDATE_FORMAT(d.out_time,'%Y-%m'),\n" +
  52. "\td.material_dapt_id,\n" +
  53. "\tdi.receiver_car_id,\n" +
  54. "\tdp.departname,\n" +
  55. "\tdi.goods_id,\n" +
  56. "\tdi.out_num,\n" +
  57. "\tIFNULL(ca.plate,'未知车辆'),\n" +
  58. "\tgi.default_provider\n" +
  59. "FROM\n" +
  60. "\tt_b_material_delivery d\n" +
  61. "\tLEFT JOIN t_b_material_delivery_info di ON d.id = di.delivery_id\n" +
  62. "\tleft join t_b_goods_info gi on di.goods_id=gi.id\n" +
  63. "\tleft join t_b_car ca on ca.id=di.receiver_car_id\n" +
  64. "\tleft join t_s_depart dp on dp.id=d.material_dapt_id where 1=1";
  65. String yearNumSql = "SELECT\n" +
  66. "\td.material_dapt_id,\n" +
  67. "\tdi.receiver_car_id,\n" +
  68. "\tsum(gi.default_provider*di.out_num)\n" +
  69. "FROM\n" +
  70. "\tt_b_material_delivery d\n" +
  71. "\tLEFT JOIN t_b_material_delivery_info di ON d.id = di.delivery_id\n" +
  72. "\tleft join t_b_goods_info gi on di.goods_id=gi.id\n" +
  73. "\tleft join t_s_depart dp on dp.id=d.material_dapt_id where 1=1 ";
  74. if (!StringUtil.isEmpty(dept)) {
  75. sql += " and d.material_dapt_id='" + dept + "'";
  76. yearNumSql += " and d.material_dapt_id='" + dept + "'";
  77. }
  78. if (!StringUtil.isEmpty(nf) && !StringUtil.isEmpty(yf)) {
  79. String tsStart = "01";
  80. String tsEnd = "";
  81. SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
  82. SimpleDateFormat sdf1 = new SimpleDateFormat("MM");
  83. SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM");
  84. if(null==nf || "".equals(nf)){
  85. Date now = new Date();
  86. nf =sdf.format(now);
  87. yf =sdf1.format(now);
  88. }
  89. tsEnd =Integer.toString(getDaysOfMonth(sdf2.parse(nf+"-"+yf)));
  90. sql += " and d.out_time >='" + nf + "-" + yf + "-" + tsStart + "' and d.out_time<='" + nf + "-" + yf + "-" + tsEnd + "'";
  91. yearNumSql += " and d.out_time >='" + nf + "-01-" + tsStart + "' and d.out_time<='" + nf + "-" + yf + "-" + tsEnd + "'";
  92. }
  93. List<Object[]> objects = systemService.findListbySql(sql);
  94. List<Object[]> years = systemService.findListbySql(yearNumSql + "GROUP BY d.material_dapt_id,di.receiver_car_id");
  95. List<TBCarYearNumRep> yearList = new ArrayList<>();
  96. if(years != null && years.size()>0){
  97. yearList=years.stream().map(e->{
  98. TBCarYearNumRep tbCarYearNumRep = new TBCarYearNumRep();
  99. tbCarYearNumRep.setDaptId(e[0].toString());
  100. tbCarYearNumRep.setCarId(e[1].toString());
  101. tbCarYearNumRep.setYearNum(e[2].toString());
  102. return tbCarYearNumRep;
  103. }).collect(Collectors.toList());
  104. }
  105. List<TBCarUseInfoRep> repList = new ArrayList<>();
  106. if(objects != null && objects.size() > 0){
  107. for (Object[] object : objects) {
  108. TBCarUseInfoRep rep = new TBCarUseInfoRep();
  109. rep.setOutTime(object[0].toString());
  110. rep.setMaterialDaptId(object[1].toString());
  111. rep.setReceiverCarId(object[2].toString());
  112. rep.setDepartName(object[3].toString());
  113. rep.setGoodsId(object[4].toString());
  114. rep.setOutNum(object[5].toString());
  115. rep.setCarName(object[6].toString());
  116. rep.setGoodsMoney(Optional.ofNullable(object[7]).map(Object::toString).orElse("0"));
  117. repList.add(rep);
  118. }
  119. }
  120. List<TBCarUseInfoCollectRep> result = new ArrayList<>();
  121. Map<String, List<TBCarYearNumRep>> collect1 = yearList.stream().collect(Collectors.groupingBy(TBCarYearNumRep::getDaptId));
  122. Map<String, List<TBCarUseInfoRep>> collect = repList.stream().collect(Collectors.groupingBy(TBCarUseInfoRep::getDepartName));
  123. if(collect != null && collect.size() > 0){
  124. collect.forEach((name,list)->{
  125. List<TBCarYearNumRep> daptYearNum = collect1.get(list.get(0).getMaterialDaptId());
  126. List<TBCarUseRep> carInfos = new ArrayList<>();
  127. Map<String, List<TBCarUseInfoRep>> carInfo = list.stream().collect(Collectors.groupingBy(TBCarUseInfoRep::getReceiverCarId));
  128. Map<String, String> carYearNum = daptYearNum.stream().collect(Collectors.toMap(TBCarYearNumRep::getCarId,TBCarYearNumRep::getYearNum));
  129. carInfo.forEach((carId,carDatas)->{
  130. Map<String, List<TBCarUseInfoRep>> dateData = carDatas.stream().collect(Collectors.groupingBy(TBCarUseInfoRep::getOutTime));
  131. String yearNum = carYearNum.get(carId);
  132. dateData.forEach((month,datas)->{
  133. double monthNum = datas.stream().mapToDouble(e -> Double.parseDouble(e.getOutNum()) * Double.parseDouble(e.getGoodsMoney())).sum();
  134. TBCarUseRep rep = new TBCarUseRep();
  135. rep.setCarId(carDatas.get(0).getCarName());
  136. rep.setMonth(month);
  137. rep.setMonthNum(Double.toString(monthNum));
  138. rep.setYearNum(yearNum);
  139. carInfos.add(rep);
  140. });
  141. });
  142. TBCarUseInfoCollectRep collectRep = new TBCarUseInfoCollectRep();
  143. collectRep.setDepartName(name);
  144. collectRep.setDepartId(list.get(0).getMaterialDaptId());
  145. collectRep.setList(carInfos);
  146. result.add(collectRep);
  147. });
  148. }
  149. Map<String,Object> monthMap = TBCarUseInfoCollectRep(result,collect1);
  150. request.setAttribute("monthZong",monthMap.get("allMonth"));
  151. request.setAttribute("yearZong",monthMap.get("allYear"));
  152. request.setAttribute("collectList",result);
  153. return new ModelAndView("cn/com/lzt/sign/tbCarGasConsumption/tBCarUseInfoList");
  154. }
  155. public Map<String,Object> TBCarUseInfoCollectRep(List<TBCarUseInfoCollectRep> result,Map<String, List<TBCarYearNumRep>> collect1){
  156. Map<String,Object> monthMap = new HashMap<>();
  157. double allMonth = 0.00d;
  158. double allYear = 0.00d;
  159. if(result.size()>0){
  160. for (TBCarUseInfoCollectRep collectRep : result) {
  161. double monthNum = collectRep.getList().stream().mapToDouble(e -> Double.parseDouble(e.getMonthNum())).sum();
  162. double yearNum = collect1.get(collectRep.getDepartId()).stream().mapToDouble(e -> Double.parseDouble(e.getYearNum())).sum();
  163. collectRep.setMonthNum(Double.toString(monthNum));
  164. collectRep.setYearNum(Double.toString(yearNum));
  165. allMonth += monthNum;
  166. allYear += yearNum;
  167. }
  168. }
  169. monthMap.put("allMonth",allMonth);
  170. monthMap.put("allYear",allYear);
  171. return monthMap;
  172. }
  173. @RequestMapping(params = {"exportXls"})
  174. public void export(HttpServletRequest request, HttpServletResponse response) throws ParseException, IOException {
  175. //基础数据准备
  176. String nf = request.getParameter("nf");
  177. String yf = request.getParameter("yf");
  178. String dept = request.getParameter("departId");
  179. String sql = "SELECT\n" +
  180. "\tDATE_FORMAT(d.out_time,'%Y-%m'),\n" +
  181. "\td.material_dapt_id,\n" +
  182. "\tdi.receiver_car_id,\n" +
  183. "\tdp.departname,\n" +
  184. "\tdi.goods_id,\n" +
  185. "\tdi.out_num,\n" +
  186. "\tca.plate,\n" +
  187. "\tgi.default_provider\n" +
  188. "FROM\n" +
  189. "\tt_b_material_delivery d\n" +
  190. "\tLEFT JOIN t_b_material_delivery_info di ON d.id = di.delivery_id\n" +
  191. "\tleft join t_b_goods_info gi on di.goods_id=gi.id\n" +
  192. "\tleft join t_b_car ca on ca.id=di.receiver_car_id\n" +
  193. "\tleft join t_s_depart dp on dp.id=d.material_dapt_id where 1=1";
  194. String yearNumSql = "SELECT\n" +
  195. "\td.material_dapt_id,\n" +
  196. "\tdi.receiver_car_id,\n" +
  197. "\tsum(gi.default_provider*di.out_num)\n" +
  198. "FROM\n" +
  199. "\tt_b_material_delivery d\n" +
  200. "\tLEFT JOIN t_b_material_delivery_info di ON d.id = di.delivery_id\n" +
  201. "\tleft join t_b_goods_info gi on di.goods_id=gi.id\n" +
  202. "\tleft join t_s_depart dp on dp.id=d.material_dapt_id where 1=1 ";
  203. if (!StringUtil.isEmpty(dept)) {
  204. sql += " and d.material_dapt_id='" + dept + "'";
  205. yearNumSql += " and d.material_dapt_id='" + dept + "'";
  206. }
  207. if (!StringUtil.isEmpty(nf) && !StringUtil.isEmpty(yf)) {
  208. String tsStart = "01";
  209. String tsEnd = "";
  210. SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
  211. SimpleDateFormat sdf1 = new SimpleDateFormat("MM");
  212. SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM");
  213. if(null==nf || "".equals(nf)){
  214. Date now = new Date();
  215. nf =sdf.format(now);
  216. yf =sdf1.format(now);
  217. }
  218. tsEnd =Integer.toString(getDaysOfMonth(sdf2.parse(nf+"-"+yf)));
  219. sql += " and d.out_time >='" + nf + "-" + yf + "-" + tsStart + "' and d.out_time<='" + nf + "-" + yf + "-" + tsEnd + "'";
  220. yearNumSql += " and d.out_time >='" + nf + "-01-" + tsStart + "' and d.out_time<='" + nf + "-" + yf + "-" + tsEnd + "'";
  221. }
  222. List<Object[]> objects = systemService.findListbySql(sql);
  223. List<Object[]> years = systemService.findListbySql(yearNumSql + "GROUP BY d.material_dapt_id,di.receiver_car_id"); List<TBCarYearNumRep> yearList=years.stream().map(e->{
  224. TBCarYearNumRep tbCarYearNumRep = new TBCarYearNumRep();
  225. tbCarYearNumRep.setDaptId(e[0].toString());
  226. tbCarYearNumRep.setCarId(e[1].toString());
  227. tbCarYearNumRep.setYearNum(e[2].toString());
  228. return tbCarYearNumRep;
  229. }).collect(Collectors.toList());
  230. List<TBCarUseInfoRep> repList = new ArrayList<>();
  231. for (Object[] object : objects) {
  232. TBCarUseInfoRep rep = new TBCarUseInfoRep();
  233. rep.setOutTime(object[0].toString());
  234. rep.setMaterialDaptId(object[1].toString());
  235. rep.setReceiverCarId(object[2].toString());
  236. rep.setDepartName(object[3].toString());
  237. rep.setGoodsId(object[4].toString());
  238. rep.setOutNum(object[5].toString());
  239. rep.setCarName(object[6].toString());
  240. rep.setGoodsMoney(Optional.ofNullable(object[7]).map(Object::toString).orElse("0"));
  241. repList.add(rep);
  242. }
  243. List<TBCarUseInfoCollectRep> result = new ArrayList<>();
  244. Map<String, List<TBCarYearNumRep>> collect1 = yearList.stream().collect(Collectors.groupingBy(TBCarYearNumRep::getDaptId));
  245. Map<String, List<TBCarUseInfoRep>> collect = repList.stream().collect(Collectors.groupingBy(TBCarUseInfoRep::getDepartName));
  246. collect.forEach((name,list)->{
  247. List<TBCarYearNumRep> daptYearNum = collect1.get(list.get(0).getMaterialDaptId());
  248. List<TBCarUseRep> carInfos = new ArrayList<>();
  249. Map<String, List<TBCarUseInfoRep>> carInfo = list.stream().collect(Collectors.groupingBy(TBCarUseInfoRep::getReceiverCarId));
  250. Map<String, String> carYearNum = daptYearNum.stream().collect(Collectors.toMap(TBCarYearNumRep::getCarId,TBCarYearNumRep::getYearNum));
  251. carInfo.forEach((carId,carDatas)->{
  252. Map<String, List<TBCarUseInfoRep>> dateData = carDatas.stream().collect(Collectors.groupingBy(TBCarUseInfoRep::getOutTime));
  253. String yearNum = carYearNum.get(carId);
  254. dateData.forEach((month,datas)->{
  255. double monthNum = datas.stream().mapToDouble(e -> Double.parseDouble(e.getOutNum()) * Double.parseDouble(e.getGoodsMoney())).sum();
  256. TBCarUseRep rep = new TBCarUseRep();
  257. rep.setCarId(carDatas.get(0).getCarName());
  258. rep.setMonth(month);
  259. rep.setMonthNum(Double.toString(monthNum));
  260. rep.setYearNum(yearNum);
  261. carInfos.add(rep);
  262. });
  263. });
  264. TBCarUseInfoCollectRep collectRep = new TBCarUseInfoCollectRep();
  265. collectRep.setDepartName(name);
  266. collectRep.setDepartId(list.get(0).getMaterialDaptId());
  267. collectRep.setList(carInfos);
  268. result.add(collectRep);
  269. });
  270. Map<String,Object> monthMap = TBCarUseInfoCollectRep(result,collect1);
  271. HSSFWorkbook wb = new HSSFWorkbook();
  272. HSSFSheet sheet = wb.createSheet("车辆耗用明细表");
  273. HSSFRow row1 = sheet.createRow(0);
  274. HSSFCell cell = row1.createCell(0);
  275. cell.setCellValue("车辆耗用明细表");
  276. sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 2));
  277. HSSFRow row2 = sheet.createRow(1);
  278. row2.createCell(0).setCellValue("序号");
  279. row2.createCell(1).setCellValue("月份");
  280. row2.createCell(2).setCellValue("领料车号");
  281. row2.createCell(3).setCellValue("累计金额");
  282. // row2.createCell(4).setCellValue("年度累计金额");
  283. int l = 2;
  284. for (int i = 2; i < result.size() + 2; i++) {
  285. TBCarUseInfoCollectRep rep = result.get(i - 2);
  286. HSSFRow row3 = sheet.createRow(l);
  287. l++;
  288. HSSFCell cel3 = row3.createCell(0);
  289. cel3.setCellValue(rep.getDepartName());
  290. List<TBCarUseRep> list = rep.getList();
  291. for (int k = 0; k < list.size(); k++) {
  292. TBCarUseRep tbCarUseRep = list.get(k);
  293. HSSFRow row4 = sheet.createRow(l);
  294. l++;
  295. row4.createCell(0).setCellValue(k + 1);
  296. row4.createCell(1).setCellValue(tbCarUseRep.getMonth());
  297. row4.createCell(2).setCellValue(tbCarUseRep.getCarId());
  298. row4.createCell(3).setCellValue(tbCarUseRep.getMonthNum());
  299. // row4.createCell(4).setCellValue(tbCarUseRep.getYearNum());
  300. }
  301. HSSFRow row5 = sheet.createRow(l);
  302. l++;
  303. row5.createCell(0).setCellValue("");
  304. row5.createCell(1).setCellValue("");
  305. row5.createCell(2).setCellValue("");
  306. row5.createCell(3).setCellValue("小计:"+rep.getMonthNum());
  307. // row5.createCell(4).setCellValue(rep.getYearNum());
  308. }
  309. HSSFRow row6 = sheet.createRow(l);
  310. row6.createCell(0).setCellValue("年度累计金额");
  311. row6.createCell(1).setCellValue("");
  312. row6.createCell(2).setCellValue("");
  313. row6.createCell(3).setCellValue(monthMap.get("allMonth").toString());
  314. // row6.createCell(4).setCellValue(allYear);
  315. ServletOutputStream servletOutputStream = response.getOutputStream();
  316. response.reset();
  317. response.setHeader("Content-disposition", "attachment; filename="+ URLEncoder.encode("车辆耗用明细表")+".xls");
  318. response.setContentType("application/octet-stream");
  319. wb.write((OutputStream) servletOutputStream);
  320. servletOutputStream.close();
  321. }
  322. }