package cn.fastfun.service.impl; import cn.fastfun.controller.dto.DeviceAttrDTO; import cn.fastfun.service.*; import cn.fastfun.service.entity.AppDevice; import cn.fastfun.service.entity.PackModel; import cn.fastfun.service.entity.Product; import cn.fastfun.service.repository.AppDeviceRepository; import cn.fastfun.util.VerifyUtil; import com.bridge.dto.ApiDTO; import com.bridge.dto.ApiPageDTO; import com.bridge.dto.QueryParam; import com.bridge.dto.QueryParamExp; import com.bridge.exception.ApiRuntimeException; import com.bridge.service.impl.JpaServiceImp; import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; import javax.annotation.Resource; import java.math.BigInteger; import java.util.*; /** * 服务实现类 Created by Bridge. */ @Service("appDeviceService") public class AppDeviceServiceImp extends JpaServiceImp implements AppDeviceService { //业务类 @Resource(name = "appDeviceService") AppDeviceService appDeviceService; @Resource AppDeviceRepository deviceRepository; @Resource ProductService productService; @Resource JdbcTemplate jdbcTemplate; @Resource UtilService utilService; @Resource PackModelService packModelService; @Resource CellModelService cellModelService; @Override public void importDevice(List> list) { List devices = new ArrayList<>(); DeviceAttrDTO diffAttrDTO = null; Map bmsData = new HashMap<>(); Map gpsData = new HashMap<>(); // 查询当前操作id的最大值 StringBuffer sql = new StringBuffer(); sql.append("SELECT COALESCE(MAX(operate_id),0) as max_opid from app_device where operate_id is not null"); ApiPageDTO page = this.getListBySQL(sql.toString(), new HashMap<>(), new QueryParam()); List> data = (List>) page.getData(); BigInteger operateID = (BigInteger)data.get(0).get("max_opid"); operateID = operateID.add(BigInteger.valueOf(1)); for (Map p : list) { AppDevice device = new AppDevice(p); if (null == device.getDeliverTime()) throw new ApiRuntimeException("发货时间读取错误"); // 数据效验 if (null == diffAttrDTO) { diffAttrDTO = new DeviceAttrDTO(device.getBatchNum(), device.getSn()); diffAttrDTO.setReceivedPlace(device.getReceivedPlace()); } DeviceAttrDTO temp = new DeviceAttrDTO(device.getBatchNum(), device.getSn()); temp.setReceivedPlace(device.getReceivedPlace()); if (!temp.getBatchNum().equals(diffAttrDTO.getBatchNum())) throw new ApiRuntimeException("存在不同的批次号"); if (!temp.getPack().equals(diffAttrDTO.getPack())) throw new ApiRuntimeException("存在不同的PACK厂"); if (!temp.getType().equals(diffAttrDTO.getType())) new ApiRuntimeException("存在不同的电池类型"); if (!temp.getReceivedPlace().equals(diffAttrDTO.getReceivedPlace())) throw new ApiRuntimeException("存在不同的收货地"); //pack和cell 校验 if (!org.apache.commons.lang3.StringUtils.isNotBlank(device.getCellModel())){ throw new ApiRuntimeException("单体型号缺失"); } if (!org.apache.commons.lang3.StringUtils.isNotBlank(device.getPackModel())){ throw new ApiRuntimeException("包型号缺失"); } PackModel packModel = packModelService.getOne(Arrays.asList(QueryParamExp.eq("cellModel", device.getCellModel()), QueryParamExp.eq("packModel", device.getPackModel()))); if (packModel==null){ throw new ApiRuntimeException("包型号和单体型号配置错误"); } // 重复导入检查 List checkDuplicatedResultOnImei = appDeviceService.findAll( QueryParamExp.eq("imei", device.getImei())); if (!checkDuplicatedResultOnImei.isEmpty()) { throw new ApiRuntimeException("imei:" + device.getImei() + "已经被录入"); } List checkDuplicatedResultOnSn = appDeviceService.findAll( QueryParamExp.eq("sn", device.getSn())); if (!checkDuplicatedResultOnSn.isEmpty()) { throw new ApiRuntimeException("sn:" + device.getSn() + "已经被录入"); } device.setOperateID(operateID.intValue()); String checkSnResult = VerifyUtil.checkImei(device.getImei()); if (!checkSnResult.equals("passed")) { throw new ApiRuntimeException("imei:" + device.getImei() + "校验未通过:" + checkSnResult); } // 电池产生数据检查 try{ bmsData = jdbcTemplate.queryForMap("select devcode from ff_battery_status where devcode='"+device.getSn()+"'"); }catch (EmptyResultDataAccessException e) { bmsData.put("devcode",null); } try{ gpsData = jdbcTemplate.queryForMap("select devcode from ff_location where devcode='"+device.getSn()+"'"); }catch (EmptyResultDataAccessException e) { gpsData.put("devcode",null); } if(StringUtils.isEmpty(bmsData.get("devcode")) && StringUtils.isEmpty(gpsData.get("devcode"))){ throw new ApiRuntimeException("sn:" + device.getSn() + "未产生过数据,批次导入失败"); } // 批量导入时由于没有x-token, 无法确定操作者 // device.setOperator(utilService.getUserName()); devices.add(device); } save(devices); } public AppDeviceRepository getRepository() { return deviceRepository; } @Override public void addImeiTitle(AppDevice device) { device.setTypeTitle(getProductTitle("产品类型", device.getType())); device.setExpandTitle(getProductTitle("产品扩展", device.getExpand())); device.setPackTitle(getProductTitle("PACK制造商", device.getPack())); device.setSpecTitle(getProductTitle("化学体系", device.getChemistry()).concat(getProductTitle("电池平台", device.getPlatform())).concat(getProductTitle("容量", device.getCapacity()))); } @Override public ApiPageDTO batchNumPageQuery() { return null; } private String getProductTitle(String typeName, String code) { Product product = productService.getOne( QueryParamExp.eq("snType", typeName), QueryParamExp.eq("snTypeCode", code)); if (null != product) return product.getSnTypeDesc(); return "未知"; } }