czt
2025-12-03 53fab3f56e8335fbf39fc07c4e10f6abdb0505bb
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
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.fzzy.igds.utils.DateUtil;
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 = getQueryWrapper(param);
        inoutRecordMapper.selectPage(page, queryWrapper);
    }
 
    /**
     * 查询数据集合
     * @param param
     */
    public List<InoutRecord> listInout(InoutParam param) {
        QueryWrapper<InoutRecord> queryWrapper = getQueryWrapper(param);
        return inoutRecordMapper.selectList(queryWrapper);
    }
 
    /**
     * 封装查询条件
     * @param param
     */
    public QueryWrapper<InoutRecord> getQueryWrapper(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.getCustomerName())) {
            queryWrapper.like("customer_name", param.getCustomerName());
        }
        if (StringUtils.isNotBlank(param.getWeightTag())) {
            queryWrapper.likeRight("progress", param.getWeightTag());
        }
        if (StringUtils.isNotBlank(param.getFoodVariety())) {
            queryWrapper.eq("food_variety", param.getFoodVariety());
        }
        if (StringUtils.isNotBlank(param.getCheckStatus())) {
            queryWrapper.eq("check_status", param.getCheckStatus());
        }
        if (StringUtils.isNotBlank(param.getDepotId())) {
            queryWrapper.eq("depot_id", param.getDepotId());
        }
        if (StringUtils.isNotBlank(param.getType())) {
            queryWrapper.eq("type", param.getType());
        }
        if (null != param.getStart()) {
            queryWrapper.ge("create_time", DateUtil.getCurZero(param.getStart()));
        }
        if (null != param.getEnd()) {
            queryWrapper.le("create_time", DateUtil.getNextZero(param.getEnd()));
        }
        queryWrapper.eq("record_status", "NORMAL"); //正常状态的单子
        queryWrapper.orderByDesc("create_time");
 
        return 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();
        }
    }
 
}