TWarehousebillsfeesServiceImpl.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. package com.ruoyi.warehouseBusiness.service.impl;
  2. import com.ruoyi.basicData.mapper.TFeesMapper;
  3. import com.ruoyi.common.config.RuoYiConfig;
  4. import com.ruoyi.common.core.domain.AjaxResult;
  5. import com.ruoyi.common.utils.DateUtils;
  6. import com.ruoyi.common.utils.StringUtils;
  7. import com.ruoyi.finance.excel.FleetExcel;
  8. import com.ruoyi.warehouseBusiness.domain.TWarehousebillsfees;
  9. import com.ruoyi.warehouseBusiness.excel.BillDetailsSubItem;
  10. import com.ruoyi.warehouseBusiness.excel.InStock;
  11. import com.ruoyi.warehouseBusiness.excel.ListMapToBeanUtils;
  12. import com.ruoyi.warehouseBusiness.excel.QueryMenu;
  13. import com.ruoyi.warehouseBusiness.mapper.TWarehousebillsfeesMapper;
  14. import com.ruoyi.warehouseBusiness.service.ITWarehousebillsfeesService;
  15. import org.apache.poi.hssf.usermodel.*;
  16. import org.apache.poi.ss.usermodel.BorderStyle;
  17. import org.apache.poi.ss.usermodel.HorizontalAlignment;
  18. import org.apache.poi.ss.usermodel.VerticalAlignment;
  19. import org.apache.poi.ss.util.CellRangeAddress;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.stereotype.Service;
  22. import java.io.*;
  23. import java.math.BigDecimal;
  24. import java.util.*;
  25. /**
  26. * 仓库费用明细Service业务层处理
  27. *
  28. * @author ruoyi
  29. * @date 2020-12-11
  30. */
  31. @Service
  32. public class TWarehousebillsfeesServiceImpl implements ITWarehousebillsfeesService {
  33. @Autowired
  34. private TWarehousebillsfeesMapper tWarehousebillsfeesMapper;
  35. @Autowired
  36. private TFeesMapper tFeesMapper;
  37. /**
  38. * 查询仓库费用明细
  39. *
  40. * @param fId 仓库费用明细ID
  41. * @return 仓库费用明细
  42. */
  43. @Override
  44. public TWarehousebillsfees selectTWarehousebillsfeesById(Long fId) {
  45. return tWarehousebillsfeesMapper.selectTWarehousebillsfeesById(fId);
  46. }
  47. /**
  48. * 查询仓库费用明细列表
  49. *
  50. * @param tWarehousebillsfees 仓库费用明细
  51. * @return 仓库费用明细
  52. */
  53. @Override
  54. public List<TWarehousebillsfees> selectTWarehousebillsfeesList(TWarehousebillsfees tWarehousebillsfees) {
  55. return tWarehousebillsfeesMapper.selectTWarehousebillsfeesList(tWarehousebillsfees);
  56. }
  57. /**
  58. * 新增仓库费用明细
  59. *
  60. * @param tWarehousebillsfees 仓库费用明细
  61. * @return 结果
  62. */
  63. @Override
  64. public int insertTWarehousebillsfees(TWarehousebillsfees tWarehousebillsfees) {
  65. tWarehousebillsfees.setCreateTime(DateUtils.getNowDate());
  66. return tWarehousebillsfeesMapper.insertTWarehousebillsfees(tWarehousebillsfees);
  67. }
  68. /**
  69. * 修改仓库费用明细
  70. *
  71. * @param tWarehousebillsfees 仓库费用明细
  72. * @return 结果
  73. */
  74. @Override
  75. public int updateTWarehousebillsfees(TWarehousebillsfees tWarehousebillsfees) {
  76. tWarehousebillsfees.setUpdateTime(DateUtils.getNowDate());
  77. return tWarehousebillsfeesMapper.updateTWarehousebillsfees(tWarehousebillsfees);
  78. }
  79. /**
  80. * 批量删除仓库费用明细
  81. *
  82. * @param fIds 需要删除的仓库费用明细ID
  83. * @return 结果
  84. */
  85. @Override
  86. public int deleteTWarehousebillsfeesByIds(Long[] fIds) {
  87. return tWarehousebillsfeesMapper.deleteTWarehousebillsfeesByIds(fIds);
  88. }
  89. /**
  90. * 删除仓库费用明细信息
  91. *
  92. * @param fId 仓库费用明细ID
  93. * @return 结果
  94. */
  95. @Override
  96. public int deleteTWarehousebillsfeesById(Long fId) {
  97. return tWarehousebillsfeesMapper.deleteTWarehousebillsfeesById(fId);
  98. }
  99. /**
  100. * 查询报表
  101. *
  102. * @param tWarehousebillsfees
  103. * @return
  104. */
  105. @Override
  106. public List<FleetExcel> selectFleetExcelList(TWarehousebillsfees tWarehousebillsfees) {
  107. List<FleetExcel> fleetExcelList = new ArrayList<>();
  108. // 开始月份
  109. int startMonth = 1;
  110. // 结束月份
  111. int endMonth = 1;
  112. if (StringUtils.isNotEmpty(tWarehousebillsfees.getfYears())) {
  113. // 年份
  114. List<String> month = tWarehousebillsfees.getfMonth();
  115. if (StringUtils.isNotEmpty(month.get(0))) {
  116. startMonth = Integer.parseInt(month.get(0));
  117. }
  118. if (StringUtils.isNotEmpty(month.get(1))) {
  119. endMonth = Integer.parseInt(month.get(1));
  120. }
  121. if (endMonth < startMonth) {
  122. return null;
  123. }
  124. for (int i = startMonth; i <= endMonth; i++) {
  125. String firstDay = DateUtils.getFirstDay(Integer.parseInt(tWarehousebillsfees.getfYears()), i);
  126. String lastDay = DateUtils.getLastDay(Integer.parseInt(tWarehousebillsfees.getfYears()), i);
  127. month.set(0, firstDay);
  128. month.set(1, lastDay);
  129. tWarehousebillsfees.setfMonth(month);
  130. List<FleetExcel> fleetExcels = tWarehousebillsfeesMapper.selectFleetExcelList(tWarehousebillsfees);
  131. if (StringUtils.isNotEmpty(fleetExcels)) {
  132. for (FleetExcel e : fleetExcels) {
  133. fleetExcelList.add(e);
  134. }
  135. fleetExcelList.add(tWarehousebillsfeesMapper.selectFleetExcelMonthList(tWarehousebillsfees));
  136. }
  137. }
  138. }
  139. return fleetExcelList;
  140. }
  141. /**
  142. * 查询报表合计
  143. *
  144. * @param fleetExcelsList
  145. * @return
  146. */
  147. @Override
  148. public List<String> selectFleetExcel(List<FleetExcel> fleetExcelsList) {
  149. List<String> stringList = new ArrayList<>();
  150. if (StringUtils.isEmpty(fleetExcelsList)) {
  151. stringList.add("0");
  152. stringList.add("0");
  153. stringList.add("0");
  154. return stringList;
  155. }
  156. BigDecimal ccf = new BigDecimal(0);
  157. BigDecimal fAmount = new BigDecimal(0);
  158. BigDecimal totalAmount = new BigDecimal(0);
  159. for (FleetExcel f : fleetExcelsList) {
  160. if (StringUtils.isEmpty(f.getUserName())) {
  161. if (StringUtils.isNotEmpty(f.getCcf())) {
  162. ccf = ccf.add(new BigDecimal(f.getCcf()));
  163. } else {
  164. ccf = ccf.add(new BigDecimal(0));
  165. }
  166. if (StringUtils.isNotEmpty(f.getfAmount())) {
  167. fAmount = fAmount.add(new BigDecimal(f.getfAmount()));
  168. } else {
  169. fAmount = fAmount.add(new BigDecimal(0));
  170. }
  171. if (StringUtils.isNotEmpty(f.getTotalAmount())) {
  172. totalAmount = totalAmount.add(new BigDecimal(f.getTotalAmount()));
  173. } else {
  174. totalAmount = totalAmount.add(new BigDecimal(0));
  175. }
  176. }
  177. }
  178. stringList.add(ccf + "");
  179. stringList.add(fAmount + "");
  180. stringList.add(totalAmount + "");
  181. return stringList;
  182. }
  183. @Override
  184. public List<Map<String, Object>> accountsDue() {
  185. return tWarehousebillsfeesMapper.accountsDue();
  186. }
  187. @Override
  188. public List<QueryMenu> selectQueryMenuList(TWarehousebillsfees tWarehousebillsfees) {
  189. return tWarehousebillsfeesMapper.selectQueryMenuList(tWarehousebillsfees);
  190. }
  191. @Override
  192. public AjaxResult exportBillSubItemDetails(TWarehousebillsfees tWarehousebillsfees,String excelName) {
  193. // 创建 workbook,对应一个 excel 文件
  194. HSSFWorkbook wb = new HSSFWorkbook();
  195. // 在 workbook 中添加一个sheet,对应 excel 文档中的 sheet
  196. HSSFSheet sheet = wb.createSheet(excelName);;
  197. // 获取 cell 样式
  198. HSSFCellStyle cellTitleStyle = getCellStyle(wb, "center", true);
  199. HSSFCellStyle cellContentStyle = getCellStyle(wb, "center", true);
  200. // 创建标题,合并标题单元格
  201. // 行号
  202. int rowNum = 0;
  203. // 第一行
  204. HSSFRow first = sheet.createRow(rowNum++);
  205. String[] left = {"序号", "业务类型", "作业类型", "客户", "提单号", "货名", "件数", "重量", "体积", "入库时间", "出库时间", "计算天数"};
  206. List<String> middle = tFeesMapper.selectTFeesNameList();
  207. String[] right = {"金额", "计划员", "计费时间", "结费时间", "仓库", "备注"};
  208. List<String> all = new ArrayList<>();
  209. Collections.addAll(all, left);
  210. all.addAll(middle);
  211. Collections.addAll(all, right);
  212. int size = all.size();
  213. for (int i = 0; i < size; i++) {
  214. HSSFCell tempCell = first.createCell(i);
  215. if (i > 11 && i < size - 6) {
  216. tempCell.setCellValue(all.get(i));
  217. } else {
  218. tempCell.setCellValue(all.get(i));
  219. }
  220. tempCell.setCellStyle(cellTitleStyle);
  221. }
  222. // 根据客户名称,发货方查询清单数据
  223. List<Map<String, Object>> list = tWarehousebillsfeesMapper.selectBillDetailsSubItemNew(tWarehousebillsfees);
  224. if (list.size() < 1) {
  225. return AjaxResult.error("无数据,导出Excel失败");
  226. }
  227. int index = 1;
  228. for (Map<String, Object> map : list) {
  229. // 根据 提单号+客户名称,获取费用数据
  230. String fMblno = StringUtils.objToStr(map.get("fMblno"));
  231. String fName = StringUtils.objToStr(map.get("tcfName"));
  232. List<Map<String, Object>> fees;
  233. if (StringUtils.isNotEmpty(fMblno)) {
  234. fees = tWarehousebillsfeesMapper.selectFeesBymblnoAndFname(fMblno, fName);
  235. } else {
  236. fees = new ArrayList<>();
  237. }
  238. // 初始化金额
  239. BigDecimal allAmount = BigDecimal.ZERO;
  240. Map<Integer, String> feeMap = new HashMap<>();
  241. for (Map<String, Object> fee : fees) {
  242. // 获取费用名称
  243. String feeName = StringUtils.objToStr(fee.get("feeName"));
  244. // 三元表达式校验金额是否为空,为空则为字符串 0,不为空则返回单价字符串
  245. String fAmount = fee.get("fAmount") == null ? "0" : String.valueOf(fee.get("fAmount"));
  246. // 金额
  247. allAmount = allAmount.add(new BigDecimal(fee.get("fAmount") == null ? "0" : String.valueOf(fee.get("fAmount"))));
  248. // 根据费用名称中间部分的数组下标
  249. int j = middle.indexOf(feeName);
  250. if (j >= 0) { // 根据下标,对费用名称下的数据进行传值
  251. feeMap.put(12 + j, fAmount);
  252. }
  253. }
  254. HSSFRow tempRow = sheet.createRow(rowNum++);
  255. // 循环单元格填入数据
  256. for (int i = 0; i < size; i++) {
  257. HSSFCell temCell = tempRow.createCell(i);
  258. String cellValue = "";
  259. if (i == 0) {
  260. // 序号
  261. cellValue = StringUtils.objToStr(index++);
  262. } else if (i == 1) {
  263. // 业务类型
  264. cellValue = StringUtils.objToStr(map.get("fBilltype"));
  265. } else if (i == 2) {
  266. // 作业类型
  267. cellValue = StringUtils.objToStr(map.get("fBusinesstype"));
  268. } else if (i == 3) {
  269. // 客户
  270. cellValue = StringUtils.objToStr(map.get("tcfName"));
  271. } else if (i == 4) {
  272. // 提单号
  273. cellValue = StringUtils.objToStr(map.get("fMblno"));
  274. } else if (i == 5) {
  275. // 货名
  276. cellValue = StringUtils.objToStr(map.get("tgfName"));
  277. } else if (i == 6) {
  278. // 件数
  279. cellValue = StringUtils.objToStr(map.get("fQty"));
  280. } else if (i == 7) {
  281. // 重量
  282. cellValue = StringUtils.objToStr(map.get("fNetweight"));
  283. } else if (i == 8) {
  284. // 体积
  285. cellValue = StringUtils.objToStr(map.get("fVolumn"));
  286. } else if (i == 9) {
  287. // 入库时间
  288. cellValue = StringUtils.objToStr(map.get("fOriginalbilldate"));
  289. } else if (i == 10) {
  290. // 出库时间
  291. cellValue = StringUtils.objToStr(map.get("fBsdate"));
  292. } else if (i == 11) {
  293. // 计费天数
  294. cellValue = StringUtils.objToStr(map.get("fBillingdays"));
  295. } else if (i == size - 6) {
  296. // 金额
  297. cellValue = allAmount.toString();
  298. } else if (i == size - 5) {
  299. // 计划员
  300. cellValue = StringUtils.objToStr(map.get("createBy"));
  301. } else if (i == size - 4) {
  302. // 计费时间
  303. cellValue = StringUtils.objToStr(map.get("fChargedate"));
  304. } else if (i == size - 3) {
  305. // 结费时间
  306. cellValue = StringUtils.objToStr(map.get("fStlamountdate"));
  307. } else if (i == size - 2) {
  308. // 仓库
  309. cellValue = StringUtils.objToStr(map.get("fhName"));
  310. } else if (i == size - 1) {
  311. // 备注
  312. cellValue = StringUtils.objToStr(map.get("remark"));
  313. }
  314. if (i <= 5 || i >= 9 && i <= 11 || i >= -5 && i <= -1) {
  315. temCell.setCellStyle(cellContentStyle);
  316. temCell.setCellValue(cellValue);
  317. } else {
  318. // 将 excel 中的数字 String 转换成 原本类型
  319. switchExcelStringToOther(wb,cellValue,cellContentStyle,temCell);
  320. }
  321. }
  322. feeMap.forEach((kev, value) -> {
  323. //tempRow.getCell(kev).setCellValue(value);
  324. // 对金额进行字符串转数字
  325. switchExcelStringToOther(wb,value,cellContentStyle,tempRow.getCell(kev));
  326. });
  327. }
  328. for (int i = 0; i < size; i++) {
  329. sheet.autoSizeColumn(i);
  330. sheet.setColumnWidth(i, sheet.getColumnWidth(i) * 17 / 10);
  331. }
  332. // 导出 excel
  333. String fileName = excelName + ".xls";
  334. // 创建导出流
  335. try {
  336. OutputStream out = new FileOutputStream(getAbsoluteFile(fileName));
  337. wb.write(out);
  338. wb.close();
  339. out.close();
  340. return AjaxResult.success(fileName);
  341. } catch (Exception e) {
  342. e.printStackTrace();
  343. return AjaxResult.error("导出Excel失败");
  344. }
  345. }
  346. private String getAbsoluteFile(String fileName) {
  347. String downloadPath = RuoYiConfig.getDownloadPath() + fileName;
  348. File desc = new File(downloadPath);
  349. if (!desc.getParentFile().exists()) {
  350. desc.getParentFile().mkdirs();
  351. }
  352. return downloadPath;
  353. }
  354. /**
  355. * 将 excel 表格中的金额型数字型字符串转换成原本类型
  356. * @param wb 工作簿
  357. * @param cellValue 值
  358. * @param cellContentStyle 样式
  359. * @param temCell cell
  360. */
  361. public void switchExcelStringToOther(HSSFWorkbook wb,String cellValue,HSSFCellStyle cellContentStyle,HSSFCell temCell){
  362. // 数据是否是数值型
  363. boolean isNum = false;
  364. // 数据是否是整数
  365. boolean isInteger = false;
  366. // 是否为百分数
  367. boolean isPercent = false;
  368. if (cellValue != null) {
  369. // 判断结果为数值型
  370. isNum = cellValue.matches("^(-?\\d+)(\\.\\d+)?$");
  371. // 判断结果是否为正数(小数部分是否为0)
  372. isInteger = cellValue.matches("^[-\\+]?[\\d]*$");
  373. // 判断结果是否为百分数
  374. isPercent = cellValue.contains("%");
  375. }
  376. if (isNum && !isPercent) {
  377. // 设置数据格式
  378. HSSFDataFormat df = wb.createDataFormat();
  379. if (isInteger) {
  380. // 只显示整数
  381. cellContentStyle.setDataFormat(df.getFormat("#0"));
  382. } else {
  383. // 小数
  384. cellContentStyle.setDataFormat(df.getFormat("##0.00"));
  385. }
  386. // 单元格格式
  387. temCell.setCellStyle(cellContentStyle);
  388. // 设置单元格内容为 double
  389. temCell.setCellValue(Double.parseDouble(cellValue));
  390. } else {
  391. temCell.setCellStyle(cellContentStyle);
  392. temCell.setCellValue(cellValue);
  393. }
  394. }
  395. /**
  396. * 封装大标题实现复用
  397. *
  398. * @param title 标题
  399. * @param row 所在行
  400. * @param sheet sheet
  401. * @param cellStyle 样式
  402. * @param cellAddresses 合并行参数
  403. */
  404. public void setBigTitle(String title, int row, HSSFSheet sheet, HSSFCellStyle cellStyle, CellRangeAddress cellAddresses) {
  405. // 合并参数
  406. sheet.addMergedRegion(cellAddresses);
  407. // 大标题部分
  408. HSSFRow bigTitleRow = sheet.createRow(row);
  409. HSSFCell cell = bigTitleRow.createCell(0);
  410. cell.setCellValue(title);
  411. cell.setCellStyle(cellStyle);
  412. }
  413. /**
  414. * 设置 excel 内内容字体
  415. *
  416. * @param style 传入的已设置的 excel 样式
  417. * @param workbook 工作簿
  418. * @param fontName 字体名称,例如: "宋体"
  419. * @param fontSize 字体大小
  420. * @param isBold 是否加粗,true:加粗,false:不加粗
  421. */
  422. public void setFont(HSSFCellStyle style, HSSFWorkbook workbook, String fontName, int fontSize, boolean isBold) {
  423. HSSFFont font = workbook.createFont();
  424. // 设置字体
  425. font.setFontName(fontName);
  426. // 是否加粗
  427. font.setBold(isBold);
  428. // 字体大小
  429. font.setFontHeightInPoints((short) fontSize);
  430. // 需要设置在谁上
  431. style.setFont(font);
  432. }
  433. /**
  434. * 获取并设置工作表 excel 样式
  435. *
  436. * @param workbook 工作簿
  437. * @param position 位置参数:left-居左,right-居右,center-居中
  438. * @param showFrame 是否显示边框,true:显示,false:不显示
  439. * @return 已设置的 excel 样式
  440. */
  441. public HSSFCellStyle getCellStyle(HSSFWorkbook workbook, String position, boolean showFrame) {
  442. // 创建标题的显示样式
  443. HSSFCellStyle headerStyle = workbook.createCellStyle();
  444. // 设置水平垂直居中+上下左右边框+边框是否显示
  445. setHeaderStyle(headerStyle, position, showFrame);
  446. // 返回
  447. return headerStyle;
  448. }
  449. /**
  450. * 封装边框以及水平+垂直居中+边框是否显示
  451. *
  452. * @param headerStyle 样式
  453. * @param position 位置参数:left-居左,right-居右,center-居中
  454. * @param showFrame 是否显示边框,true:显示,false:不显示
  455. */
  456. public void setHeaderStyle(HSSFCellStyle headerStyle, String position, boolean showFrame) {
  457. // 水平位置摆放
  458. switch (position) {
  459. case "left": // 居左
  460. headerStyle.setAlignment(HorizontalAlignment.LEFT);
  461. break;
  462. case "right": // 居右
  463. headerStyle.setAlignment(HorizontalAlignment.RIGHT);
  464. break;
  465. case "center": // 居中
  466. headerStyle.setAlignment(HorizontalAlignment.CENTER);
  467. break;
  468. }
  469. // 垂直居中
  470. headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
  471. if (showFrame) {
  472. // 上边框
  473. headerStyle.setBorderTop(BorderStyle.THIN);
  474. // 下边框
  475. headerStyle.setBorderBottom(BorderStyle.THIN);
  476. // 左边框
  477. headerStyle.setBorderLeft(BorderStyle.THIN);
  478. // 右边框
  479. headerStyle.setBorderRight(BorderStyle.THIN);
  480. }
  481. }
  482. }