package cn.com.lzt.initialstock.controller; import cn.com.lzt.goodsinfo.entity.TBGoodsInfoEntity; import cn.com.lzt.initialstock.entity.TBInitialStockEntity; import cn.com.lzt.initialstock.page.TBInitialStockPage; import cn.com.lzt.initialstock.service.TBInitialStockServiceI; import cn.com.lzt.initialstockdetail.entity.TBInitialStockDetailEntity; import cn.com.lzt.stockinfo.entity.TBStockInfoEntity; import cn.com.lzt.warehouse.entity.WarehouseEntity; import org.apache.commons.lang.xwork.StringUtils; import org.apache.log4j.Logger; import org.jeecgframework.core.beanvalidator.BeanValidators; import org.jeecgframework.core.common.controller.BaseController; import org.jeecgframework.core.common.dao.ICommonDao; import org.jeecgframework.core.common.exception.BusinessException; import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery; import org.jeecgframework.core.common.model.json.AjaxJson; import org.jeecgframework.core.common.model.json.DataGrid; import org.jeecgframework.core.constant.Globals; import org.jeecgframework.core.util.*; import org.jeecgframework.poi.excel.ExcelImportUtil; import org.jeecgframework.poi.excel.entity.ExportParams; import org.jeecgframework.poi.excel.entity.ImportParams; import org.jeecgframework.poi.excel.entity.vo.NormalExcelConstants; import org.jeecgframework.tag.core.easyui.TagUtil; import org.jeecgframework.web.system.pojo.base.TSBaseUser; import org.jeecgframework.web.system.pojo.base.TSType; import org.jeecgframework.web.system.pojo.base.TSTypegroup; import org.jeecgframework.web.system.pojo.base.TSUser; import org.jeecgframework.web.system.service.SystemService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.util.UriComponentsBuilder; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import javax.validation.ConstraintViolation; import javax.validation.Validator; import java.io.IOException; import java.net.URI; import java.util.*; /** * @Title: Controller * @Description: 期初库存表 * @author onlineGenerator * @date 2017-06-20 13:41:25 * @version V1.0 * */ @Controller @RequestMapping("/tBInitialStockController") public class TBInitialStockController extends BaseController { /** * Logger for this class */ private static final Logger logger = Logger.getLogger(TBInitialStockController.class); @Autowired private TBInitialStockServiceI tBInitialStockService; @Autowired private SystemService systemService; @Autowired private Validator validator; @Autowired public ICommonDao commonDao; /** * 期初库存表列表 页面跳转 * * @return */ @RequestMapping(params = "list") public ModelAndView list(HttpServletRequest request) { return new ModelAndView("cn/com/lzt/initialstock/tBInitialStockList"); } /** * easyui AJAX请求数据 * * @param request * @param response * @param dataGrid * @param user */ @RequestMapping(params = "datagrid") public void datagrid(TBInitialStockEntity tBInitialStock,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) { CriteriaQuery cq = new CriteriaQuery(TBInitialStockEntity.class, dataGrid); //查询条件组装器 org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, tBInitialStock); try{ //自定义追加查询条件 }catch (Exception e) { throw new BusinessException(e.getMessage()); } cq.add(); this.tBInitialStockService.getDataGridReturn(cq, true); TagUtil.datagrid(response, dataGrid); } /** * 导出excel * * @param request * @param response */ @RequestMapping(params = "exportXls") public String exportXls(TBInitialStockEntity tBInitialStock,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid,ModelMap map) { CriteriaQuery cq = new CriteriaQuery(TBInitialStockEntity.class, dataGrid); //查询条件组装器 org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, tBInitialStock); try{ //自定义追加查询条件 }catch (Exception e) { throw new BusinessException(e.getMessage()); } cq.add(); List list=this.tBInitialStockService.getListByCriteriaQuery(cq, false); List pageList=new ArrayList(); if(list!=null&&list.size()>0){ for(TBInitialStockEntity entity:list){ try{ TBInitialStockPage page=new TBInitialStockPage(); MyBeanUtils.copyBeanNotNull2Bean(entity,page); Object id0 = entity.getId(); String hql0 = "from TBInitialStockDetailEntity where 1 = 1 AND iNITIAL_STOCK_ID =? "; List tBInitialStockDetailEntityList = systemService.findHql(hql0,id0); page.setTBInitialStockDetailList(tBInitialStockDetailEntityList); pageList.add(page); }catch(Exception e){ logger.info(e.getMessage()); } } } map.put(NormalExcelConstants.FILE_NAME,"期初库存表"); map.put(NormalExcelConstants.CLASS,TBInitialStockPage.class); map.put(NormalExcelConstants.PARAMS,new ExportParams("期初库存表列表", "导出人:Jeecg", "导出信息")); map.put(NormalExcelConstants.DATA_LIST,pageList); return NormalExcelConstants.JEECG_EXCEL_VIEW; } /** * 删除期初库存表 * * @return */ @RequestMapping(params = "doDel") @ResponseBody public AjaxJson doDel(String id, HttpServletRequest request) { AjaxJson j = new AjaxJson(); TBInitialStockEntity tBInitialStock = systemService.getEntity(TBInitialStockEntity.class, id); String message = "期初库存表删除成功"; try{ tBInitialStockService.delMain(tBInitialStock); systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO); }catch(Exception e){ e.printStackTrace(); message = "期初库存表删除失败"; throw new BusinessException(e.getMessage()); } j.setMsg(message); return j; } /* @RequestMapping(params = "doDel") @ResponseBody public AjaxJson doDel(TBInitialStockEntity tBInitialStock, HttpServletRequest request) { AjaxJson j = new AjaxJson(); tBInitialStock = systemService.getEntity(TBInitialStockEntity.class, tBInitialStock.getId()); String message = "期初库存表删除成功"; try{ tBInitialStockService.delMain(tBInitialStock); systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO); }catch(Exception e){ e.printStackTrace(); message = "期初库存表删除失败"; throw new BusinessException(e.getMessage()); } j.setMsg(message); return j; }*/ /** * 批量删除期初库存表 * * @return */ @RequestMapping(params = "doBatchDel") @ResponseBody public AjaxJson doBatchDel(String ids,HttpServletRequest request){ AjaxJson j = new AjaxJson(); String message = "期初库存表删除成功"; try{ for(String id:ids.split(",")){ TBInitialStockEntity tBInitialStock = systemService.getEntity(TBInitialStockEntity.class,id); tBInitialStockService.delMain(tBInitialStock); systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO); } }catch(Exception e){ e.printStackTrace(); message = "期初库存表删除失败"; throw new BusinessException(e.getMessage()); } j.setMsg(message); return j; } /** * 同步 * * @author zhijia.wang */ @RequestMapping(params = "syncRealStock") @ResponseBody public AjaxJson syncRealStock(String id, HttpServletRequest req) { AjaxJson j = new AjaxJson(); String message = null; String hql1 = "from TBInitialStockDetailEntity where initialStockId=? "; // List detailList = null;//期初库存详情list // List realtimeStockList = new ArrayList();//实时库存list // try{ // TBInitialStockEntity initialStock = systemService.getEntity(TBInitialStockEntity.class, id); // if(initialStock != null){ // initialStock.setSyncStatus(Globals.Sync_yes);//设置为已同步状态 // detailList = systemService.findHql(hql1, initialStock.getId()); // if(detailList!=null && detailList.size()>0){ // for(int i=0; i tBInitialStockDetailList = tBInitialStockPage.getTBInitialStockDetailList(); // List realStickList = new ArrayList();//实时库存集合 tBInitialStock.setSyncStatus(Globals.Sync_no); AjaxJson j = new AjaxJson(); String message = "添加成功"; try{ //设置仓库code和name WarehouseEntity warehouseEntity = systemService.getEntity(WarehouseEntity.class, tBInitialStock.getWarehouseId()); if(warehouseEntity != null){ tBInitialStock.setWarehouseCode(warehouseEntity.getWarehouseCode()); tBInitialStock.setWarehouseName(warehouseEntity.getWarehouseName()); String hql1 = "from TBInitialStockEntity where warehouseId=? "; List initialStockEntityList = systemService.findHql(hql1, tBInitialStock.getWarehouseId()); if(initialStockEntityList != null && initialStockEntityList.size()>0){ message = "添加失败,仓库:" + warehouseEntity.getWarehouseName()+"已经添加过期初库存,不能重复添加"; j.setMsg(message); j.setSuccess(false); return j; } } //设置经手人登录名、姓名等 HttpSession session = ContextHolderUtils.getSession(); TSUser user = (TSUser)session.getAttribute("LOCAL_CLINET_USER"); if(user != null){ tBInitialStock.setByhandId(user.getId()); tBInitialStock.setByhandBy(user.getUserName());//经手人登录名称 tBInitialStock.setByhandName(user.getRealName());//经手人名称 tBInitialStock.setByhandDate(new Date());//经手日期 }else{ message = "请先选择经手人后再提交"; j.setMsg(message); j.setSuccess(false); return j; } if(tBInitialStockDetailList != null && tBInitialStockDetailList.size()>0){ Map map = new HashMap(); for(int i=0;i0){ for(int i=0;i tBInitialStockDetailList = tBInitialStockPage.getTBInitialStockDetailList(); AjaxJson j = new AjaxJson(); String message = "更新成功"; try{ //设置仓库code和name WarehouseEntity warehouseEntity = systemService.getEntity(WarehouseEntity.class, tBInitialStock.getWarehouseId()); if(warehouseEntity != null){ tBInitialStock.setWarehouseCode(warehouseEntity.getWarehouseCode()); tBInitialStock.setWarehouseName(warehouseEntity.getWarehouseName()); // String hql1 = "from TBInitialStockEntity where warehouseId=? "; // String sql1 = "select count(1) from t_b_initial_stock where WAREHOUSE_ID='" + tBInitialStock.getWarehouseId()+"'"; String sql2 = "select WAREHOUSE_NAME from t_b_initial_stock where WAREHOUSE_ID=? "; // List initialStockEntityList = tBInitialStockService.findHql(hql1, tBInitialStock.getWarehouseId()); // Long lo = tBInitialStockService.getCountForJdbc(sql1); List> list = tBInitialStockService.findForJdbc(sql2, tBInitialStock.getWarehouseId()); if(list != null && list.size() != 1){ message = "更新失败,仓库:" + warehouseEntity.getWarehouseName()+"已经添加过期初库存,不能重复添加"; j.setMsg(message); j.setSuccess(false); return j; } } //设置经手人登录名、姓名等 TSBaseUser user = systemService.getEntity(TSBaseUser.class, tBInitialStock.getByhandId()); if(user != null){ tBInitialStock.setByhandBy(user.getUserName());//经手人登录名称 tBInitialStock.setByhandName(user.getRealName());//经手人名称 tBInitialStock.setByhandDate(new Date());//经手日期 }else{ message = "请先选择经手人后再提交"; j.setMsg(message); j.setSuccess(false); return j; } if(tBInitialStockDetailList != null && tBInitialStockDetailList.size()>0){ Map map = new HashMap(); for(int i=0;i0){ for(int i=0;i tBInitialStockDetailEntityList = systemService.findHql(hql0,id0); //查询字典(计量单位) if(tBInitialStockDetailEntityList!=null && tBInitialStockDetailEntityList.size()>0){ for(int i=0; i groupList = systemService.findHql(hql1,Globals.Dic_meteringUnit); if(groupList != null && groupList.size()>0){ TSTypegroup group = groupList.get(0); List typeList = group.getTSTypes(); if(typeList != null && typeList.size()>0){ for(int j=0; j fileMap = multipartRequest.getFileMap(); for (Map.Entry entity : fileMap.entrySet()) { MultipartFile file = entity.getValue();// 获取上传文件对象 ImportParams params = new ImportParams(); params.setTitleRows(2); params.setHeadRows(2); params.setNeedSave(true); try { List list = ExcelImportUtil.importExcel(file.getInputStream(), TBInitialStockPage.class, params); TBInitialStockEntity entity1=null; for (TBInitialStockPage page : list) { entity1=new TBInitialStockEntity(); MyBeanUtils.copyBeanNotNull2Bean(page,entity1); tBInitialStockService.addMain(entity1, page.getTBInitialStockDetailList()); } j.setMsg("文件导入成功!"); } catch (Exception e) { j.setMsg("文件导入失败!"); logger.error(ExceptionUtil.getExceptionMessage(e)); }finally{ try { file.getInputStream().close(); } catch (IOException e) { e.printStackTrace(); } } } return j; } /** * 导出excel 使模板 */ @RequestMapping(params = "exportXlsByT") public String exportXlsByT(ModelMap map) { map.put(NormalExcelConstants.FILE_NAME,"期初库存表"); map.put(NormalExcelConstants.CLASS,TBInitialStockPage.class); map.put(NormalExcelConstants.PARAMS,new ExportParams("期初库存表列表", "导出人:"+ ResourceUtil.getSessionUserName().getRealName(), "导出信息")); map.put(NormalExcelConstants.DATA_LIST,new ArrayList()); return NormalExcelConstants.JEECG_EXCEL_VIEW; } /** * 导入功能跳转 * * @return */ @RequestMapping(params = "upload") public ModelAndView upload(HttpServletRequest req) { req.setAttribute("controller_name", "tBInitialStockController"); return new ModelAndView("common/upload/pub_excel_upload"); } @RequestMapping(method = RequestMethod.GET) @ResponseBody public List list() { List listTBInitialStocks=tBInitialStockService.getList(TBInitialStockEntity.class); return listTBInitialStocks; } @RequestMapping(value = "/{id}", method = RequestMethod.GET) @ResponseBody public ResponseEntity get(@PathVariable("id") String id) { TBInitialStockEntity task = tBInitialStockService.get(TBInitialStockEntity.class, id); if (task == null) { return new ResponseEntity(HttpStatus.NOT_FOUND); } return new ResponseEntity(task, HttpStatus.OK); } @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public ResponseEntity create(@RequestBody TBInitialStockPage tBInitialStockPage, UriComponentsBuilder uriBuilder) { //调用JSR303 Bean Validator进行校验,如果出错返回含400错误码及json格式的错误信息. Set> failures = validator.validate(tBInitialStockPage); if (!failures.isEmpty()) { return new ResponseEntity(BeanValidators.extractPropertyAndMessage(failures), HttpStatus.BAD_REQUEST); } //保存 List tBInitialStockDetailList = tBInitialStockPage.getTBInitialStockDetailList(); TBInitialStockEntity tBInitialStock = new TBInitialStockEntity(); try{ MyBeanUtils.copyBeanNotNull2Bean(tBInitialStock,tBInitialStockPage); }catch(Exception e){ logger.info(e.getMessage()); } tBInitialStockService.addMain(tBInitialStock, tBInitialStockDetailList); //按照Restful风格约定,创建指向新任务的url, 也可以直接返回id或对象. String id = tBInitialStockPage.getId(); URI uri = uriBuilder.path("/rest/tBInitialStockController/" + id).build().toUri(); HttpHeaders headers = new HttpHeaders(); headers.setLocation(uri); return new ResponseEntity(headers, HttpStatus.CREATED); } @RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity update(@RequestBody TBInitialStockPage tBInitialStockPage) { //调用JSR303 Bean Validator进行校验,如果出错返回含400错误码及json格式的错误信息. Set> failures = validator.validate(tBInitialStockPage); if (!failures.isEmpty()) { return new ResponseEntity(BeanValidators.extractPropertyAndMessage(failures), HttpStatus.BAD_REQUEST); } //保存 List tBInitialStockDetailList = tBInitialStockPage.getTBInitialStockDetailList(); TBInitialStockEntity tBInitialStock = new TBInitialStockEntity(); try{ MyBeanUtils.copyBeanNotNull2Bean(tBInitialStock,tBInitialStockPage); }catch(Exception e){ logger.info(e.getMessage()); } tBInitialStockService.updateMain(tBInitialStock, tBInitialStockDetailList); //按Restful约定,返回204状态码, 无内容. 也可以返回200状态码. return new ResponseEntity(HttpStatus.NO_CONTENT); } @RequestMapping(value = "/{id}", method = RequestMethod.DELETE) @ResponseStatus(HttpStatus.NO_CONTENT) public void delete(@PathVariable("id") String id) { TBInitialStockEntity tBInitialStock = tBInitialStockService.get(TBInitialStockEntity.class, id); tBInitialStockService.delMain(tBInitialStock); } /** * 弹出货品信息列表选择页面 * * @return */ @RequestMapping(params = "selectReferGoods") public ModelAndView selectReferGoods(HttpServletRequest request) { ModelAndView mv = new ModelAndView("cn/com/lzt/initialstock/selectGoodsInfo"); String ids = oConvertUtils.getString(request.getParameter("ids")); mv.addObject("ids", ids); // 需要将前端的类路径传递到refer页面 String clsname = oConvertUtils.getString(request.getParameter("classname")); mv.addObject("classname", clsname); mv.addObject("codefield", oConvertUtils.getString(request.getParameter("codefield"))); mv.addObject("namefield", oConvertUtils.getString(request.getParameter("namefield"))); mv.addObject("specification", oConvertUtils.getString(request.getParameter("specification"))); mv.addObject("meteringUnit", oConvertUtils.getString(request.getParameter("meteringUnit"))); mv.addObject("meteringUnitName", oConvertUtils.getString(request.getParameter("meteringUnitName"))); mv.addObject("costPrice", oConvertUtils.getString(request.getParameter("costPrice"))); mv.addObject("goodsId", oConvertUtils.getString(request.getParameter("goodsId"))); mv.addObject("strEmpty", oConvertUtils.getString(request.getParameter("strEmpty"))); return mv; } /** * 根据货品名称获取货品信息 * * @param request * @param response * @param dataGrid * @throws ClassNotFoundException */ @SuppressWarnings("rawtypes") @RequestMapping(params = "selRefDatagridGoods") public void selRefDatagridGoods(String classname, TBGoodsInfoEntity tBGoodsInfoEntity, HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) throws Exception { Class cls = MyClassLoader.getClassByScn(classname); CriteriaQuery cq = new CriteriaQuery(cls, dataGrid); //查询条件组装器 //增加筛选客户的条件 String[] unitstate = new String[]{Globals.Enable_Normal}; cq.in("status", unitstate); cq.eq("deleteFlag", Globals.Delete_Normal.toString()); // 检索用货品编码 if(StringUtils.isNotBlank(tBGoodsInfoEntity.getGoodsCode())){ cq.eq("goodsCode", tBGoodsInfoEntity.getGoodsCode()); } // 检索用货品名称 if(StringUtils.isNotBlank(tBGoodsInfoEntity.getGoodsName())){ cq.eq("goodsName", tBGoodsInfoEntity.getGoodsName()); } org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, cls.newInstance()); commonDao.getDataGridReturn(cq, true); List result = dataGrid.getResults(); if(result != null && result.size() > 0){ TBGoodsInfoEntity goodsInfoEntity = null; for(int i = 0 ;i < result.size();i++){ goodsInfoEntity = (TBGoodsInfoEntity)result.get(i); goodsInfoEntity.setMeteringUnit(goodsInfoEntity.getBaseUnit()); //获取参数 Object id0 = goodsInfoEntity.getId(); String hql0 = "from TBStockInfoEntity where 1 = 1 AND GOODS_ID =? "; List stockInfoEntityList = systemService.findHql(hql0,id0); if(stockInfoEntityList != null && stockInfoEntityList.size() > 0){ TBStockInfoEntity stockInfoEntity = stockInfoEntityList.get(0); goodsInfoEntity.setCostPrice(stockInfoEntity.getLatestCost()); } //查询字典(计量单位) String hql1 = "from TSTypegroup where typegroupcode =? "; if(goodsInfoEntity.getMeteringUnit() != null){ //查出所有的计量单位 List groupList = systemService.findHql(hql1,Globals.Dic_meteringUnit); if(groupList != null && groupList.size()>0){ TSTypegroup group = groupList.get(0); List typeList = group.getTSTypes(); if(typeList != null && typeList.size()>0){ for(int j=0; j(); try{ goodInfo = systemService.findUniqueByProperty(TBGoodsInfoEntity.class, "goodsCode",goodsCode); if(goodInfo != null){ goodInfo.setMeteringUnit(goodInfo.getBaseUnit()); if(goodInfo.getDeleteFlag() == null || goodInfo.getStatus() == null || !Globals.Delete_Normal.toString().equals(goodInfo.getDeleteFlag()) || !Globals.Enable_Normal.equals(goodInfo.getStatus())){ goodInfo = null; reJson.put("status", "success"); reJson.put("datainfo", goodInfo); return reJson; } } //获取参数 Object id0 = goodInfo.getId(); String hql0 = "from TBStockInfoEntity where 1 = 1 AND GOODS_ID =? "; List stockInfoEntityList = systemService.findHql(hql0,id0); if(stockInfoEntityList != null && stockInfoEntityList.size() > 0){ TBStockInfoEntity stockInfoEntity = stockInfoEntityList.get(0); goodInfo.setCostPrice(stockInfoEntity.getLatestCost()); } //查询字典(计量单位) String hql1 = "from TSTypegroup where typegroupcode =? "; if(goodInfo.getMeteringUnit() != null){ //查出所有的计量单位 List groupList = systemService.findHql(hql1,Globals.Dic_meteringUnit); if(groupList != null && groupList.size()>0){ TSTypegroup group = groupList.get(0); List typeList = group.getTSTypes(); if(typeList != null && typeList.size()>0){ for(int j=0; j