|  |  |  | 
|---|
|  |  |  | package com.doumee.service.business.impl; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.doumee.biz.system.SystemDictDataBiz; | 
|---|
|  |  |  | import com.doumee.core.constants.Constants; | 
|---|
|  |  |  | import com.doumee.core.dingding.DingDingNotice; | 
|---|
|  |  |  | import com.doumee.core.constants.Constants; | 
|---|
|  |  |  | import com.doumee.core.constants.ResponseStatus; | 
|---|
|  |  |  | import com.doumee.core.exception.BusinessException; | 
|---|
|  |  |  | import com.doumee.core.model.LoginUserInfo; | 
|---|
|  |  |  | import com.doumee.core.model.PageData; | 
|---|
|  |  |  | import com.doumee.core.model.PageWrap; | 
|---|
|  |  |  | import com.doumee.core.utils.Utils; | 
|---|
|  |  |  | import com.doumee.core.wx.WxMiniUtilService; | 
|---|
|  |  |  | import com.doumee.dao.business.LocksMapper; | 
|---|
|  |  |  | import com.doumee.dao.business.SitesMapper; | 
|---|
|  |  |  | import com.doumee.dao.business.model.Locks; | 
|---|
|  |  |  | import com.doumee.dao.business.model.Sites; | 
|---|
|  |  |  | import com.doumee.dao.business.web.request.SitesMonitorBikeDTO; | 
|---|
|  |  |  | import com.doumee.dao.business.web.request.SitesMonitorDTO; | 
|---|
|  |  |  | import com.doumee.service.business.SitesService; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.metadata.IPage; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | 
|---|
|  |  |  | import org.apache.commons.lang3.StringUtils; | 
|---|
|  |  |  | import org.checkerframework.checker.units.qual.C; | 
|---|
|  |  |  | import org.apache.shiro.SecurityUtils; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.scheduling.annotation.Async; | 
|---|
|  |  |  | import org.springframework.stereotype.Service; | 
|---|
|  |  |  | import org.springframework.util.CollectionUtils; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.math.BigDecimal; | 
|---|
|  |  |  | import java.util.ArrayList; | 
|---|
|  |  |  | import java.util.Date; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.Objects; | 
|---|
|  |  |  | import java.util.stream.Collectors; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 站点信息表Service实现 | 
|---|
|  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private SitesMapper sitesMapper; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private LocksMapper locksMapper; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private SystemDictDataBiz systemDictDataBiz; | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public String create(Sites sites) { | 
|---|
|  |  |  | sitesMapper.insert(sites); | 
|---|
|  |  |  | LoginUserInfo principal = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | QueryWrapper<Sites> wrapper = new QueryWrapper<>(); | 
|---|
|  |  |  | wrapper.lambda() | 
|---|
|  |  |  | .eq(Sites::getCode,sites.getCode()); | 
|---|
|  |  |  | Integer count = sitesMapper.selectCount(wrapper); | 
|---|
|  |  |  | if (count > 0){ | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"站点编号已存在"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | Sites insert = new Sites(); | 
|---|
|  |  |  | insert.setId(sites.getCode()); | 
|---|
|  |  |  | insert.setCreateDate(new Date()); | 
|---|
|  |  |  | insert.setCreator(principal.getId()); | 
|---|
|  |  |  | insert.setEditDate(new Date()); | 
|---|
|  |  |  | insert.setEditor(principal.getId()); | 
|---|
|  |  |  | insert.setIsdeleted(Constants.ZERO); | 
|---|
|  |  |  | insert.setInfo(sites.getInfo()); | 
|---|
|  |  |  | insert.setName(sites.getName()); | 
|---|
|  |  |  | insert.setCode(sites.getCode()); | 
|---|
|  |  |  | insert.setStatus(Constants.ZERO); | 
|---|
|  |  |  | insert.setLockNum(sites.getLockNum()); | 
|---|
|  |  |  | insert.setIp(sites.getIp()); | 
|---|
|  |  |  | sitesMapper.insert(insert); | 
|---|
|  |  |  | return sites.getId(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void updateById(Sites sites) { | 
|---|
|  |  |  | sitesMapper.updateById(sites); | 
|---|
|  |  |  | LoginUserInfo principal = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); | 
|---|
|  |  |  | QueryWrapper<Sites> wrapper = new QueryWrapper<>(); | 
|---|
|  |  |  | wrapper.lambda() | 
|---|
|  |  |  | .eq(Sites::getCode,sites.getCode()); | 
|---|
|  |  |  | Sites count = sitesMapper.selectOne(wrapper); | 
|---|
|  |  |  | if (Objects.nonNull(count) && !count.getId().equals(sites.getId())){ | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"站点编号已存在"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | Sites update = new Sites(); | 
|---|
|  |  |  | update.setId(sites.getId()); | 
|---|
|  |  |  | update.setCreateDate(new Date()); | 
|---|
|  |  |  | update.setEditor(principal.getId()); | 
|---|
|  |  |  | update.setName(sites.getName()); | 
|---|
|  |  |  | update.setLockNum(sites.getLockNum()); | 
|---|
|  |  |  | update.setIp(sites.getIp()); | 
|---|
|  |  |  | sitesMapper.updateById(update); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public Sites findOne(Sites sites) { | 
|---|
|  |  |  | QueryWrapper<Sites> wrapper = new QueryWrapper<>(sites); | 
|---|
|  |  |  | return sitesMapper.selectOne(wrapper); | 
|---|
|  |  |  | return sitesMapper.selectOne(wrapper.last(" limit 1")); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | 
|---|
|  |  |  | QueryWrapper<Sites> wrapper = new QueryWrapper<>(sites); | 
|---|
|  |  |  | return sitesMapper.selectList(wrapper); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public PageData<Sites> findPage(PageWrap<Sites> pageWrap) { | 
|---|
|  |  |  | IPage<Sites> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity()); | 
|---|
|  |  |  | 
|---|
|  |  |  | queryWrapper.lambda().eq(Sites::getInfo, pageWrap.getModel().getInfo()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (pageWrap.getModel().getName() != null) { | 
|---|
|  |  |  | queryWrapper.lambda().eq(Sites::getName, pageWrap.getModel().getName()); | 
|---|
|  |  |  | queryWrapper.lambda().like(Sites::getName, pageWrap.getModel().getName()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (pageWrap.getModel().getCode() != null) { | 
|---|
|  |  |  | queryWrapper.lambda().eq(Sites::getCode, pageWrap.getModel().getCode()); | 
|---|
|  |  |  | queryWrapper.lambda().like(Sites::getCode, pageWrap.getModel().getCode()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (pageWrap.getModel().getStatus() != null) { | 
|---|
|  |  |  | queryWrapper.lambda().eq(Sites::getStatus, pageWrap.getModel().getStatus()); | 
|---|
|  |  |  | 
|---|
|  |  |  | QueryWrapper<Sites> wrapper = new QueryWrapper<>(sites); | 
|---|
|  |  |  | return sitesMapper.selectCount(wrapper); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public PageData<Sites> findSitesMonitorDTOPage(PageWrap<SitesMonitorDTO> pageWrap) { | 
|---|
|  |  |  | //        IPage page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity()); | 
|---|
|  |  |  | IPage<Sites> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity()); | 
|---|
|  |  |  | QueryWrapper<Sites> queryWrapper = new QueryWrapper<>(); | 
|---|
|  |  |  | Utils.MP.blankToNull(pageWrap.getModel()); | 
|---|
|  |  |  | queryWrapper.select("*,(select count(r.id) from locks r where r.site_id = sites.id) as all_lock_num" + | 
|---|
|  |  |  | ",(select count(l.bike_code) from locks l where l.site_id = sites.id and l.bike_code IS NOT NULL and l.bike_code !='' ) as bikeCount"); | 
|---|
|  |  |  | //        Page<SitesMonitorDTO> sitesMonitorDTO = sitesMapper.getSitesMonitorDTO(page, pageWrap.getModel().getCode(), pageWrap.getModel().getName()); | 
|---|
|  |  |  | queryWrapper.lambda().like(StringUtils.isNotBlank(pageWrap.getModel().getCode()),Sites::getCode,pageWrap.getModel().getCode()); | 
|---|
|  |  |  | queryWrapper.lambda().like(StringUtils.isNotBlank(pageWrap.getModel().getName()),Sites::getName,pageWrap.getModel().getName()); | 
|---|
|  |  |  | queryWrapper.lambda().orderByAsc(Sites::getCode); | 
|---|
|  |  |  | IPage<Sites> sitesMonitorDTO = sitesMapper.selectPage(page, queryWrapper); | 
|---|
|  |  |  | if (!CollectionUtils.isEmpty(sitesMonitorDTO.getRecords())){ | 
|---|
|  |  |  | sitesMonitorDTO.getRecords().forEach(s->{ | 
|---|
|  |  |  | if(Constants.formatIntegerNum(s.getAllLockNum()) >0){ | 
|---|
|  |  |  | s.setRate(new BigDecimal(s.getBikeCount()).divide(new BigDecimal(s.getAllLockNum()),2,BigDecimal.ROUND_HALF_UP)); | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | s.setRate(new BigDecimal(0.0)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return PageData.from(sitesMonitorDTO); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void siteReservesNotice()  throws Exception{ | 
|---|
|  |  |  | QueryWrapper<Sites> queryWrapper = new QueryWrapper<>(); | 
|---|
|  |  |  | queryWrapper.select(" *, " + | 
|---|
|  |  |  | " (select count(r.id) from locks r where r.site_id = sites.id) as all_lock_num ," + | 
|---|
|  |  |  | "(select count(l.bike_code) from locks l where l.site_id = sites.id and l.bike_code IS NOT NULL and l.bike_code !='' ) as bikeCount"); | 
|---|
|  |  |  | queryWrapper.lambda().eq(Sites::getStatus,Constants.ZERO); | 
|---|
|  |  |  | queryWrapper.lambda().eq(Sites::getIsdeleted,Constants.ZERO); | 
|---|
|  |  |  | queryWrapper.lambda().orderByAsc(Sites::getCode); | 
|---|
|  |  |  | List<Sites> sitesList = sitesMapper.selectList(queryWrapper); | 
|---|
|  |  |  | if (!CollectionUtils.isEmpty(sitesList)){ | 
|---|
|  |  |  | for (Sites s:sitesList) { | 
|---|
|  |  |  | if(Constants.formatIntegerNum(s.getAllLockNum())<=Constants.ZERO){ | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | BigDecimal warnMin = new BigDecimal(systemDictDataBiz.queryByCode(Constants.MINI_PROGRAMME,Constants.WARN_MIN).getCode()); | 
|---|
|  |  |  | BigDecimal warnMax = new BigDecimal(systemDictDataBiz.queryByCode(Constants.MINI_PROGRAMME,Constants.WARN_MAX).getCode()); | 
|---|
|  |  |  | BigDecimal rata = new BigDecimal(s.getBikeCount()).divide(new BigDecimal(s.getAllLockNum()),2,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100L)); | 
|---|
|  |  |  | if(rata.compareTo(warnMax)>Constants.ZERO){ | 
|---|
|  |  |  | //发送钉钉通知 | 
|---|
|  |  |  | DingDingNotice.reservesNotice(StringUtils.isBlank(s.getName())?s.getId() + " - 未知" :s.getId() + " - " + s.getName(), | 
|---|
|  |  |  | systemDictDataBiz.queryByCode(Constants.DINGDING,Constants.DINGDING_URL).getCode(), | 
|---|
|  |  |  | systemDictDataBiz.queryByCode(Constants.DINGDING,Constants.DINGDING_SECRET).getCode(),Constants.ZERO,warnMax); | 
|---|
|  |  |  | }else if(rata.compareTo(warnMin)<Constants.ZERO){ | 
|---|
|  |  |  | //发送钉钉通知 | 
|---|
|  |  |  | DingDingNotice.reservesNotice(StringUtils.isBlank(s.getName())?s.getId() + " - 未知" :s.getId() + " - " + s.getName(), | 
|---|
|  |  |  | systemDictDataBiz.queryByCode(Constants.DINGDING,Constants.DINGDING_URL).getCode(), | 
|---|
|  |  |  | systemDictDataBiz.queryByCode(Constants.DINGDING,Constants.DINGDING_SECRET).getCode(),Constants.ONE,warnMin); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|