BarcodeUpdateController.java 4.4 KB
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();
        }
    }
}