创建基本的comtroller、service及implements
以本地新建的项目jecloud-erp为例,一般用户在做自己的业务时,写的业务代码,都在这个jecloud-erp-impl这个模块里面去写,以下是这个项目中的模块划分案例
代码案例
Controller
package com.je.controller;
import com.google.common.base.Strings;
import com.je.common.base.mvc.AbstractPlatformController;
import com.je.common.base.result.BaseRespResult;
import com.je.service.ProjectInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
@RestController
@RequestMapping(value="/je/erp/project")
public class ProjectInformationController extends AbstractPlatformController {
@Autowired
private ProjectInformationService projectInformationService;
@RequestMapping(value = {"/getProjectInfo"}, method = RequestMethod.POST, produces = "application/json; charset=utf-8")
public BaseRespResult online(HttpServletRequest request) {
//通用获取前端参数的方法
String type = getStringParameter(request, "type");
//如果类型参数没有传直接返回异常
if (Strings.isNullOrEmpty(type)) {
return BaseRespResult.errorResult("参数异常!");
}
return projectInformationService.getProjectInfo(type);
}
}
Service
package com.je.service;
import com.je.common.base.result.BaseRespResult;
public interface ProjectInformationService {
/**
* 获取项目信息
* @param type informationSource 工程信息来源
* engineeringStatus 工程状态
* @return
*/
BaseRespResult getProjectInfo(String type);
}
Implements
package com.je.service.impl;
import com.je.common.base.result.BaseRespResult;
import com.je.common.base.service.MetaResourceService;
import com.je.common.base.service.MetaService;
import com.je.service.ProjectInformationService;
import com.google.common.base.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class ProjectInformationServiceImpl implements ProjectInformationService {
//日志
private static final Logger logger = LoggerFactory.getLogger(ProjectInformationServiceImpl.class);
//信息来源
private static final String INFORMATION_SOURCE = "informationSource";
//工程状态
private static final String ENGINEERING_STATUS = "engineeringStatus";
//信息来源表字段
private static final String XM_XXLY_CODE = "XM_XXLY_CODE";
//工程状态表字段
private static final String XM_ZT_CODE = "XM_ZT_CODE";
//跨服务查询调用对应的RPC接口查询数据
@Autowired
private MetaResourceService metaResourceService;
//服务内通用查询
@Autowired
private MetaService metaService;
@Override
public BaseRespResult getProjectInfo(String type) {
List<Map<String, Object>> projectDicItem = null;
if (type.equals(INFORMATION_SOURCE)) {
//先查询字典中所有的项目信息来源的字典项
projectDicItem = metaResourceService.selectMap("SELECT * FROM je_core_dictionaryitem WHERE DICTIONARYITEM_DICTIONARY_ID = (\n" +
"SELECT JE_CORE_DICTIONARY_ID FROM je_core_dictionary WHERE DICTIONARY_DDCODE ='KH_XXLY') AND SY_NODETYPE != 'root'");
return getProject(projectDicItem, XM_XXLY_CODE);
} else if (type.equals(ENGINEERING_STATUS)) {
//先查询字典中所有的项目状态的字典项
projectDicItem = metaResourceService.selectMap("SELECT * FROM je_core_dictionaryitem WHERE DICTIONARYITEM_DICTIONARY_ID = (\n" +
"SELECT JE_CORE_DICTIONARY_ID FROM je_core_dictionary WHERE DICTIONARY_DDCODE ='KHGC_ZT') AND SY_NODETYPE != 'root'");
return getProject(projectDicItem, XM_ZT_CODE);
} else {
return BaseRespResult.errorResult("类型参数异常!");
}
}
private BaseRespResult getProject(List<Map<String, Object>> projectDicItem, String tableCode) {
//数据字典为空返回异常
if (projectDicItem == null || projectDicItem.size() == 0) {
return BaseRespResult.errorResult("项目信息来源异常!");
}
//定义返回结果
List<Map<String, String>> result = new ArrayList<>();
Map<String, String> resultMap = null;
List<Map<String, Object>> mapList = null;
if ((!Strings.isNullOrEmpty(tableCode)) && XM_ZT_CODE.equals(tableCode)) {
mapList = metaService.selectSql("SELECT XM_ZT_NAME,XM_ZT_CODE ,count(*) as count FROM KT_CRM_XM GROUP BY XM_ZT_CODE");
}
if ((!Strings.isNullOrEmpty(tableCode)) && XM_XXLY_CODE.equals(tableCode)) {
mapList = metaService.selectSql("SELECT XM_XXLY_NAME,XM_XXLY_CODE ,count(*) as count FROM KT_CRM_XM GROUP BY XM_XXLY_CODE ");
}
String name = "";
if (mapList != null && mapList.size() > 0) {
for (Map<String, Object> projectMap : mapList) {
resultMap = new HashMap<>();
//如果项目中有赋值统计次数
if ((!Strings.isNullOrEmpty(tableCode)) && XM_XXLY_CODE.equals(tableCode)) {
resultMap.put("name", projectMap.get("XM_XXLY_NAME").toString());
if (!Strings.isNullOrEmpty(name)) {
name = name + "," + projectMap.get("XM_XXLY_NAME").toString();
} else {
name = projectMap.get("XM_XXLY_NAME").toString();
}
resultMap.put("count", projectMap.get("count").toString());
}
if ((!Strings.isNullOrEmpty(tableCode)) && XM_ZT_CODE.equals(tableCode)) {
resultMap.put("name", projectMap.get("XM_ZT_NAME").toString());
if (!Strings.isNullOrEmpty(name)) {
name = name + "," + projectMap.get("XM_ZT_NAME").toString();
} else {
name = projectMap.get("XM_ZT_NAME").toString();
}
resultMap.put("count", projectMap.get("count").toString());
}
result.add(resultMap);
}
//找出项目中没有的状态,统计次数为零
for (Map<String, Object> itemMap : projectDicItem) {
resultMap = new HashMap<>();
String itemName = itemMap.get("DICTIONARYITEM_ITEMNAME").toString();
if (name.contains(itemName)) {
continue;
}
resultMap.put("name", itemName);
resultMap.put("count", "0");
result.add(resultMap);
}
return BaseRespResult.successResult(result);
} else {
//如果项目中都没有全部赋值为零
for (Map<String, Object> itemMap : projectDicItem) {
resultMap = new HashMap<>();
resultMap.put("name", itemMap.get("DICTIONARYITEM_ITEMNAME").toString());
resultMap.put("count", "0");
result.add(resultMap);
}
return BaseRespResult.successResult(result);
}
}
}
方法调用
1. 确定servicename的名称,当前文档案例中指定是erp,可以在application.yml文件中进行查看
2. 添加路由的方式有两种:
2.1、在网关项目jecloud-getway中找到/resources/router/urlRouter.xml文件,在这个文件中添加下面内容
<url path="/je/erp/.*" microServiceName="erp" realPath="" versionRule="0.0.0+" prefixSegmentCount="0" />
2.2、请求头路由
需要在请求头中添加参数pd,内容为指定的服务名称serviceName,本案例中是erp,例如将截图中的pd:demo改为pd:erp即可
3. 新添加的controller需要重启网关,和erp的服务:
在运维管理-作业管理中重新发布erp服务
可以在作业历史记录中,看是否发布成功
调运完成,返回结果
1.调运前一定要对代码进行编译打包,将服务往注册中心重新注册,要在注册中心能看到自己写的controller,并且重启网关,才能使用
postMan中在请求头添加路由
请求URL:
- /je/erp/project/getProjectInfo
请求方式:
- POST
请求头:
参数名 | 是否必须 | 类型 | 说明 |
---|---|---|---|
authorization | 是 | string | 请求内容签名 |
p’d | 是 | String | erp路由 |
请求参数:
参数名 | 是否必须 | 类型 | 说明 |
---|---|---|---|
type | 是 | String | 根据类型查不同项目信息 nformationSource 工程信息来源;engineeringStatus 工程状态 |
返回示例:
调用返回结果
{
"code": "1000",
"data": [
{
"name": "客户推荐",
"count": "2"
},
{
"name": "渠道信息",
"count": "0"
},
{
"name": "招标网站",
"count": "0"
}
],
"message": "操作成功",
"success": true,
"rows": null,
"totalCount": null
}
最后编辑: 王超 文档更新时间: 2024-11-08 17:00 作者:王超