From 4f618d60d104177f289c398695723453bec0b75e Mon Sep 17 00:00:00 2001 From: FOAM <491460741@qq.com> Date: Fri, 15 Mar 2024 15:53:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=AD=E5=BE=AE=E5=A4=9A=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wms/pom.xml | 16 ++ .../main/java/com/yc/wms/WmsApplication.java | 7 + .../com/yc/wms/controller/UserController.java | 117 +++++++++---- .../java/com/yc/wms/service/DcxService.java | 23 +++ .../yc/wms/service/impl/DcxServiceImpl.java | 73 ++++++++ .../main/java/com/yc/wms/until/CallBack.java | 43 +++++ .../com/yc/wms/until/SpringContextHolder.java | 157 ++++++++++++++++++ wms/src/main/resources/application.yml | 8 +- 8 files changed, 406 insertions(+), 38 deletions(-) create mode 100644 wms/src/main/java/com/yc/wms/service/DcxService.java create mode 100644 wms/src/main/java/com/yc/wms/service/impl/DcxServiceImpl.java create mode 100644 wms/src/main/java/com/yc/wms/until/CallBack.java create mode 100644 wms/src/main/java/com/yc/wms/until/SpringContextHolder.java diff --git a/wms/pom.xml b/wms/pom.xml index 6d9abd8..a3a6e2d 100644 --- a/wms/pom.xml +++ b/wms/pom.xml @@ -70,6 +70,22 @@ 3.5.1 + + com.microsoft.sqlserver + mssql-jdbc + 9.4.0.jre8 + + + cn.hutool + hutool-all + 5.8.21 + + + jakarta.persistence + jakarta.persistence-api + 2.2.3 + + diff --git a/wms/src/main/java/com/yc/wms/WmsApplication.java b/wms/src/main/java/com/yc/wms/WmsApplication.java index e5577fd..f2bad20 100644 --- a/wms/src/main/java/com/yc/wms/WmsApplication.java +++ b/wms/src/main/java/com/yc/wms/WmsApplication.java @@ -1,7 +1,9 @@ package com.yc.wms; +import com.yc.wms.until.SpringContextHolder; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; @SpringBootApplication public class WmsApplication { @@ -9,5 +11,10 @@ public class WmsApplication { public static void main(String[] args) { SpringApplication.run(WmsApplication.class, args); } + @Bean + public SpringContextHolder springContextHolder() { + return new SpringContextHolder(); + } + } diff --git a/wms/src/main/java/com/yc/wms/controller/UserController.java b/wms/src/main/java/com/yc/wms/controller/UserController.java index af59119..94c6e82 100644 --- a/wms/src/main/java/com/yc/wms/controller/UserController.java +++ b/wms/src/main/java/com/yc/wms/controller/UserController.java @@ -5,12 +5,20 @@ import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.yc.wms.model.Menu; import com.yc.wms.model.User; +import com.yc.wms.service.DcxService; import com.yc.wms.service.MenuService; import com.yc.wms.service.UserService; import com.yc.wms.until.QueryPageUtil; import com.yc.wms.until.Result; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import springfox.documentation.spring.web.json.Json; +import cn.hutool.json.JSONArray; +import cn.hutool.json.JSONObject; +import cn.hutool.json.JSONUtil; import java.util.Arrays; import java.util.HashMap; @@ -25,6 +33,7 @@ import java.util.List; * @since 2023-03-04 */ @RestController +@Slf4j @RequestMapping("/user") public class UserController { @@ -34,26 +43,65 @@ public class UserController { @Autowired private MenuService menuService; + @Autowired + private DcxService dcxService; + /** * @return * 登录 */ @PostMapping ("/login") public Result login(@RequestBody User user) { - Result result = null; - List list= userService.lambdaQuery() - .eq(User::getUserNo,user.getUserNo()) - .eq(User::getPassWord,user.getPassWord()).list(); - if(list.size()>0){ - List menulist=menuService.lambdaQuery().like(Menu::getMenuRight,list.get(0).getRoleId().toString()).list(); - HashMap map =new HashMap(); - map.put("user",list.get(0)); - map.put("menu",menulist); - result=Result.success(map); - }else{ - result=Result.fail("账号或密码输入有误!"); - } - return result; +// Result result = null; +// List list= userService.lambdaQuery() +// .eq(User::getUserNo,user.getUserNo()) +// .eq(User::getPassWord,user.getPassWord()).list(); +// if(list.size()>0){ +// List menulist=menuService.lambdaQuery().like(Menu::getMenuRight,list.get(0).getRoleId().toString()).list(); +// HashMap map =new HashMap(); +// map.put("user",list.get(0)); +// map.put("menu",menulist); +// result=Result.success(map); +// }else{ +// result=Result.fail("账号或密码输入有误!"); +// } +// return result; + JSONArray array=new JSONArray(); + JSONObject data=new JSONObject(); + int menuCount=3; + for (int i=1;i<=menuCount;i++){ + JSONObject menu=new JSONObject(); + menu.put("id",i); + menu.put("menuCode",i); + if(i==1) { + menu.put("menuName", "库存汇总查询"); + }else if(i==2) { + menu.put("menuName", "入库明细查询"); + }else if(i==3) { + menu.put("menuName", "出库明细查询"); + } + menu.put("menuLevel",i); + menu.put("menuParentCode",i); + menu.put("menuClick","admin"+i); + menu.put("menuRight", "0,1"); + menu.put("menuComponent","/admin"+i); + menu.put("menuIcon","el-icon-s-home"); + array.add(menu); + } + data.put("menu",array); + JSONObject u=new JSONObject(); + u.put("id", 2); + u.put("userNo", "1"); + u.put("userName", "1"); + u.put("passWord", "123456"); + u.put("age", 1); + u.put("sex", 1); + u.put("phone", "1"); + u.put("roleId", 1); + u.put("isValid", false); + data.put("user",u); + log.info("lll:"+new ResponseEntity(data, HttpStatus.OK)); + return Result.success(menuCount,data); } /** @@ -62,26 +110,27 @@ public class UserController { */ @PostMapping("/queryUser") public Result queryUser(@RequestBody QueryPageUtil query) { - HashMap map = query.getParam(); - String name = (String) map.get("name"); - String sex = (String) map.get("xb"); - Page page = new Page(); - page.setCurrent(query.getPageNum()); - page.setSize(query.getPageSize()); - System.out.println("当前页:" + query.getPageNum()); - System.out.println("条数:" + query.getPageSize()); - LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper(); - if (name != null && name.length() > 0) { - lambdaQueryWrapper.like(User::getUserName, name); - } - if (sex != null && sex.length() > 0) { - lambdaQueryWrapper.like(User::getSex, sex); - } - IPage iPage = userService.QueryUser(page, lambdaQueryWrapper); - - System.out.println(iPage.getTotal()); - System.out.println(iPage.getRecords()); - return Result.success(iPage.getTotal(), iPage.getRecords()); +// HashMap map = query.getParam(); +// String name = (String) map.get("name"); +// String sex = (String) map.get("xb"); +// Page page = new Page(); +// page.setCurrent(query.getPageNum()); +// page.setSize(query.getPageSize()); +// System.out.println("当前页:" + query.getPageNum()); +// System.out.println("条数:" + query.getPageSize()); +// LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper(); +// if (name != null && name.length() > 0) { +// lambdaQueryWrapper.like(User::getUserName, name); +// } +// if (sex != null && sex.length() > 0) { +// lambdaQueryWrapper.like(User::getSex, sex); +// } +// IPage iPage = userService.QueryUser(page, lambdaQueryWrapper); +// +// System.out.println(iPage.getTotal()); +// System.out.println(iPage.getRecords()); +// return Result.success(iPage.getTotal(), iPage.getRecords()); + return dcxService.readJson(query); } /** diff --git a/wms/src/main/java/com/yc/wms/service/DcxService.java b/wms/src/main/java/com/yc/wms/service/DcxService.java new file mode 100644 index 0000000..4edd468 --- /dev/null +++ b/wms/src/main/java/com/yc/wms/service/DcxService.java @@ -0,0 +1,23 @@ +package com.yc.wms.service; + + +import com.baomidou.mybatisplus.core.conditions.Wrapper; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.service.IService; +import com.yc.wms.model.User; +import com.yc.wms.until.QueryPageUtil; +import com.yc.wms.until.Result; + +import java.util.List; + +/** + *

+ * 服务类 + *

+ * + * @author wms + * @since 2023-03-04 + */ +public interface DcxService { + Result readJson(QueryPageUtil query); +} diff --git a/wms/src/main/java/com/yc/wms/service/impl/DcxServiceImpl.java b/wms/src/main/java/com/yc/wms/service/impl/DcxServiceImpl.java new file mode 100644 index 0000000..6a037e1 --- /dev/null +++ b/wms/src/main/java/com/yc/wms/service/impl/DcxServiceImpl.java @@ -0,0 +1,73 @@ +package com.yc.wms.service.impl; + +import cn.hutool.json.JSONArray; +import cn.hutool.json.JSONObject; +import com.yc.wms.service.DcxService; +import com.yc.wms.until.QueryPageUtil; +import com.yc.wms.until.Result; +import com.yc.wms.until.SpringContextHolder; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.object.SqlQuery; +import org.springframework.stereotype.Service; + +import java.util.HashMap; +import java.util.List; +import javax.persistence.EntityManager; +import javax.persistence.Query; + +@Service +@Slf4j +public class DcxServiceImpl implements DcxService { + + private EntityManager entityManager; + + @Override + public Result readJson(QueryPageUtil query) { + HashMap map = query.getParam(); +// String methodType = (String) map.get("methodType"); +// if(methodType.equals("kchzcx")){//库存汇总查询 +// return queryKchz(query); +// } + return queryKchz(query); + } + + public Result queryKchz(QueryPageUtil query) { + EntityManager entity = SpringContextHolder.getBean(EntityManager.class); + HashMap map = query.getParam(); +// String itemCode = (String) map.get("itemCode"); + int pageNum = query.getPageNum();//当前页 + int pageSize = query.getPageSize();//条数 + System.out.println("当前页:" + pageNum); + System.out.println("条数:" + pageSize); + String sql = "from (SELECT ROW_NUMBER() OVER(ORDER BY it.id) AS RowNum, it.id ," + + "it.code," + + "it.name" + + " from Item it "; + sql += ") AS subquery "; + String sqlCount = "select count(id) " + sql; + sql = "select * " + sql + " WHERE RowNum BETWEEN " + (pageNum-1) * pageSize + " AND " + (pageNum) * pageSize; + log.info("sqlCount:"+sqlCount); + log.info("sql:"+sql); + log.info("entityManager:"+entity); + List list=entity.createNativeQuery(sql).getResultList(); + List listCount=entity.createNativeQuery(sqlCount).getResultList(); + JSONArray data = new JSONArray(); + for(Object[] obj:list){ + int i=0; + JSONObject d = new JSONObject(); + d.put("id",obj[i++]==null?"":obj[i-1]); + d.put("userNo",obj[i++]==null?"":obj[i-1]); + d.put("userName", obj[i++]==null?"":obj[i-1]); + d.put("passWord","123456"); + d.put("age",19); + d.put("sex",1); + d.put("phone", "123"); + d.put("roleId", 2); + d.put("isValid", false); + data.add(d); + } + return Result.success(listCount.size(),data); + } + +} diff --git a/wms/src/main/java/com/yc/wms/until/CallBack.java b/wms/src/main/java/com/yc/wms/until/CallBack.java new file mode 100644 index 0000000..78fc6aa --- /dev/null +++ b/wms/src/main/java/com/yc/wms/until/CallBack.java @@ -0,0 +1,43 @@ +/* + * Copyright 2019-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.yc.wms.until; + +/** + * @author: liaojinlong + * @date: 2020/6/9 17:02 + * @since: 1.0 + * @see {@link SpringContextHolder} + * 针对某些初始化方法,在SpringContextHolder 初始化前时,
+ * 可提交一个 提交回调任务。
+ * 在SpringContextHolder 初始化后,进行回调使用 + */ + +public interface CallBack { + /** + * 回调执行方法 + */ + void executor(); + + /** + * 本回调任务名称 + * @return / + */ + default String getCallBackName() { + return Thread.currentThread().getId() + ":" + this.getClass().getName(); + } +} + diff --git a/wms/src/main/java/com/yc/wms/until/SpringContextHolder.java b/wms/src/main/java/com/yc/wms/until/SpringContextHolder.java new file mode 100644 index 0000000..1c01d38 --- /dev/null +++ b/wms/src/main/java/com/yc/wms/until/SpringContextHolder.java @@ -0,0 +1,157 @@ +/* + * Copyright 2019-2020 Zheng Jie + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.yc.wms.until; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.DisposableBean; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * @author Jie + * @date 2019-01-07 + */ +@Slf4j +public class SpringContextHolder implements ApplicationContextAware, DisposableBean { + + private static ApplicationContext applicationContext = null; + private static final List CALL_BACKS = new ArrayList<>(); + private static boolean addCallback = true; + + /** + * 针对 某些初始化方法,在SpringContextHolder 未初始化时 提交回调方法。 + * 在SpringContextHolder 初始化后,进行回调使用 + * + * @param callBack 回调函数 + */ + public synchronized static void addCallBacks(CallBack callBack) { + if (addCallback) { + SpringContextHolder.CALL_BACKS.add(callBack); + } else { + log.warn("CallBack:{} 已无法添加!立即执行", callBack.getCallBackName()); + callBack.executor(); + } + } + + /** + * 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型. + */ + @SuppressWarnings("unchecked") + public static T getBean(String name) { + assertContextInjected(); + return (T) applicationContext.getBean(name); + } + + /** + * 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型. + */ + public static T getBean(Class requiredType) { + assertContextInjected(); + return applicationContext.getBean(requiredType); + } + + /** + * 获取SpringBoot 配置信息 + * + * @param property 属性key + * @param defaultValue 默认值 + * @param requiredType 返回类型 + * @return / + */ + public static T getProperties(String property, T defaultValue, Class requiredType) { + T result = defaultValue; + try { + result = getBean(Environment.class).getProperty(property, requiredType); + } catch (Exception ignored) {} + return result; + } + + /** + * 获取SpringBoot 配置信息 + * + * @param property 属性key + * @return / + */ + public static String getProperties(String property) { + return getProperties(property, null, String.class); + } + + /** + * 获取SpringBoot 配置信息 + * + * @param property 属性key + * @param requiredType 返回类型 + * @return / + */ + public static T getProperties(String property, Class requiredType) { + return getProperties(property, null, requiredType); + } + + /** + * 检查ApplicationContext不为空. + */ + private static void assertContextInjected() { + if (applicationContext == null) { + throw new IllegalStateException("applicaitonContext属性未注入, 请在applicationContext" + + ".xml中定义SpringContextHolder或在SpringBoot启动类中注册SpringContextHolder."); + } + } + + /** + * 清除SpringContextHolder中的ApplicationContext为Null. + */ + private static void clearHolder() { + log.debug("清除SpringContextHolder中的ApplicationContext:" + + applicationContext); + applicationContext = null; + } + + @Override + public void destroy() { + SpringContextHolder.clearHolder(); + } + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + if (SpringContextHolder.applicationContext != null) { + log.warn("SpringContextHolder中的ApplicationContext被覆盖, 原有ApplicationContext为:" + SpringContextHolder.applicationContext); + } + SpringContextHolder.applicationContext = applicationContext; + if (addCallback) { + for (CallBack callBack : SpringContextHolder.CALL_BACKS) { + callBack.executor(); + } + CALL_BACKS.clear(); + } + SpringContextHolder.addCallback = false; + } + + /** + * 获取 @Service 的所有 bean 名称 + * @return / + */ + public static List getAllServiceBeanName() { + return new ArrayList<>(Arrays.asList(applicationContext + .getBeanNamesForAnnotation(Service.class))); + } +} diff --git a/wms/src/main/resources/application.yml b/wms/src/main/resources/application.yml index 1cca6bf..23de31a 100644 --- a/wms/src/main/resources/application.yml +++ b/wms/src/main/resources/application.yml @@ -3,10 +3,10 @@ server: spring: datasource: - url: jdbc:mysql://47.100.54.81:53306/wms?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=false&useOldAliasMetadataBehavior=true&autoReconnect=true&failOverReadOnly=false - driver-class-name: com.mysql.cj.jdbc.Driver - username: root - password: Youchain@56 + url: jdbc:sqlserver://47.100.54.81:1433;DatabaseName=zwtest + driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver + username: super + password: 1K4QfD%ESd1fe@VJ hikari: connection-timeout: 60000 validation-timeout: 3000