优化场景:
一个接口,三个客户端同时使用
controller层
//type= 1 -pc端 ; type=2 -小程序 ; type=3 -app
public ResponseResult getCount(@PathVariable int type, String address, String jobType) {
//获取用户信息
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if (Constents.ANONYMOUSUSER.equals(principal)) {
return this.farmWorkService.getCountAutoFertilizations(type, address, jobType);
}
return null;
}
service
public ResponseResult getCount(int type, String address, String jobType ) {
ResponseResult responseResult = new ResponseResult();
//键值对的形式存储
Map<String, Object> data = new HashMap<>();
try {
// pc端
if (StringUtil.isEmpty(address) && type == 1) {
//查询植保当前年度信息
List<Map<String, Object>> plantInfo = this.farmWorkMapper.getPlantInfoByAddrType(address, jobType ,"all" );
//植保情况
data.put("PlantInfo", plantInfo);
responseResult.setData(data);
logger.info("获取pc端数据结束");
return responseResult;
}
//小程序端
if (type == 2) {
List<Map<String, Object>> plantInfo = this.farmWorkMapper.getPlantInfoByAddrType(address, jobType , "city");
data.put("PlantInfo", plantInfo);
responseResult.setData(data);
logger.info("获取小程序端结束");
return responseResult;
}
// app端
if (type == 3) {
List<Map<String, Object>> plantInfo = this.farmWorkMapper.getPlantInfoByAddrType(address, jobType , "town");
data.put("PlantInfo", plantInfo);
responseResult.setData(data);
logger.info("获取app端结束");
return responseResult;
}
} catch (Exception e) {
logger.error("获取数据异常", e);
return new ResponseResult(BaseErrorType.PLANTING_ERROR);
}
return responseResult;
}
Mapper
List<Map<String, Object>> getPlantInfoByAddrType(String paramString1, @Param("jobType") String paramString2,@Param("addrType")String addrType);
Mapper.xml
<where>
<choose>
<when test="addrType != null and addrType == 'prov'">
AND address_prov=#{param1}
</when>
<when test="addrType != null and addrType == 'city'">
AND address_city=#{param1}
</when>
<when test="addrType != null and addrType == 'town'">
AND address_town=#{param1}
</when>
</choose>
</where>