AppDeviceLogServiceImpl.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. package cn.fastfun.service.impl;
  2. import cn.fastfun.controller.param.*;
  3. import cn.fastfun.service.AppDeviceLogService;
  4. import cn.fastfun.service.AppDeviceService;
  5. import cn.fastfun.service.UtilService;
  6. import cn.fastfun.service.entity.AppDevice;
  7. import cn.fastfun.service.entity.AppDeviceLog;
  8. import cn.fastfun.service.entity.AppImeiHistory;
  9. import cn.fastfun.util.ObjectUtil;
  10. import com.bridge.dto.ApiDTO;
  11. import com.bridge.dto.ApiPageDTO;
  12. import com.bridge.dto.QueryParam;
  13. import com.bridge.dto.QueryParamExp;
  14. import com.bridge.exception.ApiRuntimeException;
  15. import com.bridge.service.JpaService;
  16. import com.bridge.service.impl.JpaServiceImp;
  17. import io.swagger.annotations.ApiOperation;
  18. import io.swagger.annotations.ApiParam;
  19. import io.swagger.models.auth.In;
  20. import lombok.extern.slf4j.Slf4j;
  21. import org.apache.commons.lang3.BooleanUtils;
  22. import org.springframework.dao.EmptyResultDataAccessException;
  23. import org.springframework.jdbc.core.JdbcTemplate;
  24. import org.springframework.stereotype.Service;
  25. import org.springframework.util.CollectionUtils;
  26. import org.springframework.util.StringUtils;
  27. import org.springframework.web.bind.annotation.RequestBody;
  28. import org.springframework.web.bind.annotation.RequestMapping;
  29. import org.springframework.web.bind.annotation.RequestMethod;
  30. import javax.annotation.Resource;
  31. import javax.persistence.Query;
  32. import javax.transaction.Transactional;
  33. import java.math.BigInteger;
  34. //import java.sql.Timestamp;
  35. import java.sql.Timestamp;
  36. import java.util.*;
  37. import java.util.concurrent.atomic.AtomicBoolean;
  38. @Slf4j
  39. @Service
  40. public class AppDeviceLogServiceImpl extends JpaServiceImp<AppDeviceLog, String> implements AppDeviceLogService {
  41. @Resource
  42. AppDeviceService appDeviceService;
  43. @Resource
  44. AppDeviceLogService appDeviceLogService;
  45. @Resource
  46. UtilService utilService;
  47. @Resource
  48. JdbcTemplate jdbcTemplate;
  49. //业务类
  50. @Resource(name = "appImeiHistoryService")
  51. JpaService<AppImeiHistory, String> appImeiHistoryService;
  52. @Override
  53. @Transactional
  54. public void putInStorage(LibraryInFormParam param) {
  55. List<AppDevice> deviceList = appDeviceService.findAll(QueryParamExp.in("sn", param.getSn().toArray(new String[]{})));
  56. param.setSn(new ArrayList<>());
  57. deviceList.forEach(p -> param.getSn().add(p.getSn()));
  58. if (!CollectionUtils.isEmpty(param.getSn())) {
  59. // 查询当前操作id的最大值
  60. StringBuffer sql = new StringBuffer();
  61. sql.append("SELECT COALESCE(MAX(operate_id),0) as max_opid from app_device_log where operate_id is not null");
  62. log.info("SQL: {}", sql.toString());
  63. ApiPageDTO page = appDeviceLogService.getListBySQL(sql.toString(), new HashMap<>(), new QueryParam());
  64. List<Map<String, Object>> data = (List<Map<String, Object>>) page.getData();
  65. BigInteger operateID = (BigInteger) data.get(0).get("max_opid");
  66. operateID = operateID.add(BigInteger.valueOf(1));
  67. Date time = new Date();
  68. BigInteger finalOperateID = operateID;
  69. param.getSn().forEach(p -> {
  70. String batchNum;
  71. AppDevice appDevice = appDeviceService.getOne(QueryParamExp.eq("sn", p));
  72. if (null == appDevice) throw new ApiRuntimeException("设备信息不存在!");
  73. batchNum = appDevice.getBatchNum();
  74. if (appDevice.getStatus() < 1) {
  75. appDevice.setStatus(1); // 已入库
  76. Map<String, Boolean> checkRes = appDeviceLogService.selfCheck(p);
  77. appDevice.setCheckStatus(BooleanUtils.toInteger(checkRes.get("checkResult"))); //自检
  78. appDevice.setCheckStatusDataConnect(BooleanUtils.toInteger(checkRes.get("checkDataConnectResult"))); //自检
  79. appDevice.setCheckStatusLocation(BooleanUtils.toInteger(checkRes.get("checkLocationResult"))); //自检
  80. appDevice.setCheckStatusLock(BooleanUtils.toInteger(checkRes.get("checkLockResult"))); //自检
  81. appDevice.setCheckStatusFault(BooleanUtils.toInteger(checkRes.get("checkFaultResult"))); //自检
  82. appDevice.setCheckStatusVoltage(BooleanUtils.toInteger(checkRes.get("checkVoltageResult"))); //自检
  83. //TODO 发送解锁指令
  84. appDevice.setInstorageTime(time);
  85. appDeviceService.save(appDevice);
  86. AppDeviceLog appDeviceLog = new AppDeviceLog(p, appDevice.getImei()).toInStorage(time, batchNum, finalOperateID);
  87. appDeviceLog.setOperator(utilService.getUserName());
  88. save(appDeviceLog); // 记录入库日志
  89. } else if (deviceList.size() == 1) { //如果只有1个,认为是单个入库,检测是否重复入库;批次入库只跳过
  90. throw new ApiRuntimeException(p + "设备已入库,不能重复入库!");
  91. }
  92. });
  93. } else {
  94. throw new ApiRuntimeException("设备未录入!");
  95. }
  96. }
  97. @Override
  98. @Transactional
  99. public Map<String, Boolean> selfCheck(String sn) {
  100. Map<String, Boolean> res = new HashMap<>();
  101. Boolean checkResult = true;
  102. Boolean checkDataConnectResult = true;
  103. Boolean checkLocationResult = true;
  104. Boolean checkLockResult = true;
  105. Boolean checkFaultResult = true;
  106. Boolean checkVoltageResult = true;
  107. Map<String, Object> bmsData = new HashMap<>();
  108. Map<String, Object> gpsData = new HashMap<>();
  109. try {
  110. bmsData = jdbcTemplate.queryForMap("select devcode,error_code, locked_state, status_time, cell_voltages from ff_battery_status where devcode='" + sn + "'");
  111. } catch (EmptyResultDataAccessException e) {
  112. bmsData.put("devcode", null);
  113. }
  114. try {
  115. gpsData = jdbcTemplate.queryForMap("select devcode from ff_location where devcode='" + sn + "'");
  116. } catch (EmptyResultDataAccessException e) {
  117. gpsData.put("devcode", null);
  118. }
  119. if (StringUtils.isEmpty(bmsData.get("devcode"))) {
  120. checkLockResult = false;
  121. checkFaultResult = false;
  122. checkVoltageResult = false;
  123. checkDataConnectResult = false;
  124. } else {
  125. if (StringUtils.isEmpty(bmsData.get("locked_state"))) {
  126. checkFaultResult = true;
  127. } else {
  128. //锁定 0=解锁,1=锁定
  129. if (bmsData.get("locked_state").equals(0)) {
  130. checkFaultResult = true;
  131. }
  132. }
  133. if (StringUtils.isEmpty(bmsData.get("error_code"))) {
  134. checkFaultResult = true;
  135. } else {
  136. if (bmsData.get("error_code").equals(0)) {
  137. checkFaultResult = true;
  138. }
  139. }
  140. List<Double> voltage = ObjectUtil.StringToArrayList((String) bmsData.get("cell_voltages"), ",");
  141. if (!StringUtils.isEmpty(bmsData.get("cell_voltages")) && (Collections.max(voltage) > 4.5 || Collections.min(voltage) < 2.5)) {
  142. checkVoltageResult = false;
  143. }
  144. Calendar cal = Calendar.getInstance();
  145. cal.setTime(new Date());
  146. Long time1 = cal.getTimeInMillis();
  147. cal.setTime((Date) bmsData.get("status_time"));
  148. Long time2 = cal.getTimeInMillis();
  149. if (!StringUtils.isEmpty(bmsData.get("status_time")) && (time1 - time2) / 1000.0 / 3600 > 6) {
  150. checkDataConnectResult = false;
  151. }
  152. new Timestamp(System.currentTimeMillis());
  153. }
  154. if (!StringUtils.isEmpty(gpsData.get("devcode"))) {
  155. checkLocationResult = false;
  156. }
  157. checkResult = checkDataConnectResult && checkLocationResult && checkLockResult && checkFaultResult && checkVoltageResult;
  158. res.put("checkResult", checkResult);
  159. res.put("checkDataConnectResult", checkDataConnectResult);
  160. res.put("checkLocationResult", checkLocationResult);
  161. res.put("checkLockResult", checkLockResult);
  162. res.put("checkFaultResult", checkFaultResult);
  163. res.put("checkVoltageResult", checkVoltageResult);
  164. return res;
  165. }
  166. @Override
  167. @Transactional
  168. public void transfer(TransferFormParam param) {
  169. if (CollectionUtils.isEmpty(param.getSn()) && !CollectionUtils.isEmpty(param.getBatchNum())) {
  170. List<AppDevice> deviceList = appDeviceService.findAll(QueryParamExp.in("operateID", param.getBatchNum().toArray(new Integer[]{})));
  171. param.setSn(new ArrayList<>());
  172. deviceList.forEach(p -> param.getSn().add(p.getSn()));
  173. }
  174. if (!CollectionUtils.isEmpty(param.getSn())) {
  175. // 查询当前操作id的最大值
  176. StringBuffer sql = new StringBuffer();
  177. sql.append("SELECT COALESCE(MAX(operate_id),0) as max_opid from app_device_log where operate_id is not null");
  178. log.info("SQL: {}", sql.toString());
  179. ApiPageDTO page = appDeviceLogService.getListBySQL(sql.toString(), new HashMap<>(), new QueryParam());
  180. List<Map<String, Object>> data = (List<Map<String, Object>>) page.getData();
  181. BigInteger operateID = (BigInteger) data.get(0).get("max_opid");
  182. operateID = operateID.add(BigInteger.valueOf(1));
  183. Date time = new Date();
  184. BigInteger finalOperateID = operateID;
  185. //批量调拨时,若有不符合调拨条件的电池,则停止本次批量调拨
  186. param.getSn().forEach(p -> {
  187. AppDevice appDevice = appDeviceService.getOne(QueryParamExp.eq("sn", p));
  188. if (null == appDevice) {
  189. throw new ApiRuntimeException(p + "设备信息不存在!");
  190. }
  191. // @TODO: 自建不通过
  192. // if (1 != appDevice.getStatus() && 2 != appDevice.getStatus()) {
  193. // throw new ApiRuntimeException(p + "不是入库状态,不能调拨!");
  194. // }
  195. // if (1 != appDevice.getCheckStatus()) {
  196. // throw new ApiRuntimeException(p + "自检未通过,不能调拨!");
  197. // }
  198. });
  199. param.getSn().forEach(p -> {
  200. String batchNum;
  201. AppDevice appDevice = appDeviceService.getOne(QueryParamExp.eq("sn", p));
  202. batchNum = appDevice.getBatchNum();
  203. // 将可调拨的电池进行调拨
  204. if (1 == appDevice.getStatus()) {
  205. appDevice.setStatus(2); // 已划拨
  206. appDevice.setTransferTime(time);
  207. appDevice.setOwnerId(param.getCustomId()); // 设置归属
  208. appDevice.setCustomer(param.getCustomId()); // 设置归属
  209. appDevice.setTfDescribe(param.getDescribe());
  210. appDevice.setTfUsed(param.getUsed());
  211. appDeviceService.save(appDevice);
  212. AppDeviceLog appDeviceLog = new AppDeviceLog(p, appDevice.getImei()).toTransfer(param, time, batchNum, finalOperateID);
  213. appDeviceLog.setOperator(utilService.getUserName());
  214. appDeviceLog.setOutCustomId(param.getCustomId());
  215. save(appDeviceLog); // 记录划拨日志
  216. } else {
  217. throw new ApiRuntimeException(p + "不是入库状态或自检未通过,不能调拨!");
  218. }
  219. });
  220. }
  221. }
  222. @Override
  223. @Transactional
  224. public void transferBack(TransferFormParam param) {
  225. AppDevice appDevice = appDeviceService.getOne(QueryParamExp.eq("sn", param.getSn().get(0)));
  226. // 查询当前操作id的最大值
  227. StringBuffer sql = new StringBuffer();
  228. sql.append("SELECT COALESCE(MAX(operate_id),0) as max_opid from app_device_log where operate_id is not null");
  229. log.info("SQL: {}", sql.toString());
  230. ApiPageDTO page = appDeviceLogService.getListBySQL(sql.toString(), new HashMap<>(), new QueryParam());
  231. List<Map<String, Object>> data = (List<Map<String, Object>>) page.getData();
  232. BigInteger operateID = (BigInteger) data.get(0).get("max_opid");
  233. operateID = operateID.add(BigInteger.valueOf(1));
  234. Date time = new Date();
  235. BigInteger finalOperateID = operateID;
  236. String batchNum;
  237. if (null == appDevice) throw new ApiRuntimeException("设备信息不存在!");
  238. if (3 != appDevice.getStatus()) throw new ApiRuntimeException("设备不是出库状态!");
  239. batchNum = appDevice.getBatchNum();
  240. appDevice.setInstorageTime(time);
  241. appDevice.setStatus(1); // 已入库
  242. appDevice.setTransferBackTime(time);
  243. appDeviceService.save(appDevice);
  244. AppDeviceLog appDeviceLog = new AppDeviceLog(appDevice.getSn(), appDevice.getImei()).transferBack(param, time, batchNum, finalOperateID);
  245. appDeviceLog.setOperator(utilService.getUserName());
  246. save(appDeviceLog); // 记录调回日志
  247. }
  248. @Override
  249. @Transactional
  250. public void outStorage(LibraryOutFormParam param) {
  251. if (CollectionUtils.isEmpty(param.getSn()) && !CollectionUtils.isEmpty(param.getBatchNum())) {
  252. List<AppDevice> deviceList = appDeviceService.findAll(QueryParamExp.in("operateID", param.getBatchNum().toArray(new Integer[]{})));
  253. param.setSn(new ArrayList<>());
  254. deviceList.forEach(p -> param.getSn().add(p.getSn()));
  255. }
  256. if (!CollectionUtils.isEmpty(param.getSn())) {
  257. // 查询当前操作id的最大值
  258. StringBuffer sql = new StringBuffer();
  259. sql.append("SELECT COALESCE(MAX(operate_id),0) as max_opid from app_device_log where operate_id is not null");
  260. log.info("SQL: {}", sql.toString());
  261. ApiPageDTO page = appDeviceLogService.getListBySQL(sql.toString(), new HashMap<>(), new QueryParam());
  262. List<Map<String, Object>> data = (List<Map<String, Object>>) page.getData();
  263. BigInteger operateID = (BigInteger) data.get(0).get("max_opid");
  264. operateID = operateID.add(BigInteger.valueOf(1));
  265. Date time = new Date();
  266. //批量出库时,若有不符合出库条件的电池,则停止本次批量出库
  267. param.getSn().forEach(p -> {
  268. AppDevice appDevice = appDeviceService.getOne(QueryParamExp.eq("sn", p));
  269. if (null == appDevice) {
  270. throw new ApiRuntimeException(p + "设备信息不存在!");
  271. }
  272. if (2 != appDevice.getStatus() && 3 != appDevice.getStatus()) {
  273. throw new ApiRuntimeException(p + "不是调拨状态,不能出库!");
  274. }
  275. });
  276. BigInteger finalOperateID = operateID;
  277. param.getSn().forEach(p -> {
  278. String batchNum;
  279. AppDevice appDevice = appDeviceService.getOne(QueryParamExp.eq("sn", p));
  280. // 将可出库的电池出库
  281. if (2 == appDevice.getStatus()) {
  282. batchNum = appDevice.getBatchNum();
  283. appDevice.setOutstorageTime(time);
  284. appDevice.setStatus(3); // 已出库
  285. appDeviceService.save(appDevice);
  286. AppDeviceLog appDeviceLog = new AppDeviceLog(p, appDevice.getImei()).outStorage(param, time, batchNum, finalOperateID);
  287. appDeviceLog.setOperator(utilService.getUserName());
  288. save(appDeviceLog); // 记录出库日志
  289. } else {
  290. throw new ApiRuntimeException(p + "不是调拨状态,不能出库!");
  291. }
  292. });
  293. }
  294. }
  295. @Override
  296. @Transactional
  297. public void handle(HandelFormParam param) {
  298. // 查询当前操作id的最大值
  299. StringBuffer sql = new StringBuffer();
  300. sql.append("SELECT COALESCE(MAX(operate_id),0) as max_opid from app_device_log where operate_id is not null");
  301. log.info("SQL: {}", sql.toString());
  302. ApiPageDTO page = appDeviceLogService.getListBySQL(sql.toString(), new HashMap<>(), new QueryParam());
  303. List<Map<String, Object>> data = (List<Map<String, Object>>) page.getData();
  304. BigInteger operateID = (BigInteger) data.get(0).get("max_opid");
  305. operateID = operateID.add(BigInteger.valueOf(1));
  306. Date time = new Date();
  307. BigInteger finalOperateID = operateID;
  308. param.getSn().forEach(p -> {
  309. String batchNum;
  310. AppDevice appDevice = appDeviceService.getOne(QueryParamExp.eq("sn", p));
  311. if (null == appDevice) throw new ApiRuntimeException("设备信息不存在!");
  312. if (1 != appDevice.getStatus()) throw new ApiRuntimeException("设备不是入库状态!");
  313. if (StringUtils.isEmpty(appDevice.getTransferTime())) throw new ApiRuntimeException("设备未被调拨过!");
  314. batchNum = appDevice.getBatchNum();
  315. appDevice.setHandleTime(time);
  316. appDevice.setStatus(4); // 处置
  317. appDeviceService.save(appDevice);
  318. //TODO 发送上锁指令
  319. AppDeviceLog appDeviceLog = new AppDeviceLog(p, appDevice.getImei()).toHandle(param, time, batchNum, finalOperateID);
  320. appDeviceLog.setOperator(utilService.getUserName());
  321. save(appDeviceLog); // 记录处置日志
  322. });
  323. }
  324. /**
  325. * 获取客户列表
  326. *
  327. * @return java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
  328. */
  329. @Override
  330. public List<Map<String, Object>> getCustomList() {
  331. StringBuilder sql = new StringBuilder();
  332. sql.append("SELECT id,organ_code,title from app_custom where organ_code is not null");
  333. log.info("SQL: {}", sql.toString());
  334. List<Map<String, Object>> mapList = this.getListBySQL(sql.toString(), new HashMap<>());
  335. return mapList;
  336. }
  337. }