czt
2025-12-02 da2d0647babd92e8929a09d4bfaaf413b980fe24
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package com.fzzy.igds.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fzzy.igds.constant.Constant;
import com.fzzy.igds.constant.RedisConst;
import com.fzzy.igds.data.InoutParam;
import com.fzzy.igds.domain.InoutRecord;
import com.fzzy.igds.mapper.InoutRecordMapper;
import com.fzzy.igds.utils.ContextUtil;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
 
/**
 * @Description
 * @Author CZT
 * @Date 2025/12/2 10:36
 */
@Slf4j
@Service
public class InoutRecordService {
 
    @Resource
    private InoutRecordMapper inoutRecordMapper;
    @Resource
    private RedisCache redisCache;
 
    /**
     * 分页查询数据
     * @param page
     * @param param
     */
    public void listPageInout(Page<InoutRecord> page, InoutParam param) {
        QueryWrapper<InoutRecord> queryWrapper = new QueryWrapper<>();
 
        param.setCompanyId(ContextUtil.getCompanyId());
        param.setDeptId(ContextUtil.subDeptId(null));
        queryWrapper.eq("company_id", param.getCompanyId());
        queryWrapper.eq("dept_id", param.getDeptId());
 
        if (StringUtils.isNotBlank(param.getDepotId())) {
            queryWrapper.eq("depot_id", param.getDepotId());
        }
        if (StringUtils.isNotBlank(param.getType())) {
            queryWrapper.eq("type", param.getType());
        }
        queryWrapper.orderByDesc("create_time");
        inoutRecordMapper.selectPage(page, queryWrapper);
    }
 
 
    /**
     * 根据id查询数据
     * @param id
     * @return
     */
    public InoutRecord selectById(String id) {
        if(StringUtils.isBlank(id)){
            return null;
 
        }
        return inoutRecordMapper.selectById(id);
    }
 
    /**
     * 验证车牌号是否在流程中
     * @param companyId
     * @param plateNum
     * @return
     */
    public int checkExist(String companyId, String plateNum) {
        if (StringUtils.isEmpty(companyId)) {
            companyId = ContextUtil.getCompanyId();
        }
        QueryWrapper<InoutRecord> queryWrapper = new QueryWrapper<>();
 
        queryWrapper.eq("company_id", companyId);
        queryWrapper.eq("plate_num", plateNum);
        queryWrapper.ne("progress", Constant.PROGRESS_RECORD);
        queryWrapper.eq("record_status", "NORMAL");
 
        return inoutRecordMapper.selectCount(queryWrapper);
    }
 
    /**
     * 新增出入库记录
     *
     * @param data
     * @return
     */
    public int addInoutRecord(InoutRecord data) {
 
        if (StringUtils.isBlank(data.getId())) {
            String id = this.createId(data.getRegisterTime(), data.getCompanyId());
            if (Constant.TYPE_IN.equals(data.getType())) {
                data.setId("R_" + id);
            } else if (Constant.TYPE_OUT.equals(data.getType())) {
                data.setId("C_" + id);
            }else {
                data.setId("M_" + id);
            }
        }
        data.setCreateBy(ContextUtil.getLoginUserName());
        data.setCreateTime(new Date());
        data.setUpdateBy(ContextUtil.getLoginUserName());
        data.setUpdateTime(new Date());
 
        int num = inoutRecordMapper.insert(data);
        //TODO 更新缓存
        //updateInoutCache(data);
        return num;
        
    }
 
    /**
     * 新增出入库记录
     *
     * @param data
     * @return
     */
    public int updateInoutRecord(InoutRecord data) {
 
        data.setUpdateBy(ContextUtil.getLoginUserName());
        data.setUpdateTime(new Date());
 
        int num = inoutRecordMapper.updateById(data);
        //TODO 更新缓存
        //updateInoutCache(data);
        return num;
 
    }
 
    /**
     * 出入库流程ID创建 202001030001 202001030001
     * @param registerTime
     * @param companyId
     * @return
     */
    public String createId(Date registerTime, String companyId) {
 
        // 时间戳标签
        String timeKey = DateFormatUtils.format(registerTime, "yyyyMMdd");
 
        // 从缓存中获取已有的组织编码
        String cacheKey = RedisConst.buildKey(companyId, Constant.CACHE_RECORD_ID);
 
        String cacheId = (String) redisCache.getCacheObject(cacheKey);
 
        if (null != cacheId && cacheId.indexOf(timeKey) >= 0) {
            String temp = cacheId.substring(cacheId.length() - 4);
            Integer i = Integer.valueOf(temp);
            i++;
            temp = String.valueOf(i);
            if (temp.length() == 1) {
                cacheId = timeKey + "000" + temp;
            }
            if (temp.length() == 2) {
                cacheId = timeKey + "00" + temp;
            }
            if (temp.length() == 3) {
                cacheId = timeKey + "0" + temp;
            }
            if (temp.length() == 4) {
                cacheId = timeKey + temp;
            }
        } else {
            String result = this.getMaxId(companyId, timeKey);
            if (null == result) {
                cacheId = timeKey + "0001";
            } else {
                // 获取最后四位
                int i = Integer.valueOf(result.substring(result.length() - 4));
                i++;
                String temp = String.valueOf(i);
                if (temp.length() == 1) {
                    cacheId = timeKey + "000" + temp;
                }
                if (temp.length() == 2) {
                    cacheId = timeKey + "00" + temp;
                }
                if (temp.length() == 3) {
                    cacheId = timeKey + "0" + temp;
                }
                if (temp.length() == 4) {
                    cacheId = timeKey + temp;
                }
            }
        }
 
        // 更新缓存
        redisCache.setCacheObject(cacheKey, cacheId);
 
        return cacheId;
    }
 
    /**
     * 查询最大id号,为空则返回null
     * @param companyId
     * @param timeKey
     * @return
     */
    public String getMaxId(String companyId, String timeKey) {
 
        if (StringUtils.isEmpty(companyId)) {
            companyId = ContextUtil.getCompanyId();
        }
        QueryWrapper<InoutRecord> queryWrapper = new QueryWrapper<>();
 
        queryWrapper.eq("company_id", companyId);
        queryWrapper.like("id", timeKey);
        queryWrapper.orderByDesc("create_time");
 
        List<InoutRecord> inoutRecords = inoutRecordMapper.selectList(queryWrapper);
        if(null == inoutRecords || inoutRecords.isEmpty()){
            return null;
        }else {
            return inoutRecords.get(0).getId();
        }
    }
 
}