StatisticsController.java 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. package cn.com.lzt.sign.controller;
  2. import cn.com.lzt.oilconsumption.controller.TBCarGasRefuelController;
  3. import cn.com.lzt.oilconsumption.entity.TBCarGasRep;
  4. import cn.com.lzt.sign.entity.TBMaterialInfoEntity;
  5. import cn.com.lzt.sign.page.Statistics;
  6. import cn.com.lzt.sign.page.StatisticsInfo;
  7. import com.alibaba.fastjson.JSONObject;
  8. import com.daju.base.BaseController;
  9. import org.apache.poi.hssf.usermodel.HSSFCell;
  10. import org.apache.poi.hssf.usermodel.HSSFRow;
  11. import org.apache.poi.hssf.usermodel.HSSFSheet;
  12. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  13. import org.apache.poi.ss.util.CellRangeAddress;
  14. import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
  15. import org.jeecgframework.core.common.model.json.DataGrid;
  16. import org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil;
  17. import org.jeecgframework.web.system.service.SystemService;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.stereotype.Controller;
  20. import org.springframework.ui.ModelMap;
  21. import org.springframework.web.bind.annotation.RequestMapping;
  22. import org.springframework.web.servlet.ModelAndView;
  23. import javax.servlet.ServletOutputStream;
  24. import javax.servlet.http.HttpServletRequest;
  25. import javax.servlet.http.HttpServletResponse;
  26. import java.io.IOException;
  27. import java.io.OutputStream;
  28. import java.net.URLEncoder;
  29. import java.text.ParseException;
  30. import java.text.SimpleDateFormat;
  31. import java.util.*;
  32. import java.util.logging.Logger;
  33. import java.util.stream.Collectors;
  34. /**
  35. * @Description: TODO
  36. * @author: scott
  37. * @date: 2021年11月20日 13:05
  38. */
  39. @Controller
  40. @RequestMapping({"/statisticsController"})
  41. public class StatisticsController extends BaseController {
  42. private static final Logger logger = Logger.getLogger(String.valueOf(StatisticsController.class));
  43. @Autowired
  44. private SystemService systemService;
  45. @RequestMapping(params = {"list"})
  46. public ModelAndView list(HttpServletRequest request) throws ParseException {
  47. //基础数据准备
  48. String nf = request.getParameter("nf");
  49. String yf = request.getParameter("yf");
  50. String dept = request.getParameter("departId");
  51. String tsEnd = "";
  52. SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
  53. SimpleDateFormat sdf1 = new SimpleDateFormat("MM");
  54. SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM");
  55. Date now = new Date();
  56. if(null==nf || "".equals(nf)){
  57. nf =sdf.format(now);
  58. }
  59. if(null!=yf && !"".equals(yf)) {
  60. tsEnd = nf + "-" + yf+"-"+Integer.toString(getDaysOfMonth(sdf2.parse(nf + "-" + yf)));
  61. }else{
  62. tsEnd = nf + "-12-31";
  63. }
  64. //查询科室
  65. String sql_wh = "SELECT\n" +
  66. "\tbb.material_dapt_id departId,\n" +
  67. "\t( SELECT departname FROM t_s_depart WHERE t_s_depart.id = bb.material_dapt_id ) departname,\n" +
  68. "\tsubstr(bb.out_time,1,7),\n" +
  69. "\t\t(\n" +
  70. " SELECT GROUP_CONCAT( id ) FROM t_b_material_delivery_info WHERE delivery_id in (\n" +
  71. "\n" +
  72. "SELECT\n" +
  73. "\t\tid\n" +
  74. "\tFROM\n" +
  75. "\t\tt_b_material_delivery aa \n" +
  76. "\tWHERE\n" +
  77. "\t\n" +
  78. "\t\t substr(aa.out_time,1,7) = substr(bb.out_time,1,7)\n" +
  79. "\t\tAND bb.material_dapt_id = aa.material_dapt_id) \n" +
  80. "\t) goodsmouth \n" +
  81. "\t\n" +
  82. "FROM\n" +
  83. "\tt_b_material_delivery bb\n" +
  84. "where bb.out_time >='"+nf+"-01-01' and bb.out_time<='"+tsEnd+"'" ;
  85. if(null!=dept && !"".equals(dept)){
  86. sql_wh+=" and bb.material_dapt_id = '"+dept+"'";
  87. }
  88. if(null!=yf && !"".equals(yf)){
  89. sql_wh+=" and substr(bb.out_time,6,2) = '"+yf+"'";
  90. }
  91. sql_wh+=" group by bb.material_dapt_id,substr(bb.out_time,1,7) order by bb.material_dapt_id,substr(bb.out_time,6,2)";
  92. List<Object[]> departList = systemService.findListbySql(sql_wh);
  93. List<Statistics> collectList = departList.stream().map(e -> {
  94. Statistics sta = new Statistics();
  95. if(e[0]!=null){
  96. sta.setDepartId(e[0].toString());
  97. }
  98. if(e[1]!=null){
  99. sta.setDepartname(e[1].toString());
  100. }
  101. if(e[2]!=null) {
  102. sta.setYearmonth(e[2].toString());
  103. }
  104. if(e[3]!=null){
  105. sta.setGoodsmonth(e[3].toString());
  106. }
  107. return sta;
  108. }).collect(Collectors.toList());
  109. Double monthZong = 0.0;
  110. // Double yearZong = 0.0;
  111. //查询每个科室的货品价格(按货品类型)(年)
  112. for(int i=0;i<collectList.size();i++){
  113. Double yearhnum =0.0 ;
  114. Double monthnum =0.0 ;
  115. String goodsmonth = collectList.get(i).getGoodsmonth();
  116. if(goodsmonth!=null && !"".equals(goodsmonth)){
  117. String[] goodsAry = goodsmonth.split(",");
  118. goodsmonth ="";
  119. for(int j=0;j<goodsAry.length;j++){
  120. if(j==0){
  121. goodsmonth = "'"+goodsAry[j]+"'";
  122. }else{
  123. goodsmonth +=",'"+goodsAry[j]+"'";
  124. }
  125. }
  126. }
  127. String infomouth = "select info.typename,sum(info.money) money from (\n" +
  128. "SELECT\n" +
  129. "\t out_num*( SELECT default_provider FROM t_b_goods_info WHERE t_b_goods_info.id = t_b_material_delivery_info.goods_id ) money,\n" +
  130. " (select typename from t_s_type where t_s_type.typecode = ( SELECT belong_category FROM t_b_goods_info WHERE t_b_goods_info.id = t_b_material_delivery_info.goods_id )) typename\n" +
  131. "FROM\n" +
  132. "\t t_b_material_delivery_info \n" +
  133. "WHERE\n" +
  134. "\t id IN ("+goodsmonth+") ) info GROUP BY info.typename";
  135. List<Object[]> infomouthList = systemService.findListbySql(infomouth);
  136. List<StatisticsInfo> goodsmouthList = infomouthList.stream().map(e -> {
  137. StatisticsInfo sta = new StatisticsInfo();
  138. if(e[0]!=null){
  139. sta.setGoodsTypeName(e[0].toString());
  140. }
  141. if(e[1]!=null){
  142. sta.setMonthMoney(e[1].toString());
  143. }
  144. return sta;
  145. }).collect(Collectors.toList());
  146. for(int k=0;k<goodsmouthList.size();k++){
  147. monthnum+=(Double.valueOf(goodsmouthList.get(k).getMonthMoney()));
  148. }
  149. monthZong+=monthnum;
  150. collectList.get(i).setYearnum(yearhnum);
  151. collectList.get(i).setMonthnum(monthnum);
  152. collectList.get(i).setStatisticsInfoList(goodsmouthList);
  153. collectList.get(i).setMonth(yf);
  154. collectList.get(i).setYear(nf);
  155. }
  156. request.setAttribute("monthZong",monthZong);
  157. request.setAttribute("collectList",collectList);
  158. return new ModelAndView("cn/com/lzt/sign/statistics/type-list");
  159. }
  160. @RequestMapping(params = {"claim"})
  161. public ModelAndView claim(HttpServletRequest request) throws ParseException {
  162. //基础数据准备
  163. String nf = request.getParameter("nf");
  164. String yf = request.getParameter("yf");
  165. String dept = request.getParameter("departId");
  166. String tsEnd = "";
  167. SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
  168. SimpleDateFormat sdf1 = new SimpleDateFormat("MM");
  169. SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM");
  170. if(null==nf || "".equals(nf)){
  171. Date now = new Date();
  172. nf =sdf.format(now);
  173. }
  174. if(null!=yf && !"".equals(yf)) {
  175. tsEnd = nf + "-" + yf+"-"+Integer.toString(getDaysOfMonth(sdf2.parse(nf + "-" + yf)));
  176. }else{
  177. tsEnd = nf + "-12-31";
  178. }
  179. //查询科室
  180. String sql_wh = "SELECT\n" +
  181. "\tbb.material_dapt_id departId,\n" +
  182. "\t( SELECT departname FROM t_s_depart WHERE t_s_depart.id = bb.material_dapt_id ) departname,\n" +
  183. "\tsubstr(bb.out_time,1,7),\n" +
  184. "\t\t(\n" +
  185. " SELECT GROUP_CONCAT( id ) FROM t_b_material_delivery_info WHERE delivery_id in (\n" +
  186. "\n" +
  187. "SELECT\n" +
  188. "\t\tid\n" +
  189. "\tFROM\n" +
  190. "\t\tt_b_material_delivery aa \n" +
  191. "\tWHERE\n" +
  192. "\t\n" +
  193. "\t\t substr(aa.out_time,1,7) = substr(bb.out_time,1,7)\n" +
  194. "\t\tAND bb.material_dapt_id = aa.material_dapt_id) \n" +
  195. "\t) goodsmouth \n" +
  196. "\t\n" +
  197. "FROM\n" +
  198. "\tt_b_material_delivery bb\n" +
  199. "where bb.out_time >='"+nf+"-01-01' and bb.out_time<='"+tsEnd+"'" ;
  200. if(null!=dept && !"".equals(dept)){
  201. sql_wh+=" and bb.material_dapt_id = '"+dept+"'";
  202. }
  203. if(null!=yf && !"".equals(yf)){
  204. sql_wh+=" and substr(bb.out_time,6,2) = '"+yf+"'";
  205. }
  206. sql_wh+=" group by bb.material_dapt_id,substr(bb.out_time,1,7) order by bb.material_dapt_id,substr(bb.out_time,6,2)";
  207. List<Object[]> departList = systemService.findListbySql(sql_wh);
  208. List<Statistics> collectList = departList.stream().map(e -> {
  209. Statistics sta = new Statistics();
  210. if(e[0]!=null){
  211. sta.setDepartId(e[0].toString());
  212. }
  213. if(e[1]!=null){
  214. sta.setDepartname(e[1].toString());
  215. }
  216. if(e[2]!=null) {
  217. sta.setYearmonth(e[2].toString());
  218. }
  219. if(e[3]!=null){
  220. sta.setGoodsmonth(e[3].toString());
  221. }
  222. return sta;
  223. }).collect(Collectors.toList());
  224. Double monthZong = 0.0;
  225. // Double yearZong = 0.0;
  226. for(int i=0;i<collectList.size();i++) {
  227. Double yearhnum = 0.0;
  228. Double monthnum = 0.0;
  229. String goodsmonth = collectList.get(i).getGoodsmonth();
  230. if (goodsmonth != null && !"".equals(goodsmonth)) {
  231. String[] goodsAry = goodsmonth.split(",");
  232. goodsmonth = "";
  233. for (int j = 0; j < goodsAry.length; j++) {
  234. if (j == 0) {
  235. goodsmonth = "'" + goodsAry[j] + "'";
  236. } else {
  237. goodsmonth += ",'" + goodsAry[j] + "'";
  238. }
  239. }
  240. }
  241. String infomouth = " select sum(out_num*( SELECT default_provider FROM t_b_goods_info WHERE t_b_goods_info.id = t_b_material_delivery_info.goods_id )) money,( SELECT goods_name FROM t_b_goods_info WHERE t_b_goods_info.id = t_b_material_delivery_info.goods_id ) goodsname FROM \n" +
  242. " t_b_material_delivery_info \n" +
  243. " WHERE " +
  244. "\t id IN (" + goodsmonth + ") group by goods_id";
  245. List<Object[]> infomouthList = systemService.findListbySql(infomouth);
  246. List<StatisticsInfo> goodsmouthList = infomouthList.stream().map(e -> {
  247. StatisticsInfo sta = new StatisticsInfo();
  248. if (e[0] != null) {
  249. sta.setMonthMoney(e[0].toString());
  250. }
  251. if (e[1] != null) {
  252. sta.setGoodsName(e[1].toString());
  253. }
  254. return sta;
  255. }).collect(Collectors.toList());
  256. for (int k = 0; k < goodsmouthList.size(); k++) {
  257. monthnum += (Double.valueOf(goodsmouthList.get(k).getMonthMoney()));
  258. }
  259. monthZong += monthnum;
  260. collectList.get(i).setYearnum(yearhnum);
  261. collectList.get(i).setMonthnum(monthnum);
  262. collectList.get(i).setStatisticsInfoList(goodsmouthList);
  263. collectList.get(i).setMonth(yf);
  264. collectList.get(i).setYear(nf);
  265. }
  266. request.setAttribute("monthZong",monthZong);
  267. // request.setAttribute("yearZong",yearZong);
  268. request.setAttribute("collectList",collectList);
  269. return new ModelAndView("cn/com/lzt/sign/statistics/claim-list");
  270. }
  271. public static int getDaysOfMonth(Date date) {
  272. Calendar calendar = Calendar.getInstance();
  273. calendar.setTime(date);
  274. return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
  275. }
  276. @RequestMapping(params = {"exportXls"})
  277. public void exportXls(String id, HttpServletRequest request, HttpServletResponse response, ModelMap map) throws IOException, ParseException {
  278. //基础数据准备
  279. String nf = request.getParameter("nfinfo");
  280. String yf = request.getParameter("yfinfo");
  281. String dept = request.getParameter("departIdinfo");
  282. String tsEnd = "";
  283. SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
  284. SimpleDateFormat sdf1 = new SimpleDateFormat("MM");
  285. SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM");
  286. if (null == nf || "".equals(nf)) {
  287. Date now = new Date();
  288. nf = sdf.format(now);
  289. }
  290. if(null!=yf && !"".equals(yf)) {
  291. tsEnd = nf + "-" + yf+"-"+Integer.toString(getDaysOfMonth(sdf2.parse(nf + "-" + yf)));
  292. }else{
  293. tsEnd = nf + "-12-31";
  294. }
  295. //查询科室
  296. String sql_wh = "SELECT\n" +
  297. "\tbb.material_dapt_id departId,\n" +
  298. "\t( SELECT departname FROM t_s_depart WHERE t_s_depart.id = bb.material_dapt_id ) departname,\n" +
  299. "\tsubstr(bb.out_time,1,7),\n" +
  300. "\t\t(\n" +
  301. " SELECT GROUP_CONCAT( id ) FROM t_b_material_delivery_info WHERE delivery_id in (\n" +
  302. "\n" +
  303. "SELECT\n" +
  304. "\t\tid\n" +
  305. "\tFROM\n" +
  306. "\t\tt_b_material_delivery aa \n" +
  307. "\tWHERE\n" +
  308. "\t\n" +
  309. "\t\t substr(aa.out_time,1,7) = substr(bb.out_time,1,7)\n" +
  310. "\t\tAND bb.material_dapt_id = aa.material_dapt_id) \n" +
  311. "\t) goodsmouth \n" +
  312. "\t\n" +
  313. "FROM\n" +
  314. "\tt_b_material_delivery bb\n" +
  315. "where bb.out_time >='"+nf+"-01-01' and bb.out_time<='"+tsEnd+"'" ;
  316. if(null!=dept && !"".equals(dept)){
  317. sql_wh+=" and bb.material_dapt_id = '"+dept+"'";
  318. }
  319. if(null!=yf && !"".equals(yf)){
  320. sql_wh+=" and substr(bb.out_time,6,2) = '"+yf+"'";
  321. }
  322. sql_wh+=" group by bb.material_dapt_id,substr(bb.out_time,1,7) order by bb.material_dapt_id,substr(bb.out_time,6,2)";
  323. List<Object[]> departList = systemService.findListbySql(sql_wh);
  324. List<Statistics> collectList = departList.stream().map(e -> {
  325. Statistics sta = new Statistics();
  326. if(e[0]!=null){
  327. sta.setDepartId(e[0].toString());
  328. }
  329. if(e[1]!=null){
  330. sta.setDepartname(e[1].toString());
  331. }
  332. if(e[2]!=null) {
  333. sta.setYearmonth(e[2].toString());
  334. }
  335. if(e[3]!=null){
  336. sta.setGoodsmonth(e[3].toString());
  337. }
  338. return sta;
  339. }).collect(Collectors.toList());
  340. Double monthZong = 0.0;
  341. // Double yearZong = 0.0;
  342. //查询每个科室的货品价格(按货品类型)(年)
  343. for(int i=0;i<collectList.size();i++){
  344. Double yearhnum =0.0 ;
  345. Double monthnum =0.0 ;
  346. String goodsmonth = collectList.get(i).getGoodsmonth();
  347. if(goodsmonth!=null && !"".equals(goodsmonth)){
  348. String[] goodsAry = goodsmonth.split(",");
  349. goodsmonth ="";
  350. for(int j=0;j<goodsAry.length;j++){
  351. if(j==0){
  352. goodsmonth = "'"+goodsAry[j]+"'";
  353. }else{
  354. goodsmonth +=",'"+goodsAry[j]+"'";
  355. }
  356. }
  357. }
  358. String infomouth = "select info.typename,sum(info.money) money from (\n" +
  359. "SELECT\n" +
  360. "\t out_num*( SELECT default_provider FROM t_b_goods_info WHERE t_b_goods_info.id = t_b_material_delivery_info.goods_id ) money,\n" +
  361. " (select typename from t_s_type where t_s_type.typecode = ( SELECT belong_category FROM t_b_goods_info WHERE t_b_goods_info.id = t_b_material_delivery_info.goods_id )) typename\n" +
  362. "FROM\n" +
  363. "\t t_b_material_delivery_info \n" +
  364. "WHERE\n" +
  365. "\t id IN ("+goodsmonth+") ) info GROUP BY info.typename";
  366. List<Object[]> infomouthList = systemService.findListbySql(infomouth);
  367. List<StatisticsInfo> goodsmouthList = infomouthList.stream().map(e -> {
  368. StatisticsInfo sta = new StatisticsInfo();
  369. if(e[0]!=null){
  370. sta.setGoodsTypeName(e[0].toString());
  371. }
  372. if(e[1]!=null){
  373. sta.setMonthMoney(e[1].toString());
  374. }
  375. return sta;
  376. }).collect(Collectors.toList());
  377. for(int k=0;k<goodsmouthList.size();k++){
  378. monthnum+=(Double.valueOf(goodsmouthList.get(k).getMonthMoney()));
  379. }
  380. monthZong+=monthnum;
  381. collectList.get(i).setYearnum(yearhnum);
  382. collectList.get(i).setMonthnum(monthnum);
  383. collectList.get(i).setStatisticsInfoList(goodsmouthList);
  384. collectList.get(i).setMonth(yf);
  385. collectList.get(i).setYear(nf);
  386. }
  387. HSSFWorkbook wb = new HSSFWorkbook();
  388. HSSFSheet sheet = wb.createSheet("物料耗用分类明细表");
  389. HSSFRow row1 = sheet.createRow(0);
  390. HSSFCell cell = row1.createCell(0);
  391. cell.setCellValue("物料耗用分类明细表");
  392. sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 2));
  393. HSSFRow row2 = sheet.createRow(1);
  394. row2.createCell(0).setCellValue("序号");
  395. row2.createCell(1).setCellValue("月份");
  396. row2.createCell(2).setCellValue("货品分类名称");
  397. row2.createCell(3).setCellValue("累计金额");
  398. int l = 2;
  399. for (int i = 2; i < collectList.size() + 2; i++) {
  400. Statistics statistics = collectList.get(i - 2);
  401. HSSFRow row3 = sheet.createRow(l);
  402. l++;
  403. HSSFCell cel3 = row3.createCell(0);
  404. cel3.setCellValue(statistics.getDepartname());
  405. List<StatisticsInfo> statisticsInfoList = statistics.getStatisticsInfoList();
  406. for (int k = 0; k < statisticsInfoList.size(); k++) {
  407. StatisticsInfo statisticsInfo = statisticsInfoList.get(k);
  408. HSSFRow row4 = sheet.createRow(l);
  409. l++;
  410. row4.createCell(0).setCellValue(k + 1);
  411. row4.createCell(1).setCellValue(statistics.getYearmonth());
  412. row4.createCell(2).setCellValue(statisticsInfo.getGoodsTypeName());
  413. row4.createCell(3).setCellValue(statisticsInfo.getMonthMoney());
  414. // row4.createCell(4).setCellValue(statisticsInfo.getYearMoney());
  415. }
  416. HSSFRow row5 = sheet.createRow(l);
  417. l++;
  418. row5.createCell(0).setCellValue("");
  419. row5.createCell(1).setCellValue("");
  420. row5.createCell(2).setCellValue("");
  421. row5.createCell(3).setCellValue("小计:"+statistics.getMonthnum());
  422. // row5.createCell(4).setCellValue(statistics.getYearnum());
  423. }
  424. HSSFRow row6 = sheet.createRow(l);
  425. row6.createCell(0).setCellValue("年度累计金额");
  426. row6.createCell(1).setCellValue("");
  427. row6.createCell(2).setCellValue("");
  428. row6.createCell(3).setCellValue(""+monthZong);
  429. // row6.createCell(4).setCellValue("小计:"+yearZong);
  430. ServletOutputStream servletOutputStream = response.getOutputStream();
  431. response.reset();
  432. response.setHeader("Content-disposition", "attachment; filename="+ URLEncoder.encode("物料耗用分类明细表.xls"));
  433. response.setContentType("application/octet-stream");
  434. wb.write((OutputStream) servletOutputStream);
  435. servletOutputStream.close();
  436. }
  437. @RequestMapping(params = {"exportXlsInfo"})
  438. public void exportXlsInfo(String id, HttpServletRequest request, HttpServletResponse response, ModelMap map) throws IOException, ParseException {
  439. //基础数据准备
  440. String nf = request.getParameter("nfinfo");
  441. String yf = request.getParameter("yfinfo");
  442. String dept = request.getParameter("departIdinfo");
  443. String tsEnd = "";
  444. SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
  445. SimpleDateFormat sdf1 = new SimpleDateFormat("MM");
  446. SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM");
  447. if(null==nf || "".equals(nf)){
  448. Date now = new Date();
  449. nf =sdf.format(now);
  450. }
  451. if(null!=yf && !"".equals(yf)) {
  452. tsEnd = nf + "-" + yf+"-"+Integer.toString(getDaysOfMonth(sdf2.parse(nf + "-" + yf)));
  453. }else{
  454. tsEnd = nf + "-12-31";
  455. }
  456. //查询科室
  457. String sql_wh = "SELECT\n" +
  458. "\tbb.material_dapt_id departId,\n" +
  459. "\t( SELECT departname FROM t_s_depart WHERE t_s_depart.id = bb.material_dapt_id ) departname,\n" +
  460. "\tsubstr(bb.out_time,1,7),\n" +
  461. "\t\t(\n" +
  462. " SELECT GROUP_CONCAT( id ) FROM t_b_material_delivery_info WHERE delivery_id in (\n" +
  463. "\n" +
  464. "SELECT\n" +
  465. "\t\tid\n" +
  466. "\tFROM\n" +
  467. "\t\tt_b_material_delivery aa \n" +
  468. "\tWHERE\n" +
  469. "\t\n" +
  470. "\t\t substr(aa.out_time,1,7) = substr(bb.out_time,1,7)\n" +
  471. "\t\tAND bb.material_dapt_id = aa.material_dapt_id) \n" +
  472. "\t) goodsmouth \n" +
  473. "\t\n" +
  474. "FROM\n" +
  475. "\tt_b_material_delivery bb\n" +
  476. "where bb.out_time >='"+nf+"-01-01' and bb.out_time<='"+tsEnd+"'" ;
  477. if(null!=dept && !"".equals(dept)){
  478. sql_wh+=" and bb.material_dapt_id = '"+dept+"'";
  479. }
  480. if(null!=yf && !"".equals(yf)){
  481. sql_wh+=" and substr(bb.out_time,6,2) = '"+yf+"'";
  482. }
  483. sql_wh+=" group by bb.material_dapt_id,substr(bb.out_time,1,7) order by bb.material_dapt_id,substr(bb.out_time,6,2)";
  484. List<Object[]> departList = systemService.findListbySql(sql_wh);
  485. List<Statistics> collectList = departList.stream().map(e -> {
  486. Statistics sta = new Statistics();
  487. if(e[0]!=null){
  488. sta.setDepartId(e[0].toString());
  489. }
  490. if(e[1]!=null){
  491. sta.setDepartname(e[1].toString());
  492. }
  493. if(e[2]!=null) {
  494. sta.setYearmonth(e[2].toString());
  495. }
  496. if(e[3]!=null){
  497. sta.setGoodsmonth(e[3].toString());
  498. }
  499. return sta;
  500. }).collect(Collectors.toList());
  501. Double monthZong = 0.0;
  502. // Double yearZong = 0.0;
  503. for(int i=0;i<collectList.size();i++) {
  504. Double yearhnum = 0.0;
  505. Double monthnum = 0.0;
  506. String goodsmonth = collectList.get(i).getGoodsmonth();
  507. if (goodsmonth != null && !"".equals(goodsmonth)) {
  508. String[] goodsAry = goodsmonth.split(",");
  509. goodsmonth = "";
  510. for (int j = 0; j < goodsAry.length; j++) {
  511. if (j == 0) {
  512. goodsmonth = "'" + goodsAry[j] + "'";
  513. } else {
  514. goodsmonth += ",'" + goodsAry[j] + "'";
  515. }
  516. }
  517. }
  518. String infomouth = " select sum(out_num*( SELECT default_provider FROM t_b_goods_info WHERE t_b_goods_info.id = t_b_material_delivery_info.goods_id )) money,( SELECT goods_name FROM t_b_goods_info WHERE t_b_goods_info.id = t_b_material_delivery_info.goods_id ) goodsname FROM \n" +
  519. " t_b_material_delivery_info \n" +
  520. " WHERE " +
  521. "\t id IN (" + goodsmonth + ") group by goods_id";
  522. List<Object[]> infomouthList = systemService.findListbySql(infomouth);
  523. List<StatisticsInfo> goodsmouthList = infomouthList.stream().map(e -> {
  524. StatisticsInfo sta = new StatisticsInfo();
  525. if (e[0] != null) {
  526. sta.setMonthMoney(e[0].toString());
  527. }
  528. if (e[1] != null) {
  529. sta.setGoodsName(e[1].toString());
  530. }
  531. return sta;
  532. }).collect(Collectors.toList());
  533. for (int k = 0; k < goodsmouthList.size(); k++) {
  534. monthnum += (Double.valueOf(goodsmouthList.get(k).getMonthMoney()));
  535. }
  536. monthZong += monthnum;
  537. collectList.get(i).setYearnum(yearhnum);
  538. collectList.get(i).setMonthnum(monthnum);
  539. collectList.get(i).setStatisticsInfoList(goodsmouthList);
  540. collectList.get(i).setMonth(yf);
  541. collectList.get(i).setYear(nf);
  542. }
  543. HSSFWorkbook wb = new HSSFWorkbook();
  544. HSSFSheet sheet = wb.createSheet("物料领用明細表");
  545. HSSFRow row1 = sheet.createRow(0);
  546. HSSFCell cell = row1.createCell(0);
  547. cell.setCellValue("物料领用明細表");
  548. sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 2));
  549. HSSFRow row2 = sheet.createRow(1);
  550. row2.createCell(0).setCellValue("序号");
  551. row2.createCell(1).setCellValue("月份");
  552. row2.createCell(2).setCellValue("货品名称");
  553. row2.createCell(3).setCellValue("累计金额");
  554. // row2.createCell(4).setCellValue("年度累计金额");
  555. int l = 2;
  556. for (int i = 2; i < collectList.size() + 2; i++) {
  557. Statistics statistics = collectList.get(i - 2);
  558. HSSFRow row3 = sheet.createRow(l);
  559. l++;
  560. HSSFCell cel3 = row3.createCell(0);
  561. cel3.setCellValue(statistics.getDepartname());
  562. List<StatisticsInfo> statisticsInfoList = statistics.getStatisticsInfoList();
  563. for (int k = 0; k < statisticsInfoList.size(); k++) {
  564. StatisticsInfo statisticsInfo = statisticsInfoList.get(k);
  565. HSSFRow row4 = sheet.createRow(l);
  566. l++;
  567. row4.createCell(0).setCellValue(k + 1);
  568. row4.createCell(1).setCellValue(statistics.getYearmonth());
  569. row4.createCell(2).setCellValue(statisticsInfo.getGoodsName());
  570. row4.createCell(3).setCellValue(statisticsInfo.getMonthMoney());
  571. // row4.createCell(4).setCellValue(statisticsInfo.getYearMoney());
  572. }
  573. HSSFRow row5 = sheet.createRow(l);
  574. l++;
  575. row5.createCell(0).setCellValue("");
  576. row5.createCell(1).setCellValue("");
  577. row5.createCell(2).setCellValue("");
  578. row5.createCell(3).setCellValue("小计:"+statistics.getMonthnum());
  579. // row5.createCell(4).setCellValue("小计:"+statistics.getYearnum());
  580. }
  581. HSSFRow row6 = sheet.createRow(l);
  582. row6.createCell(0).setCellValue("年度累计金额");
  583. row6.createCell(1).setCellValue("");
  584. row6.createCell(2).setCellValue("");
  585. row6.createCell(3).setCellValue(monthZong);
  586. // row6.createCell(4).setCellValue(yearZong);
  587. ServletOutputStream servletOutputStream = response.getOutputStream();
  588. response.reset();
  589. response.setHeader("Content-disposition", "attachment; filename="+URLEncoder.encode("物料领用明细表.xls"));
  590. response.setContentType("application/octet-stream");
  591. wb.write((OutputStream) servletOutputStream);
  592. servletOutputStream.close();
  593. }
  594. }