BillDetailService.java 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package co.dc.aptiv.service;
  2. import co.dc.aptiv.AptivApp;
  3. import co.dc.aptiv.BillFrame;
  4. import co.dc.aptiv.ScanFrame;
  5. import co.dc.aptiv.common.XLSService;
  6. import co.dc.aptiv.pojo.BillDetailPojo;
  7. import co.dc.aptiv.pojo.BillPojo;
  8. import co.dc.aptiv.pojo.MaterielPojo;
  9. import co.dc.commons.basedao.BaseDao;
  10. import co.dc.commons.utils.DateTimeUtil;
  11. import org.apache.commons.configuration.PropertiesConfiguration;
  12. import org.apache.commons.dbutils.handlers.BeanHandler;
  13. import org.apache.commons.dbutils.handlers.BeanListHandler;
  14. import org.apache.commons.logging.Log;
  15. import org.apache.commons.logging.LogFactory;
  16. import org.springframework.stereotype.Service;
  17. import javax.annotation.Resource;
  18. import java.sql.SQLException;
  19. import java.util.ArrayList;
  20. import java.util.HashMap;
  21. import java.util.List;
  22. import java.util.Map;
  23. /**
  24. * @describe: 单据详情
  25. * @author: liuxinglong
  26. * @version: 2018/4/17 15:21
  27. */
  28. @Service("billDetailService")
  29. public class BillDetailService extends BaseDao<BillDetailPojo> {
  30. private static final Log log = LogFactory.getLog(BillDetailService.class);
  31. @Resource
  32. private BillService billService;
  33. @Resource
  34. private PropertiesConfiguration configuration;
  35. private ScanFrame scanFrame;
  36. private BillFrame billFrame;
  37. public BillFrame getBillFrame() {
  38. return billFrame;
  39. }
  40. public void setBillFrame(BillFrame billFrame) {
  41. this.billFrame = billFrame;
  42. }
  43. public ScanFrame getScanFrame() {
  44. return scanFrame;
  45. }
  46. public void setScanFrame(ScanFrame scanFrame) {
  47. this.scanFrame = scanFrame;
  48. }
  49. @Override
  50. protected String getTableName() {
  51. return BillDetailPojo.tableName;
  52. }
  53. /**
  54. * 根据pcbillid查询产品列表
  55. *
  56. * @param pcbillid
  57. * @return
  58. */
  59. public List<BillDetailPojo> getinfoGoods(long pcbillid) {
  60. log.info("根据pcbillid查询产品列表-long pcbillid=" + pcbillid);
  61. List<BillDetailPojo> billDetailPojos = new ArrayList<BillDetailPojo>();
  62. try {
  63. billDetailPojos = queryRunner.query("select * from " + getTableName() + " where pcbillid = " + pcbillid + " and delflag = 0 order by modifyTime desc",
  64. new BeanListHandler<BillDetailPojo>(BillDetailPojo.class));
  65. } catch (SQLException e) {
  66. log.error("根据pcbillid查询产品列表-异常:", e);
  67. }
  68. return billDetailPojos;
  69. }
  70. /**
  71. * 查询所有为上传的产品列表并且变更上传字段的值
  72. *
  73. * @return
  74. */
  75. public List<BillDetailPojo> getunupedGoods() {
  76. log.info("查询所有为上传的产品列表");
  77. List<BillDetailPojo> billDetailPojos = new ArrayList<BillDetailPojo>();
  78. Map<String, Object> where = new HashMap<String, Object>();
  79. where.put("updatedflag", 0);
  80. billDetailPojos = listByParam(where);
  81. if (billDetailPojos != null && billDetailPojos.size() > 0) {
  82. update("updatedflag", 1, "updatedflag", 0);
  83. }
  84. return billDetailPojos;
  85. }
  86. /**
  87. * 获取物料在详情表中最后一条记录的创建日期
  88. *
  89. * @param materielId 物料Id
  90. * @return
  91. */
  92. public String getLastDate(long materielId) {
  93. try {
  94. String sql = "SELECT * FROM bill_detail WHERE materielId = " + materielId + " ORDER BY createTime DESC LIMIT 1";
  95. BillDetailPojo billDetailPojo = queryRunner.query(sql, new BeanHandler<BillDetailPojo>(BillDetailPojo.class));
  96. if (billDetailPojo != null) {
  97. return billDetailPojo.getCreateTime().substring(0, 10);
  98. }
  99. } catch (SQLException e) {
  100. e.printStackTrace();
  101. }
  102. return null;
  103. }
  104. /**
  105. * 保存单据详情
  106. *
  107. * @param billId 单据Id
  108. * @param materielId 物料Id
  109. * @param qrCode 生成的二维码
  110. * @param colledtionCode 采集器返回信息
  111. * @return
  112. */
  113. public BillDetailPojo addBillDetail(Long billId, Long materielId, String qrCode, String colledtionCode) {
  114. log.info("保存码详情");
  115. BillDetailPojo billDetailPojo = new BillDetailPojo();
  116. try {
  117. billDetailPojo.setBillId(billId);
  118. billDetailPojo.setMaterielId(materielId);
  119. billDetailPojo.setQrCode(qrCode);
  120. billDetailPojo.setCollectionCode(colledtionCode);
  121. billDetailPojo.setState(qrCode.equals(colledtionCode) ? 1 : 0);
  122. billDetailPojo.setCreateTime(AptivApp.qrcodeService.createDateTime);
  123. billDetailPojo.setModifyTime(DateTimeUtil.getDateTimeString());
  124. billDetailPojo.setDelFlag(0);
  125. Long billDetailId = this.save(billDetailPojo);
  126. billDetailPojo.setId(billDetailId);
  127. } catch (Exception e) {
  128. log.error("生产模式:二维码唯一索引生效" + e);
  129. billDetailPojo.setId(-1);
  130. billDetailPojo.setQrCode("正常生产捕获重码,请停止打印并联系管理员!" + billDetailPojo.getQrCode());
  131. billDetailPojo.setState(0);
  132. AptivApp.qrcodeService.sendErrorCommand("生产模式:二维码唯一索引生效!");
  133. javax.swing.SwingUtilities.invokeLater(new Runnable() {
  134. public void run() {
  135. getScanFrame().alertMsg(1, "正常生产捕获重码,请停止打印并联系管理员!");
  136. }
  137. });
  138. }
  139. return billDetailPojo;
  140. }
  141. /**
  142. * 导出详情
  143. *
  144. * @param billPojos
  145. */
  146. public void exportDetails(List<BillPojo> billPojos) {
  147. if (billPojos == null || billPojos.size() < 1) {
  148. getBillFrame().alertMsg(1, "当前查询结果为空,不能导出!");
  149. return;
  150. }
  151. List<MaterielPojo> materielPojos = AptivApp.materielService.listAll();
  152. Map<Long, MaterielPojo> materielMap = new HashMap<Long, MaterielPojo>(materielPojos.size());
  153. for (MaterielPojo pojo : materielPojos) {
  154. materielMap.put(pojo.getId(), pojo);
  155. }
  156. List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
  157. Integer index = 1;
  158. for (BillPojo billPojo : billPojos) {
  159. List<BillDetailPojo> billDetailPojos = listByParam("billId", billPojo.getId() + "");
  160. //单据查详情
  161. for (int j = 0; j < billDetailPojos.size(); j++) {
  162. Map<String, Object> map = new HashMap<String, Object>(8);
  163. map.put("index", index);
  164. map.put("billCode", billPojo.getBillCode());
  165. MaterielPojo materielPojo = materielMap.get(billDetailPojos.get(j).getMaterielId());
  166. map.put("dpn", materielPojo != null ? materielPojo.getDpn() : "物料已删除");
  167. map.put("qrCode", billDetailPojos.get(j).getQrCode());
  168. map.put("collectionCode", billDetailPojos.get(j).getCollectionCode());
  169. map.put("state", billDetailPojos.get(j).getState() == 1 ? "合格" : "不合格");
  170. map.put("createTime", billDetailPojos.get(j).getCreateTime());
  171. map.put("modifyTime", billDetailPojos.get(j).getModifyTime());
  172. mapList.add(map);
  173. index++;
  174. }
  175. }
  176. //检查码是否超过行限制
  177. if (index > 65535) {
  178. getBillFrame().alertMsg(1, "导出的码数据不能超过65535行!");
  179. return;
  180. }
  181. //设置路径并导出excel
  182. String fileName = "C:\\Users\\" + System.getProperties().getProperty("user.name") + "\\Desktop\\" + "码详情" + DateTimeUtil.getDateTimeString().replace(":", "-") + ".xls";
  183. boolean bool = export(mapList, fileName);
  184. if (bool) {
  185. getBillFrame().alertMsg(0, "导出成功,路径为:" + fileName);
  186. } else {
  187. getBillFrame().alertMsg(1, "数据导出失败!");
  188. }
  189. }
  190. /**
  191. * 导出
  192. *
  193. * @param mapList 结果
  194. * @param fileName 文件名
  195. */
  196. public boolean export(List<Map<String, Object>> mapList, String fileName) {
  197. try {
  198. XLSService xlsService = new XLSService(fileName);
  199. //全部数据 设置第一行名称
  200. xlsService.createRow(1, 0);
  201. //顶级经销商可以导出所有的机构或门店
  202. xlsService.setStringCell(0, "序号");
  203. xlsService.setStringCell(1, "单据编号");
  204. xlsService.setStringCell(2, "DPN");
  205. xlsService.setStringCell(3, "生成码");
  206. xlsService.setStringCell(4, "采集码");
  207. xlsService.setStringCell(5, "是否合格");
  208. xlsService.setStringCell(6, "码生成时间");
  209. xlsService.setStringCell(7, "码采集时间");
  210. //单据查详情
  211. for (Map<String, Object> map : mapList) {
  212. xlsService.createRow(1, Integer.parseInt(map.get("index").toString()));
  213. xlsService.setIntCell(0, Integer.parseInt(map.get("index").toString()));
  214. xlsService.setStringCell(1, map.get("billCode").toString());
  215. xlsService.setStringCell(2, map.get("dpn").toString());
  216. xlsService.setStringCell(3, map.get("qrCode").toString());
  217. xlsService.setStringCell(4, map.get("collectionCode").toString());
  218. xlsService.setStringCell(5, map.get("state").toString());
  219. xlsService.setStringCell(6, map.get("createTime").toString());
  220. xlsService.setStringCell(7, map.get("modifyTime").toString());
  221. }
  222. xlsService.getWorkbook().removeSheetAt(0);
  223. xlsService.exportXLS();
  224. return true;
  225. } catch (Exception e) {
  226. log.error("导出机构异常" + e);
  227. return false;
  228. }
  229. }
  230. }