美文网首页乐高玩具
乐高项目分析(1)

乐高项目分析(1)

作者: wanggs | 来源:发表于2018-01-02 15:16 被阅读17次

    购买历史

    根据userId获取所有订单,遍历订单使用键值对筛选必要字段

    代码片段

    package app.front.api;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.JSONObject;
    
    import app.base.dao.legouser.OrdersDao;
    import app.base.dao.legouser.OrdersItemDao;
    import app.base.entity.legouser.Orders;
    import app.base.entity.legouser.OrdersItem;
    import app.base.entity.legouser.User;
    import app.front.UserContext;
    import app.front.api.params.BuyHistoryParam;
    import component.common.msg.Ret;
    import component.common.msg.Rets;
    import component.dao.query.Query;
    
    /**
     * 
     * @author tanghc
     *
     */
    @RestController
    @RequestMapping("/api/history")
    public class BuyHistoryApi {
        
        @Autowired
        OrdersDao ordersDao;
        @Autowired
        OrdersItemDao ordersItemDao;
        
        /**
         * 购买历史
         * @param param
         * @return
         */
        @RequestMapping(value="/listBuyHistory" ,method=RequestMethod.POST)
        public Ret buyHistory(@RequestBody BuyHistoryParam param) {
            User user = UserContext.getLoginUser();
            
            Query query= new Query();
            query.eq("userId", user.getId()).notNull("payTime");
            query.setPage(param.getPageIndex(), param.getPageSize());
            
            List<Orders> list = ordersDao.find(query);
            
            JSONArray orderRet = new JSONArray();
            for (Orders obj : list) {
                JSONObject json = (JSONObject)JSON.toJSON(obj);
                json.put("orderItems", this.buildOrderItems(obj));
                orderRet.add(json);
            }
            
            return Rets.successData(orderRet);
        }
        
        private List<OrdersItem> buildOrderItems(Orders obj) {
            Query query = new Query();
            query.eq("ordersId", obj.getId());
            
            return ordersItemDao.find(query);
        }
    }
    
    

    心愿列表详情

    分析: 设置分页,根据用户Id获取所有心愿单,遍历使用键值对筛选必要字段

        @Autowired
        private DreamItemDao dreamItemDao;
        
        @Autowired
        private GoodsDao goodsDao;
        
        @Autowired
        private GoodsService goodsService;
        /**
         * 心愿列表详情
         *
         * @return
         */
        @SuppressWarnings({ "unchecked", "rawtypes" })
        @RequestMapping(value = "/selectListDream", method = RequestMethod.POST)
        public Ret dreamList(@RequestBody PageInfo pageInfo) {
    
            User user = UserContext.getLoginUser();
            pageInfo.setPageSize(5);
            Query query = new Query();
            query.eq("userId", user.getId());
            query.addSort("orderIndex", "desc");
            query.setPage(pageInfo.getPageIndex(), pageInfo.getPageSize());
    
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
            pageInfo = QueryUtils.query(dreamItemDao, query);
            List<DreamItem> dreamList = pageInfo.getRows();
    
            for (DreamItem dreamItem : dreamList) {
                Goods goods = goodsDao.get(dreamItem.getGoodsId());
                Map<String, Object> map = new HashMap<String, Object>();
                dreamItem.setGoodsImgUrl(ImgUrlFormatter.formatter(dreamItem.getGoodsImgUrl()));
                map.put("dreamItem", dreamItem);
                map.put("collectCnt", goods.getCollectCnt());
                map.put("setCode", goods.getSetCodes().replace(",", "+"));
                try {
                    // 获取宝贝对于用户的状态 ,有没有玩过,可以玩,几天后可玩
                    map.put("state", goodsService.getStateByGoods(dreamItem.getGoodsId(), user.getId()));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                list.add(map);
            }
            pageInfo.setList(list);
            return Rets.successData(pageInfo);
        }
    

    获取宝贝对于用户的状态 ,有没有玩过,可以玩,几天后可玩

    /**
         * 获取宝贝对于用户的状态 ,有没有玩过,可以玩,几天后可玩
         *
         * @param goodsId
         * @param userId
         * @return
         * @throws Exception
         * @author ylshi
         */
        public String getStateByGoods(long goodsId, long userId) throws Exception {
            String state = "";
            //如果这个宝贝在订单中,表示用户已经玩过了
            int num = orderService.getGoodsOrderNum(goodsId, userId);
            if (num > 0) {
                state = "已经玩过";
                return state;
            }
            //如果这个宝贝还有库存,状态为马上可以玩
            int stock = goodsPositionService.getGoodsStock(goodsId, GoodsState.State.inStore.getState());
            if (stock > 0) {
                state = "马上可玩";
                return state;
            }
            //如果这个宝贝没有库存,查看订单中预计返还时间最小的订单
            Orders orders = orderService.orderBack(goodsId);
            if (orders != null) {
                if (orders.getBackTIme() != null) {
                    //如果返还时间为0或负数,代表该宝贝已经返回或正在返回,状态为马上可玩
                    int day = DateUtil.getDay(orders.getBackTIme(), new Date());
                    if (day <= 0) {
                        state = "马上可玩";
                        return state;
                    }
                    state = this.getGoodsState(day);
                }
            }
    
            return state;
        }
    
    

    根据天数 算出多少天之后可玩

     /**
         * 根据天数 算出多少天之后可玩
         *
         * @param day
         * @return
         * @author ylshi
         */
        public String getGoodsState(int day) {
            String state = "";
            if (day <= 0) {
                state = "马上可玩";
            } else {
                //计算出返回时间距离现在还有多长时间,以年月周日顺序排
                int year = day / 365;
                int month = (day - year * 365) / 30;
                int week = (day - year * 365 - month * 30) / 7;
                int day2 = day - year * 365 - month * 30 - week * 7;
    
                if (year != 0) {
                    state += year + "年";
                }
                if (month != 0) {
                    state += month + "月";
                }
                if (week != 0) {
                    state += week + "周";
                }
                if (day2 != 0) {
                    state += day2 + "天";
                }
                state += "后可玩";
            }
            return state;
        }
    

    添加到心愿单

    根据用户id和宝贝id,首先判断是否添加过,是添加过return;,否获取根据宝贝id获取宝贝信息...

      /**
         * 添加到心愿单
         *
         * @param
         * @return
         */
        @RequestMapping(value = "/addDreamItem", method = RequestMethod.POST)
        public Ret addDreamItem(@RequestBody Goods goods) {
            User user = UserContext.getLoginUser();
    
            dreamItemService.addDream(user.getId(), goods.getId());
    
            return Rets.success();
        }
    

    service

        /**
         * 添加心愿单
         *
         * @param goodsId
         */
        public void addDream(long userId, long goodsId) {
            Query query = new Query();
            query.eq("userId", userId).eq("goodsId", goodsId).setLimit(1);
    
            DreamItem dreamItem = this.getDao().getByExpression(query);
            // 如果之前添加过就不让添加
            if (dreamItem != null) {
                return;
            }
    
            Goods goods = goodsDao.get(goodsId);
    
            dreamItem = new DreamItem();
            dreamItem.setGmtCreate(new Date());
            dreamItem.setGoodsId(goodsId);
            dreamItem.setGoodsImgUrl(goods.getImgUrl());
            dreamItem.setGoodsName(goods.getGoodsName());
            dreamItem.setOrderIndex(0);
            dreamItem.setUserId(userId);
    
            this.getDao().save(dreamItem);
        }
    

    (批量)加入心愿单

    api

     /**
         * (批量)加入心愿单
         *
         * @param goodsIds
         * @return
         * @author ylshi
         */
        @RequestMapping(value = "/joinDreamItem", method = RequestMethod.POST)
        @ResponseBody
        public CommonResult joinDreamItem(@RequestBody Long[] goodsIds) {
            User user = UserContext.getLoginUser();
            try {
                dreamItemService.joinDreamItem(goodsIds, user.getId());
                return Rets.success();
            } catch (Exception e) {
                return Rets.errorMsg("操作失败");
            }
    
        }
    

    service

    
    

    相关文章

      网友评论

        本文标题:乐高项目分析(1)

        本文链接:https://www.haomeiwen.com/subject/ggzfnxtx.html