DeviceInfoServiceImpl.java 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511
  1. package com.xy.service;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.collection.CollUtil;
  4. import cn.hutool.core.collection.ListUtil;
  5. import cn.hutool.core.date.DatePattern;
  6. import cn.hutool.core.date.DateTime;
  7. import cn.hutool.core.date.DateUtil;
  8. import cn.hutool.core.map.MapUtil;
  9. import cn.hutool.core.text.StrBuilder;
  10. import cn.hutool.core.util.BooleanUtil;
  11. import cn.hutool.core.util.NumberUtil;
  12. import cn.hutool.core.util.ObjectUtil;
  13. import cn.hutool.core.util.StrUtil;
  14. import cn.hutool.json.JSONUtil;
  15. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  16. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  17. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  18. import com.baomidou.mybatisplus.core.metadata.IPage;
  19. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  20. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  21. import com.xy.collections.list.JArrayList;
  22. import com.xy.collections.list.JList;
  23. import com.xy.collections.map.JMap;
  24. import com.xy.config.DeviceThreadPoolConfig;
  25. import com.xy.consts.DictConsts;
  26. import com.xy.device.*;
  27. import com.xy.dto.*;
  28. import com.xy.dto.api.biz.ContainerAddDTO;
  29. import com.xy.dto.be.MercDto;
  30. import com.xy.dto.common.MercPlaceDto;
  31. import com.xy.entity.*;
  32. import com.xy.enums.FileExportType;
  33. import com.xy.error.CommRuntimeException;
  34. import com.xy.mapper.DeviceInfoMapper;
  35. import com.xy.mapper.entity.DeviceInfoQueryPage;
  36. import com.xy.service.be.MercFeignService;
  37. import com.xy.service.common.MercPlaceService;
  38. import com.xy.service.common.MercRegionService;
  39. import com.xy.sys.EnumDataClearSize;
  40. import com.xy.util.ExcelUtils;
  41. import com.xy.utils.*;
  42. import com.xy.utils.Enum.AlgorithmTypeEnum;
  43. import com.xy.utils.enums.DeviceActiveStateEnum;
  44. import com.xy.utils.enums.DeviceLockState;
  45. import com.xy.utils.enums.DeviceNetSateType;
  46. import com.xy.utils.enums.DeviceTypeEnum;
  47. import io.swagger.annotations.Api;
  48. import io.swagger.annotations.ApiOperation;
  49. import jodd.introspector.MapperFunction;
  50. import lombok.RequiredArgsConstructor;
  51. import lombok.extern.slf4j.Slf4j;
  52. import org.springframework.stereotype.Service;
  53. import org.springframework.transaction.annotation.Transactional;
  54. import org.springframework.validation.annotation.Validated;
  55. import org.springframework.web.bind.annotation.PostMapping;
  56. import org.springframework.web.bind.annotation.RequestBody;
  57. import javax.servlet.http.HttpServletResponse;
  58. import javax.validation.Valid;
  59. import java.io.IOException;
  60. import java.math.BigDecimal;
  61. import java.time.LocalDateTime;
  62. import java.util.*;
  63. import java.util.stream.Collectors;
  64. import static com.xy.utils.PlusBeans.*;
  65. /**
  66. * <p>
  67. * 设备-信息 服务实现类
  68. * </p>
  69. *
  70. * @author lijin
  71. * @since 2022-12-23
  72. */
  73. @Slf4j
  74. @Service
  75. @RequiredArgsConstructor
  76. @Api(tags = "设备-信息")
  77. public class DeviceInfoServiceImpl extends ServiceImpl<DeviceInfoMapper, DeviceInfo> implements DeviceInfoService {
  78. private final CloudWalkApiService cloudWalkApiService;
  79. private final AlgorithmService algorithmService;
  80. private final CountApiService countApiService;
  81. /**
  82. * 质检商户code
  83. */
  84. public static final String QA_MERC_CODE = "10001";
  85. private final MercFeignService mercFeignService;
  86. private final DeviceSysinfoServiceImpl deviceSysinfoService;
  87. private final DeviceStatusServiceImpl deviceStatusService;
  88. private final DeviceRegisterServiceImpl deviceRegisterService;
  89. private final DeviceEventMsgServiceImpl deviceEventMsgService;
  90. private final DeviceDataServiceImpl deviceDataService;
  91. private final DeviceTempRecordsServiceImpl deviceTempRecordsService;
  92. private final DeviceNetRecordServiceImpl deviceNetRecordService;
  93. private final GoodsDeviceService goodsDeviceService;
  94. private final FileExportService fileExportService;
  95. private final RedisService<String> redisService;
  96. private final GoodsService goodsService;
  97. private final AlipayDeviceService alipayDeviceService;
  98. private final String keyPrefix = "device:history:";
  99. private final MercRegionService mercRegionService;
  100. private final DeviceAlgorithmChargingServiceImpl deviceAlgorithmChargingService;
  101. private final MercPlaceService mercPlaceService;
  102. @Override
  103. @ApiOperation("设备列表-管理员")
  104. public R<List<DeviceInfoDto.ListByAdminName>> listByAdminName(@RequestBody @Validated DeviceInfoDto.GroupByAdminNameDto dto) {
  105. String noAdmin = "未分配管理员";
  106. List<DeviceInfoDto.ListByAdminName> deviceInfoList = baseMapper.listByAdminName(dto);
  107. deviceInfoList.forEach(i->{
  108. SysDictRedis dictDeviceType = SysDictUtils.get(DictConsts.DEVICE_TYPE, String.valueOf(i.getDeviceType()));
  109. i.setDeviceTypeName(dictDeviceType.getMsg());
  110. if(!Emptys.check(i.getDeviceName())){
  111. i.setDeviceName("");
  112. }
  113. });
  114. //没有管理员的的设置默认值
  115. deviceInfoList.stream().filter(s -> s.getAdminName() == null).forEach(s -> s.setAdminName(noAdmin));
  116. return R.ok(deviceInfoList);
  117. }
  118. //@Override
  119. @ApiOperation("设备分页-管理员")
  120. @PostMapping("pageByAdminName")
  121. public R<PageBean<DeviceInfoDto.ListByAdminName>> pageByAdminName(@RequestBody @Validated DeviceInfoDto.PageByAdminNameDto dto) {
  122. PageBean page = dto.getPage();
  123. String noAdmin = "未分配管理员";
  124. IPage<DeviceInfoDto.ListByAdminName> deviceInfoList = baseMapper.pageByAdminName(toIPage(page), dto);
  125. deviceInfoList.getRecords().forEach(i->{
  126. SysDictRedis dictDeviceType = SysDictUtils.get(DictConsts.DEVICE_TYPE, String.valueOf(i.getDeviceType()));
  127. i.setDeviceTypeName(dictDeviceType.getMsg());
  128. if(!Emptys.check(i.getDeviceName())){
  129. i.setDeviceName("");
  130. }
  131. });
  132. //没有管理员的的设置默认值
  133. deviceInfoList.getRecords().stream().filter(s -> s.getAdminName() == null).forEach(s -> s.setAdminName(noAdmin));
  134. return R.ok(toPageBean(DeviceInfoDto.ListByAdminName.class,deviceInfoList));
  135. }
  136. @Override
  137. @ApiOperation("设备分组-管理员")
  138. public R<List<DeviceInfoDto.GroupByAdminNameVo>> groupByAdminName(@RequestBody @Validated DeviceInfoDto.GroupByAdminNameDto dto) {
  139. String noAdmin = "未分配管理员";
  140. List<DeviceInfoDto.ListByAdminName> deviceInfoList = listByAdminName(dto).getData();
  141. //根据管理员名字分组
  142. Map<String, List<DeviceInfoDto.ListByAdminName>> deviceMap = deviceInfoList.stream().collect(Collectors.groupingBy(DeviceInfoDto.ListByAdminName::getAdminName));
  143. List<DeviceInfoDto.GroupByAdminNameVo> list = new ArrayList<>();
  144. //不包含未分配管理员的
  145. deviceMap.forEach(
  146. (k,i)->{
  147. DeviceInfoDto.GroupByAdminNameVo vo = new DeviceInfoDto.GroupByAdminNameVo();
  148. if(!noAdmin.equals(k)){
  149. vo.setAdminName(k)
  150. .setDeviceNum(i.size())
  151. .setDeviceInfos(i);
  152. list.add(vo);
  153. }
  154. }
  155. );
  156. //包含分配管理员的
  157. List<DeviceInfoDto.ListByAdminName> noAdminNamesList = deviceMap.get(noAdmin);
  158. if(Emptys.check(noAdminNamesList)){
  159. DeviceInfoDto.GroupByAdminNameVo vo = new DeviceInfoDto.GroupByAdminNameVo();
  160. vo.setAdminName(noAdmin)
  161. .setDeviceNum(noAdminNamesList.size())
  162. .setDeviceInfos(noAdminNamesList);
  163. list.add(vo);
  164. }
  165. return R.ok(list);
  166. }
  167. @PostMapping("eventList")
  168. @ApiOperation("根据事件编码查询设备")
  169. public R<PageBean<DeviceInfoDto.EventListVo>> eventList(@RequestBody @Validated DeviceInfoDto.EventList eventList) {
  170. String countAs = "id";
  171. QueryWrapper<DeviceEventMsg> queryWrapper = new QueryWrapper<DeviceEventMsg>()
  172. .select(LambdaUtils.getUnderlineCaseName(DeviceEventMsg::getDeviceId) + " as " + LambdaUtils.getProperty(DeviceEventMsg::getDeviceId), "count(*) as " + countAs)
  173. .eq(LambdaUtils.getUnderlineCaseName(DeviceEventMsg::getCode), eventList.getCode())
  174. .between(LambdaUtils.getUnderlineCaseName(DeviceEventMsg::getCreateTime), eventList.getBeginTime(), eventList.getEndTime())
  175. .groupBy(LambdaUtils.getUnderlineCaseName(DeviceEventMsg::getDeviceId))
  176. .last("HAVING " + countAs + " >= " + eventList.getSize());
  177. IPage<DeviceEventMsg> iPage = deviceEventMsgService.page(toIPage(eventList.getPage()), queryWrapper);
  178. PageBean<DeviceInfoDto.EventListVo> pageBean = new PageBean<DeviceInfoDto.EventListVo>()
  179. .setSize(iPage.getSize())
  180. .setCurrent(iPage.getCurrent())
  181. .setTotal(iPage.getTotal());
  182. List<DeviceEventMsg> list = iPage.getRecords();
  183. if (!Emptys.check(list)) {
  184. return R.ok(pageBean);
  185. }
  186. List<DeviceInfo> deviceInfos = listByIds(new JArrayList<>(list).getProperty(DeviceEventMsg::getDeviceId));
  187. List<DeviceInfoDto.EventListVo> eventListVos = copy(DeviceInfoDto.EventListVo.class, deviceInfos);
  188. List<DeviceInfoDto.EventListVo> builder = copy(eventListVos)
  189. .target(list, DeviceInfoDto.EventListVo::getDeviceId, DeviceInfoDto.EventListVo::getSize, DeviceEventMsg::getDeviceId, DeviceEventMsg::getId)
  190. .builder();
  191. return R.ok(pageBean.setRecords(builder));
  192. }
  193. /**
  194. * 商户设备列表分页-穿梭框用
  195. *
  196. * @param dto
  197. * @return {@link R}<{@link List}<{@link DeviceInfoDto.MyDeviceInfo}>>
  198. */
  199. public R<PageBean<DeviceInfoDto.MyDeviceInfo>> myDeviceList(@RequestBody @Validated DeviceInfoDto.MyDeviceDTO dto) {
  200. PageBean pageBean = dto.getPage();
  201. String searchKey = dto.getSearchKey();
  202. Long mercId = dto.getMercId();
  203. Integer type = dto.getType();
  204. Long strategyId = dto.getStrategyId();
  205. GoodsDeviceDto.SelectList selectList = new GoodsDeviceDto.SelectList();
  206. //selectList.setMercId(mercId).setPriceStrategyId(strategyId);
  207. List<GoodsDeviceDto.Vo> goodsDevices = R.feignCheckData(goodsDeviceService.list(selectList));
  208. if (type == 2 && CollUtil.isEmpty(goodsDevices)) {
  209. //右侧已选的
  210. return R.ok(new PageBean<>());
  211. }
  212. List<Long> deviceIds = goodsDevices.stream().map(GoodsDeviceDto.Vo::getDeviceId).collect(Collectors.toList());
  213. LambdaQueryWrapper<DeviceInfo> lqw = new MybatisPlusQuery().eqWrapper(dto, DeviceInfo.class)
  214. .build();
  215. lqw.in(type == 2 && CollUtil.isNotEmpty(deviceIds), DeviceInfo::getDeviceId, deviceIds);
  216. //左侧排除掉已关联的
  217. lqw.notIn(type == 1 && CollUtil.isNotEmpty(deviceIds), DeviceInfo::getDeviceId, deviceIds);
  218. lqw.and(StrUtil.isNotEmpty(searchKey), wrapper -> wrapper
  219. .eq(DeviceInfo::getDeviceId, searchKey)
  220. .or()
  221. .likeRight(DeviceInfo::getDeviceName, searchKey));
  222. IPage<DeviceInfo> iPage = page(toIPage(pageBean), lqw);
  223. return R.ok(toPageBean(DeviceInfoDto.MyDeviceInfo.class, iPage));
  224. }
  225. @ApiOperation("点位设备数量查询")
  226. @Override
  227. public R<List<DeviceInfoDto.PlaceDeviceNumVo>> placeDeviceNum(@RequestBody @Validated DeviceInfoDto.PlaceDeviceNumDto dto) {
  228. return R.ok(baseMapper.placeDeviceNum(dto));
  229. }
  230. @Override
  231. @ApiOperation("设备列表带卡包数")
  232. public R<PageBean<DeviceInfoDto.AlgorithmChargingVo>> algorithmChargingDevice(@RequestBody @Validated DeviceInfoDto.AlgorithmCharging algorithmCharging) {
  233. PageBean pageBean = algorithmCharging.getPage();
  234. //查询设备
  235. Integer value = SysDictUtils.getValue(EnumDeviceActiveStatus.Code.CODE.getCode(), EnumDeviceActiveStatus.N_1.getCode(), Integer.class);
  236. LambdaUpdateWrapper<DeviceInfo> lambdaUpdateWrapper = new LambdaUpdateWrapper<DeviceInfo>()
  237. .eq(DeviceInfo::getMercId, algorithmCharging.getMercId())
  238. .eq(DeviceInfo::getActiveState, value)
  239. .in(Emptys.check(algorithmCharging.getDeviceIds()), DeviceInfo::getDeviceId, algorithmCharging.getDeviceIds())
  240. .orderByDesc(DeviceInfo::getCreateTime);
  241. IPage<DeviceInfo> page = page(toIPage(pageBean), lambdaUpdateWrapper);
  242. PageBean<DeviceInfoDto.AlgorithmChargingVo> algorithmChargingVoPageBean = toPageBean(DeviceInfoDto.AlgorithmChargingVo.class, page);
  243. List<DeviceInfoDto.AlgorithmChargingVo> algorithmChargingVos = algorithmChargingVoPageBean.getRecords();
  244. if (!Emptys.check(algorithmChargingVos)) {
  245. return R.ok(algorithmChargingVoPageBean);
  246. }
  247. //查询卡包数量
  248. List<DeviceAlgorithmChargingDto.CountVo> data = deviceAlgorithmChargingService.count(new DeviceAlgorithmChargingDto.Count()
  249. .setDeviceIds(new JArrayList<>(algorithmChargingVos).getProperty(DeviceInfoDto.AlgorithmChargingVo::getDeviceId))
  250. .setMercId(algorithmCharging.getMercId())
  251. ).getData();
  252. JMap<Long, DeviceAlgorithmChargingDto.CountVo> cover = new JArrayList<>(data).toMap(DeviceAlgorithmChargingDto.CountVo::getDeviceId).cover();
  253. algorithmChargingVos.forEach(algorithmChargingVo -> {
  254. DeviceAlgorithmChargingDto.CountVo countVo = cover.get(algorithmChargingVo.getDeviceId());
  255. Beans.copy(algorithmChargingVo, countVo);
  256. });
  257. return R.ok(algorithmChargingVoPageBean);
  258. }
  259. @Override
  260. @ApiOperation("设备在线数查询")
  261. public R<Map<Long, DeviceInfoDto.NetStateCountVo>> netStateCount(DeviceInfoDto.NetStateCountDto dto) {
  262. List<DeviceInfoDto.NetStateCount> netStateCountList = baseMapper.netStateCount(dto.getMercIdList());
  263. List<Long> netStateCountMercList = netStateCountList.stream().map(DeviceInfoDto.NetStateCount::getMercId).distinct().collect(Collectors.toList());
  264. Map<Long, DeviceInfoDto.NetStateCountVo> voMap = new HashMap<>();
  265. List<Long> mercIdList = Emptys.check(dto.getMercIdList()) ? dto.getMercIdList() : netStateCountMercList;
  266. if (!Emptys.check(mercIdList)) {
  267. return R.ok();
  268. }
  269. for (Long mercId : mercIdList) {
  270. Integer count = 0;
  271. Integer offline = 0;
  272. DeviceInfoDto.NetStateCountVo vo = new DeviceInfoDto.NetStateCountVo().setMercId(mercId);
  273. for (DeviceInfoDto.NetStateCount i : netStateCountList) {
  274. if (Objects.equals(i.getMercId(), mercId)) {
  275. count += i.getCount();
  276. if ("1".equals(i.getNetState())) {
  277. vo.setOnLineCount(i.getCount());
  278. } else {
  279. offline += i.getCount();
  280. }
  281. }
  282. }
  283. vo.setCount(count).setOffLineCount(offline);
  284. voMap.put(mercId, vo);
  285. }
  286. return R.ok(voMap);
  287. }
  288. @Override
  289. @ApiOperation("分页")
  290. public R<PageBean<DeviceInfoDto.Vo>> pageSingle(DeviceInfoDto.PageSingle dto) {
  291. PageBean pageBean = dto.getPage();
  292. LambdaQueryWrapper<DeviceInfo> lqw = new MybatisPlusQuery().eqWrapper(dto, DeviceInfo.class)
  293. .build();
  294. IPage<DeviceInfo> iPage = page(toIPage(pageBean), lqw);
  295. return R.ok(toPageBean(DeviceInfoDto.Vo.class, iPage));
  296. }
  297. @Override
  298. @ApiOperation("对象查询")
  299. public R<DeviceInfoDto.Vo> obj(DeviceInfoDto.Obj obj) {
  300. //设备信息
  301. LambdaQueryWrapper<DeviceInfo> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(obj, DeviceInfo.class).build();
  302. List<DeviceInfo> list = list(lambdaQueryWrapper);
  303. if (!Emptys.check(list)) {
  304. return R.ok();
  305. }
  306. DeviceInfoDto.Vo deviceInfo = copy(DeviceInfoDto.Vo.class, list.get(0));
  307. int num = 0;
  308. if (obj.getIsSysinfo()) {
  309. num++;
  310. }
  311. if (obj.getIsStatus()) {
  312. num++;
  313. }
  314. if (obj.getIsRegister()) {
  315. num++;
  316. }
  317. if (num > 0) {
  318. ThreadPoolUtils.Execute execute = ThreadPoolUtils.excPoll(DeviceThreadPoolConfig.DEVICE_COMMON_POLL, num);
  319. if (obj.getIsSysinfo()) {
  320. execute.execute(() -> {
  321. //系统信息
  322. DeviceSysinfoDto.Vo deviceSysinfo = deviceSysinfoService.get(new DeviceSysinfoDto.Vo().setDeviceId(deviceInfo.getDeviceId())).getData();
  323. deviceInfo.setDeviceSysinfo(deviceSysinfo);
  324. });
  325. }
  326. if (obj.getIsStatus()) {
  327. execute.execute(() -> {
  328. //状态信息
  329. DeviceStatusDto.Vo deviceStatus = deviceStatusService.obj(new DeviceStatusDto.Vo().setDeviceId(deviceInfo.getDeviceId())).getData();
  330. deviceInfo.setDeviceStatus(deviceStatus);
  331. });
  332. }
  333. if (obj.getIsRegister()) {
  334. execute.execute(() -> {
  335. //注册信息
  336. DeviceRegisterDto.Vo deviceRegister = deviceRegisterService.obj(new DeviceRegisterDto.Vo().setDeviceId(deviceInfo.getDeviceId())).getData();
  337. deviceInfo.setDeviceRegister(deviceRegister);
  338. });
  339. }
  340. execute.end();
  341. }
  342. return R.ok(deviceInfo);
  343. }
  344. @ApiOperation("反显设备名称")
  345. @Override
  346. public R<Map<Long, String>> getDeviceNameList(DeviceInfoDto.DeviceIdDto dto) {
  347. LambdaQueryWrapper<DeviceInfo> lqw = new LambdaQueryWrapper<DeviceInfo>()
  348. .in(DeviceInfo::getDeviceId, dto.getDeviceId())
  349. .select(DeviceInfo::getDeviceId, DeviceInfo::getDeviceName);
  350. List<DeviceInfo> deviceInfoList = list(lqw);
  351. return R.ok(deviceInfoList.stream().collect(Collectors.toMap(DeviceInfo::getDeviceId, i -> Optional.ofNullable(i.getDeviceName()).orElse(""))));
  352. }
  353. @Override
  354. public R<DeviceSysinfoDto.Vo> getDeviceSysinfo(DeviceSysinfoDto.DeviceSysInfo dto) {
  355. String dtoDeviceSN = dto.getDeviceSN();
  356. DeviceSysinfo one = deviceSysinfoService.getOne(Wrappers.<DeviceSysinfo>lambdaQuery().eq(DeviceSysinfo::getDeviceSn, dtoDeviceSN));
  357. return R.ok(BeanUtil.copyProperties(one, DeviceSysinfoDto.Vo.class));
  358. }
  359. @Override
  360. @ApiOperation("设备访问历史添加")
  361. public R history(DeviceInfoDto.Obj obj) {
  362. //获取字典
  363. SysDictRedis sysDictRedis = SysDictUtils.get(EnumDataClearSize.Code.CODE.getCode(), EnumDataClearSize.DEVICE_HISTORY_TWIG.getCode());
  364. Integer value = Integer.valueOf(sysDictRedis.getValue());
  365. //获取redis
  366. String key = keyPrefix + AuthorizeUtils.getLoginId(Long.class);
  367. List<String> list = redisService.getList(key);
  368. list.add(0, String.valueOf(obj.getDeviceId()));
  369. //去重
  370. List<String> redisList = new ArrayList<>();
  371. JList<String> comparing = new JArrayList<>(list).comparing();
  372. if (comparing.size() > value) {
  373. for (int i = 0; i < value; i++) {
  374. redisList.add(comparing.get(i));
  375. }
  376. } else {
  377. redisList = comparing;
  378. }
  379. redisService.removeList(key);
  380. redisService.setList(key, redisList);
  381. return R.ok();
  382. }
  383. @Override
  384. @ApiOperation("开门检测")
  385. public R<DeviceInfoDto.Vo> checkOpenDoor(DeviceInfoDto.Obj obj) {
  386. DeviceInfoDto.Vo deviceInfo = obj(new DeviceInfoDto.Obj()
  387. .setDeviceId(obj.getDeviceId())
  388. .setIsStatus(true)
  389. ).getData();
  390. if (deviceInfo == null) {
  391. return R.fail("设备不存在");
  392. }
  393. DeviceStatusDto.Vo deviceStatus = deviceInfo.getDeviceStatus();
  394. SysDictRedis qualitySets = SysDictUtils.get(EnumQualityMercSets.Code.CODE.getCode(), EnumQualityMercSets.MERC_CODE.getCode());
  395. //设备当前商户是质检时不检查
  396. if (!qualitySets.getValue().equals(deviceInfo.getMercCode())) {
  397. check(deviceInfo.getActiveState(), 2, "设备未激活");
  398. check(deviceInfo.getFreezeStatus(), 2, "设备已冻结");
  399. check(obj.getDoor() != null && obj.getDoor() == 1 ? deviceStatus.getLockStateR() : deviceStatus.getLockStateL(), 2, "设备已锁机");
  400. check(deviceStatus.getIsPay(), Boolean.FALSE, "设备不可交易");
  401. check(deviceInfo.getBusyState(), 2, "设备已停运");
  402. SysDictRedis sysDictRedis = SysDictUtils.get(EnumDeviceFaultLevelPayThreshold.Code.CODE.getCode(), EnumDeviceFaultLevelPayThreshold.NOT_PAY.getCode());
  403. check(deviceInfo.getFaultLevel(), sysDictRedis.getValue(), "设备故障");
  404. }
  405. check(deviceStatus.getNetState(), 2, "设备已离线");
  406. check(obj.getDoor() != null && obj.getDoor() == 1 ? deviceStatus.getDoorStateR() : deviceStatus.getDoorStateL(), 1, "设备正在使用中,请稍后");
  407. return R.ok(deviceInfo);
  408. }
  409. @PostMapping("historyList")
  410. @ApiOperation("设备访问历史查询")
  411. public R<List<DeviceInfoDto.Vo>> historyList() {
  412. //获取redis
  413. String key = keyPrefix + AuthorizeUtils.getLoginId(Long.class);
  414. List<String> deviceIds = redisService.getList(key);
  415. if (!Emptys.check(deviceIds)) {
  416. return R.ok();
  417. }
  418. //查询数据库
  419. List<DeviceInfo> list = list(new LambdaQueryWrapper<DeviceInfo>().in(DeviceInfo::getDeviceId, deviceIds));
  420. return R.ok(copy(DeviceInfoDto.Vo.class, list));
  421. }
  422. @ApiOperation("修改")
  423. @PostMapping("update")
  424. public R update(@RequestBody @Validated DeviceInfoDto.Update update) {
  425. DeviceInfo deviceInfo = copy(DeviceInfo.class, update)
  426. .setUpdateTime(LocalDateTime.now());
  427. updateById(deviceInfo);
  428. Long deviceId = deviceInfo.getDeviceId();
  429. DeviceInfoDto.Vo device = R.feignCheckData(this.obj(new DeviceInfoDto.Obj().setDeviceId(deviceId).setIsSysinfo(true)));
  430. Long algorithmId = update.getAlgorithmId();
  431. if (algorithmId != null && AlgorithmTypeEnum.CLOUD.getId() == algorithmId) {
  432. //云从算法
  433. boolean b = cloudWalkApiService.checkDeviceExist(deviceId);
  434. if (!b) {
  435. //货柜不存在,新增
  436. cloudWalkApiService.containerAdd(new ContainerAddDTO().setContainerCode(String.valueOf(deviceId)));
  437. }
  438. }
  439. Integer deviceType = device.getDeviceType();
  440. if (ObjectUtil.equals(DeviceTypeEnum.TYPE5.getCode(), deviceType)) {
  441. DeviceUpdateDTO deviceUpdateDTO = new DeviceUpdateDTO();
  442. if (StrUtil.isNotEmpty(update.getDeviceName())) {
  443. deviceUpdateDTO.setDeviceName(update.getDeviceName());
  444. }
  445. String deviceSn = device.getDeviceSysinfo().getDeviceSn();
  446. if (deviceUpdateDTO != null) {
  447. deviceUpdateDTO.setTerminalId(String.valueOf(update.getDeviceId()));
  448. deviceUpdateDTO.setBoardSn(deviceSn);
  449. alipayDeviceService.deviceUpdate(deviceUpdateDTO);
  450. }
  451. }
  452. return R.ok();
  453. }
  454. @ApiOperation("批量修改")
  455. @PostMapping("updateBatch")
  456. public R updateBatch(@RequestBody List<DeviceInfoDto.Update> updates) {
  457. LocalDateTime now = LocalDateTime.now();
  458. List<DeviceInfo> deviceInfos = new ArrayList<>(updates.size());
  459. updates.forEach(update -> deviceInfos.add(copy(DeviceInfo.class, update).setUpdateTime(now)));
  460. updateBatchById(deviceInfos);
  461. return R.ok();
  462. }
  463. @Override
  464. @ApiOperation("更新商户线路")
  465. public R updateLine(@RequestBody @Validated DeviceInfoDto.UpdateLine updateLine) {
  466. LambdaUpdateWrapper<DeviceInfo> luw = new LambdaUpdateWrapper<DeviceInfo>().eq(DeviceInfo::getMercId, updateLine.getMercId());
  467. //绑定线路,更换线路
  468. if (DeviceInfoDto.UPDATE.equals(updateLine.getType())) {
  469. DeviceInfo deviceInfo = new DeviceInfo();
  470. deviceInfo.setPlaceLineId(updateLine.getPlaceLineId());
  471. luw.in(DeviceInfo::getDeviceId, updateLine.getDeviceIds());
  472. baseMapper.update(deviceInfo, luw);
  473. }
  474. //删除线路
  475. if (DeviceInfoDto.DEL.equals(updateLine.getType())) {
  476. luw.eq(DeviceInfo::getPlaceLineId, updateLine.getWherePlaceLineId());
  477. luw.set(DeviceInfo::getPlaceLineId, null);
  478. baseMapper.update(null, luw);
  479. }
  480. //解绑线路 设置线路ID为null
  481. if (DeviceInfoDto.CLEAR.equals(updateLine.getType())) {
  482. luw.in(DeviceInfo::getDeviceId, updateLine.getDeviceIds());
  483. luw.set(DeviceInfo::getPlaceLineId, null);
  484. baseMapper.update(null, luw);
  485. }
  486. return R.ok();
  487. }
  488. @Override
  489. @ApiOperation("更新商户点位")
  490. public R updatePlace(@RequestBody @Validated DeviceInfoDto.UpdatePlace updatePlace) {
  491. LambdaUpdateWrapper<DeviceInfo> luw = new LambdaUpdateWrapper<DeviceInfo>().eq(DeviceInfo::getMercId, updatePlace.getMercId());
  492. //绑定点位,更换点位
  493. if (DeviceInfoDto.UPDATE.equals(updatePlace.getType())) {
  494. DeviceInfo deviceInfo = new DeviceInfo();
  495. deviceInfo.setPlaceId(updatePlace.getPlaceId());
  496. luw.in(DeviceInfo::getDeviceId, updatePlace.getDeviceIds());
  497. baseMapper.update(deviceInfo, luw);
  498. }
  499. //删除点位
  500. if (DeviceInfoDto.DEL.equals(updatePlace.getType())) {
  501. luw.eq(DeviceInfo::getPlaceId, updatePlace.getWherePlaceId());
  502. luw.set(DeviceInfo::getPlaceId, null);
  503. baseMapper.update(null, luw);
  504. }
  505. //解绑点位 设置点位ID为null
  506. if (DeviceInfoDto.CLEAR.equals(updatePlace.getType())) {
  507. luw.in(DeviceInfo::getDeviceId, updatePlace.getDeviceIds());
  508. luw.set(DeviceInfo::getPlaceId, null);
  509. baseMapper.update(null, luw);
  510. }
  511. return R.ok();
  512. }
  513. @ApiOperation("分页查询")
  514. @Override
  515. public R<PageBean<DeviceInfoDto.Vo2>> page(@RequestBody DeviceInfoDto.Page page) {
  516. return R.ok(queryPage(page));
  517. }
  518. @ApiOperation("导出设备列表")
  519. @PostMapping("exportDevices")
  520. public void exportDevices(HttpServletResponse response, @RequestBody @Valid DeviceInfoDto.Page page) throws IOException {
  521. PageBean<DeviceInfoDto.Vo2> pageBean = queryPage(page);
  522. List<DeviceInfoDto.Vo2> records = pageBean.getRecords();
  523. List<DeviceInfoDto.DeviceExcelVO> deviceExcelVOS = BeanUtil.copyToList(records, DeviceInfoDto.DeviceExcelVO.class);
  524. // 输出
  525. ExcelUtils.write(response, "设备列表.xls", "设备列表", DeviceInfoDto.DeviceExcelVO.class, deviceExcelVOS);
  526. }
  527. @ApiOperation("导出设备(异步)")
  528. @PostMapping("exportDevices/async")
  529. public void exportDevicesAsync(@RequestBody @Validated DeviceInfoDto.Page page) {
  530. PageBean<DeviceInfoDto.Vo2> pageBean = queryPage(page);
  531. List<DeviceInfoDto.Vo2> records = pageBean.getRecords();
  532. List<DeviceInfoDto.DeviceExcelVO> deviceExcelVOS = BeanUtil.copyToList(records, DeviceInfoDto.DeviceExcelVO.class);
  533. //异步导出参数封装
  534. ExcelDTO<DeviceInfoDto.DeviceExcelVO> excelDTO = new ExcelDTO<>();
  535. excelDTO.setData(deviceExcelVOS);
  536. excelDTO.setHead(DeviceInfoDto.DeviceExcelVO.class);
  537. excelDTO.setSheetName(FileExportType.DEVICE_INFO.getDescription());
  538. excelDTO.setFileExportType(FileExportType.DEVICE_INFO);
  539. //执行导出
  540. fileExportService.exportExcelAsync(excelDTO);
  541. }
  542. @PostMapping("nearbyPage")
  543. @ApiOperation("附近设备分页查询")
  544. public R<PageBean<DeviceInfoDto.Vo2>> nearbyPage(@RequestBody DeviceInfoDto.Page page) {
  545. if (!Emptys.check(page.getLon()) || !Emptys.check(page.getLat())) {
  546. throw new CommRuntimeException("经纬度不能为空");
  547. }
  548. return R.ok(queryPage(page));
  549. }
  550. @ApiOperation(value = "商户设备授权", hidden = true)
  551. @Override
  552. @Transactional(rollbackFor = Exception.class)
  553. public R<Boolean> mercDeviceAuth(DeviceInfoDto.MercDeviceAuthDto auth) {
  554. Long mercId = auth.getMercId();
  555. String mercCode = auth.getMercCode();
  556. Long algorithmId = auth.getAlgorithmId();
  557. String mercName = auth.getMercName();
  558. //商户最终设备列表
  559. List<Long> deviceIds = auth.getDeviceIds();
  560. List<DeviceInfo> devices = getDevicesByMercId(mercId);
  561. //取消商户设备授权
  562. if (CollUtil.isEmpty(deviceIds)) {
  563. if (CollUtil.isEmpty(devices)) {
  564. return R.ok(Boolean.TRUE);
  565. }
  566. }
  567. //更新商户设备授权
  568. List<DeviceInfo> deviceInfos = this.listByIds(deviceIds);
  569. List<DeviceStatus> deviceStatuses = new ArrayList<>();
  570. for (DeviceInfo deviceInfo : deviceInfos) {
  571. Integer deviceType = deviceInfo.getDeviceType();
  572. Long deviceId = deviceInfo.getDeviceId();
  573. if (ObjectUtil.equals(deviceType, DeviceTypeEnum.TYPE5.getCode())) {
  574. //支付宝设备算法
  575. algorithmId = AlgorithmTypeEnum.ALIPAY.getId();
  576. } else {
  577. //非支付宝算法
  578. if (AlgorithmTypeEnum.CLOUD.getId() == algorithmId) {
  579. //云从算法
  580. boolean b = cloudWalkApiService.checkDeviceExist(deviceId);
  581. if (!b) {
  582. //货柜不存在,新增
  583. cloudWalkApiService.containerAdd(new ContainerAddDTO().setContainerCode(String.valueOf(deviceId)));
  584. }
  585. }
  586. }
  587. int refMercId = deviceInfo.getMercId().intValue();
  588. String refMercCode = deviceInfo.getMercCode();
  589. // 只有解绑后,才能给顶级商户授权
  590. if (refMercId != -1 && refMercId != mercId.intValue()) {
  591. StrBuilder sb = StrBuilder.create();
  592. String errMsg = sb.append("设备[")
  593. .append(deviceInfo.getDeviceId())
  594. .append("]")
  595. .append("已被商户[")
  596. .append(deviceInfo.getMercName())
  597. .append("]绑定,请先进行解绑!")
  598. .toString();
  599. //非质检商户需要进行判断,质检商户跳过
  600. if (!QA_MERC_CODE.equals(refMercCode)) {
  601. //已关联别商户
  602. return R.fail(errMsg, Boolean.FALSE);
  603. }
  604. }
  605. //绑定关系
  606. deviceInfo.setMercId(mercId).setMercCode(mercCode).setAlgorithmId(algorithmId).setMercName(mercName);
  607. //标记机器可交易
  608. DeviceStatus deviceStatus = new DeviceStatus()
  609. .setDeviceId(deviceInfo.getDeviceId())
  610. .setIsPay(true);
  611. deviceStatuses.add(deviceStatus);
  612. }
  613. saveOrUpdateBatch(deviceInfos);
  614. if (Emptys.check(deviceStatuses)) {
  615. deviceStatusService.updateBatchById(deviceStatuses);
  616. }
  617. return R.ok(Boolean.TRUE);
  618. }
  619. /**
  620. * 解绑机器 回收
  621. *
  622. * @param dto
  623. * @return
  624. */
  625. @Override
  626. public R<Boolean> unBindMercDevice(DeviceInfoDto.MercDeviceUnBindDto dto) {
  627. List<DeviceInfo> deviceInfos = this.listByIds(dto.getDeviceIds());
  628. return R.ok(removeMerRefDevicesToTopMerc(deviceInfos));
  629. }
  630. /**
  631. * 回收机器
  632. *
  633. * @param deviceInfos
  634. * @return
  635. */
  636. private Boolean removeMerRefDevicesToTopMerc(List<DeviceInfo> deviceInfos) {
  637. MercDto.Vo mercCheck = R.feignCheckData(mercFeignService.obj(new MercDto.ListDTO().setMercCode(QA_MERC_CODE)));
  638. if (CollUtil.isNotEmpty(deviceInfos)) {
  639. deviceInfos.forEach(deviceInfo -> {
  640. //回收
  641. deviceInfo.setMercId(mercCheck.getId());
  642. deviceInfo.setMercDeviceCode(StrUtil.EMPTY);
  643. deviceInfo.setMercName(mercCheck.getName());
  644. deviceInfo.setMercCode(QA_MERC_CODE);
  645. });
  646. //批量更新
  647. return updateBatchById(deviceInfos);
  648. }
  649. return Boolean.FALSE;
  650. }
  651. /**
  652. * 批量移除商户设备绑定关系
  653. *
  654. * @param deviceInfos
  655. * @return
  656. */
  657. private Boolean removeMerRefDevices(List<DeviceInfo> deviceInfos, Long parentId) {
  658. if (CollUtil.isNotEmpty(deviceInfos) && parentId != null) {
  659. deviceInfos.forEach(deviceInfo -> {
  660. //非顶级兴元商户,解绑后,机器归父商户
  661. if (parentId != 1) {
  662. MercDto.Vo mercParent = R.feignCheckData(mercFeignService.obj(new MercDto.ListDTO().setId(parentId)));
  663. if (mercParent != null) {
  664. deviceInfo.setMercId(mercParent.getId());
  665. deviceInfo.setMercName(mercParent.getName());
  666. deviceInfo.setMercCode(mercParent.getMercCode());
  667. } else {
  668. deviceInfo.setMercId(-1L);
  669. deviceInfo.setMercName(StrUtil.EMPTY);
  670. deviceInfo.setMercCode(StrUtil.EMPTY);
  671. }
  672. }
  673. //一级商户,解绑后,直接释放
  674. if (parentId == 0) {
  675. MercDto.Vo mercCheck = R.feignCheckData(mercFeignService.obj(new MercDto.ListDTO().setMercCode(QA_MERC_CODE)));
  676. if (mercCheck != null) {
  677. deviceInfo.setMercId(mercCheck.getId());
  678. deviceInfo.setMercName(mercCheck.getName());
  679. deviceInfo.setMercCode(mercCheck.getMercCode());
  680. } else {
  681. deviceInfo.setMercId(-1L);
  682. deviceInfo.setMercName(StrUtil.EMPTY);
  683. deviceInfo.setMercCode(StrUtil.EMPTY);
  684. }
  685. }
  686. });
  687. //批量更新
  688. return updateBatchById(deviceInfos);
  689. }
  690. return Boolean.FALSE;
  691. }
  692. @ApiOperation("集合查询")
  693. @Override
  694. public R<List<DeviceInfoDto.Vo>> list(DeviceInfoDto.ListDto dto) {
  695. List<DeviceInfo> list = list(new LambdaQueryWrapper<DeviceInfo>()
  696. .in(CollUtil.isNotEmpty(dto.getDeviceIds()), DeviceInfo::getDeviceId, dto.getDeviceIds())
  697. .eq(Emptys.check(dto.getMercId()), DeviceInfo::getMercId, dto.getMercId())
  698. );
  699. return R.ok(copy(DeviceInfoDto.Vo.class, list));
  700. }
  701. @Override
  702. @ApiOperation("通用集合查询")
  703. public R<List<DeviceInfoDto.Vo>> listCommon(DeviceInfoDto.ListCommon dto) {
  704. String deviceSearch = dto.getDeviceSearch();
  705. DeviceInfoDto.Vo vo = dto.getVo();
  706. QueryWrapper<DeviceInfo> queryWrapper = new MybatisPlusQuery().eqWrapper(vo == null ? new DeviceInfoDto.Vo() : vo, DeviceInfo.class).buildQW();
  707. // List<Long> placeLineIds = dto.getPlaceLineIds();
  708. List<Long> deviceIds = dto.getDeviceIds();
  709. List<String> columnList = dto.getColumnList();
  710. // if (CollUtil.isNotEmpty(placeLineIds)) {
  711. // queryWrapper.in(LambdaUtils.getUnderlineCaseName(DeviceInfo::getPlaceLineId), placeLineIds);
  712. // }
  713. //fixed
  714. if (StrUtil.isNotEmpty(deviceSearch)) {
  715. queryWrapper.and(wrapper -> wrapper.likeRight(LambdaUtils.getUnderlineCaseName(DeviceInfo::getDeviceName), deviceSearch).or()
  716. .eq(LambdaUtils.getUnderlineCaseName(DeviceInfo::getDeviceId), deviceSearch));
  717. }
  718. if (CollUtil.isNotEmpty(columnList)) {
  719. queryWrapper.select(columnList.stream().toArray(String[]::new));
  720. }
  721. if (CollUtil.isNotEmpty(deviceIds)) {
  722. queryWrapper.in(LambdaUtils.getUnderlineCaseName(DeviceInfo::getDeviceId), deviceIds);
  723. }
  724. return R.ok(copy(DeviceInfoDto.Vo.class, list(queryWrapper)));
  725. }
  726. @Override
  727. @ApiOperation("商户设备算法列表")
  728. public R<List<Long>> mercAlgorithmIdList(DeviceInfoDto.MercAlgorithmIdListDto dto) {
  729. String algorithmIdStr = LambdaUtils.getProperty(DeviceInfo::getAlgorithmId);
  730. String mercStr = StringTools.humpToLine(LambdaUtils.getProperty(DeviceInfo::getMercId));
  731. QueryWrapper<DeviceInfo> lqw = new QueryWrapper<DeviceInfo>()
  732. .isNotNull(StringTools.humpToLine(algorithmIdStr))
  733. .eq(mercStr, dto.getMercId())
  734. .select(String.format("DISTINCT (%s) as %s", StringTools.humpToLine(algorithmIdStr), algorithmIdStr));
  735. List<Long> list = listObjs(lqw, (MapperFunction<Object, Long>) o -> (Long) o);
  736. return R.ok(list);
  737. }
  738. @Override
  739. @ApiOperation("小程序-商户设备首页统计")
  740. public R<DeviceInfoDto.MercHomeStatisticalVO> mercHomeStatistical(DeviceInfoDto.MercHomeQueryDTO dto) {
  741. Long mercId = dto.getMercId();
  742. List<Long> myDeviceIds = dto.getMyDeviceIds();
  743. //初始化数据
  744. DeviceInfoDto.MercHomeStatisticalVO mercHomeStatisticalVO = new DeviceInfoDto.MercHomeStatisticalVO()
  745. .setClosedNum(0).setOfflineNum(0)
  746. .setOnlineNum(0).setOperatingNum(0).setNeedToFillNum(0);
  747. if (CollUtil.isEmpty(myDeviceIds)) {
  748. return R.ok(mercHomeStatisticalVO);
  749. }
  750. List<DeviceInfo> mercDevices = listByIds(myDeviceIds);
  751. if (CollUtil.isEmpty(mercDevices)) {
  752. return R.ok(mercHomeStatisticalVO);
  753. }
  754. //在线、离线
  755. List<DeviceStatus> deviceStatuses = deviceStatusService.listByIds(myDeviceIds);
  756. //分组统计
  757. Map<Integer, Long> countNetstateMap = deviceStatuses.stream().collect(Collectors
  758. .groupingBy(DeviceStatus::getNetState, Collectors.counting()));
  759. Integer onlineDictValue = SysDictUtils.getValue(EnumDeviceOnlineStatus.Code.CODE.getCode(), EnumDeviceOnlineStatus.CONNECTED.getCode(), Integer.class);
  760. Integer offlineDictValue = SysDictUtils.getValue(EnumDeviceOnlineStatus.Code.CODE.getCode(), EnumDeviceOnlineStatus.DISCONNECT.getCode(), Integer.class);
  761. int onlineNum = countNetstateMap.get(onlineDictValue) == null ? 0 : countNetstateMap.get(onlineDictValue).intValue();
  762. int offlineNum = countNetstateMap.get(offlineDictValue) == null ? 0 : countNetstateMap.get(offlineDictValue).intValue();
  763. mercHomeStatisticalVO.setOnlineNum(onlineNum);
  764. mercHomeStatisticalVO.setOfflineNum(offlineNum);
  765. //锁机、未锁机
  766. Map<Integer, Long> countLockLstateMap = deviceStatuses.stream().collect(Collectors
  767. .groupingBy(DeviceStatus::getLockStateL, Collectors.counting()));
  768. Long lockLStateNum = countLockLstateMap.get(DeviceLockState.LOCK.getCode());
  769. Long unLockLStateNum = countLockLstateMap.get(DeviceLockState.UN_LOCK.getCode());
  770. mercHomeStatisticalVO.setOperatingNum(unLockLStateNum == null ? 0 : unLockLStateNum.intValue());
  771. mercHomeStatisticalVO.setClosedNum(lockLStateNum == null ? 0 : lockLStateNum.intValue());
  772. //待补货
  773. Integer deviceNum = R.feignCheckData(goodsDeviceService.countOutOfStockDevice(new GoodsDeviceDto.CountOutOfStockDevice().setMercId(mercId)));
  774. mercHomeStatisticalVO.setNeedToFillNum(deviceNum);
  775. return R.ok(mercHomeStatisticalVO);
  776. }
  777. @Override
  778. @ApiOperation("小程序-商户设备首页列表")
  779. public R<List<DeviceInfoDto.MercHomeListVO>> mercHomeList(DeviceInfoDto.MercHomeQueryDTO dto) {
  780. String searchKey = dto.getSearchKey();
  781. Long mercId = dto.getMercId();
  782. String deviceName = dto.getDeviceName();
  783. Long deviceId = dto.getDeviceId();
  784. List<Long> searchPlaceIdList = new ArrayList<>();
  785. //根据管理员名字查询点位ID列表
  786. if (Emptys.check(dto.getAdminName())) {
  787. MercPlaceDto.ListDto placeDto = new MercPlaceDto.ListDto();
  788. placeDto.setMercId(mercId).setAdminName(dto.getAdminName());
  789. List<MercPlaceDto.Vo> searchPlaceList = mercPlaceService.list(placeDto).getData();
  790. if (Emptys.check(searchPlaceList)) {
  791. searchPlaceIdList = searchPlaceList.stream().map(MercPlaceDto.Vo::getId).distinct().collect(Collectors.toList());
  792. } else {
  793. return R.ok();
  794. }
  795. }
  796. boolean isQa = false;
  797. if ("393010594508869".equals(String.valueOf(mercId))) {
  798. isQa = true;
  799. } else {
  800. dto.setActiveState("1");
  801. }
  802. List<Long> searchDeviceIds = new ArrayList<>();
  803. if (StrUtil.isNotEmpty(searchKey) || StrUtil.isNotEmpty(dto.getAdminName())) {
  804. //名称或者编号搜索设备
  805. LambdaQueryWrapper<DeviceInfo> deviceLqw = Wrappers.<DeviceInfo>lambdaQuery()
  806. .eq(DeviceInfo::getMercId, mercId)
  807. .in(Emptys.check(searchPlaceIdList), DeviceInfo::getPlaceId, searchPlaceIdList);
  808. if (Emptys.check(searchKey)) {
  809. deviceLqw.and(wrapper -> wrapper
  810. .eq(Emptys.check(searchKey), DeviceInfo::getDeviceId, searchKey)
  811. .or()
  812. .like(Emptys.check(searchKey), DeviceInfo::getDeviceName, searchKey)
  813. );
  814. }
  815. List<DeviceInfo> list = list(deviceLqw);
  816. if (CollUtil.isEmpty(list)) {
  817. return R.ok(new ArrayList<>());
  818. }
  819. searchDeviceIds = list.stream().map(DeviceInfo::getDeviceId).collect(Collectors.toList());
  820. }
  821. List<Long> myDeviceIds = dto.getDeviceIdList();
  822. Integer busyStatus = dto.getBusyStatus();
  823. Integer onlineStatus = dto.getOnlineStatus();
  824. Integer deviceType = dto.getDeviceType();
  825. //条件查询 在线状态,运营状态,设备类型,
  826. List<Long> deviceIdList = new ArrayList<>();
  827. if (deviceId != null && myDeviceIds.indexOf(deviceId) > 0) {
  828. deviceIdList.add(deviceId);
  829. }
  830. if (CollUtil.isNotEmpty(myDeviceIds) && deviceId == null) {
  831. deviceIdList.addAll(myDeviceIds);
  832. }
  833. if (CollUtil.isNotEmpty(deviceIdList)) {
  834. dto.setDeviceIdList(deviceIdList);
  835. List<Long> queryDeviceIds = new ArrayList<>();
  836. if (CollUtil.isNotEmpty(searchDeviceIds)) {
  837. for (Long id : searchDeviceIds) {
  838. int index = deviceIdList.indexOf(id);
  839. if (index >= 0) {
  840. queryDeviceIds.add(id);
  841. }
  842. }
  843. //无符合权限的搜索设备,返空
  844. if (CollUtil.isEmpty(queryDeviceIds)) {
  845. return R.ok(new ArrayList<>());
  846. } else {
  847. deviceIdList = queryDeviceIds;
  848. dto.setDeviceIdList(queryDeviceIds);
  849. }
  850. }
  851. } else {
  852. return R.ok(new ArrayList<>());
  853. }
  854. if (onlineStatus != null) {
  855. List<DeviceStatus> deviceStatusList = deviceStatusService.list(Wrappers.<DeviceStatus>lambdaQuery()
  856. .eq(DeviceStatus::getNetState, onlineStatus)
  857. .in(CollUtil.isNotEmpty(myDeviceIds), DeviceStatus::getDeviceId, deviceIdList)
  858. );
  859. if (CollUtil.isNotEmpty(deviceStatusList)) {
  860. List<Long> dbDeviceIdList = deviceStatusList.stream().map(DeviceStatus::getDeviceId).collect(Collectors.toList());
  861. deviceIdList = dbDeviceIdList;
  862. } else {
  863. return R.ok(new ArrayList<>());
  864. }
  865. }
  866. List<DeviceInfoDto.MercHomeCountVO> list = this.baseMapper.merHomeCountList(dto);
  867. if (CollUtil.isEmpty(list)) {
  868. return R.ok(Collections.emptyList());
  869. }
  870. List<DeviceInfoDto.MercHomeListVO> dataList = new ArrayList<>(list.size());
  871. LambdaQueryWrapper<DeviceInfo> lqw = new LambdaQueryWrapper<>();
  872. //非质检商户才需要激活
  873. lqw.eq(!isQa, DeviceInfo::getActiveState, DeviceActiveStateEnum.TRUE.getCode());
  874. lqw.eq(mercId != null, DeviceInfo::getMercId, mercId);
  875. lqw.eq(busyStatus != null, DeviceInfo::getBusyState, busyStatus);
  876. lqw.eq(deviceType != null, DeviceInfo::getDeviceType, deviceType);
  877. lqw.in(CollUtil.isNotEmpty(deviceIdList), DeviceInfo::getDeviceId, deviceIdList);
  878. lqw.like(StrUtil.isNotEmpty(deviceName), DeviceInfo::getDeviceName, deviceName).orderByAsc(true, DeviceInfo::getDeviceName, DeviceInfo::getDeviceId);
  879. List<DeviceInfoDto.Vo> deviceInfoList = copy(DeviceInfoDto.Vo.class, this.list(lqw));
  880. //根据点位ID查询管理员名字
  881. List<Long> placeIdList = deviceInfoList.stream().map(DeviceInfoDto.Vo::getPlaceId).distinct().filter(Objects::nonNull).collect(Collectors.toList());
  882. List<MercPlaceDto.Vo> mercPlaceList = mercPlaceService.list(new MercPlaceDto.ListDto().setIds(placeIdList)).getData();
  883. if (Emptys.check(mercPlaceList)) {
  884. //Map<Long, String> mercPlaceMap = mercPlaceList.stream().collect(Collectors.toMap(i -> i.getId(), i -> i.getAdminName()));
  885. Map<Long, String> mercPlaceMap = mercPlaceList.stream().collect(HashMap::new, (map, item) -> map.put(item.getId(), item.getAdminName()), HashMap::putAll);
  886. deviceInfoList.forEach(i -> i.setAdminName(mercPlaceMap.get(i.getPlaceId())));
  887. }
  888. //没有管理员的的设置默认值
  889. deviceInfoList.stream().filter(s -> s.getAdminName() == null).forEach(s -> s.setAdminName("未分配管理员"));
  890. List<String> adminNameList = deviceInfoList.stream().map(DeviceInfoDto.Vo::getAdminName).distinct().collect(Collectors.toList());
  891. //根据管理员名字分组
  892. Map<String, List<DeviceInfoDto.Vo>> deviceMap = deviceInfoList.stream().collect(Collectors.groupingBy(DeviceInfoDto.Vo::getAdminName));
  893. DateTime date = DateTime.now();
  894. DateTime start = DateUtil.beginOfDay(date);
  895. DateTime end = DateUtil.endOfDay(date);
  896. for (String adminName : adminNameList) {
  897. DeviceInfoDto.MercHomeListVO vo = new DeviceInfoDto.MercHomeListVO();
  898. vo.setAdminName(adminName);
  899. //区域下的设备列表
  900. List<DeviceInfoDto.MercHomeDeviceVo> deviceInfos = BeanUtil.copyToList(deviceMap.get(adminName), DeviceInfoDto.MercHomeDeviceVo.class);
  901. vo.setDeviceNum(deviceInfos.size());
  902. if (CollUtil.isEmpty(deviceInfos)) {
  903. continue;
  904. }
  905. //设备销售统计
  906. List<Long> deviceIds = deviceInfos.stream().map(DeviceInfoDto.MercHomeDeviceVo::getDeviceId).collect(Collectors.toList());
  907. //设备状态查询
  908. List<DeviceStatusDto.Vo> deviceStatusList = deviceStatusService.list(new DeviceStatusDto.SelectList().setDeviceIds(deviceIds)).getData();
  909. Map<Long, DeviceStatusDto.Vo> datdeviceStatusMap = new HashMap<>();
  910. if (CollUtil.isNotEmpty(deviceStatusList)) {
  911. datdeviceStatusMap = deviceStatusList.stream().collect(Collectors.toMap(DeviceStatusDto.Vo::getDeviceId, d -> d));
  912. }
  913. List<GoodsDeviceDto.Vo> goodsDeviceList = R.feignCheckData(goodsService.queryGoodsDeviceInfo(new GoodsDto.QueryGoodsDeviceInfo().setMercId(mercId).setDeviceIds(deviceIds)));
  914. Map<Long, List<GoodsDeviceDto.Vo>> deviceIdGoodsMap = goodsDeviceList.stream()
  915. .collect(Collectors.groupingBy(GoodsDeviceDto.Vo::getDeviceId));
  916. List<AlgorithmDto.ListNameId> algorithmList = R.feignCheckData(algorithmService.ListNameId());
  917. Map<Long, String> algorithmListMap = algorithmList.stream().collect(Collectors.toMap(AlgorithmDto.ListNameId::getId, AlgorithmDto.ListNameId::getAlias));
  918. for (DeviceInfoDto.MercHomeDeviceVo device : deviceInfos) {
  919. Long dId = device.getDeviceId();
  920. //设备类型 反显
  921. SysDictRedis dictDeviceType = SysDictUtils.get(DictConsts.DEVICE_TYPE, String.valueOf(device.getDeviceType()));
  922. device.setDeviceTypeName(dictDeviceType.getMsg());
  923. //运营状态 反显
  924. SysDictRedis dictBusyState = SysDictUtils.get(DictConsts.DEVICE_BUSY_STATUS, String.valueOf(device.getBusyState()));
  925. device.setBusyStateName(dictBusyState.getMsg());
  926. Integer zero = new Integer(0);
  927. CountDto.OrderByCreateTimeAndMercId orderByCreateTimeAndMercId = new CountDto.OrderByCreateTimeAndMercId()
  928. .setMerdId(mercId).setBeginTime(start).setEndTime(end).setDeviceIds(CollUtil.newArrayList(dId));
  929. log.info("设备订单统计:{}", JSONUtil.toJsonPrettyStr(orderByCreateTimeAndMercId));
  930. //完成订单
  931. CountDto.SuccessVo successVo = R.feignCheckData(countApiService.orderBySuccess(orderByCreateTimeAndMercId));
  932. //今日销售、库存情况 反显
  933. device.setDayOrderNum(successVo != null ? successVo.getOrdersSize() : zero);
  934. device.setDaySalesPrice(successVo != null ? successVo.getOrderTotalMoney() : zero);
  935. DeviceStatusDto.Vo deviceStatus = datdeviceStatusMap.get(device.getDeviceId());
  936. if (CollUtil.isNotEmpty(deviceIdGoodsMap)) {
  937. List<GoodsDeviceDto.Vo> goodsDevice = deviceIdGoodsMap.get(dId);
  938. if (CollUtil.isNotEmpty(goodsDevice)) {
  939. device.setOnSaleNum(goodsDevice.stream().mapToInt(GoodsDeviceDto.Vo::getStock).sum());
  940. device.setFillNum(goodsDevice.stream().mapToInt(GoodsDeviceDto.Vo::getFillCount).sum());
  941. }
  942. }
  943. //算法類型
  944. Long algorithmId = device.getAlgorithmId();
  945. if (algorithmId != null) {
  946. String name = algorithmListMap.get(algorithmId);
  947. device.setAlgorithmAlias(name);
  948. }
  949. //联网状态
  950. Integer netState = deviceStatus == null ? DeviceNetSateType.DISCONNECT.getCode() : deviceStatus.getNetState();
  951. device.setNetState(netState);
  952. if (netState == null) {
  953. device.setNetStateName(StrUtil.EMPTY);
  954. } else {
  955. device.setNetStateName(DeviceNetSateType.getEnumByCode(netState).getDescription());
  956. }
  957. if (deviceStatus != null) {
  958. Integer deviceStateL = deviceStatus.getLockStateL();
  959. Integer deviceStateR = deviceStatus.getLockStateR();
  960. device.setTempValue(deviceStatus.getTempValue());
  961. device.setDeviceStateL(deviceStateL);
  962. device.setDeviceStateR(deviceStateR);
  963. Boolean isUseBattery = deviceStatus.getIsUseBattery();
  964. if (BooleanUtil.isTrue(isUseBattery)) {
  965. //使用电池。即断电状态
  966. device.setSysPower(2);
  967. } else {
  968. device.setSysPower(1);
  969. }
  970. DeviceLockState deviceLockStateL = DeviceLockState.getEnumByCode(deviceStateL);
  971. DeviceLockState deviceLockStateR = DeviceLockState.getEnumByCode(deviceStateR);
  972. device.setDeviceStateRName(deviceLockStateR == null ? "未知" : deviceLockStateR.getDescription());
  973. device.setDeviceStateLName(deviceLockStateL == null ? "未知" : deviceLockStateL.getDescription());
  974. }
  975. }
  976. //名称排序
  977. if (CollUtil.isNotEmpty(deviceInfos)) {
  978. deviceInfos = ListUtil.sortByProperty(deviceInfos, LambdaUtils.getProperty(DeviceInfoDto.MercHomeDeviceVo::getDeviceName));
  979. deviceInfos = ListUtil.sortByProperty(deviceInfos, LambdaUtils.getProperty(DeviceInfoDto.MercHomeDeviceVo::getDeviceId));
  980. }
  981. vo.setDeviceInfos(deviceInfos);
  982. dataList.add(vo);
  983. }
  984. DeviceInfoDto.MercHomeListVO orderTemp = null;
  985. for (int i = 0; i < dataList.size(); i++) {
  986. if ("未分配管理员".equals(dataList.get(i).getAdminName())) {
  987. orderTemp = dataList.get(i);
  988. dataList.remove(i);
  989. dataList.add(orderTemp);
  990. break;
  991. }
  992. }
  993. return R.ok(dataList);
  994. }
  995. /**
  996. * 获取商户设备列表
  997. *
  998. * @param mercId
  999. * @return
  1000. */
  1001. private List<DeviceInfo> getDevicesByMercId(Long mercId) {
  1002. return list(Wrappers.<DeviceInfo>lambdaQuery().eq(DeviceInfo::getMercId, mercId).eq(DeviceInfo::getActiveState, DeviceActiveStateEnum.TRUE.getCode()));
  1003. }
  1004. /**
  1005. * 小程序商户设备搜索
  1006. *
  1007. * @param page
  1008. * @return
  1009. */
  1010. @Override
  1011. public R<PageBean<DeviceInfoDto.MerHomeSearchVO>> mercDeviceSearchPage(@RequestBody DeviceInfoDto.Page page) {
  1012. PageBean<DeviceInfoDto.MerHomeSearchVO> pageData = new PageBean<>();
  1013. //小程序独有查询字段 缺货状态:stockStatus ,是否查故障设备:fault
  1014. Boolean fault = page.getFault();
  1015. Long mercId = page.getMercId();
  1016. List<Long> myDeviceIds = page.getMyDeviceIds();
  1017. if (CollUtil.isEmpty(myDeviceIds)) {
  1018. //无设备
  1019. return R.ok(pageData);
  1020. }
  1021. if (BooleanUtil.isTrue(fault)) {
  1022. //查询故障设备
  1023. List<DeviceEventMsg> deviceEventMsgs = deviceEventMsgService.list(Wrappers.<DeviceEventMsg>lambdaQuery()
  1024. .eq(mercId != null, DeviceEventMsg::getMercId, page.getMercId())
  1025. .in(DeviceEventMsg::getDeviceId, myDeviceIds));
  1026. List<Long> deviceIdList = deviceEventMsgs.stream().map(DeviceEventMsg::getDeviceId).collect(Collectors.toList());
  1027. if (CollUtil.isEmpty(deviceIdList)) {
  1028. return R.ok(new PageBean<>());
  1029. }
  1030. page.setDeviceIdList(deviceIdList);
  1031. } else {
  1032. page.setDeviceIdList(myDeviceIds);
  1033. }
  1034. PageBean<DeviceInfoDto.Vo2> pageBean = queryPage(page);
  1035. List<DeviceInfoDto.Vo2> records = pageBean.getRecords();
  1036. if (CollUtil.isEmpty(records)) {
  1037. return R.ok(pageData);
  1038. }
  1039. List<DeviceInfoDto.MerHomeSearchVO> merHomeSearchRecords = new ArrayList<>();
  1040. BeanUtil.copyProperties(pageBean, pageData);
  1041. if (CollUtil.isNotEmpty(records)) {
  1042. List<Long> deviceIds = records.stream().map(DeviceInfoDto.Vo2::getDeviceId).collect(Collectors.toList());
  1043. String type = EnumDeviceDataType.DAY.getCode();
  1044. String todayDate = DateUtil.format(new Date(), DatePattern.PURE_DATE_PATTERN);
  1045. //查询当天
  1046. DeviceDataDto.ListDTO dto = new DeviceDataDto.ListDTO()
  1047. .setDeviceIds(deviceIds).setType(type).setDateValue(Integer.valueOf(todayDate)).setMercId(mercId);
  1048. List<DeviceDataDto.Vo> deviceDataList = deviceDataService.list(dto);
  1049. Map<Long, DeviceDataDto.Vo> dataMap = MapUtil.newHashMap();
  1050. //统计数据反显
  1051. if (CollUtil.isNotEmpty(deviceDataList)) {
  1052. dataMap = deviceDataList.stream().collect(Collectors.toMap(DeviceDataDto.Vo::getDeviceId, d -> d));
  1053. }
  1054. for (DeviceInfoDto.Vo2 v : records) {
  1055. DeviceInfoDto.MerHomeSearchVO merHomeSearchVO = new DeviceInfoDto.MerHomeSearchVO();
  1056. Long deviceId = v.getDeviceId();
  1057. DeviceDataDto.Vo vo = dataMap.get(deviceId);
  1058. if (vo != null) {
  1059. //今日订单数
  1060. v.setDayOrderNum(vo != null ? vo.getSalesCount() : 0);
  1061. v.setDaySalesPrice(vo != null ? vo.getSalesMoney() : 0);
  1062. BeanUtil.copyProperties(vo, merHomeSearchVO);
  1063. }
  1064. DeviceSysinfoDto.Vo deviceSysinfo = v.getDeviceSysinfo();
  1065. DeviceStatusDto.Vo deviceStatus = v.getDeviceStatus();
  1066. merHomeSearchVO.setAppUpmVersion(deviceSysinfo.getAppUpmVersion());
  1067. merHomeSearchVO.setTempValue(deviceStatus.getTempValue());
  1068. merHomeSearchVO.setNetDbm(deviceStatus.getNetDbm());
  1069. Integer deviceStateL = deviceStatus.getLockStateL();
  1070. Integer deviceStateR = deviceStatus.getLockStateR();
  1071. merHomeSearchVO.setDeviceStateL(deviceStateL);
  1072. merHomeSearchVO.setDeviceStateR(deviceStateR);
  1073. DeviceLockState deviceLockStateL = DeviceLockState.getEnumByCode(deviceStateL);
  1074. DeviceLockState deviceLockStateR = DeviceLockState.getEnumByCode(deviceStateR);
  1075. merHomeSearchVO.setDeviceStateRName(deviceLockStateR == null ? "未知" : deviceLockStateR.getDescription());
  1076. merHomeSearchVO.setDeviceStateLName(deviceLockStateL == null ? "未知" : deviceLockStateL.getDescription());
  1077. merHomeSearchRecords.add(merHomeSearchVO);
  1078. }
  1079. pageData.setRecords(merHomeSearchRecords);
  1080. }
  1081. return R.ok(pageData);
  1082. }
  1083. /**
  1084. * 小程序商户设备详情-数据统计
  1085. *
  1086. * @param dto
  1087. * @return
  1088. */
  1089. @Override
  1090. @ApiOperation("商户设备详情-数据统计")
  1091. public R<DeviceInfoDto.DeviceDataCountVO> dataCount(DeviceInfoDto.DeviceDataCountDTO dto) {
  1092. // 统计类型(1=经营数据,2=经营图表,3=温度图表,4=信号图表,5=商品管理)
  1093. Integer type = dto.getType();
  1094. switch (type) {
  1095. case 1:
  1096. //经营数据
  1097. return R.ok(dataCount1(dto));
  1098. case 2:
  1099. //经营图表
  1100. return R.ok(dataCount2(dto));
  1101. case 3:
  1102. //温度图表
  1103. return R.ok(dataCount3(dto));
  1104. case 4:
  1105. //信号图表
  1106. return R.ok(dataCount4(dto));
  1107. case 5:
  1108. //商品管理
  1109. return R.ok(dataCount5(dto));
  1110. default:
  1111. break;
  1112. }
  1113. return R.ok();
  1114. }
  1115. /**
  1116. * 经营数据
  1117. *
  1118. * @param dto
  1119. * @return
  1120. */
  1121. private DeviceInfoDto.DeviceDataCountVO dataCount1(DeviceInfoDto.DeviceDataCountDTO dto) {
  1122. Long deviceId = dto.getDeviceId();
  1123. Long mercId = dto.getMercId();
  1124. DeviceInfoDto.DeviceDataCountVO vo = new DeviceInfoDto.DeviceDataCountVO();
  1125. //当天
  1126. DateTime date = DateTime.now();
  1127. DateTime start = DateUtil.beginOfDay(date);
  1128. DateTime end = DateUtil.endOfDay(date);
  1129. List<Long> deviceIds = CollUtil.newArrayList(deviceId);
  1130. CountDto.OrderByCreateTimeAndMercId orderByCreateTimeAndMercId = new CountDto.OrderByCreateTimeAndMercId()
  1131. .setMerdId(mercId).setBeginTime(start).setEndTime(end).setDeviceIds(deviceIds);
  1132. //完成订单
  1133. CountDto.SuccessVo successVo = R.feignCheckData(countApiService.orderBySuccess(orderByCreateTimeAndMercId));
  1134. DeviceDataDto.Vo dayData = new DeviceDataDto.Vo();
  1135. if (successVo != null) {
  1136. dayData.setSalesMoney(successVo.getOrderTotalMoney());
  1137. dayData.setRefundMoney(successVo.getRefundMoney());
  1138. dayData.setSalesCount(successVo.getOrdersSize());
  1139. }
  1140. //当月
  1141. DeviceDataDto.Vo monthData = new DeviceDataDto.Vo();
  1142. //月度统计
  1143. DateTime startM = DateUtil.beginOfMonth(date);
  1144. DateTime endM = DateUtil.endOfMonth(date);
  1145. CountDto.OrderByCreateTimeAndMercId monthOrder = new CountDto.OrderByCreateTimeAndMercId()
  1146. .setMerdId(mercId).setBeginTime(startM).setEndTime(endM).setDeviceIds(deviceIds);
  1147. //完成订单
  1148. CountDto.SuccessVo successVoM = R.feignCheckData(countApiService.orderBySuccess(monthOrder));
  1149. if (monthData != null) {
  1150. monthData.setRefundMoney(successVoM.getRefundMoney());
  1151. monthData.setSalesMoney(successVoM.getOrderTotalMoney());
  1152. monthData.setSalesCount(successVoM.getOrdersSize());
  1153. }
  1154. vo.setDayBusinessData(copy(DeviceInfoDto.BusinessData.class, dayData));
  1155. vo.setMonthBusinessData(copy(DeviceInfoDto.BusinessData.class, monthData));
  1156. return vo;
  1157. }
  1158. /**
  1159. * 经营图表
  1160. *
  1161. * @param dto
  1162. * @return
  1163. */
  1164. private DeviceInfoDto.DeviceDataCountVO dataCount2(DeviceInfoDto.DeviceDataCountDTO dto) {
  1165. Long deviceId = dto.getDeviceId();
  1166. //近一个月 (销售额,订单数,退款金额,退款数)
  1167. DeviceInfoDto.DeviceDataCountVO vo = new DeviceInfoDto.DeviceDataCountVO();
  1168. DeviceInfoDto.BusinessChart businessChart = new DeviceInfoDto.BusinessChart();
  1169. List<String> categories = DataTime.dayListByLastDay(30);
  1170. List<Integer> dateList = new ArrayList<>(30);
  1171. businessChart.setCategories(categories);
  1172. categories.forEach(d -> {
  1173. dateList.add(Integer.valueOf(d.replaceAll(StrUtil.DASHED, StrUtil.EMPTY)));
  1174. });
  1175. Integer startDay = dateList.get(0);
  1176. Integer endDay = dateList.get(categories.size() - 1);
  1177. List<DeviceDataDto.Vo> listByDay = deviceDataService.getListByDay(deviceId, startDay, endDay);
  1178. if (CollUtil.isEmpty(listByDay)) {
  1179. return vo;
  1180. }
  1181. //每天的数据
  1182. Map<Integer, DeviceDataDto.Vo> dataDayMap = listByDay.stream().collect(Collectors.toMap(DeviceDataDto.Vo::getDateValue, i -> i));
  1183. List<DeviceInfoDto.MyChartSeries3> series = new ArrayList<>();
  1184. String[] names = {"销售额", "订单数", "退款金额", "退款数"};
  1185. for (int i = 0; i < names.length; i++) {
  1186. DeviceInfoDto.MyChartSeries3 myChartSeries = new DeviceInfoDto.MyChartSeries3();
  1187. //某个类型每天的数据
  1188. List<String> data = new ArrayList<>();
  1189. if (i == 0) {
  1190. //销售额
  1191. dateList.forEach(d -> {
  1192. //每日数据填充
  1193. DeviceDataDto.Vo deviceData = dataDayMap.get(d);
  1194. if (deviceData == null) {
  1195. data.add(String.valueOf(BigDecimal.ZERO));
  1196. } else {
  1197. Integer salesMoney = deviceData.getSalesMoney() == null ? 0 : deviceData.getSalesMoney();
  1198. BigDecimal day = BigDecimal.valueOf(salesMoney).divide(BigDecimal.valueOf(100));
  1199. data.add(String.valueOf(day));
  1200. }
  1201. });
  1202. } else if (i == 1) {
  1203. //订单数
  1204. dateList.forEach(d -> {
  1205. //每日数据填充
  1206. DeviceDataDto.Vo deviceData = dataDayMap.get(d);
  1207. String value = "0";
  1208. if (deviceData == null) {
  1209. data.add(value);
  1210. } else {
  1211. data.add(deviceData == null ? value : String.valueOf(deviceData.getSalesCount()));
  1212. }
  1213. });
  1214. } else if (i == 2) {
  1215. //退款金额
  1216. dateList.forEach(d -> {
  1217. //每日数据填充
  1218. DeviceDataDto.Vo deviceData = dataDayMap.get(d);
  1219. if (deviceData == null) {
  1220. data.add(String.valueOf(BigDecimal.ZERO));
  1221. } else {
  1222. Integer refundMoney = deviceData.getRefundMoney() == null ? 0 : deviceData.getRefundMoney();
  1223. BigDecimal day = BigDecimal.valueOf(refundMoney).divide(BigDecimal.valueOf(100));
  1224. data.add(String.valueOf(day));
  1225. }
  1226. });
  1227. } else if (i == 3) {
  1228. //退款数
  1229. dateList.forEach(d -> {
  1230. //每日数据填充
  1231. DeviceDataDto.Vo deviceData = dataDayMap.get(d);
  1232. if (deviceData == null) {
  1233. data.add("0");
  1234. } else {
  1235. data.add(deviceData == null ? "0" : String.valueOf(deviceData.getRefundCount()));
  1236. }
  1237. });
  1238. }
  1239. myChartSeries.setName(names[i]);
  1240. myChartSeries.setData(data);
  1241. series.add(myChartSeries);
  1242. }
  1243. businessChart.setSeries(series);
  1244. vo.setBusinessChart(businessChart);
  1245. return vo;
  1246. }
  1247. /**
  1248. * 温度图表
  1249. *
  1250. * @param dto
  1251. * @return
  1252. */
  1253. private DeviceInfoDto.DeviceDataCountVO dataCount3(DeviceInfoDto.DeviceDataCountDTO dto) {
  1254. DeviceInfoDto.DeviceDataCountVO vo = new DeviceInfoDto.DeviceDataCountVO();
  1255. Long deviceId = dto.getDeviceId();
  1256. String choosDate = dto.getChoosDate();
  1257. if (StrUtil.isEmpty(choosDate)) {
  1258. choosDate = DateUtil.formatDate(new Date());
  1259. }
  1260. String startTime = choosDate + " 00:00:00";
  1261. String endTime = choosDate + " 23:59:59";
  1262. //查询选定日期的温度数据
  1263. List<DeviceTempRecords> deviceTempRecords = deviceTempRecordsService.list(Wrappers.<DeviceTempRecords>lambdaQuery()
  1264. .eq(DeviceTempRecords::getDeviceId, deviceId).between(DeviceTempRecords::getCreateTime, startTime, endTime)
  1265. .orderBy(true, true, DeviceTempRecords::getCreateTime));
  1266. if (CollUtil.isEmpty(deviceTempRecords)) {
  1267. return vo;
  1268. }
  1269. DeviceInfoDto.TemperatureChart temperatureChart = new DeviceInfoDto.TemperatureChart();
  1270. List<String> categories = new ArrayList<>();
  1271. String name = "温度";
  1272. List<DeviceInfoDto.MyChartSeries2> series = new ArrayList<>();
  1273. List<BigDecimal> data = new ArrayList<>();
  1274. deviceTempRecords.forEach(d -> {
  1275. Integer tempValue = d.getTempValue();
  1276. LocalDateTime createTime = d.getCreateTime();
  1277. String time = DateUtil.format(createTime, "HH:mm:ss");
  1278. categories.add(time);
  1279. data.add(tempValue == null ? null : NumberUtil.toBigDecimal(tempValue));
  1280. });
  1281. DeviceInfoDto.MyChartSeries2 myChartSeries = new DeviceInfoDto.MyChartSeries2();
  1282. myChartSeries.setName(name);
  1283. myChartSeries.setData(data);
  1284. series.add(myChartSeries);
  1285. temperatureChart.setSeries(series);
  1286. temperatureChart.setCategories(categories);
  1287. vo.setTemperatureChart(temperatureChart);
  1288. return vo;
  1289. }
  1290. /**
  1291. * 信号图表
  1292. *
  1293. * @param dto
  1294. * @return
  1295. */
  1296. private DeviceInfoDto.DeviceDataCountVO dataCount4(DeviceInfoDto.DeviceDataCountDTO dto) {
  1297. DeviceInfoDto.DeviceDataCountVO vo = new DeviceInfoDto.DeviceDataCountVO();
  1298. Long deviceId = dto.getDeviceId();
  1299. String choosDate = dto.getChoosDate();
  1300. if (StrUtil.isEmpty(choosDate)) {
  1301. choosDate = DateUtil.formatDate(new Date());
  1302. }
  1303. String startTime = choosDate + " 00:00:00";
  1304. String endTime = choosDate + " 23:59:59";
  1305. //查询选定日期的温度数据
  1306. List<DeviceNetRecord> deviceNetRecords = deviceNetRecordService.list(Wrappers.<DeviceNetRecord>lambdaQuery()
  1307. .eq(DeviceNetRecord::getDeviceId, deviceId).between(DeviceNetRecord::getCreateTime, startTime, endTime)
  1308. .orderBy(true, true, DeviceNetRecord::getCreateTime));
  1309. if (CollUtil.isEmpty(deviceNetRecords)) {
  1310. return vo;
  1311. }
  1312. DeviceInfoDto.SignalChart signalChart = new DeviceInfoDto.SignalChart();
  1313. List<String> categories = new ArrayList<>();
  1314. String name = "信号";
  1315. List<DeviceInfoDto.MyChartSeries> series = new ArrayList<>();
  1316. List<Integer> data = new ArrayList<>();
  1317. deviceNetRecords.forEach(d -> {
  1318. Integer value = d.getSimDbm();
  1319. LocalDateTime createTime = d.getCreateTime();
  1320. String time = DateUtil.format(createTime, "HH:mm:ss");
  1321. categories.add(time);
  1322. data.add(value);
  1323. });
  1324. DeviceInfoDto.MyChartSeries myChartSeries = new DeviceInfoDto.MyChartSeries();
  1325. myChartSeries.setName(name);
  1326. myChartSeries.setData(data);
  1327. series.add(myChartSeries);
  1328. signalChart.setSeries(series);
  1329. signalChart.setCategories(categories);
  1330. vo.setSignalChart(signalChart);
  1331. return vo;
  1332. }
  1333. /**
  1334. * 商品管理
  1335. *
  1336. * @param dto
  1337. * @return
  1338. */
  1339. private DeviceInfoDto.DeviceDataCountVO dataCount5(DeviceInfoDto.DeviceDataCountDTO dto) {
  1340. Long deviceId = dto.getDeviceId();
  1341. DeviceInfoDto.DeviceDataCountVO vo = new DeviceInfoDto.DeviceDataCountVO();
  1342. DeviceInfoDto.GoodsData goodsData = new DeviceInfoDto.GoodsData();
  1343. //在售商品种类
  1344. //根据设备ID查商品id
  1345. GoodsDeviceDto.SelectList selectList = new GoodsDeviceDto.SelectList();
  1346. selectList.setDeviceIds(CollUtil.newArrayList(deviceId));
  1347. selectList.setMercId(dto.getMercId());
  1348. R<List<GoodsDeviceDto.Vo>> r = R.feignCheck(goodsDeviceService.list(selectList));
  1349. List<GoodsDeviceDto.Vo> goodsDeviceList = r.getData();
  1350. Integer stock = 0;
  1351. Integer afterFillStock = 0;
  1352. if (CollUtil.isNotEmpty(goodsDeviceList)) {
  1353. stock = goodsDeviceList.stream().mapToInt(GoodsDeviceDto.Vo::getStock).sum();
  1354. afterFillStock = goodsDeviceList.stream().mapToInt(GoodsDeviceDto.Vo::getFillCount).sum();
  1355. }
  1356. goodsData.setAfterFillStock(afterFillStock);
  1357. goodsData.setStock(stock);
  1358. goodsData.setCategoryNum(CollUtil.isNotEmpty(goodsDeviceList) ? goodsDeviceList.size() : new Integer(0));
  1359. vo.setGoodsData(goodsData);
  1360. return vo;
  1361. }
  1362. public PageBean<DeviceInfoDto.Vo2> queryPage(DeviceInfoDto.Page page) {
  1363. IPage<DeviceInfoQueryPage> iPage = baseMapper.queryPage(toIPage(page.getPage()), page);
  1364. return toPageBean(DeviceInfoDto.Vo2.class, iPage);
  1365. }
  1366. private <T> void check(T value, T value2, String msg) {
  1367. if (value.toString().equals(value2.toString())) {
  1368. throw new CommRuntimeException(msg);
  1369. }
  1370. }
  1371. }