AppDeviceServiceImp.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package cn.fastfun.service.impl;
  2. import cn.fastfun.controller.dto.DeviceAttrDTO;
  3. import cn.fastfun.service.*;
  4. import cn.fastfun.service.entity.AppDevice;
  5. import cn.fastfun.service.entity.PackModel;
  6. import cn.fastfun.service.entity.Product;
  7. import cn.fastfun.service.repository.AppDeviceRepository;
  8. import cn.fastfun.util.VerifyUtil;
  9. import com.bridge.dto.ApiDTO;
  10. import com.bridge.dto.ApiPageDTO;
  11. import com.bridge.dto.QueryParam;
  12. import com.bridge.dto.QueryParamExp;
  13. import com.bridge.exception.ApiRuntimeException;
  14. import com.bridge.service.impl.JpaServiceImp;
  15. import org.springframework.dao.EmptyResultDataAccessException;
  16. import org.springframework.jdbc.core.JdbcTemplate;
  17. import org.springframework.stereotype.Service;
  18. import org.springframework.util.StringUtils;
  19. import javax.annotation.Resource;
  20. import java.math.BigInteger;
  21. import java.util.*;
  22. /**
  23. * 服务实现类 Created by Bridge.
  24. */
  25. @Service("appDeviceService")
  26. public class AppDeviceServiceImp extends JpaServiceImp<AppDevice, String> implements AppDeviceService {
  27. //业务类
  28. @Resource(name = "appDeviceService")
  29. AppDeviceService appDeviceService;
  30. @Resource
  31. AppDeviceRepository deviceRepository;
  32. @Resource
  33. ProductService productService;
  34. @Resource
  35. JdbcTemplate jdbcTemplate;
  36. @Resource
  37. UtilService utilService;
  38. @Resource
  39. PackModelService packModelService;
  40. @Resource
  41. CellModelService cellModelService;
  42. @Override
  43. public void importDevice(List<Map<String, Object>> list) {
  44. List<AppDevice> devices = new ArrayList<>();
  45. DeviceAttrDTO diffAttrDTO = null;
  46. Map<String, Object> bmsData = new HashMap<>();
  47. Map<String, Object> gpsData = new HashMap<>();
  48. // 查询当前操作id的最大值
  49. StringBuffer sql = new StringBuffer();
  50. sql.append("SELECT COALESCE(MAX(operate_id),0) as max_opid from app_device where operate_id is not null");
  51. ApiPageDTO page = this.getListBySQL(sql.toString(), new HashMap<>(), new QueryParam());
  52. List<Map<String, Object>> data = (List<Map<String, Object>>) page.getData();
  53. BigInteger operateID = (BigInteger)data.get(0).get("max_opid");
  54. operateID = operateID.add(BigInteger.valueOf(1));
  55. for (Map<String, Object> p : list) {
  56. AppDevice device = new AppDevice(p);
  57. if (null == device.getDeliverTime()) throw new ApiRuntimeException("发货时间读取错误");
  58. // 数据效验
  59. if (null == diffAttrDTO) {
  60. diffAttrDTO = new DeviceAttrDTO(device.getBatchNum(), device.getSn());
  61. diffAttrDTO.setReceivedPlace(device.getReceivedPlace());
  62. }
  63. DeviceAttrDTO temp = new DeviceAttrDTO(device.getBatchNum(), device.getSn());
  64. temp.setReceivedPlace(device.getReceivedPlace());
  65. if (!temp.getBatchNum().equals(diffAttrDTO.getBatchNum()))
  66. throw new ApiRuntimeException("存在不同的批次号");
  67. if (!temp.getPack().equals(diffAttrDTO.getPack()))
  68. throw new ApiRuntimeException("存在不同的PACK厂");
  69. if (!temp.getType().equals(diffAttrDTO.getType())) new ApiRuntimeException("存在不同的电池类型");
  70. if (!temp.getReceivedPlace().equals(diffAttrDTO.getReceivedPlace()))
  71. throw new ApiRuntimeException("存在不同的收货地");
  72. //pack和cell 校验
  73. if (!org.apache.commons.lang3.StringUtils.isNotBlank(device.getCellModel())){
  74. throw new ApiRuntimeException("单体型号缺失");
  75. }
  76. if (!org.apache.commons.lang3.StringUtils.isNotBlank(device.getPackModel())){
  77. throw new ApiRuntimeException("包型号缺失");
  78. }
  79. PackModel packModel = packModelService.getOne(Arrays.asList(QueryParamExp.eq("cellModel", device.getCellModel()),
  80. QueryParamExp.eq("packModel", device.getPackModel())));
  81. if (packModel==null){
  82. throw new ApiRuntimeException("包型号和单体型号配置错误");
  83. }
  84. // 重复导入检查
  85. List<AppDevice> checkDuplicatedResultOnImei = appDeviceService.findAll(
  86. QueryParamExp.eq("imei", device.getImei()));
  87. if (!checkDuplicatedResultOnImei.isEmpty()) {
  88. throw new ApiRuntimeException("imei:" + device.getImei() + "已经被录入");
  89. }
  90. List<AppDevice> checkDuplicatedResultOnSn = appDeviceService.findAll(
  91. QueryParamExp.eq("sn", device.getSn()));
  92. if (!checkDuplicatedResultOnSn.isEmpty()) {
  93. throw new ApiRuntimeException("sn:" + device.getSn() + "已经被录入");
  94. }
  95. device.setOperateID(operateID.intValue());
  96. String checkSnResult = VerifyUtil.checkImei(device.getImei());
  97. if (!checkSnResult.equals("passed")) {
  98. throw new ApiRuntimeException("imei:" + device.getImei() + "校验未通过:" + checkSnResult);
  99. }
  100. // 电池产生数据检查
  101. try{
  102. bmsData = jdbcTemplate.queryForMap("select devcode from ff_battery_status where devcode='"+device.getSn()+"'");
  103. }catch (EmptyResultDataAccessException e) {
  104. bmsData.put("devcode",null);
  105. }
  106. try{
  107. gpsData = jdbcTemplate.queryForMap("select devcode from ff_location where devcode='"+device.getSn()+"'");
  108. }catch (EmptyResultDataAccessException e) {
  109. gpsData.put("devcode",null);
  110. }
  111. if(StringUtils.isEmpty(bmsData.get("devcode")) && StringUtils.isEmpty(gpsData.get("devcode"))){
  112. throw new ApiRuntimeException("sn:" + device.getSn() + "未产生过数据,批次导入失败");
  113. }
  114. // 批量导入时由于没有x-token, 无法确定操作者
  115. // device.setOperator(utilService.getUserName());
  116. devices.add(device);
  117. }
  118. save(devices);
  119. }
  120. public AppDeviceRepository getRepository() {
  121. return deviceRepository;
  122. }
  123. @Override
  124. public void addImeiTitle(AppDevice device) {
  125. device.setTypeTitle(getProductTitle("产品类型", device.getType()));
  126. device.setExpandTitle(getProductTitle("产品扩展", device.getExpand()));
  127. device.setPackTitle(getProductTitle("PACK制造商", device.getPack()));
  128. device.setSpecTitle(getProductTitle("化学体系", device.getChemistry()).concat(getProductTitle("电池平台", device.getPlatform())).concat(getProductTitle("容量", device.getCapacity())));
  129. }
  130. @Override
  131. public ApiPageDTO batchNumPageQuery() {
  132. return null;
  133. }
  134. private String getProductTitle(String typeName, String code) {
  135. Product product = productService.getOne(
  136. QueryParamExp.eq("snType", typeName),
  137. QueryParamExp.eq("snTypeCode", code));
  138. if (null != product) return product.getSnTypeDesc();
  139. return "未知";
  140. }
  141. }