代码优化-杂合

1、日期处理

@Override
    public Integer getSendMsgNum(String userMobile) {
        Date now = new Date();
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(now);
        calendar.add(Calendar.DATE, -1);
        Date yesterday = calendar.getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        QueryWrapper<AisinoMsLogMessage> queryWrapper = new QueryWrapper<AisinoMsLogMessage>();
        queryWrapper.between("message_time", simpleDateFormat.format(yesterday), simpleDateFormat.format(now));
        queryWrapper.eq("message_type", 1);
        queryWrapper.eq("message_status", 0);
        Integer count = aisinoMsLogMessageMapper.selectCount(queryWrapper);
        return count;
    }

2、代码优化

  优化前

@Override
    public List<CommunityResult> getCommunityList(CommunityParams communityParams ){
        if(communityParams.getPageCount()!=null){
            communityParams.setLimit(communityParams.getLimit());
        }
        Page pageContext = getPageContext();
        List<CommunityResult> communityList=this.baseMapper.CommunityList(pageContext, communityParams);
        for (int i = 0; i < communityList.size(); i++) {
            String likeOpenName = this.aisinoWxLikeService.queryOpenName(communityList.get(i).getCommunityId());
            communityList.get(i).setLikeOpenName(likeOpenName);
            AisinoWxLikeParam  aisinoWxLikeParam=new AisinoWxLikeParam();
            aisinoWxLikeParam.setOpenId(communityParams.getOpenId());
            aisinoWxLikeParam.setCommunityId(communityList.get(i).getCommunityId());
            AisinoWxLikeResult aisinoMsLike = this.aisinoWxLikeService.findBySpec(aisinoWxLikeParam);
            String pictureUrl=communityList.get(i).getPictures();
            String pictureList="";
            List<Map<String,String>> urlList = new ArrayList<>();
            for (String url : pictureUrl.split(",")) {
                if(ToolUtil.isNotEmpty(url)){
                    Map<String,String> map = new HashMap<>();
                    map.put("url",url);
                    urlList.add(map);
                }
            }
            communityList.get(i).setPictureList(urlList);
            communityList.get(i).setFlag(aisinoMsLike == null ? true : false);
            communityList.get(i).setViewFlag(false);
        }
        return communityList;
    }

  优化后

 @Override
    public List<CommunityResult> getCommunityList(CommunityParams communityParams ){
        if(communityParams.getPageCount()!=null){
            communityParams.setLimit(communityParams.getLimit());
        }
        Page pageContext = getPageContext();
        List<CommunityResult> communityList=this.baseMapper.CommunityList(pageContext, communityParams);
        for (CommunityResult communityResult : communityList) {
            String likeOpenName = this.aisinoWxLikeService.queryOpenName(communityResult.getCommunityId());
            communityResult.setLikeOpenName(likeOpenName);

            AisinoWxLike aisinoMsLike = this.aisinoWxLikeService.getOne(new QueryWrapper<AisinoWxLike>().eq("open_id", communityParams.getOpenId()).eq("community_id", communityResult.getCommunityId()));

            String pictureUrl = communityResult.getPictures();
            List<Map<String, String>> urlList = new ArrayList<>();
            for (String url : pictureUrl.split(",")) {
                if (ToolUtil.isNotEmpty(url)) {
                    Map<String, String> map = new HashMap<>();
                    map.put("url", url);
                    urlList.add(map);
                }
            }
            communityResult.setPictureList(urlList);
            communityResult.setFlag(aisinoMsLike == null);
            communityResult.setViewFlag(false);
        }
        return communityList;
    }

3、遍历优化

  Map<String, Object> resultMap = new HashMap<>();
        List<ApplyOrder> applyOrders = this.applyOrderService.list(new QueryWrapper<ApplyOrder>().eq("code", applyCode));

        List<String> orderNos = new ArrayList<>();
        if(ToolUtil.isNotEmpty(applyOrders)){
            applyOrders.forEach(r->{
                orderNos.add(r.getOrderNo());
            });
        }
        //ProductOrder order = this.productOrderService.getOne(new QueryWrapper<ProductOrder>().eq("order_no", applyOrders.get(0).getOrderNo()));
        List<ProductOrder> orders = this.productOrderService.list(new QueryWrapper<ProductOrder>().in("order_no", orderNos));
        

 

posted @ 2022-03-31 11:00  关陈七  阅读(25)  评论(0)    收藏  举报