Java tutorial
/* * Copyright 2015 Metersbonwe.com All right reserved. This software is the * confidential and proprietary information of Metersbonwe.com ("Confidential * Information"). You shall not disclose such Confidential Information and shall * use it only in accordance with the terms of the license agreement you entered * into with Metersbonwe.com. */ package com.mbv.web.rest.controller; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import com.alibaba.fastjson.JSONObject; import com.mbv.common.constant.MbvConstant; import com.mbv.common.exception.MbvException; import com.mbv.inventory.bean.ProductInfo; import com.mbv.inventory.bean.VpGrnBean; import com.mbv.inventory.bean.VpGrnDtlBean; import com.mbv.inventory.entity.VpGrnDtlEntity; import com.mbv.inventory.entity.VpGrnEntity; import com.mbv.inventory.service.InventoryService; import com.mbv.inventory.service.VpGrnService; import com.mbv.web.rest.util.ExcelUtils; import com.mbv.web.rest.util.JqGridPagesUtils; import com.mbv.web.rest.vo.JqGridBaseEntityVo; import com.mbv.web.rest.vo.VpGrnVo; /** * @???controller * @201596 * @version */ @Controller @RequestMapping("/vpGrn") public class VpGrnController extends BaseController { private Logger log = LoggerFactory.getLogger(VpGrnController.class); @Resource private VpGrnService vpGrnService; @Resource private InventoryService inventoryService; /** * @?? * @201596 * @param * @version * @throws MbvException */ @RequestMapping(value = "/queryByParams", method = RequestMethod.POST) @ResponseBody public JqGridBaseEntityVo<VpGrnEntity> queryByParams(JqGridBaseEntityVo<VpGrnEntity> entity, VpGrnVo vo, HttpServletRequest request, HttpServletResponse response) throws MbvException { String warehCode = request.getSession().getAttribute(MbvConstant.WAREH_CODE).toString(); String unitCode = request.getSession().getAttribute(MbvConstant.UNIT_CODE).toString(); if (unitCode == null || "".equals(unitCode) || warehCode == null || "".equals(warehCode)) { throw new MbvException("Session?????"); } if (vo == null) { vo = new VpGrnVo(); VpGrnBean bean = new VpGrnBean(); bean.setWarehCode(warehCode); bean.setUnitCode(unitCode); vo.setBean(bean); } else { if (vo.getBean() == null) { VpGrnBean bean = new VpGrnBean(); bean.setWarehCode(warehCode); bean.setUnitCode(unitCode); vo.setBean(bean); } else { vo.getBean().setWarehCode(warehCode); vo.getBean().setUnitCode(unitCode); } } String rcptTime1 = vo.getBean().getRcptTime1(); String rcptTime2 = vo.getBean().getRcptTime2(); if (StringUtils.isNotEmpty(rcptTime1) && StringUtils.isNotEmpty(rcptTime2)) { vo.getBean().setRcptTime2(rcptTime2 + " 23:59:59"); } else if (StringUtils.isNotEmpty(rcptTime1) && StringUtils.isEmpty(rcptTime2)) { if (rcptTime1.length() == 7) { vo.getBean().setQueryMonth(rcptTime1); } else if (rcptTime1.length() == 10) { vo.getBean().setQueryDay(rcptTime1); } } else if (StringUtils.isNotEmpty(rcptTime2) && StringUtils.isEmpty(rcptTime1)) { if (rcptTime2.length() == 7) { vo.getBean().setQueryMonth(rcptTime2); vo.getBean().setRcptTime2(""); } else if (rcptTime2.length() == 10) { vo.getBean().setQueryDay(rcptTime2); vo.getBean().setRcptTime2(""); } } List<VpGrnEntity> list = new ArrayList<VpGrnEntity>(); try { // ? int totalCount = this.vpGrnService.queryByParamsCount(vo.getBean()); log.info("totalCount:" + totalCount); // ? if (totalCount > 0) { // ? int offset = (entity.getPage() - 1) * entity.getRows(); int limit = entity.getRows(); log.info("offset:" + offset); log.info("limit:" + limit); // ?? list = this.vpGrnService.queryByParams(vo.getBean(), offset, limit); // ? JqGridPagesUtils.setPagingParams(entity, list, totalCount); } // return entity; } catch (MbvException me) { me.printStackTrace(); throw new MbvException(me.getMessage()); } catch (RuntimeException re) { re.printStackTrace(); throw new MbvException(MbvConstant.MBV_SYS_ERROR_TIP); } catch (Exception e) { e.printStackTrace(); throw new MbvException(MbvConstant.MBV_SYS_ERROR_TIP); } } /** * @??excel * @2015916 * @param * @version */ @RequestMapping(value = "/importGrn", method = RequestMethod.POST) @ResponseBody public void importBill(@RequestParam(value = "add_vpGrn_importedFile", required = false) MultipartFile file, HttpServletRequest request, HttpServletResponse response) { JSONObject json = new JSONObject(); try { // ? if (file.isEmpty()) { throw new MbvException("??"); } // ?? if (file.getSize() > 20971520) { throw new Exception("?20M?"); } json = importFile(file); } catch (MbvException me) { me.printStackTrace(); json.put("success", false); json.put("msg", me.getMessage()); } catch (RuntimeException re) { re.printStackTrace(); json.put("success", false); json.put("msg", re.getMessage()); } catch (Exception e) { e.printStackTrace(); json.put("success", false); json.put("msg", e.getMessage()); } // ??? outPrintJson(response, json.toString()); } private JSONObject importFile(MultipartFile file) throws MbvException, Exception { JSONObject json = new JSONObject(); try { List<Object[]> models = ExcelUtils.resolveExcelFile(file); List<VpGrnDtlBean> list = new ArrayList<VpGrnDtlBean>(); // ?? for (int i = 0; i < models.size(); i++) { VpGrnDtlBean detail = new VpGrnDtlBean(); String prodNum = models.get(i)[0] == null ? "" : models.get(i)[0].toString().trim(); log.info("prodNum:" + prodNum); boolean isNum = inventoryService.validateNum(prodNum); if (!isNum) { throw new MbvException("?" + (i + 1) + "??" + prodNum + "??0-9"); } if (prodNum.length() != 11) { throw new MbvException("?" + (i + 1) + "?? " + prodNum + "11??"); } detail.setProdNum(prodNum); String qty = models.get(i)[1] == null ? "" : models.get(i)[1].toString().trim(); isNum = inventoryService.validateNum(qty); if (!isNum) { throw new MbvException("?" + (i + 1) + "??" + prodNum + "???0?"); } if (Integer.valueOf(qty) < 1) { throw new MbvException("?" + (i + 1) + "??" + prodNum + "???0?"); } detail.setQty(Integer.valueOf(qty)); log.info("qty:" + qty); //????? ProductInfo pi = inventoryService.getProdInfoByProdNum(prodNum); if (pi != null) { detail.setProdClsNum(pi.getProdClsNum()); detail.setProdName(pi.getProdName()); detail.setColorCode(pi.getProductSkuInfos().get(0).getColorCode()); detail.setColorName(pi.getProductSkuInfos().get(0).getColorName()); detail.setSpecCode(pi.getProductSkuInfos().get(0).getSpecCode()); detail.setSpecName(pi.getProductSkuInfos().get(0).getSpecName()); } list.add(detail); } json.put("list", list); json.put("success", true); json.put("msg", "?!"); } catch (MbvException me) { me.printStackTrace(); json.put("success", false); json.put("msg", me.getMessage()); } catch (Exception e) { e.printStackTrace(); json.put("success", false); json.put("msg", e.getMessage()); } return json; } /** * @??excel * @2015916 * @param * @version */ @RequestMapping(value = "/importUpdateGrn", method = RequestMethod.POST) @ResponseBody public void importUpdateBill( @RequestParam(value = "update_vpGrn_importedFile", required = false) MultipartFile file, HttpServletRequest request, HttpServletResponse response) { JSONObject json = new JSONObject(); try { // ? if (file.isEmpty()) { throw new MbvException("??"); } // ?? if (file.getSize() > 20971520) { throw new Exception("?20M?"); } json = importFile(file); } catch (MbvException me) { me.printStackTrace(); json.put("success", false); json.put("msg", me.getMessage()); } catch (RuntimeException re) { re.printStackTrace(); json.put("success", false); json.put("msg", re.getMessage()); } catch (Exception e) { e.printStackTrace(); json.put("success", false); json.put("msg", e.getMessage()); } // ??? outPrintJson(response, json.toString()); } /** * @???? * @author gel * @2015827 * @param * @version * @throws Exception */ @RequestMapping(value = "/saveVpGrn", method = RequestMethod.POST) @ResponseBody public void addVpGrn(@RequestBody VpGrnVo vo, HttpServletResponse response) throws Exception { // vo if (vo == null) { throw new MbvException("???"); } // ?? VpGrnEntity vpGrnEntity = vo.getEntity(); if (vpGrnEntity == null) { throw new MbvException("???"); } try { // ?? String errMessage = vpGrnService.validate(vpGrnEntity); if (errMessage != null && !"".equals(errMessage)) { throw new MbvException(errMessage); } List<VpGrnDtlEntity> details = vo.getDetails(); // ???? errMessage = vpGrnService.validateDetail(vpGrnEntity, details); if (errMessage != null && !"".equals(errMessage)) { throw new MbvException(errMessage); } // service? boolean isSuccess; isSuccess = vpGrnService.addVpGrn(vpGrnEntity, details); Map<String, Object> map = new HashMap<String, Object>(); map.put("result", isSuccess); // ?? returnSuccess(response, map); } catch (MbvException me) { me.printStackTrace(); throw new MbvException(me.getMessage()); } catch (RuntimeException re) { re.printStackTrace(); throw new MbvException(MbvConstant.MBV_SYS_ERROR_TIP); } catch (Exception e) { e.printStackTrace(); throw new MbvException(MbvConstant.MBV_SYS_ERROR_TIP); } } /** * ?? */ @RequestMapping(value = "/getDefaultValue", method = RequestMethod.POST) @ResponseBody public void getDefaultValue(HttpServletRequest request, HttpServletResponse response) throws MbvException { // session?? String orgCode = request.getSession().getAttribute(MbvConstant.UNIT_CODE).toString(); if (orgCode == null || "".equals(orgCode)) { throw new MbvException("Session???"); } try { String docCode = vpGrnService.getVpGrnMaxDocCode(MbvConstant.SEQ_NAME_VPGRN); if (docCode == null || "".equals(docCode)) { throw new MbvException("????"); } String warehCode = request.getSession().getAttribute(MbvConstant.WAREH_CODE).toString(); if (warehCode == null || "".equals(warehCode)) { throw new MbvException("Session???"); } VpGrnEntity entity = new VpGrnEntity(); entity.setDocCode(docCode); entity.setUnitCode(orgCode); entity.setWarehCode(warehCode); Map<String, Object> map = new HashMap<String, Object>(); map.put("data", entity); // ?? returnSuccess(response, map); } catch (MbvException me) { me.printStackTrace(); throw new MbvException(me.getMessage()); } catch (RuntimeException re) { re.printStackTrace(); throw new MbvException(MbvConstant.MBV_SYS_ERROR_TIP); } catch (Exception e) { e.printStackTrace(); throw new MbvException(MbvConstant.MBV_SYS_ERROR_TIP); } } /** * @???id? * @2015915 * @param * @version */ @RequestMapping(value = "/findVpGrnById", method = RequestMethod.POST) @ResponseBody public void findVpGrn(@RequestBody VpGrnVo vo, HttpServletResponse response) throws MbvException { // vo if (vo == null || vo.getEntity() == null) { throw new MbvException("??"); } try { VpGrnEntity entity = vpGrnService.getVpGrnByEntity(vo.getEntity()); Map<String, Object> map = new HashMap<String, Object>(); map.put("data", entity); // ?? returnSuccess(response, map); } catch (MbvException me) { me.printStackTrace(); throw new MbvException(me.getMessage()); } catch (RuntimeException re) { re.printStackTrace(); throw new MbvException(MbvConstant.MBV_SYS_ERROR_TIP); } catch (Exception e) { e.printStackTrace(); throw new MbvException(MbvConstant.MBV_SYS_ERROR_TIP); } } /** * @???docCode? * @2015915 * @param * @version */ @RequestMapping(value = "/findVpGrnByDocCode", method = RequestMethod.POST) @ResponseBody public void findVpGrnByDocCode(@RequestBody VpGrnVo vo, HttpServletRequest request, HttpServletResponse response) throws MbvException { // vo if (vo == null || vo.getEntity() == null || vo.getEntity().getDocCode() == null || "".equals(vo.getEntity().getDocCode())) { throw new MbvException("??"); } log.info("findVpGrnByDocCode docCode:" + vo.getEntity().getDocCode()); try { VpGrnBean Bean = vpGrnService.getVpGrnBeanByEntity(vo.getEntity()); if (Bean == null) { throw new MbvException("????"); } Map<String, Object> map = new HashMap<String, Object>(); map.put("data", Bean); // ?? returnSuccess(response, map); } catch (MbvException me) { me.printStackTrace(); throw new MbvException(me.getMessage()); } catch (RuntimeException re) { re.printStackTrace(); throw new MbvException(MbvConstant.MBV_SYS_ERROR_TIP); } catch (Exception e) { e.printStackTrace(); throw new MbvException(MbvConstant.MBV_SYS_ERROR_TIP); } } /** * @???? * @2015915 * @param * @version * @throws Exception */ @RequestMapping(value = "/updateVpGrn", method = RequestMethod.POST) @ResponseBody public void updateVpGrn(@RequestBody VpGrnVo vo, HttpServletResponse response) throws Exception { // vo if (vo == null || vo.getEntity() == null) { throw new MbvException("???"); } VpGrnEntity entity = vo.getEntity(); try { // ?? String errMessage = vpGrnService.validate(entity); if (errMessage != null && !"".equals(errMessage)) { throw new MbvException(errMessage); } List<VpGrnDtlEntity> details = vo.getDetails(); // ???? errMessage = vpGrnService.validateDetail(entity, details); if (errMessage != null && !"".equals(errMessage)) { throw new MbvException(errMessage); } boolean updateResult = vpGrnService.updateVpGrn(entity, details); log.info("updateVpGrn updateResult:" + updateResult); Map<String, Object> map = new HashMap<String, Object>(); map.put("result", updateResult); // ?? returnSuccess(response, map); } catch (MbvException me) { me.printStackTrace(); throw new MbvException(me.getMessage()); } catch (RuntimeException re) { re.printStackTrace(); throw new MbvException(MbvConstant.MBV_SYS_ERROR_TIP); } catch (Exception e) { e.printStackTrace(); throw new MbvException(MbvConstant.MBV_SYS_ERROR_TIP); } } /** * @???Id? * @2015917 * @param * @version * @throws Exception */ @RequestMapping(value = "/deleteVpGrnById", method = RequestMethod.POST) @ResponseBody public void deleteVpGrnById(@RequestBody VpGrnVo vo, HttpServletResponse response) throws Exception { // vo if (vo == null || vo.getEntity() == null || vo.getEntity().getDocState() == null || "".equals(vo.getEntity().getDocState()) || vo.getEntity().getId() < 1) { throw new MbvException("???"); } log.info("id:" + vo.getEntity().getId() + ",docState:" + vo.getEntity().getDocState()); try { boolean deleteResult = vpGrnService.deleteVpGrnById(vo.getEntity()); if (deleteResult) { log.info("??"); } // ?? returnSuccess(response, null); } catch (MbvException me) { me.printStackTrace(); throw new MbvException(me.getMessage()); } catch (RuntimeException re) { re.printStackTrace(); throw new MbvException(MbvConstant.MBV_SYS_ERROR_TIP); } catch (Exception e) { e.printStackTrace(); throw new MbvException(MbvConstant.MBV_SYS_ERROR_TIP); } } }