jiaosong
2023-08-18 4d95498b87e61917f50ab2f1d122d48f82a8a99d
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package doumeemes.service.ext.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import doumeemes.core.constants.ResponseStatus;
import doumeemes.core.exception.BusinessException;
import doumeemes.core.model.LoginUserInfo;
import doumeemes.core.model.PageData;
import doumeemes.core.model.PageWrap;
import doumeemes.core.utils.Constants;
import doumeemes.core.utils.redis.RedisUtil;
import doumeemes.dao.business.WOutboundMapper;
import doumeemes.dao.business.WTransferMapper;
import doumeemes.dao.business.WorkorderMapper;
import doumeemes.dao.business.model.*;
import doumeemes.dao.ext.*;
import doumeemes.dao.ext.dto.QueryBarcodeParamExtDTO;
import doumeemes.dao.ext.dto.QueryCompanyExtDTO;
import doumeemes.dao.ext.vo.BarcodeParamContent;
import doumeemes.dao.ext.vo.BarcodeParamExtListVO;
import doumeemes.dao.ext.vo.CompanyExtListVO;
import doumeemes.service.ext.BarcodeParamExtService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
 
/**
 * 二维码设置Service实现
 * @author 江蹄蹄
 * @date 2022/04/20 11:01
 */
@Service
public class BarcodeParamExtServiceImpl implements BarcodeParamExtService {
 
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private CompanyExtMapper companyExtMapper;
    @Autowired
    private BarcodeParamExtMapper barcodeParamExtMapper;
    @Autowired
    private MaterialExtMapper materialExtMapper;
    @Autowired
    private WTransferMapper wTransferMapper;
    @Autowired
    private WOutboundMapper wOutboundMapper;
    @Autowired
    private AppliancesExtMapper appliancesExtMapper;
    @Autowired
    private WarehouseExtMapper warehouseExtMapper;
    @Autowired
    private WarehouseLocationExtMapper warehouseLocationExtMapper;
    @Autowired
    private WorkorderMapper workorderMapper;
    @Autowired
    private DeviceExtMapper deviceExtMapper;
    @Autowired
    private CompanyUserExtMapper companyUserExtMapper;
 
    @Override
    public PageData<BarcodeParamExtListVO> findPage(PageWrap<QueryBarcodeParamExtDTO> pageWrap) {
        PageHelper.startPage(pageWrap.getPage(), pageWrap.getCapacity());
        List<BarcodeParamExtListVO> result = barcodeParamExtMapper.selectList(pageWrap.getModel());
        return PageData.from(new PageInfo<>(result));
    }
    @Override
    public   void initParamData(Company com){
        String[] names = new String[]{"WL","CK","HW","GD","GZ","S","YG","ZKD","CKD","RKD","K"};
        BarcodeParam param = new BarcodeParam();
        param.setCreateTime(new Date());
        param.setStatus(Constants.ONE);
        param.setDepartId(com.getRootDepartment().getId());
        param.setRootDepartId(com.getRootDepartment().getId());
        param.setDeleted(Constants.ZERO);
        for(int i=0;i<10;i++){
            param.setPrefix(names[i]);
            param.setType(i);
            barcodeParamExtMapper.insert(param);
        }
        CompanyExtListVO c = new CompanyExtListVO();
        c.setId(com.getId());
        loadCom(c);
    }
 
 
    @Override
    public  void loadCom(CompanyExtListVO com ) {
        QueryBarcodeParamExtDTO d = new QueryBarcodeParamExtDTO();
        d.setDeleted(Constants.ZERO);
        d.setDmodelCompanyId(com.getId());
        //查询全部企业部门信息
        List<BarcodeParamExtListVO> clist =    barcodeParamExtMapper.selectList(d);
        //加入redis缓存,以企业id为编码,缓存企业二维码
        RedisUtil.addObject(redisTemplate,Constants.RedisKeys.COM_BARCODE_KEY+com.getId(),clist);
    }
    @Override
    public BarcodeParamExtListVO getByCode(Integer comId,Integer departId,String code){
        if(comId==null || StringUtils.isBlank(code)){
            return null;
        }
        //查询缓存数据
        List<BarcodeParamExtListVO> clist = RedisUtil.getObject(redisTemplate,Constants.RedisKeys.COM_BARCODE_KEY+comId,ArrayList.class);
        if(clist!=null){
            for(BarcodeParamExtListVO model:clist){
                if(StringUtils.isNotBlank(model.getPrefix()) && code.startsWith(model.getPrefix()) &&Constants.equalsInteger(model.getDepartId(),departId) ){
                    return model;
                }
            }
        }
        return null;
 
    }
    @Override
    public  BarcodeParamExtListVO getByCodeId(Integer comId,Integer departId, Integer brcode){
        if(comId==null || brcode == null){
            return null;
        }
        //查询缓存数据
        List<BarcodeParamExtListVO> clist = RedisUtil.getObject(redisTemplate,Constants.RedisKeys.COM_BARCODE_KEY+comId,ArrayList.class);
        if(clist!=null){
            for(BarcodeParamExtListVO model:clist){
                if(Constants.equalsInteger(brcode,model.getId())&&Constants.equalsInteger(model.getDepartId(),departId)){
                    return model;
                }
            }
        }
 
            return null;
 
    }
    @Override
    public  Integer getByType(Integer comId,Integer departId, Integer type){
        if(comId==null || type == null){
            return null;
        }
        //查询缓存数据
        List<BarcodeParamExtListVO> clist = RedisUtil.getObject(redisTemplate,Constants.RedisKeys.COM_BARCODE_KEY+comId,ArrayList.class);
        if(clist!=null){
            for(BarcodeParamExtListVO model:clist){
                if(Constants.equalsInteger(type,model.getType())&&Constants.equalsInteger(model.getDepartId(),departId)){
                    return model.getId();
                }
            }
        }
            return null;
 
    }
 
 
 
    @Override
    public BarcodeParamContent getBarcodeContent(String code, LoginUserInfo loginUserInfo){
        BarcodeParamContent barcodeParamContent = new BarcodeParamContent();
        if(loginUserInfo.getComDepartment().getId()==null || StringUtils.isBlank(code)){
            return null;
        }
        //查询缓存数据
        List<BarcodeParamExtListVO> clist = RedisUtil.getObject(redisTemplate,Constants.RedisKeys.COM_BARCODE_KEY+loginUserInfo.getCompany().getId(),ArrayList.class);
        if(clist!=null){
            for(BarcodeParamExtListVO model:clist){
                if(Constants.equalsInteger(model.getDepartId(),loginUserInfo.getComDepartment().getId()) && StringUtils.isNotBlank(model.getPrefix()) && code.startsWith(model.getPrefix())){
                    barcodeParamContent.setBarcodeType(model.getType());
                    if(model.getType() == Constants.BARCODEPARAM_TYPE.material){
                        Material material =   materialExtMapper.selectOne(new QueryWrapper<Material>()
                                .eq("CODE",code.replaceFirst(model.getPrefix(),""))
                                .eq("QRCODE_ID",model.getId())
                                .eq("ROOT_DEPART_ID",loginUserInfo.getRootDepartment().getId())
                        );
                        if(Objects.isNull(material)){
                            throw new BusinessException(ResponseStatus.OBJECT_EMP.getCode(), "未匹配到数据信息");
                        }
                        barcodeParamContent.setId(material.getId());
                    }else if(model.getType() == Constants.BARCODEPARAM_TYPE.warehouse){
                        Warehouse warehouse = warehouseExtMapper.selectOne(new QueryWrapper<Warehouse>()
                                .eq("CODE", code.replaceFirst(model.getPrefix(),""))
                                .eq("QRCODE", model.getId())
                                .eq("ROOT_DEPART_ID",loginUserInfo.getRootDepartment().getId())
                        );
                        if(Objects.isNull(warehouse)){
                            throw new BusinessException(ResponseStatus.OBJECT_EMP.getCode(), "未匹配到数据信息");
                        }
                        barcodeParamContent.setId(warehouse.getId());
                    }else if(model.getType() == Constants.BARCODEPARAM_TYPE.localtion){
                        WarehouseLocation warehouseLocation = warehouseLocationExtMapper.selectOne(new QueryWrapper<WarehouseLocation>()
                                .eq("CODE", code.replaceFirst(model.getPrefix(),""))
                                .eq("QRCODE", model.getId())
                                .eq("ROOT_DEPART_ID",loginUserInfo.getRootDepartment().getId())
                        );
                        if(Objects.isNull(warehouseLocation)){
                            throw new BusinessException(ResponseStatus.OBJECT_EMP.getCode(), "未匹配到数据信息");
                        }
                        barcodeParamContent.setId(warehouseLocation.getId());
                    }else if(model.getType() == Constants.BARCODEPARAM_TYPE.workorder){
                        Workorder workorder = workorderMapper.selectOne(new QueryWrapper<Workorder>()
                                .eq("CODE", code.replaceFirst(model.getPrefix(),""))
                                .eq("QRCODE_ID", model.getId())
                                .eq("ROOT_DEPART_ID",loginUserInfo.getRootDepartment().getId())
                        );
                        if(Objects.isNull(workorder)){
                            throw new BusinessException(ResponseStatus.OBJECT_EMP.getCode(), "未匹配到数据信息");
                        }
                        barcodeParamContent.setId(workorder.getId());
 
                    }else if(model.getType() == Constants.BARCODEPARAM_TYPE.appliance){
                        Appliances appliances = appliancesExtMapper.selectOne(new QueryWrapper<Appliances>()
                                .eq("CODE", code.replaceFirst(model.getPrefix(),""))
                                .eq("BRCODE", model.getId())
                                .eq("ROOT_DEPART_ID",loginUserInfo.getRootDepartment().getId()).last(" limit 1")
                        );
                        if(Objects.isNull(appliances)){
                            throw new BusinessException(ResponseStatus.OBJECT_EMP.getCode(), "未匹配到数据信息");
                        }
                        barcodeParamContent.setId(appliances.getId());
                    }else if(model.getType() == Constants.BARCODEPARAM_TYPE.device){
                        Device device = deviceExtMapper.selectOne(new QueryWrapper<Device>()
                                .eq("CODE", code.replaceFirst(model.getPrefix(),""))
                                .eq("ROOT_DEPART_ID",loginUserInfo.getRootDepartment().getId())
                        );
                        if(Objects.isNull(device)){
                            throw new BusinessException(ResponseStatus.OBJECT_EMP.getCode(), "未匹配到数据信息");
                        }
                        barcodeParamContent.setId(device.getId());
                    }else if(model.getType() == Constants.BARCODEPARAM_TYPE.user){
                        CompanyUser companyUser = companyUserExtMapper.selectOne(new QueryWrapper<CompanyUser>()
                                .eq("CODE", code.replaceFirst(model.getPrefix(),""))
                                .eq("QRCODEID", model.getId())
                                .eq("ROOT_DEPART_ID",loginUserInfo.getRootDepartment().getId())
                        );
                        if(Objects.isNull(companyUser)){
                            throw new BusinessException(ResponseStatus.OBJECT_EMP.getCode(), "未匹配到数据信息");
                        }
                        barcodeParamContent.setId(companyUser.getId());
                    }else if(model.getType() == Constants.BARCODEPARAM_TYPE.transfer){
                        WTransfer wTransfer = wTransferMapper.selectOne(new QueryWrapper<WTransfer>()
                                .eq("CODE",code)
                                .eq("ROOT_DEPART_ID",loginUserInfo.getRootDepartment().getId())
                        );
                        if(Objects.isNull(wTransfer)){
                            throw new BusinessException(ResponseStatus.OBJECT_EMP.getCode(), "未匹配到数据信息");
                        }
                        barcodeParamContent.setId(wTransfer.getId());
                    }else if(model.getType() == Constants.BARCODEPARAM_TYPE.out){
                        WOutbound wOutbound = wOutboundMapper.selectOne(new QueryWrapper<WOutbound>()
                                .eq("CODE",code)
                                .eq("TYPE",Constants.ZERO)
                                .eq("ROOT_DEPART_ID",loginUserInfo.getRootDepartment().getId())
                        );
                        if(Objects.isNull(wOutbound)){
                            throw new BusinessException(ResponseStatus.OBJECT_EMP.getCode(), "未匹配到数据信息");
                        }
                        barcodeParamContent.setId(wOutbound.getId());
                    }else if(model.getType() == Constants.BARCODEPARAM_TYPE.in){
                        WOutbound wOutbound = wOutboundMapper.selectOne(new QueryWrapper<WOutbound>()
                                .eq("CODE",code)
                                .eq("TYPE",Constants.ONE)
                                .eq("ROOT_DEPART_ID",loginUserInfo.getRootDepartment().getId())
                        );
                        if(Objects.isNull(wOutbound)){
                            throw new BusinessException(ResponseStatus.OBJECT_EMP.getCode(), "未匹配到数据信息");
                        }
                        barcodeParamContent.setId(wOutbound.getId());
                    }else{
                        throw new BusinessException(ResponseStatus.OBJECT_EMP.getCode(), "未匹配到条码类别:【"+model.getType()+"】");
                    }
                    return barcodeParamContent;
                }
            }
        }
        throw new BusinessException(ResponseStatus.OBJECT_EMP.getCode(), "未读取到条码分类信息,请检查条码信息");
    }
 
 
    @Override
    public  void loadAll() {
        QueryCompanyExtDTO dto = new QueryCompanyExtDTO();
        dto.setDeleted(Constants.ZERO);
        //查询全部企业信息
        List<CompanyExtListVO> list =    companyExtMapper.selectList(dto);
        if(list != null && list.size()>0){
            QueryBarcodeParamExtDTO d = new QueryBarcodeParamExtDTO();
            d.setDeleted(Constants.ZERO);
            //查询全部企业部门信息
            List<BarcodeParamExtListVO> codeList =    barcodeParamExtMapper.selectList(d);
            for(CompanyExtListVO com : list){
                //当前企业的全部部门信息
                List<BarcodeParamExtListVO> clist = getAllBarcodeByComId(com.getId(),codeList);
                //加入redis缓存,以企业id为编码,缓存企业二维码
                RedisUtil.addObject(redisTemplate,Constants.RedisKeys.COM_BARCODE_KEY+com.getId(),clist);
            }
        }
    }
    /**
     * 根据企业编码获取二维码配置
     * @param id
     * @param alllist
     * @return
     */
    private  List<BarcodeParamExtListVO>  getAllBarcodeByComId(Integer id, List<BarcodeParamExtListVO> alllist) {
        List<BarcodeParamExtListVO> list = null;
        if(alllist!= null){
            for(BarcodeParamExtListVO d :alllist){
                if(d.getDmodel()!=null && Constants.equalsInteger(id,d.getDmodel().getCompanyId())){
                    if(list == null){
                        list = new ArrayList<>();
                    }
                    list.add(d);
                }
            }
        }
        return  list;
    }
}