刘光辉
13 小时以前 34981c30a78e8bbd7791131059a9210f9928b62c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package jnpf.limsService.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jnpf.limsEntity.LimsBizLogEntity;
import jnpf.limsEntity.bizlog.BizLogAction;
import jnpf.limsEntity.bizlog.BizLogType;
import jnpf.limsEntity.bizlog.LimsBizLogQueryParam;
import jnpf.limsEntity.bizlog.LimsBizLogVo;
import jnpf.limsMapper.LimsBizLogMapper;
import jnpf.limsService.LimsBizLogService;
import jnpf.permission.UserApi;
import jnpf.permission.entity.UserEntity;
import jnpf.util.DateUtil;
import jnpf.util.UploaderUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
@Slf4j
@Service
public class LimsBizLogServiceImpl
        extends ServiceImpl<LimsBizLogMapper, LimsBizLogEntity>
        implements LimsBizLogService {
 
    @Autowired
    private UserApi userApi;
 
    @Override
    public List<LimsBizLogVo> listByBizCode(LimsBizLogQueryParam q) {
        QueryWrapper<LimsBizLogEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(LimsBizLogEntity::getBizCode, q.getBizCode())
                .eq(LimsBizLogEntity::getDeleteMark, 0);
        if (StringUtils.hasText(q.getBizType())) {
            wrapper.lambda().eq(LimsBizLogEntity::getBizType, q.getBizType());
        }
        if (StringUtils.hasText(q.getActionType())) {
            wrapper.lambda().eq(LimsBizLogEntity::getActionType, q.getActionType());
        }
        if (StringUtils.hasText(q.getOperatorUserId())) {
            wrapper.lambda().eq(LimsBizLogEntity::getOperatorUserId, q.getOperatorUserId());
        }
        if (q.getStartTime() != null) {
            wrapper.lambda().ge(LimsBizLogEntity::getCreatorTime, q.getStartTime());
        }
        if (q.getEndTime() != null) {
            wrapper.lambda().le(LimsBizLogEntity::getCreatorTime, q.getEndTime());
        }
        wrapper.lambda().orderByDesc(LimsBizLogEntity::getCreatorTime).orderByDesc(LimsBizLogEntity::getLogSeq);
 
        Page<LimsBizLogEntity> page = new Page<>(q.getCurrentPage(), q.getPageSize());
        Page<LimsBizLogEntity> result = this.page(page, wrapper);
        List<LimsBizLogEntity> rows = result.getRecords();
 
        // 批量补 user 信息
        List<String> userIds = rows.stream()
                .map(LimsBizLogEntity::getOperatorUserId)
                .filter(StringUtils::hasText)
                .distinct()
                .collect(Collectors.toList());
        Map<String, UserEntity> userMap = new HashMap<>();
        if (!userIds.isEmpty()) {
            List<UserEntity> users = userApi.getUserName(userIds);
            if (users != null) {
                userMap = users.stream().collect(Collectors.toMap(UserEntity::getId, u -> u, (a, b) -> a));
            }
        }
 
        List<LimsBizLogVo> voList = new ArrayList<>(rows.size());
        for (LimsBizLogEntity e : rows) {
            voList.add(toVo(e, userMap));
        }
        // 把 total 写回 Pagination 入参,Controller 复制到 PaginationVO 即可
        q.setData(voList, result.getTotal());
        return voList;
    }
 
    private LimsBizLogVo toVo(LimsBizLogEntity e, Map<String, UserEntity> userMap) {
        LimsBizLogVo vo = new LimsBizLogVo();
        vo.setId(e.getId());
        vo.setLogSeq(e.getLogSeq());
        vo.setBizCode(e.getBizCode());
        vo.setBizType(e.getBizType());
        vo.setActionType(e.getActionType());
        vo.setActionLabel(e.getActionLabel());
        vo.setCreatorTime(e.getCreatorTime() == null ? null
                : DateUtil.dateToString(e.getCreatorTime(), "yyyy-MM-dd HH:mm"));
        vo.setCreatorUserId(e.getOperatorUserId());
 
        BizLogAction action = parseAction(e.getActionType());
        vo.setType(action == null ? null : action.getFrontendType());
 
        UserEntity user = userMap.get(e.getOperatorUserId());
        if (user != null) {
            vo.setCreatorUserName(user.getRealName());
            vo.setHeadIcon(UploaderUtil.uploaderImg(user.getHeadIcon()));
        }
 
        // JSON 字符串直接透传,前端 JSON.parse 解析(与平台原生日志一致)
        vo.setDataLog(e.getDataLog());
        vo.setExtraJson(e.getExtraJson());
        return vo;
    }
 
    private BizLogAction parseAction(String code) {
        if (code == null) return null;
        for (BizLogAction a : BizLogAction.values()) {
            if (a.getCode().equals(code)) return a;
        }
        return null;
    }
 
    /** 仅供切面/调用方查 bizType 文案,目前未直接用,保留扩展 */
    @SuppressWarnings("unused")
    private BizLogType parseType(String code) {
        if (code == null) return null;
        for (BizLogType t : BizLogType.values()) {
            if (t.getCode().equals(code)) return t;
        }
        return null;
    }
}