| | |
| | | package com.doumee.service.business.impl; |
| | | |
| | | import com.doumee.biz.system.SystemDictDataBiz; |
| | | import com.doumee.core.annotation.excel.ExcelImporter; |
| | | 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.StringTools; |
| | | import com.doumee.core.utils.Utils; |
| | | import com.doumee.dao.business.BaseParamMapper; |
| | | import com.doumee.dao.business.BikesMapper; |
| | | import com.doumee.dao.business.MemberRidesMapper; |
| | | import com.doumee.dao.business.join.BikesJoinMapper; |
| | | import com.doumee.dao.business.model.*; |
| | | import com.doumee.dao.business.vo.BikesImportVO; |
| | | import com.doumee.dao.business.web.response.BikeLogDTO; |
| | | import com.doumee.dao.business.web.response.MemberRidesDTO; |
| | | import com.doumee.dao.business.web.response.MiniProgrammeDTO; |
| | |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.doumee.service.system.SystemDictDataService; |
| | | import com.github.xiaoymin.knife4j.core.util.StrUtil; |
| | | import com.github.yulichang.wrapper.MPJLambdaWrapper; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.apache.poi.ss.usermodel.CellType; |
| | | import org.apache.shiro.SecurityUtils; |
| | | import org.checkerframework.checker.units.qual.C; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.CollectionUtils; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.math.BigDecimal; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | @Service |
| | | public class BikesServiceImpl implements BikesService { |
| | | |
| | | |
| | | @Autowired |
| | | private RedisTemplate<String, Object> redisTemplate; |
| | | @Autowired |
| | | private BikesMapper bikesMapper; |
| | | @Autowired |
| | | private BikesJoinMapper bikesJoinMapper; |
| | | |
| | | |
| | | private BaseParamMapper baseParamMapper; |
| | | @Autowired |
| | | SystemDictDataMapper systemDictDataMapper; |
| | | private BikesJoinMapper bikesJoinMapper; |
| | | @Autowired |
| | | private MemberRidesMapper memberRidesMapper; |
| | | @Autowired |
| | | private SystemDictDataMapper systemDictDataMapper; |
| | | |
| | | @Override |
| | | public String create(Bikes bikes) { |
| | | if(StringUtils.isBlank(bikes.getParamId() ) |
| | | ||StringUtils.isBlank(bikes.getCode()) |
| | | ||StringUtils.isBlank(bikes.getDeviceSn())){ |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST); |
| | | } |
| | | if(baseParamMapper.selectCount(new QueryWrapper<BaseParam>().lambda().eq(BaseParam::getIsdeleted,Constants.ZERO) |
| | | .eq(BaseParam::getType,Constants.FOUR)) ==0){ |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,请选择正确的电车类型!"); |
| | | } |
| | | QueryWrapper<Bikes> wrapper = new QueryWrapper<>(); |
| | | wrapper.lambda() |
| | | .eq(Bikes::getCode,bikes.getCode()) |
| | | .eq(Bikes::getIsdeleted,Constants.ZERO) |
| | | .eq(Bikes::getType,Constants.ONE); |
| | | Long count = bikesMapper.selectCount(wrapper); |
| | | if (count > 0){ |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"车牌号号已存在"); |
| | | } |
| | | LoginUserInfo user =(LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); |
| | | bikes.setId(UUID.randomUUID().toString().replace("-","")); |
| | | bikes.setCreateDate(new Date()); |
| | | bikes.setCreator(user.getId()); |
| | | bikes.setEditDate(bikes.getCreateDate()); |
| | | bikes.setEditor(user.getId()); |
| | | bikes.setStatus(Constants.ZERO); |
| | | bikes.setIsdeleted(Constants.ZERO); |
| | | bikes.setType(Constants.ONE);//只能新增电车数据 |
| | | bikesMapper.insert(bikes); |
| | | return bikes.getId(); |
| | | } |
| | |
| | | |
| | | @Override |
| | | public void updateById(Bikes bikes) { |
| | | bikesMapper.updateById(bikes); |
| | | Bikes model =bikesMapper.selectById(bikes.getId()); |
| | | if (model ==null || Constants.equalsInteger(model.getIsdeleted(),Constants.ONE)){ |
| | | throw new BusinessException(ResponseStatus.DATA_EMPTY); |
| | | } |
| | | QueryWrapper<Bikes> wrapper = new QueryWrapper<>(); |
| | | wrapper.lambda() |
| | | .eq(Bikes::getCode,bikes.getCode()) |
| | | .eq(Bikes::getIsdeleted,Constants.ZERO) |
| | | .ne(Bikes::getId,bikes.getId()) |
| | | .eq(Bikes::getType,Constants.ONE); |
| | | long count = bikesMapper.selectCount(wrapper); |
| | | if (count > 0){ |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"车牌号号已存在"); |
| | | } |
| | | if(StringUtils.isNotBlank(bikes.getParamId()) && |
| | | baseParamMapper.selectCount(new QueryWrapper<BaseParam>().lambda().eq(BaseParam::getIsdeleted,Constants.ZERO) |
| | | .eq(BaseParam::getType,Constants.FOUR)) ==0){ |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,请选择正确的电车类型!"); |
| | | } |
| | | |
| | | LoginUserInfo user =(LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); |
| | | bikesMapper.update(null,new UpdateWrapper<Bikes>().lambda() |
| | | .set(Bikes::getEditDate,new Date()) |
| | | .set(Bikes::getEditor,user.getId()) |
| | | .set(StringUtils.isNotBlank(bikes.getCode()),Bikes::getCode,bikes.getCode()) |
| | | .set(!StringUtils.equals( model.getCode(),bikes.getCode()),Bikes::getImgurl,null) |
| | | .set(StringUtils.isNotBlank(bikes.getDeviceSn()),Bikes::getDeviceSn,bikes.getDeviceSn()) |
| | | .set(StringUtils.isNotBlank(bikes.getParamId()),Bikes::getParamId,bikes.getParamId()) |
| | | .set(Bikes::getStatus, Constants.ZERO) |
| | | .set(Bikes::getIsdeleted, Constants.ZERO) |
| | | .eq(Bikes::getId,bikes.getId()) |
| | | ); |
| | | } |
| | | |
| | | @Override |
| | |
| | | @Override |
| | | public Bikes findById(String id) { |
| | | return bikesMapper.selectById(id); |
| | | } |
| | | @Override |
| | | public void updateByJtt( Bikes m){ |
| | | if(StringUtils.isBlank(m.getDeviceSn() )){ |
| | | return; |
| | | } |
| | | String tSn = StringTools.leftPad(m.getDeviceSn(),12,'0') ; |
| | | Bikes bikes = bikesJoinMapper.selectOne(new QueryWrapper<Bikes>().lambda() |
| | | .eq(Bikes::getDeviceSn,tSn) |
| | | .eq(Bikes::getIsdeleted,Constants.ZERO) |
| | | .eq(Bikes::getType,Constants.ONE) |
| | | .last("limit 1")); |
| | | if(bikes == null){ |
| | | return; |
| | | } |
| | | bikesJoinMapper.update(null,new UpdateWrapper<Bikes>().lambda() |
| | | .set(m.getLatitude()!=null,Bikes::getLatitude,m.getLatitude()) |
| | | .set(m.getVoltage()!=null,Bikes::getVoltage,m.getVoltage()) |
| | | .set(m.getLongitude()!=null,Bikes::getLongitude,m.getLongitude()) |
| | | .set(m.getHeartDate()!=null,Bikes::getHeartDate,m.getHeartDate()) |
| | | .eq(Bikes::getId,bikes.getId())); |
| | | } |
| | | |
| | | @Override |
| | |
| | | |
| | | QueryWrapper<SystemDictData> wrapper = new QueryWrapper<>(); |
| | | wrapper.lambda() |
| | | .in(SystemDictData::getLabel, Arrays.asList(Constants.FORCE_BACK_SITE,Constants.FORCE_BACK_LOCK)); |
| | | .in(SystemDictData::getLabel, Arrays.asList(Constants.FORCE_BACK_SITE,Constants.FORCE_BACK_LOCK,Constants.LOW_BIKE_VOLTAGE)); |
| | | List<SystemDictData> systemDictData = systemDictDataMapper.selectList(wrapper); |
| | | Map<String, SystemDictData> collect = systemDictData.stream().collect(Collectors.toMap(s -> s.getLabel(), s -> s)); |
| | | |
| | | double low = 0; |
| | | try { |
| | | low = Double.parseDouble(collect.get(Constants.LOW_BIKE_VOLTAGE).getCode()); |
| | | }catch (Exception e){ |
| | | } |
| | | IPage<Bikes> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity()); |
| | | MPJLambdaWrapper<Bikes> queryWrapper = new MPJLambdaWrapper<>(); |
| | | Utils.MP.blankToNull(pageWrap.getModel()); |
| | |
| | | .leftJoin(Sites.class,Sites::getId,Bikes::getSiteId) |
| | | .like(StringUtils.isNotBlank(pageWrap.getModel().getSiteId()),Bikes::getSiteId,pageWrap.getModel().getSiteId()) |
| | | .like(StringUtils.isNotBlank(pageWrap.getModel().getSiteName()), Sites::getName,pageWrap.getModel().getSiteName()) |
| | | .like(StringUtils.isNotBlank(pageWrap.getModel().getCode()), Bikes::getCode,pageWrap.getModel().getCode()) |
| | | .like(StringUtils.isNotBlank(pageWrap.getModel().getDeviceSn()), Bikes::getDeviceSn,pageWrap.getModel().getDeviceSn()) |
| | | .eq( pageWrap.getModel().getLockId() !=null,Bikes::getLockId,pageWrap.getModel().getLockId()) |
| | | .ne(Bikes::getLockId,Constants.formatIntegerFromStr(collect.get(Constants.FORCE_BACK_LOCK).getCode())) |
| | | .ne(Bikes::getSiteId,collect.get(Constants.FORCE_BACK_SITE).getCode()) |
| | | .eq( Bikes::getType,Constants.formatIntegerNum(pageWrap.getModel().getType()))//默认查询自行车 |
| | | .ne(Constants.formatIntegerNum(pageWrap.getModel().getType())==0,Bikes::getLockId,Constants.formatIntegerFromStr(collect.get(Constants.FORCE_BACK_LOCK).getCode())) |
| | | .ne(Constants.formatIntegerNum(pageWrap.getModel().getType())==0, Bikes::getSiteId,collect.get(Constants.FORCE_BACK_SITE).getCode()) |
| | | .and(Constants.formatIntegerNum(pageWrap.getModel().getHasBike()) == Constants.ONE, |
| | | s->s.isNull(Bikes::getCode) |
| | | .or(b->b.eq(Bikes::getCode,""))) |
| | | .and(Constants.formatIntegerNum(pageWrap.getModel().getHasBike()) == Constants.TWO,s->s |
| | | .isNotNull(Bikes::getCode) |
| | | .and(b->b.ne(Bikes::getCode,""))); |
| | | .isNotNull(Constants.formatIntegerNum(pageWrap.getModel().getType())==0,Bikes::getCode) |
| | | .and(Constants.formatIntegerNum(pageWrap.getModel().getType())==0,b->b.ne(Bikes::getCode,""))); |
| | | if(pageWrap.getModel().getLowVoltage()!=null){ |
| | | if(pageWrap.getModel().getLowVoltage() == Constants.ONE){ |
| | | //低电量 |
| | | queryWrapper.lt(Bikes::getVoltage,low); |
| | | }else{ |
| | | //正常电量 |
| | | queryWrapper.ge(Bikes::getVoltage,low); |
| | | } |
| | | } |
| | | queryWrapper.selectAll(Bikes.class) |
| | | .selectAs(BaseParam::getName,Bikes::getParamName) |
| | | .selectAs(Sites::getName,Bikes::getSiteName) ; |
| | | return PageData.from(bikesJoinMapper.selectJoinPage(page, Bikes.class, queryWrapper)); |
| | | PageData<Bikes> pageData = PageData.from(bikesJoinMapper.selectJoinPage(page, Bikes.class, queryWrapper)); |
| | | double finalLow = low; |
| | | if(com.github.xiaoymin.knife4j.core.util.CollectionUtils.isNotEmpty(pageData.getRecords())){ |
| | | pageData.getRecords().forEach(i->{ |
| | | if(Constants.equalsInteger(i.getType(),Constants.ONE)){ |
| | | if(Objects.isNull(i.getVoltage())){ |
| | | i.setLowVoltage(Constants.ONE); |
| | | }else{ |
| | | i.setLowVoltage(i.getVoltage().compareTo(new BigDecimal(Double.toString(finalLow)))>=0?0:1); |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | |
| | | return pageData; |
| | | |
| | | } |
| | | @Override |
| | | public PageData<Bikes> findPage(PageWrap<Bikes> pageWrap) { |
| | |
| | | QueryWrapper<Bikes> wrapper = new QueryWrapper<>(bikes); |
| | | return bikesMapper.selectCount(wrapper); |
| | | } |
| | | |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = {BusinessException.class,Exception.class}) |
| | | public String importBatch(MultipartFile file){ |
| | | Boolean importing = (Boolean) redisTemplate.opsForValue().get(Constants.RedisKeys.import_bikes); |
| | | if(importing!=null && importing){ |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,已存在导入任务正在执行中,请稍后再试!"); |
| | | } |
| | | LoginUserInfo user =(LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); |
| | | |
| | | redisTemplate.opsForValue().set(Constants.RedisKeys.import_bikes,true); |
| | | try { |
| | | ExcelImporter ie = null; |
| | | List<BikesImportVO> dataList =null; |
| | | try { |
| | | ie = new ExcelImporter(file,1,0, CellType.STRING); // 确保单元格类型为字符串 |
| | | dataList = ie.getDataList(BikesImportVO.class,null); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | if(dataList == null || dataList.size() ==0){ |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,录入数据为空!"); |
| | | } |
| | | List<Bikes> list = bikesJoinMapper.selectJoinList(Bikes.class,new MPJLambdaWrapper<Bikes>() |
| | | .selectAll(Bikes.class) |
| | | .eq(Member::getIsdeleted,Constants.ZERO) |
| | | ); |
| | | List<BaseParam> params = baseParamMapper.selectList(new QueryWrapper<BaseParam>().lambda() |
| | | .eq(BaseParam::getIsdeleted,Constants.ZERO) |
| | | .eq(BaseParam::getType,Constants.FOUR) |
| | | ); |
| | | List<Bikes> newList = new ArrayList<>(); |
| | | Date date = new Date(); |
| | | for(int i=0;i<dataList.size();i++){ |
| | | BikesImportVO model = dataList.get(i); |
| | | if(StringUtils.isBlank(model.getSn()) |
| | | &&StringUtils.isBlank(model.getCode()) |
| | | &&StringUtils.isBlank(model.getParamName())){ |
| | | continue; |
| | | } |
| | | Bikes bike = checkModelParam(model,newList,i,list,params,user.getId(),date); |
| | | newList.add(bike); |
| | | } |
| | | if(newList == null || newList.size() ==0){ |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,录入有效数据为空!"); |
| | | } |
| | | bikesMapper.insert(newList); |
| | | return "导入成功"; |
| | | }catch (BusinessException e){ |
| | | throw e; |
| | | }catch (Exception e){ |
| | | throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"员工信息导入失败,请稍后重试"); |
| | | }finally { |
| | | redisTemplate.delete(Constants.RedisKeys.import_bikes); |
| | | } |
| | | } |
| | | |
| | | private Bikes checkModelParam(BikesImportVO model, List<Bikes> newList, int index, List<Bikes> list, List<BaseParam> params,String userId,Date date) { |
| | | Bikes bike = new Bikes(); |
| | | if(StringUtils.isBlank(model.getParamName()) |
| | | ||StringUtils.isBlank(model.getCode()) |
| | | ||StringUtils.isBlank(model.getSn())){ |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第"+(index+3)+"行人员信息不完整,请检查表格内容!"); |
| | | } |
| | | if(params ==null){ |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,电车类型未配置,请联系系统技术人员处理!"); |
| | | } |
| | | for(BaseParam p: params){ |
| | | if(StringUtils.equals(model.getParamName(),p.getName())){ |
| | | bike.setParamId(p.getId()); |
| | | } |
| | | } |
| | | if(bike.getParamId() ==null){ |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第"+(index+3)+"行车辆类型【"+model.getParamName()+"】不存在,请检查表格内容!"); |
| | | } |
| | | if(list!=null){ |
| | | for(Bikes bikes: list){ |
| | | if(StringUtils.equals(model.getCode(),bikes.getCode())){ |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第"+(index+3)+"行车牌号【"+model.getCode()+"】已存在,请检查表格内容!"); |
| | | } |
| | | if(StringUtils.equals(model.getSn(),bikes.getDeviceSn())){ |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第"+(index+3)+"行控制器sn【"+model.getSn()+"】已存在,请检查表格内容!"); |
| | | } |
| | | } |
| | | } |
| | | for(Bikes bikes: newList){ |
| | | if(StringUtils.equals(model.getCode(),bikes.getCode())){ |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第"+(index+3)+"行车牌号【"+model.getCode()+"】重复出现,请检查表格内容!"); |
| | | } |
| | | if(StringUtils.equals(model.getSn(),bikes.getDeviceSn())){ |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第"+(index+3)+"行控制器sn【"+model.getSn()+"】重复出现,请检查表格内容!"); |
| | | } |
| | | } |
| | | bike.setCode(model.getCode()); |
| | | bike.setDeviceSn(model.getSn()); |
| | | bike.setId(UUID.randomUUID().toString()); |
| | | bike.setCreateDate(new Date()); |
| | | bike.setCreator(userId); |
| | | bike.setEditDate(date); |
| | | bike.setEditor(userId); |
| | | bike.setStatus(Constants.ZERO); |
| | | bike.setIsdeleted(Constants.ZERO); |
| | | bike.setType(Constants.ONE);//只能新增电车数据 |
| | | return bike; |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public List<Bikes> getEleBikes(String memberId) { |
| | | //查询当前是否处于骑行中 |
| | | MemberRides memberRides = memberRidesMapper.selectOne(new QueryWrapper<MemberRides>().lambda().eq(MemberRides::getMemberId,memberId) |
| | | .in(MemberRides::getStatus,Constants.MEMBER_RIDES_STATUS.RIDES_RUNNING.getKey() |
| | | ,Constants.MEMBER_RIDES_STATUS.RIDES_PAUSING.getKey()) |
| | | .eq(MemberRides::getType,Constants.ONE) |
| | | .orderByDesc(MemberRides::getCreateDate) |
| | | .last(" limit 1 ") |
| | | ); |
| | | QueryWrapper<Bikes> queryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.lambda().eq(Bikes::getIsdeleted,Constants.ZERO) |
| | | .eq(Bikes::getType,Constants.ONE) |
| | | .isNotNull(Bikes::getLatitude) |
| | | .isNotNull(Bikes::getLongitude); |
| | | if(Objects.nonNull(memberRides)&&StringUtils.isNotBlank(memberRides.getBikeCode())){ |
| | | queryWrapper.lambda().eq(Bikes::getCode,memberRides.getBikeCode()); |
| | | }else{ |
| | | queryWrapper.lambda().eq(Bikes::getStatus,Constants.ZERO); |
| | | } |
| | | return bikesMapper.selectList(queryWrapper); |
| | | } |
| | | |
| | | |
| | | } |