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.io.IOException; 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.VpGdnBean; import com.mbv.inventory.bean.VpGdnDtlBean; import com.mbv.inventory.entity.VpGdnDtlEntity; import com.mbv.inventory.entity.VpGdnEntity; import com.mbv.inventory.service.InventoryService; import com.mbv.inventory.service.VpGdnService; 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.VpGdnVo; /** * @???controller * @201596 * @version */ @Controller @RequestMapping("/vpGdn") public class VpGdnController extends BaseController { private Logger log = LoggerFactory.getLogger(VpGdnController.class); @Resource private VpGdnService vpGdnService; @Resource private InventoryService inventoryService; /** * @?? * @201596 * @param * @version * @throws MbvException */ @RequestMapping(value = "/queryByParams", method = RequestMethod.POST) @ResponseBody public JqGridBaseEntityVo<VpGdnEntity> queryByParams(JqGridBaseEntityVo<VpGdnEntity> entity, VpGdnVo 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 VpGdnVo(); VpGdnBean bean = new VpGdnBean(); bean.setWarehCode(warehCode); bean.setUnitCode(unitCode); vo.setBean(bean); } else { if (vo.getBean() == null) { VpGdnBean bean = new VpGdnBean(); bean.setWarehCode(warehCode); bean.setUnitCode(unitCode); vo.setBean(bean); } else { vo.getBean().setWarehCode(warehCode); vo.getBean().setUnitCode(unitCode); } } String dispTime1 = vo.getBean().getDispTime1(); String dispTime2 = vo.getBean().getDispTime2(); if (StringUtils.isNotEmpty(dispTime1) && StringUtils.isNotEmpty(dispTime2)) { vo.getBean().setDispTime2(dispTime2 + " 23:59:59"); } else if (StringUtils.isNotEmpty(dispTime1) && StringUtils.isEmpty(dispTime2)) { if (dispTime1.length() == 7) { vo.getBean().setQueryMonth(dispTime1); } else if (dispTime1.length() == 10) { vo.getBean().setQueryDay(dispTime1); } } else if (StringUtils.isNotEmpty(dispTime2) && StringUtils.isEmpty(dispTime1)) { if (dispTime2.length() == 7) { vo.getBean().setQueryMonth(dispTime2); vo.getBean().setDispTime2(""); } else if (dispTime2.length() == 10) { vo.getBean().setQueryDay(dispTime2); vo.getBean().setDispTime2(""); } } log.info("dispTime1:" + vo.getBean().getDispTime1()); log.info("dispTime2:" + vo.getBean().getDispTime2()); List<VpGdnEntity> list = new ArrayList<VpGdnEntity>(); try { // ? int totalCount = this.vpGdnService.queryByParamsCount(vo.getBean()); log.info("totalCount:" + totalCount); // ? if (totalCount > 0) { // ? int offset = (entity.getPage() - 1) * entity.getRows(); int limit = entity.getRows(); // ?? list = this.vpGdnService.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); } } /** * @???? * @author gel * @2015827 * @param * @version * @throws Exception */ @RequestMapping(value = "/saveVpGdn", method = RequestMethod.POST) @ResponseBody public void addSysParam(@RequestBody VpGdnVo vo, HttpServletResponse response) throws Exception { // vo if (vo == null) { throw new MbvException("???"); } // ?? VpGdnEntity VpGdnEntity = vo.getEntity(); if (VpGdnEntity == null) { throw new MbvException("???"); } try { // ?? String errMessage = vpGdnService.validate(VpGdnEntity); if (errMessage != null && !"".equals(errMessage)) { throw new MbvException(errMessage); } List<VpGdnDtlEntity> details = vo.getDetails(); // ???? errMessage = vpGdnService.validateDetail(VpGdnEntity, details); if (errMessage != null && !"".equals(errMessage)) { throw new MbvException(errMessage); } // service? boolean isSuccess = vpGdnService.addVpGdn(VpGdnEntity, details, null); 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) { try { String docCode = vpGdnService.getVpGdnMaxDocCode(MbvConstant.SEQ_NAME_VPGDN); if (docCode == null || "".equals(docCode)) { throw new MbvException("????"); } log.info("docCode:" + docCode); VpGdnEntity entity = new VpGdnEntity(); entity.setDocCode(docCode); // session?? String orgCode = request.getSession().getAttribute(MbvConstant.UNIT_CODE).toString(); String warehCode = request.getSession().getAttribute(MbvConstant.WAREH_CODE).toString(); if (orgCode == null || "".equals(orgCode) || warehCode == null || "".equals(warehCode)) { throw new MbvException("Session?????"); } log.info("orgCode:" + orgCode); log.info("warehCode:" + warehCode); 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 = "/findVpGdnById", method = RequestMethod.POST) @ResponseBody public void findVpGdn(@RequestBody VpGdnVo vo, HttpServletResponse response) throws MbvException { // vo if (vo == null) { throw new MbvException("??"); } try { VpGdnEntity entity = vpGdnService.getVpGdnByEntity(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 = "/findVpGdnByDocCode", method = RequestMethod.POST) @ResponseBody public void findVpGdnByDocCode(@RequestBody VpGdnVo 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("findVpGdnByDocCode:" + vo.getEntity().getDocCode()); try { VpGdnBean bean = vpGdnService.getVpGdnBeanByEntity(vo.getEntity()); if (bean == null) { throw new MbvException("????"); } log.info("findVpGdnByDocCode:" + vo.getEntity().getDocCode()); 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 = "/updateVpGdn", method = RequestMethod.POST) @ResponseBody public void updateVpGdn(@RequestBody VpGdnVo vo, HttpServletResponse response) throws Exception { // vo if (vo == null || vo.getEntity().getId() == null || vo.getEntity().getId() == 0) { throw new MbvException("??"); } VpGdnEntity entity = vo.getEntity(); try { // ?? String errMessage = vpGdnService.validate(entity); if (errMessage != null && !"".equals(errMessage)) { throw new MbvException(errMessage); } List<VpGdnDtlEntity> details = vo.getDetails(); // ???? errMessage = vpGdnService.validateDetail(entity, details); if (errMessage != null && !"".equals(errMessage)) { throw new MbvException(errMessage); } boolean updateResult = vpGdnService.updateVpGdn(entity, details); log.info("updateVpGdn 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); } } /** * @??? * @2015917 * @param * @version * @throws Exception */ @RequestMapping(value = "/deleteVpGdn", method = RequestMethod.POST) @ResponseBody public void deleteVpGdn(@RequestBody VpGdnVo vo, HttpServletResponse response) throws Exception { // vo if (vo == null) { throw new MbvException("??"); } List<Integer> deleteIds = vo.getDeleteIds(); // ?id ? List<VpGdnEntity> vpGdnEntityList = new ArrayList<VpGdnEntity>(); try { for (int id : deleteIds) { VpGdnEntity vde = new VpGdnEntity(); vde.setId((long) id); vpGdnEntityList.add(vpGdnService.getVpGdnByEntity(vde)); } boolean deleteResult = vpGdnService.deleteVpGdnBatch(vpGdnEntityList); Map<String, Object> map = new HashMap<String, Object>(); map.put("result", deleteResult); // ?? 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 = "/deleteVpGdnById", method = RequestMethod.POST) @ResponseBody public void deleteVpGdnById(@RequestBody VpGdnVo vo, HttpServletResponse response) throws Exception { // vo if (vo == null || vo.getEntity() == null || vo.getEntity().getId() < 1 || vo.getEntity().getDocState() == null || "".equals(vo.getEntity().getDocState())) { throw new MbvException("???"); } log.info("id:" + vo.getEntity().getId()); try { boolean deleteResult = vpGdnService.deleteVpGdnByEntity(vo.getEntity()); if (deleteResult) { log.info("??"); } Map<String, Object> map = new HashMap<String, Object>(); map.put("result", deleteResult); // ?? 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); } } /** * @??excel * @2015916 * @param * @version */ @RequestMapping(value = "/importGdn", method = RequestMethod.POST) @ResponseBody public void importVpGdnAddBill( @RequestParam(value = "add_vpGdn_importedFile", required = false) MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws IOException { 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<VpGdnDtlBean> list = new ArrayList<VpGdnDtlBean>(); // ?? for (int i = 0; i < models.size(); i++) { VpGdnDtlBean detail = new VpGdnDtlBean(); String prodNum = models.get(i)[0] == null || "".equals(models.get(i)[0]) ? "" : models.get(i)[0].toString().trim(); 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??"); } log.info("prodNum:" + prodNum); 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(); throw new MbvException(me.getMessage()); } catch (Exception e) { e.printStackTrace(); throw new MbvException(e.getMessage()); } return json; } /** * @??excel * @2015916 * @param * @version */ @RequestMapping(value = "/importUpdateGdn", method = RequestMethod.POST) @ResponseBody public void importVpGdnUpdateBill( @RequestParam(value = "update_vpGdn_importedFile", required = false) MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws IOException { 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()); } }