BomManagerImpl.java
7.4 KB
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
package com.myproject.manager.impl;
import com.myproject.bean.search.PageList;
import com.myproject.bean.update.*;
import com.myproject.dao.mongo.IBomDao;
import com.myproject.dao.mongo.IComponentDao;
import com.myproject.exception.ValidateException;
import com.myproject.manager.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/**
* Created by kangmor on 2015/10/15.
*/
@Service
public class BomManagerImpl implements IBomManager {
@Autowired
private IBomDao bomDao;
@Autowired
private IComponentDao componentDao;
@Autowired
private IStorageManager storageManager;
// @Autowired
// private IWarehousingManager warehousingManager;
@Autowired
private IStoragePosManager storagePosManager;
@Autowired
private IWorkOrderManager workOrderManager;
protected final transient Logger log = LogManager.getLogger(getClass());
@Override
public Bom addComponent(Bom bom, String componentId, int amount) throws ValidateException {
if (bom.getId() != null) {
if (!StringUtils.isEmpty(componentId)) {
Component component = componentDao.findOneById(componentId);
if (component != null) {
if (amount < 0) {
amount = 0;
}
List<Comps> compsList = bom.getComponents();
boolean found = false;
if (compsList == null) {
compsList = new ArrayList<Comps>();
} else {
for (Comps comps : compsList) {
if (componentId.equals(comps.getComponentId())) {
comps.setAmount(amount);
found = true;
break;
}
}
}
if (!found) {
Comps comps = new Comps(componentId, amount);
compsList.add(comps);
}
bom.setComponents(compsList);
save(bom);
}
}
}
return bom;
}
public List<StoragePos> checkoutPerComponent(WorkOrder workOrder) throws ValidateException {
List<Component> outOfStockList = new ArrayList();
log.debug("Start to checkout Bom: " + workOrder.getBom().getId() + " with one per component");
List<Comps> components = workOrder.getBom().getComponents();
List<UsedComponent> usedComponents = workOrder.getUsedComponentList();
if (usedComponents == null) {
usedComponents = new ArrayList<>();
}
List<StoragePos> storagePosList = new ArrayList<>();
if (components != null && components.size() > 0) {
for (Comps comps : components) {
StoragePos storagePos = storagePosManager.findComponent(comps.getComponentId());
if (storagePos != null) {
Storage storage = storageManager.get(storagePos.getStorageId());
int checkoutAmount = comps.getAmount() * workOrder.getAmount();
if (storagePos.getBarcode().getAmount() < checkoutAmount) {
checkoutAmount = storagePos.getBarcode().getAmount();
}
//TODO:暂时注释掉
//warehousingManager.prepareCheckout(storage.getCid(), storagePos.getBarcode().getBarcode(), checkoutAmount);
UsedComponent usedComponent = new UsedComponent();
usedComponent.setAmount(checkoutAmount);
usedComponent.setBarcodeId(storagePos.getBarcode().getId());
//usedComponent.setComponentId(storagePos.getBarcode().getComponentId());
usedComponents.add(usedComponent);
storagePosList.add(storagePos);
} else {
Component component = componentDao.findOneById(comps.getComponentId());
outOfStockList.add(component);
}
}
}
workOrder.setUsedComponentList(usedComponents);
workOrderManager.save(workOrder);
if (outOfStockList != null && outOfStockList.size() > 0) {
StringBuffer componentNames = new StringBuffer();
for (Component component : outOfStockList) {
componentNames.append(component.getName() + " ");
}
log.error("Component: " + componentNames.toString() + " are out of stock");
throw new ValidateException("bom.error.outOfStock", new String[]{componentNames.toString()});
}
return storagePosList;
}
@Override
public Bom findByName(String name) {
log.debug("Find bom by bom name " + name);
if (StringUtils.isEmpty(name))
return null;
else return bomDao.findOneByCondition(new String[]{"name"}, new String[]{name});
}
@Override
public Bom removeComponent(Bom bom, String componentId) throws ValidateException {
boolean doSave = false;
if (bom.getId() != null) {
if (!StringUtils.isEmpty(componentId)) {
if (bom.getComponents() != null && bom.getComponents().size() > 0) {
for (Comps comps : bom.getComponents()) {
if (componentId.equals(comps.getComponentId())) {
bom.getComponents().remove(comps);
doSave = true;
break;
}
}
}
}
}
if (doSave) {
save(bom);
}
return bom;
}
@Override
public Bom get(String id) {
return bomDao.findOneById(id);
}
@Override
public Bom save(Bom bom) throws ValidateException {
validateUniqueName(bom);
log.debug("Save bom with name " + bom.getName());
return bomDao.save(bom);
}
protected void validateUniqueName(Bom bom) throws ValidateException {
Bom savedBom = findByName(bom.getName());
if (savedBom != null && !savedBom.getId().equals(bom.getId())) {
throw new ValidateException("bom.error.unique", new String[]{bom.getName()});
}
}
@Override
public void delete(Bom object) throws ValidateException {
bomDao.removeOne(object);
}
@Override
public List<Bom> findAll() {
return bomDao.findAll();
}
@Override
public PageList findByQuery(Query query, PageList pageList) {
pageList.setList(bomDao.findByQuery(query, pageList.getPageNumber(), pageList.getObjectsPerPage()));
pageList.setFullListSize(bomDao.countByQuery(query));
return pageList;
}
@Override
public void validate(Bom object, List errors) {
}
public void setBomDao(IBomDao bomDao) {
this.bomDao = bomDao;
}
public void setComponentDao(IComponentDao componentDao) {
this.componentDao = componentDao;
}
public void setWorkOrderManager(IWorkOrderManager workOrderManager) {
this.workOrderManager = workOrderManager;
}
}