zhuguifei
2026-03-10 58402bd5e762361363a0f7d7907153c77dbb819f
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
package com.shlanbao.tzsc.pms.cos.spare.service.impl;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
 
import com.shlanbao.tzsc.utils.tools.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import com.shlanbao.tzsc.base.dao.CosSparePartsCopDaoI;
import com.shlanbao.tzsc.base.dao.CosSparePartsDaoI;
import com.shlanbao.tzsc.base.dao.MdEqpTypeDaoI;
import com.shlanbao.tzsc.base.mapping.CosSpareParts;
import com.shlanbao.tzsc.base.mapping.MdEqpType;
import com.shlanbao.tzsc.base.mapping.MdUnit;
import com.shlanbao.tzsc.base.model.DataGrid;
import com.shlanbao.tzsc.base.model.PageParams;
import com.shlanbao.tzsc.base.service.BaseService;
import com.shlanbao.tzsc.pms.cos.spare.beans.CosSparePartsBean;
import com.shlanbao.tzsc.pms.cos.spare.service.CosSparePartsServiceI;
import com.shlanbao.tzsc.utils.params.SysEqpTypeBase;
 
import javax.servlet.http.HttpServletRequest;
 
@Service
public class CosSparePartsServiceImpl extends BaseService implements
        CosSparePartsServiceI {
 
    @Autowired
    public CosSparePartsDaoI cosSparePartsDaoI;
 
    @Autowired
    public CosSparePartsCopDaoI cosSparePartsCop;
    
    @Autowired
    public MdEqpTypeDaoI mdEqpTypeDao;
 
    @Autowired
    private HttpServletRequest request;
 
    // 添加或修改
    @LogAnno(operateType = "备品备件添加或修改")
    @Override
    public boolean saveOrUpdateBean(CosSparePartsBean bean) throws Exception {
        CosSpareParts spare = null;
        if (StringUtil.notNull(bean.getId())) {
            spare = getMappingBeanById(bean.getId());
            FillUserInfoUtil.fillUpdateUserInfo(spare,request);
        }else {
            spare = new CosSpareParts();
            FillUserInfoUtil.fillCreateUserInfo(spare, request);
        }
        spare.setSpareCode(bean.getSpareCode());
        spare.setNumber(bean.getNumber());
        spare.setName(bean.getName());
        spare.setPrice(bean.getPrice());
        spare.setEqpType(mdEqpTypeDao.findById(MdEqpType.class, bean.getEqpTypeId()));
        spare.setRemark(bean.getRemark());
        spare.setType(bean.getType());
        spare.setDel("0");
        if (StringUtil.notNull(bean.getUnitId())) {
            MdUnit unit = new MdUnit();
            unit.setId(bean.getUnitId());
            spare.setUnit(unit);
        }
        if (StringUtil.notNull(bean.getEqpTypeId())) {
            MdEqpType eqpType = new MdEqpType();
            eqpType.setId(bean.getEqpTypeId());
            spare.setEqpType(eqpType);
        }
 
        return cosSparePartsDaoI.saveOrUpdate(spare);
    }
 
    // 查询list
    @Override
    public List<CosSparePartsBean> queryListByBeans(CosSparePartsBean bean)
            throws Exception {
        return queryListByBeans(bean, null, 0l, false);
    }
 
    // WCT备品备件查询
    @Override
    public DataGrid queryGridByBean(CosSparePartsBean bean,
            PageParams pageParams) throws Exception {
        Long length = queryTotal();
        // long length=15;
        List<CosSparePartsBean> beans = queryListByBeans(bean, pageParams,
                length, true);
        return new DataGrid(beans, length);
    }
    @LogAnno(operateType = "备品备件删除")
    @Override
    public boolean deleteById(String id){
        CosSpareParts b=cosSparePartsDaoI.findById(CosSpareParts.class, id);
        b.setDel("1");
        return true;
    }
    // 根据id查询
    @Override
    public CosSparePartsBean getBeanById(String id) throws Exception {
        CosSpareParts spare = cosSparePartsDaoI.findById(CosSpareParts.class,
                id);
        CosSparePartsBean b = new CosSparePartsBean();
        BeanConvertor.copyProperties(spare, b);
        try {
        } catch (Exception e) {
            
        }
        try {
            b.setEqpTypeId(spare.getEqpType().getId());
            b.setEqpTypeCode(spare.getEqpType().getCode());
            b.setEqpTypeName(spare.getEqpType().getName());
        } catch (Exception e) {
            b.setEqpTypeId(null);
            b.setEqpTypeId(null);
            b.setEqpTypeCode(null);
        }
        try {
            b.setUnitId(spare.getUnit().getId());
            b.setUnitName(spare.getUnit().getName());
        } catch (Exception e) {
            b.setUnitId(null);
            b.setUnitName(null);
        }
        return b;
    }
 
    // 根据id查询mapping类
    public CosSpareParts getMappingBeanById(String id) throws Exception {
        return cosSparePartsDaoI.findById(CosSpareParts.class, id);
    }
 
    /**
     * WCT备品备件更换框查询
     * 
     * @param bean
     * @param pageParams
     * @param length
     * @param isNameFuzzy
     * @return
     * @throws Exception
     */
    public List<CosSparePartsBean> queryListByBeans(CosSparePartsBean bean,
            PageParams pageParams, Long length, boolean isNameFuzzy)
            throws Exception {
 
        String hql = "from CosSpareParts o where 1=1 and del= '0' ";
        String param = "";
        if (StringUtil.notNull(bean.getId()))
            param += "and o.id='" + bean.getId() + "' ";
        if (StringUtil.notNull(bean.getEqpTypeId()))
            param += "and o.eqpType.id='" + bean.getEqpTypeId() + "' ";
        if (isNameFuzzy && StringUtil.notNull(bean.getName()))
            param += "and o.name like '%" + bean.getName().trim() + "%' ";
        else if (StringUtil.notNull(bean.getName()))
            param += "and o.name='" + bean.getName().trim() + "' ";
        // star-数量
        if (StringUtil.notNull(bean.getNumber()))
            param += "and o.number='" + bean.getNumber() + "' ";
        // end
        if (StringUtil.notNull(bean.getSpareCode()))
            param += "and o.spareCode='" + bean.getSpareCode() + "' ";
        if (StringUtil.notNull(bean.getEqpTypeName()))
            param += "and o.eqpType.name='" + bean.getEqpTypeName() + "' ";
        if (StringUtil.notNull(bean.getUnitId()))
            param += "and o.unit.id='" + bean.getUnitId() + "' ";
        if (StringUtil.notNull(bean.getType())) {
            param += "and o.type like'%" + bean.getType().trim() + "%'";
        }
        List<CosSpareParts> list = null;
        list=cosSparePartsDaoI.query(hql.concat(param));
        List<CosSparePartsBean> beans = new ArrayList<CosSparePartsBean>();
        if (list.size() > 0) {
            for (CosSpareParts spare : list) {
                CosSparePartsBean b = new CosSparePartsBean();
                BeanConvertor.copyProperties(spare, b);
                if (spare.getEqpType() != null
                        && !spare.getEqpType().equals("")) {
                    b.setEqpTypeId(spare.getEqpType().getId());
                    b.setEqpTypeCode(spare.getEqpType().getCode());
                    b.setEqpTypeName(spare.getEqpType().getName());
                }
                if (spare.getUnit() != null && !spare.getUnit().equals("")) {
                    b.setUnitId(spare.getUnit().getId());
                    b.setUnitName(spare.getUnit().getName());
                }
                b.setNumber(spare.getNumber());
                b.setType(spare.getType());
                b.setId(spare.getId());
                beans.add(b);
            }
        }
        return beans;
    }
 
    // PMS备品备件查询
    @Override
    public DataGrid queryGridByBeanPMS(CosSparePartsBean bean,
            PageParams pageParams) throws Exception {
        Long length;
        // long length=15;
        /*List<CosSparePartsBean> beans = queryListByBeansPMS(bean, pageParams,
                length, true);*/
        String hql = "from CosSpareParts o where 1=1 and del='0' ";
        String param = "";
        if (StringUtil.notNull(bean.getId()))
            param += "and o.id='" + bean.getId() + "' ";
        if (StringUtil.notNull(bean.getEqpTypeId()))
            param += "and o.eqpType.id='" + bean.getEqpTypeId() + "' ";
        if (true && StringUtil.notNull(bean.getName()))
            param += "and o.name like '%" + bean.getName().trim() + "%' ";
        else if (StringUtil.notNull(bean.getName()))
            param += "and o.name='" + bean.getName().trim() + "' ";
        // star-数量
        if (StringUtil.notNull(bean.getNumber()))
            param += "and o.number='" + bean.getNumber() + "' ";
        // end
        if (StringUtil.notNull(bean.getSpareCode()))
            param += "and o.spareCode='" + bean.getSpareCode() + "' ";
        if (StringUtil.notNull(bean.getEqpTypeName()))
            param += "and o.eqpType.name='" + bean.getEqpTypeName() + "' ";
        if (StringUtil.notNull(bean.getUnitId()))
            param += "and o.unit.id='" + bean.getUnitId() + "' ";
        if (StringUtil.notNull(bean.getType())) {
            param += "and o.type like'%" + bean.getType().trim() + "%'";
        }
        List<CosSpareParts> list = null;
        if (pageParams != null) {
            list = cosSparePartsDaoI.queryByPage(hql.concat(param), pageParams);
        } else {
            list = cosSparePartsDaoI.query(hql);
        }
        length = cosSparePartsDaoI.queryTotal("select count(1) "+hql.concat(param));
        List<CosSparePartsBean> beans = new ArrayList<CosSparePartsBean>();
        if (list.size() > 0) {
            for (CosSpareParts spare : list) {
                CosSparePartsBean b = new CosSparePartsBean();
                BeanConvertor.copyProperties(spare, b);
                if (spare.getEqpType() != null
                        && !spare.getEqpType().equals("")) {
                    b.setEqpTypeId(spare.getEqpType().getId());
                    b.setEqpTypeCode(spare.getEqpType().getCode());
                    b.setEqpTypeName(spare.getEqpType().getName());
                }
                if (spare.getUnit() != null && !spare.getUnit().equals("")) {
                    b.setUnitId(spare.getUnit().getId());
                    b.setUnitName(spare.getUnit().getName());
                }
                b.setNumber(spare.getNumber());
                b.setType(spare.getType());
                b.setId(spare.getId());
                FillUserInfoUtil.fillCreateAndUpdateUserInfo(spare,b);
                beans.add(b);
            }
        }
        return new DataGrid(beans, length);
    }
 
    /**
     * PMS系统备品备件查询 张璐
     */
    public List<CosSparePartsBean> queryListByBeansPMS(CosSparePartsBean bean,
            PageParams pageParams, long length, boolean isNameFuzzy)
            throws Exception {
        String hql = "from CosSpareParts o where 1=1 and del='0' ";
        String param = "";
        if (StringUtil.notNull(bean.getId()))
            param += "and o.id='" + bean.getId() + "' ";
        if (StringUtil.notNull(bean.getEqpTypeId()))
            param += "and o.eqpType.id='" + bean.getEqpTypeId() + "' ";
        if (isNameFuzzy && StringUtil.notNull(bean.getName()))
            param += "and o.name like '%" + bean.getName().trim() + "%' ";
        else if (StringUtil.notNull(bean.getName()))
            param += "and o.name='" + bean.getName().trim() + "' ";
        // star-数量
        if (StringUtil.notNull(bean.getNumber()))
            param += "and o.number='" + bean.getNumber() + "' ";
        // end
        if (StringUtil.notNull(bean.getSpareCode()))
            param += "and o.spareCode='" + bean.getSpareCode() + "' ";
        if (StringUtil.notNull(bean.getEqpTypeName()))
            param += "and o.eqpType.name='" + bean.getEqpTypeName() + "' ";
        if (StringUtil.notNull(bean.getUnitId()))
            param += "and o.unit.id='" + bean.getUnitId() + "' ";
        if (StringUtil.notNull(bean.getType())) {
            param += "and o.type like'%" + bean.getType().trim() + "%'";
        }
        List<CosSpareParts> list = null;
 
        if (pageParams != null) {
            length = cosSparePartsDaoI.queryTotal(hql.concat(param));
            list = cosSparePartsDaoI.queryByPage(hql.concat(param), pageParams);
        } else {
            length = cosSparePartsDaoI.queryTotal(hql.concat(param));
            list = cosSparePartsDaoI.query(hql);
        }
        List<CosSparePartsBean> beans = new ArrayList<CosSparePartsBean>();
        if (list.size() > 0) {
            for (CosSpareParts spare : list) {
                CosSparePartsBean b = new CosSparePartsBean();
                BeanConvertor.copyProperties(spare, b);
                if (spare.getEqpType() != null
                        && !spare.getEqpType().equals("")) {
                    b.setEqpTypeId(spare.getEqpType().getId());
                    b.setEqpTypeCode(spare.getEqpType().getCode());
                    b.setEqpTypeName(spare.getEqpType().getName());
                }
                if (spare.getUnit() != null && !spare.getUnit().equals("")) {
                    b.setUnitId(spare.getUnit().getId());
                    b.setUnitName(spare.getUnit().getName());
                }
                b.setNumber(spare.getNumber());
                b.setType(spare.getType());
                b.setId(spare.getId());
                FillUserInfoUtil.fillCreateAndUpdateUserInfo(spare,b);
                beans.add(b);
            }
        }
        return beans;
    }
 
    private long queryTotal() throws Exception {
        String sql = "select count(0) from CosSpareParts where del='0'";
        return cosSparePartsDaoI.queryTotal(sql);
    }
 
    @Override
    public void deleteBean(String id) {
        cosSparePartsDaoI.deleteById(id, CosSpareParts.class);
    }
 
    /**
     * 【功能说明】:备品备件导入Excel,批量保存
     * shisihai
     * 
     * */
    @LogAnno(operateType = "备品备件批量导入")
    @Override
    public boolean batchInputExeclAndReadWrite(List<CosSparePartsBean> lt,
            CosSparePartsBean cspb) throws Exception {
        StringBuffer sb = null;
        CosSpareParts p=null;
        StringBuffer sb2=null;
        for (int i = 0; i < lt.size(); i++) {
            CosSparePartsBean csp = lt.get(i);
            sb2=new StringBuffer();
            sb = new StringBuffer();
            if (csp == null || csp.getName().trim().equals("")) {
                continue;
            }
            // 判断是否已经存在
             sb2.append(" from CosSpareParts o where 1=1 and del='0' ");
             if(csp.getSpareCode()!=null){
                 sb2.append(" and o.spareCode='"+ csp.getSpareCode()+"'");
             }
             if(csp.getType()!=null){
                 sb2.append(" and o.type='"+csp.getType()+"'");
             }
             if(csp.getEqpTypeId()!=null){
                 sb2.append(" and o.eqpType='"+csp.getEqpTypeId()+"'");
             }
             if(csp.getName()!=null){
                 String name=csp.getName();
                 if(name.contains("'")){
                     name=name.replace("'", "");
                 };
                 sb2.append(" and o.name='"+name+"'");
             }
            List<CosSpareParts> list = cosSparePartsDaoI.query(sb2.toString());
            sb2=null;
            if (list != null && list.size() > 0) {
                p=list.get(0);//数据库已存在
                int num=0;//用于接收库存的整数
                double num4=0.0;//用于接收库存的小数
                
                int num2=0;//用于接收导入的整数
                double num3=0.0;//用于接收导入的小数
                
                
                //库存信息
                if(p.getNumber()!=null){
                    try {
                        num=Integer.parseInt(p.getNumber());
                    } catch (Exception e) {
                        try{
                            num4=Double.parseDouble(p.getNumber());
                            num4=MathUtil.roundHalfUp(num4, 2);
                        }catch(Exception e2){
                            e2.printStackTrace();
                        }
                    }
                }
                
                //需要累加的信息
                if(csp.getNumber()!=null){//现在需要导入的数量
                    String numS=csp.getNumber();
                    try {
                        num2=Integer.parseInt(numS);
                    } catch (Exception e) {
                        try{
                        num3=Double.parseDouble(numS);
                        num3=MathUtil.roundHalfUp(num3, 2);
                        }catch(Exception e2 ){
                            e2.printStackTrace();
                        }
                        
                    }
                }
                //如果导入累加的数量为0,继续下一个    
            if(num2==0&&num3==0){
                continue;
            }
            String sql=null;
            if(num3==0){
                if(num4==0){
                    sql="UPDATE COS_SPARE_PARTS SET ATTR2 = "+(num+num2)+" where id='"+p.getId()+"'";
                }else{
                    sql="UPDATE COS_SPARE_PARTS SET ATTR2 = "+(num4+num2)+" where id='"+p.getId()+"'";
                }
            }else{
                if(num4==0){
                    sql="UPDATE COS_SPARE_PARTS SET ATTR2 = "+(num+num3)+" where id='"+p.getId()+"'";
                }else{
                    sql="UPDATE COS_SPARE_PARTS SET ATTR2 = "+(num4+num3)+" where id='"+p.getId()+"'";
                }
            }
                cosSparePartsDaoI.updateBySql(sql.toString(), null);
            } else {
                sb.append(" INSERT into COS_SPARE_PARTS(ID,NAME,EQP_TYPE,REMARK,PRICE,UNIT,ATTR1,ATTR2,TYPE,SEARCH_W,position,sell_num,random_num,amount,DEL) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
                List<Object> params = new ArrayList<Object>();
                params.add(UUID.randomUUID().toString());
                //去掉特殊的字符
                String name=csp.getName();
                 if(name.contains("'")){
                     name=name.replace("'", "");
                 };
                params.add(name);
                params.add(csp.getEqpTypeId());
                params.add(csp.getRemark());
                params.add(csp.getPrice());
                params.add(csp.getUnitId());
                params.add(csp.getNumber());
                params.add(csp.getSpareCode());
                params.add(csp.getType());
                params.add(csp.getSEARCH_W());
                params.add(csp.getPosition());
                params.add(csp.getSell_num());
                params.add(csp.getRandom_num());
                params.add(csp.getAmount());
                params.add("0");
                cosSparePartsDaoI.updateBySql(sb.toString(), params);
            }
        }
        return false;
    }
 
    /**
     * 单位名:单位id
     * 
     * @return
     */
    public Map<String, String> retUnit() {
        return SysEqpTypeBase.getUnit();
    }
 
    @Override
    public void batchSaveSparePart(List<CosSparePartsBean> lt) throws Exception {
        for (CosSparePartsBean cosSparePart : lt) {
            //查询是否存在,存在更新
            String hql="select id from CosSpareParts o where o.spareCode=? and o.name=? and o.categoryCode=?";
            List<CosSparePartsBean> datas=cosSparePartsCop.query(hql, cosSparePart.getSpareCode(),cosSparePart.getName(),cosSparePart.getCategoryCode());
            if(datas!=null && datas.size()>1){
                throw new Exception("保存MES下发的备品备件数据错误!备件code为:"+cosSparePart.getSpareCode()+"存在多个!");
            }
            if(datas!=null && datas.size()==1){
                cosSparePart.setId(datas.get(0).getId());
            }
            cosSparePart.setDel("0");
            cosSparePartsCop.saveOrUpdate(cosSparePart);
        }
    }
    @LogAnno(operateType = "批量删除备品备件")
    @Override
    public void batchDeleteBean(String ids) {
        String[] split = ids.split(",");
        try {
            for (int i = 0; i < split.length; i++) {
                CosSpareParts b=cosSparePartsDaoI.findById(CosSpareParts.class, split[i]);
                b.setDel("1");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}