BarcodeUpdateController.java
4.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
package com.myproject.webapp.controller.barcode;
import com.google.common.base.Strings;
import com.myproject.bean.update.Barcode;
import com.myproject.bean.update.StoragePos;
import com.myproject.exception.ValidateException;
import com.myproject.manager.IBarcodeManager;
import com.myproject.manager.IComponentManager;
import com.myproject.manager.IStoragePosManager;
import com.myproject.webapp.controller.storage.BaseUpdateController;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
/**
* Created by kangmor on 2015/12/2.
*/
@Controller
@RequestMapping("/barcode/barcodeUpdate*")
public class BarcodeUpdateController extends BaseUpdateController {
private final static String UPDATE_VIEW = "barcode/barcodeUpdate";
@Autowired
protected IBarcodeManager barcodeManager;
@Autowired
protected IComponentManager componentManager;
@Autowired
protected IStoragePosManager storagePosManager;
public BarcodeUpdateController() {
setSuccessView(UPDATE_VIEW);
}
@RequestMapping(method = RequestMethod.GET)
public ModelAndView handleRequest(final HttpServletRequest request) {
Model model = new ExtendedModelMap();
initMessage(request);
Barcode barcode = loadBarcode(request);
model.addAttribute("barcode", barcode);
return new ModelAndView(UPDATE_VIEW, model.asMap());
}
protected Barcode loadBarcode(HttpServletRequest request) {
final String id = request.getParameter("id");
if (!isAdd(request) && StringUtils.isNotBlank(id)) {
return barcodeManager.get(id);
} else{
Barcode barcode = new Barcode();
barcode.setBarcode(System.currentTimeMillis() + "");
return barcode;
}
}
@RequestMapping(method = RequestMethod.POST)
protected String submit(@Valid Barcode barcode, BindingResult result,
final HttpServletRequest request, final HttpServletResponse response) throws Exception {
if(result.hasErrors()) {
return getSuccessView();
} else {
try {
if(super.isAdd(request)){
boolean isNew = Strings.isNullOrEmpty(barcode.getId());
barcodeManager.save(barcode);
if(!isNew){
StoragePos pos = storagePosManager.getByBarcode(barcode.getBarcode());
if(pos != null){//已经在库中,更新数量备注等相关信息
Barcode barcodeInPos = pos.getBarcode();
log.info("更新仓位【"+pos.getPosName()+"】物料"+barcodeInPos.getBarcode()+"的数量【"+barcodeInPos.getAmount()+"=>"+barcode.getAmount()+"】和备注信息["+barcodeInPos.getMemo()+"]=>" +barcode.getMemo());
barcodeInPos.setAmount(barcode.getAmount());
barcodeInPos.setMemo(barcode.getMemo());
storagePosManager.save(pos);
}
}
saveMessage(request, getText("barcode.saveSuccess", request.getLocale()));
}else if(super.isDelete(request)){
barcode = barcodeManager.get(barcode.getId());
if(barcode == null){
throw new ValidateException("未找到相关条码");
}
if(barcode.getUsedCount()>0){
//throw new ValidateException("已被使用的条码无法删除");
}
barcodeManager.delete(barcode);
saveMessage(request, getText("barcode.deleteSuccess", request.getLocale()));
return "redirect:barcodeSearch.html";
}
} catch (ValidateException e) {
handValidateException(e, request);
}
return getSuccessView();
}
}
}