parent
f95ede3ed1
commit
df7cc7b04f
@ -0,0 +1,35 @@
|
||||
package com.smart.module.finance.service;
|
||||
|
||||
import com.smart.common.model.Result;
|
||||
import com.smart.common.util.ExcelExport;
|
||||
import com.smart.module.finance.entity.Order;
|
||||
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public interface OrderService {
|
||||
|
||||
/**
|
||||
* 保存订单
|
||||
* @param entity 订单实体对象
|
||||
* @return 返回操作结果
|
||||
*/
|
||||
Result save(Order entity);
|
||||
|
||||
/**
|
||||
* 查询订单列表
|
||||
* @param entity 订单实体对象
|
||||
* @return 返回订单列表结果
|
||||
*/
|
||||
Result list(Order entity);
|
||||
|
||||
/**
|
||||
* 导出订单数据为Excel文件
|
||||
* @param orgId 机构ID
|
||||
* @param parkManageId 停车场ID
|
||||
* @return 返回导出结果
|
||||
* @throws IOException 输入输出异常
|
||||
* @throws InvalidFormatException 无效的格式异常
|
||||
*/
|
||||
ExcelExport exportData(Long orgId, Long parkManageId) throws IOException, InvalidFormatException;
|
||||
}
|
@ -0,0 +1,121 @@
|
||||
package com.smart.module.finance.service.impl;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import com.smart.common.constant.SystemConstant;
|
||||
import com.smart.common.dynamicquery.DynamicQuery;
|
||||
import com.smart.common.model.PageBean;
|
||||
import com.smart.common.model.Result;
|
||||
import com.smart.common.util.DateUtils;
|
||||
import com.smart.common.util.ExcelExport;
|
||||
import com.smart.common.util.OrderUtils;
|
||||
import com.smart.common.util.ShiroUtils;
|
||||
import com.smart.module.finance.entity.Order;
|
||||
import com.smart.module.finance.repository.OrderRepository;
|
||||
import com.smart.module.finance.service.OrderService;
|
||||
import com.smart.module.sys.entity.SysUser;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Service // 声明为Spring的服务类
|
||||
public class OrderServiceImpl implements OrderService {
|
||||
|
||||
@Autowired
|
||||
private OrderRepository orderRepository; // 注入订单仓库接口
|
||||
@Autowired
|
||||
private DynamicQuery dynamicQuery; // 注入动态查询工具类
|
||||
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor=Exception.class) // 声明方法为事务,若出现异常则回滚
|
||||
public Result save(Order entity) {
|
||||
entity.setGmtCreate(DateUtils.getTimestamp()); // 设置订单的创建时间为当前时间
|
||||
entity.setOrderNo(OrderUtils.getOrderNo(entity.getParkManageId())); // 设置订单号
|
||||
orderRepository.save(entity); // 保存订单到数据库
|
||||
return Result.ok(); // 返回一个成功的结果对象
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result list(Order entity) {
|
||||
String nativeSql = "SELECT COUNT(*) FROM app_order "; // 构造统计订单数量的SQL语句
|
||||
nativeSql += common(entity); // 添加通用的查询条件
|
||||
Long count = dynamicQuery.nativeQueryCount(nativeSql); // 使用动态查询工具执行SQL语句并返回结果数量
|
||||
PageBean<Order> data = new PageBean<>(); // 创建一个空的分页数据对象
|
||||
if(count>0){
|
||||
nativeSql = "SELECT * FROM app_order "; // 构造查询订单列表的SQL语句
|
||||
nativeSql += common(entity); // 添加通用的查询条件
|
||||
nativeSql += " ORDER BY gmt_create desc"; // 按创建时间降序排序
|
||||
Pageable pageable = PageRequest.of(entity.getPageNo(),entity.getPageSize()); // 创建分页请求对象
|
||||
List<Order> list = dynamicQuery.nativeQueryPagingList(Order.class, pageable, nativeSql); // 使用动态查询工具执行SQL查询并获取订单列表
|
||||
data = new PageBean(list, count); // 将查询结果封装为分页数据对象
|
||||
}
|
||||
return Result.ok(data); // 返回包含订单列表的成功结果对象
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true) // 声明方法为只读事务
|
||||
public ExcelExport exportData(Long orgId, Long parkManageId) throws IOException, InvalidFormatException {
|
||||
SysUser user = ShiroUtils.getUserEntity(); // 获取当前登录用户的信息
|
||||
Map<String, Integer> dataMap = new LinkedHashMap<>(); // 创建一个有序的数据列映射Map对象
|
||||
dataMap.put("plate_number", ExcelExport.CELL_ALIGN_LEFT); // 添加列名和对齐方式到数据列映射Map中
|
||||
dataMap.put("type", ExcelExport.CELL_ALIGN_LEFT);
|
||||
dataMap.put("total_Fee", ExcelExport.CELL_ALIGN_LEFT);
|
||||
dataMap.put("gmt_create", ExcelExport.CELL_ALIGN_LEFT);
|
||||
String nativeSql = "SELECT plate_number,(CASE TYPE WHEN 0 THEN '微信' WHEN 1 THEN '支付宝' ELSE '其它' END) type,total_Fee,gmt_create FROM app_order WHERE 1=1"; // 构造查询订单数据的SQL语句
|
||||
List<Map<String, Object>> list;
|
||||
if(ShiroUtils.isHasRole(SystemConstant.ROLE_ADMIN)){ // 如果当前用户是管理员
|
||||
if(orgId!=null){
|
||||
nativeSql +=" AND org_id="+orgId;
|
||||
}
|
||||
}else{
|
||||
nativeSql +=" AND org_id="+user.getOrgId(); // 否则,将查询条件限制为当前用户所在机构
|
||||
}
|
||||
if(parkManageId!=null){ // 如果停车场ID不为空
|
||||
nativeSql +=" AND park_manage_id=?"; // 添加停车场ID作为查询条件
|
||||
list = dynamicQuery.nativeQueryListMap(nativeSql,parkManageId); // 使用动态查询工具执行带参数的SQL查询
|
||||
}else{
|
||||
list = dynamicQuery.nativeQueryListMap(nativeSql); // 执行不带参数的SQL查询
|
||||
}
|
||||
/**
|
||||
* 开始生成模板、导出数据
|
||||
*/
|
||||
InputStream stream = ClassUtils.getDefaultClassLoader()
|
||||
.getResourceAsStream("static/excelTemplate/orderExport.xls"); // 加载Excel模板文件
|
||||
ExcelExport excelExport = new ExcelExport(
|
||||
FileUtil.writeFromStream(stream, new File("excelTemplate/orderExport.xls")), 1); // 创建一个Excel导出对象
|
||||
excelExport.setDataList(list, dataMap, false, ""); // 将查询结果填充到Excel导出对象中
|
||||
return excelExport; // 返回Excel导出对象
|
||||
}
|
||||
|
||||
public String common(Order entity){
|
||||
String description = entity.getDescription(); // 获取订单描述信息
|
||||
String commonSql = " WHERE 1=1"; // 初始化通用的SQL查询条件
|
||||
if(StringUtils.isNotBlank(description)){ // 如果描述信息不为空
|
||||
commonSql += "AND body like '"+description+"%' "; // 添加描述信息作为查询条件
|
||||
}
|
||||
if(ShiroUtils.isHasRole(SystemConstant.ROLE_ADMIN)){ // 如果当前用户是管理员
|
||||
if(entity.getOrgId()!=null){
|
||||
commonSql +=" AND org_id="+entity.getOrgId(); // 添加机构ID作为查询条件
|
||||
}
|
||||
}else{
|
||||
Long orgId = ShiroUtils.getUserEntity().getOrgId(); // 获取当前用户所在机构ID
|
||||
commonSql +=" AND org_id="+orgId; // 添加机构ID作为查询条件
|
||||
}
|
||||
if(entity.getParkManageId()!=null){ // 如果停车场ID不为空
|
||||
commonSql +=" AND park_manage_id="+entity.getParkManageId(); // 添加停车场ID作为查询条件
|
||||
}
|
||||
return commonSql; // 返回组装后的SQL查询条件
|
||||
}
|
||||
}
|
Loading…
Reference in new issue