项目实训——后端篇4:查询历史记录功能

我们的项目中提供了用户的查询功能,同时保留了用户的查询历史记录。对于我们的项目来说,查询历史记录功能能够追踪用户活动、提高用户体验、确保法律合规、支持数据分析与优化,并在需要时提供数据恢复与备份,从而提升整体系统的功能性和用户满意度。

1、实体类设计

History实体类的设计如下

    /**
     * 主键ID
     */
    @TableId(type = IdType.AUTO)
    private Integer id;
    /**
     * 浏览历史关联用户id
     */
    private Integer userId;
    /**
     * 浏览历史类型
     */
    private Integer operateType;
    /**
     * 浏览历史关键字
     */
    private String keyword;
    /**
     * 浏览时间
     */
    private Date createTime;
    /**
     * 更新时间
     */
    private Date updateTime;

包括了用户id、浏览历史类型、历史记录关键字、创建和更新时间等

2、相关接口

查询历史记录功能没有独立的接口,其主要功能与药品查询和疾病查询功能相耦合

例如药品查询

     /**
     * 查询相关疾病下的药
     */
    @GetMapping("findIllnessOne")
    public String findIllnessOne(Map<String, Object> map, Integer id) {
        Map<String, Object> illnessOne = illnessService.findIllnessOne(id);
        Illness illness = illnessService.get(id);
        if (loginUser != null) {
            historyService.insetOne(loginUser.getId(), MedicalConstants.TYPE_ILLNESS, illness.getIllnessName());
        }
        map.putAll(illnessOne);
        return "illness-reviews";
    }

这里在查询药品的时候顺带把历史记录插入到了数据库里

3、服务实现

HistoryService虽然继承BaseService类,但其增删查改函数大多都进行了重写

该服务类中有两个基本函数:insertOne()和findList(),前者用于往数据库中插入一条历史记录,后者用于从数据库中寻找某个用户的所有历史记录,并按照创建时间进行排序形成列表

 public boolean insetOne(Integer uid, Integer type, String nameValue) {
        History history = new History();
        history.setUserId(uid).setKeyword(nameValue).setOperateType(type);
        return historyDao.insert(history) > 0;
    }

    public List<Map<String, Object>> findList(Integer userId) {
        List<History> list = historyDao.selectList(new QueryWrapper<History>().eq("user_id", userId)
                .orderByDesc("create_time"));
        List<History> histories = list.stream().collect(Collectors.collectingAndThen(
                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(History::getKeyword))), LinkedList::new));
        histories.sort((h1, h2) -> -h1.getCreateTime().compareTo(h2.getCreateTime()));
        List<History> historyList = histories.stream().limit(10).collect(Collectors.toList());
        System.out.println(histories.size());
        List<Map<String, Object>> mapList = new LinkedList<>();
        historyList.forEach(his -> {
            Map<String, Object> map = cn.hutool.core.bean.BeanUtil.beanToMap(his);
            Integer operateType = MapUtil.getInt(map, "operateType");
            if (operateType == 1) {
                List<String> keyword = Arrays.asList((MapUtil.getStr(map, "keyword")).split(","));
                IllnessKind illnessKind = illnessKindDao.selectById(Integer.valueOf(keyword.get(0)));
                map.put("kind", illnessKind.getId());
                map.put("nameValue", keyword.get(1));
                map.put("searchValue", illnessKind.getName() + ("无".equals(keyword.get(1)) ? "" : ("|" + keyword.get(1))));
            } else if (operateType == 2) {
                map.put("nameValue", MapUtil.getStr(map, "keyword"));
                map.put("kind", "无");
                map.put("searchValue", MapUtil.getStr(map, "keyword"));
            } else if (operateType == 3) {
                map.put("nameValue", MapUtil.getStr(map, "keyword"));
                map.put("searchValue", MapUtil.getStr(map, "keyword"));
                map.put("kind", "无");
            }
            mapList.add(map);
        });
        return mapList;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值