parent
5b522d0deb
commit
3ab085b4bc
@ -0,0 +1,237 @@ |
||||
package com.bweb.controller; |
||||
|
||||
import com.alibaba.excel.EasyExcel; |
||||
import com.alibaba.excel.ExcelWriter; |
||||
import com.alibaba.excel.write.metadata.fill.FillWrapper; |
||||
import com.alibaba.excel.write.metadata.style.WriteCellStyle; |
||||
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy; |
||||
import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy; |
||||
import com.bweb.excel.ExcelUtil; |
||||
import com.hfkj.common.utils.DateUtil; |
||||
import com.hfkj.common.utils.ResponseMsgUtil; |
||||
import com.hfkj.config.CommonSysConst; |
||||
import com.hfkj.entity.BsMerchant; |
||||
import com.hfkj.model.ResponseData; |
||||
import com.hfkj.service.gas.BsGasOrderCountService; |
||||
import com.hfkj.service.merchant.BsMerchantService; |
||||
import io.swagger.annotations.Api; |
||||
import io.swagger.annotations.ApiOperation; |
||||
import org.apache.commons.collections4.MapUtils; |
||||
import org.apache.commons.lang3.StringUtils; |
||||
import org.apache.poi.ss.usermodel.HorizontalAlignment; |
||||
import org.apache.poi.ss.usermodel.IndexedColors; |
||||
import org.apache.poi.ss.usermodel.VerticalAlignment; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
import org.springframework.stereotype.Controller; |
||||
import org.springframework.web.bind.annotation.*; |
||||
|
||||
import javax.annotation.Resource; |
||||
import java.math.BigDecimal; |
||||
import java.util.*; |
||||
import java.util.stream.Collectors; |
||||
|
||||
@Controller |
||||
@RequestMapping(value = "/gasOrderCount") |
||||
@Api(value = "加油订单统计") |
||||
public class BsGasOrderCountController { |
||||
private static Logger log = LoggerFactory.getLogger(BsGasOrderCountController.class); |
||||
@Resource |
||||
private BsGasOrderCountService gasOrderCountService; |
||||
@Resource |
||||
private BsMerchantService merchantService; |
||||
|
||||
@RequestMapping(value="/countOilData",method = RequestMethod.GET) |
||||
@ResponseBody |
||||
@ApiOperation(value = "统计加油数据") |
||||
public ResponseData exportOilOrder(@RequestParam(name = "channelType", required = false) Integer channelType, |
||||
@RequestParam(name = "startTime", required = true) Long startTime) { |
||||
try { |
||||
// 获取统计数据
|
||||
Calendar startDate = Calendar.getInstance(); |
||||
startDate.setTime(new Date(startTime)); |
||||
startDate.set(Calendar.HOUR_OF_DAY, 0); |
||||
startDate.set(Calendar.MINUTE, 0); |
||||
startDate.set(Calendar.SECOND, 0); |
||||
|
||||
List<HashMap<String,Object>> dataList = new LinkedList<>(); |
||||
|
||||
int day = 1; // 当天
|
||||
// 当天数据
|
||||
List<HashMap<String, Object>> mapList = gasOrderCountService.countOrderRatio(channelType, DateUtil.date2String(startDate.getTime(), DateUtil.Y_M_D_HMS), day-1); |
||||
// 利用当天数据比例进行倒序排序
|
||||
List<HashMap<String, Object>> merList = new LinkedList<>(); |
||||
for (HashMap<String, Object> data : mapList) { |
||||
HashMap<String,Object> mer = new HashMap<>(); |
||||
mer.put("merNo", data.get("merNo")); |
||||
mer.put("merName", data.get("merName")); |
||||
merList.add(mer); |
||||
} |
||||
|
||||
// 当天
|
||||
HashMap<String,Object> dataMap = new HashMap<>(); |
||||
dataMap.put("day", day); |
||||
dataMap.put("data", mapList); |
||||
dataList.add(dataMap); |
||||
|
||||
// 前3天
|
||||
HashMap<String,Object> dataMap3 = new HashMap<>(); |
||||
day = 3; |
||||
dataMap3.put("day", day); |
||||
dataMap3.put("data", gasOrderCountService.countOrderRatio(channelType, DateUtil.date2String(startDate.getTime(), DateUtil.Y_M_D_HMS), day-1)); |
||||
dataList.add(dataMap3); |
||||
|
||||
// 前10天
|
||||
HashMap<String,Object> dataMap10 = new HashMap<>(); |
||||
day = 10; |
||||
dataMap10.put("day", day); |
||||
dataMap10.put("data", gasOrderCountService.countOrderRatio(channelType, DateUtil.date2String(startDate.getTime(), DateUtil.Y_M_D_HMS), day-1)); |
||||
dataList.add(dataMap10); |
||||
|
||||
// 前30天
|
||||
HashMap<String,Object> dataMap30 = new HashMap<>(); |
||||
day = 30; |
||||
dataMap30.put("day", day); |
||||
dataMap30.put("data", gasOrderCountService.countOrderRatio(channelType, DateUtil.date2String(startDate.getTime(), DateUtil.Y_M_D_HMS), day-1)); |
||||
dataList.add(dataMap30); |
||||
|
||||
// 前60天
|
||||
HashMap<String,Object> dataMap60 = new HashMap<>(); |
||||
day = 60; |
||||
dataMap60.put("day", day); |
||||
dataMap60.put("data", gasOrderCountService.countOrderRatio(channelType, DateUtil.date2String(startDate.getTime(), DateUtil.Y_M_D_HMS), day-1)); |
||||
dataList.add(dataMap60); |
||||
|
||||
List<List<Object>> excelDataList = new ArrayList<>(); |
||||
for (HashMap<String,Object> merchant : merList) { |
||||
List<Object> excelData = new ArrayList<>(); |
||||
excelData.add(merchant.get("merNo")); |
||||
excelData.add(merchant.get("merName")); |
||||
|
||||
for (HashMap<String,Object> data : dataList) { |
||||
List<HashMap<String, Object>> totalCountCollect = ((List<HashMap<String,Object>>) data.get("data")).stream() |
||||
.filter(o -> o.get("merNo").equals(merchant.get("merNo"))).collect(Collectors.toList()); |
||||
if (!totalCountCollect.isEmpty()) { |
||||
excelData.add(totalCountCollect.get(0).get("totalPrice")); |
||||
excelData.add(totalCountCollect.get(0).get("totalPriceRatio")+"%"); |
||||
excelData.add(totalCountCollect.get(0).get("totalCount")); |
||||
excelData.add(totalCountCollect.get(0).get("totalCountRatio")+"%"); |
||||
} else { |
||||
excelData.add(0); |
||||
excelData.add(0+"%"); |
||||
excelData.add(0); |
||||
excelData.add(0+"%"); |
||||
} |
||||
} |
||||
excelDataList.add(excelData); |
||||
} |
||||
|
||||
List<Object> footData = new ArrayList<>(); |
||||
footData.add("合计"); |
||||
footData.add(merList.size()+"家油站"); |
||||
footData.add(countPrice((List<HashMap<String, Object>>) dataMap.get("data"))); |
||||
footData.add(""); |
||||
footData.add(countOilNumber((List<HashMap<String, Object>>) dataMap.get("data"))); |
||||
footData.add(""); |
||||
footData.add(countPrice((List<HashMap<String, Object>>) dataMap3.get("data"))); |
||||
footData.add(""); |
||||
footData.add(countOilNumber((List<HashMap<String, Object>>) dataMap3.get("data"))); |
||||
footData.add(""); |
||||
footData.add(countPrice((List<HashMap<String, Object>>) dataMap10.get("data"))); |
||||
footData.add(""); |
||||
footData.add(countOilNumber((List<HashMap<String, Object>>) dataMap10.get("data"))); |
||||
footData.add(""); |
||||
footData.add(countPrice((List<HashMap<String, Object>>) dataMap30.get("data"))); |
||||
footData.add(""); |
||||
footData.add(countOilNumber((List<HashMap<String, Object>>) dataMap30.get("data"))); |
||||
footData.add(""); |
||||
footData.add(countPrice((List<HashMap<String, Object>>) dataMap60.get("data"))); |
||||
footData.add(""); |
||||
footData.add(countOilNumber((List<HashMap<String, Object>>) dataMap60.get("data"))); |
||||
footData.add(""); |
||||
excelDataList.add(footData); |
||||
|
||||
// 设置内容的策略
|
||||
WriteCellStyle contentWriteCellStyle = ExcelUtil.getWriteCellStyle(); |
||||
WriteCellStyle headWriteCellStyle = new WriteCellStyle(); |
||||
// 设置头部水平居中对齐
|
||||
headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER); |
||||
// 设置头部垂直居中对齐
|
||||
headWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); |
||||
|
||||
String path = "/temporary/"+System.currentTimeMillis()+".xlsx"; |
||||
EasyExcel.write( CommonSysConst.getSysConfig().getFilesystem() + path) |
||||
.head(getSaleSheetHeadList(startTime, dataList)) |
||||
.registerWriteHandler(new SimpleColumnWidthStyleStrategy(20)) |
||||
.registerWriteHandler(new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle)) |
||||
.sheet("统计") |
||||
.doWrite(excelDataList); |
||||
return ResponseMsgUtil.success(path); |
||||
|
||||
} catch (Exception e) { |
||||
log.error("error!",e); |
||||
return ResponseMsgUtil.exception(e); |
||||
} |
||||
} |
||||
|
||||
public static BigDecimal countPrice(List<HashMap<String, Object>> mapList) { |
||||
BigDecimal totalprice = new BigDecimal("0"); |
||||
for (HashMap<String, Object> map : mapList) { |
||||
if (StringUtils.isNotBlank(MapUtils.getString(map, "totalPrice"))) { |
||||
totalprice = totalprice.add(new BigDecimal(MapUtils.getString(map, "totalPrice"))); |
||||
} |
||||
} |
||||
return totalprice; |
||||
} |
||||
|
||||
public static BigDecimal countOilNumber(List<HashMap<String, Object>> mapList) { |
||||
BigDecimal totalprice = new BigDecimal("0"); |
||||
for (HashMap<String, Object> map : mapList) { |
||||
if (StringUtils.isNotBlank(MapUtils.getString(map, "totalCount"))) { |
||||
totalprice = totalprice.add(new BigDecimal(MapUtils.getString(map, "totalCount"))); |
||||
} |
||||
} |
||||
return totalprice; |
||||
} |
||||
|
||||
private static List<List<String>> getSaleSheetHeadList(Long startTime, List<HashMap<String,Object>> dataList) { |
||||
List<List<String>> list = new ArrayList<List<String>>(); |
||||
|
||||
List<String> merNo = new ArrayList<String>(); |
||||
merNo.add("油站编码"); |
||||
list.add(merNo); |
||||
|
||||
List<String> merName = new ArrayList<String>(); |
||||
merName.add("油站名称"); |
||||
list.add(merName); |
||||
|
||||
for (HashMap<String,Object> data : dataList) { |
||||
|
||||
// 获取时间
|
||||
Calendar date = Calendar.getInstance(); |
||||
date.setTime(new Date(startTime)); |
||||
date.add(Calendar.DAY_OF_MONTH, 1 - Integer.parseInt(data.get("day").toString())); |
||||
|
||||
List<String> daysHead = new ArrayList<String>(); |
||||
daysHead.add(DateUtil.date2String(date.getTime(), DateUtil.Y_M_D)); |
||||
daysHead.add("加油金额"); |
||||
list.add(daysHead); |
||||
|
||||
List<String> daysHead2 = new ArrayList<String>(); |
||||
daysHead2.add(DateUtil.date2String(date.getTime(), DateUtil.Y_M_D)); |
||||
daysHead2.add("占比"); |
||||
list.add(daysHead2); |
||||
|
||||
List<String> daysHead3 = new ArrayList<String>(); |
||||
daysHead3.add(DateUtil.date2String(date.getTime(), DateUtil.Y_M_D)); |
||||
daysHead3.add("加油订单"); |
||||
list.add(daysHead3); |
||||
|
||||
List<String> daysHead4 = new ArrayList<String>(); |
||||
daysHead4.add(DateUtil.date2String(date.getTime(), DateUtil.Y_M_D)); |
||||
daysHead4.add("占比"); |
||||
list.add(daysHead4); |
||||
} |
||||
return list; |
||||
} |
||||
} |
@ -0,0 +1,30 @@ |
||||
package com.bweb.excel; |
||||
|
||||
import com.alibaba.excel.write.metadata.style.WriteCellStyle; |
||||
import com.alibaba.excel.write.metadata.style.WriteFont; |
||||
import org.apache.poi.ss.usermodel.HorizontalAlignment; |
||||
import org.apache.poi.ss.usermodel.VerticalAlignment; |
||||
|
||||
/** |
||||
* @className: excelUtil |
||||
* @author: HuRui |
||||
* @date: 2025/8/28 |
||||
**/ |
||||
public class ExcelUtil { |
||||
|
||||
public static WriteCellStyle getWriteCellStyle() { |
||||
WriteCellStyle contentWriteCellStyle = new WriteCellStyle(); |
||||
// 设置内容水平居中对齐
|
||||
contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER); |
||||
// 设置内容垂直居中对齐
|
||||
contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); |
||||
// 自动换行
|
||||
contentWriteCellStyle.setWrapped(false); |
||||
// 设置字体样式和大小
|
||||
/*WriteFont contentWriteFont = new WriteFont(); |
||||
contentWriteFont.setFontHeightInPoints((short) 12); |
||||
contentWriteFont.setFontName("微软雅黑"); |
||||
contentWriteCellStyle.setWriteFont(contentWriteFont);*/ |
||||
return contentWriteCellStyle; |
||||
} |
||||
} |
@ -0,0 +1,25 @@ |
||||
package com.hfkj.service.gas; |
||||
|
||||
import com.hfkj.entity.BsGasOrder; |
||||
|
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* 加油订单 |
||||
* @className: HighGasOrderService |
||||
* @author: HuRui |
||||
* @date: 2022/9/6 |
||||
**/ |
||||
public interface BsGasOrderCountService { |
||||
|
||||
/** |
||||
* 统计订单占比 |
||||
* @param channelType 渠道 |
||||
* @param days 天数 |
||||
* @return |
||||
*/ |
||||
List<HashMap<String, Object>> countOrderRatio(Integer channelType, String startTime, Integer days); |
||||
|
||||
} |
@ -0,0 +1,26 @@ |
||||
package com.hfkj.service.gas.impl; |
||||
|
||||
import com.hfkj.dao.BsGasOrderMapper; |
||||
import com.hfkj.dao.BsGasOrderMapperExt; |
||||
import com.hfkj.service.gas.BsGasOrderCountService; |
||||
import org.springframework.stereotype.Service; |
||||
|
||||
import javax.annotation.Resource; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* @className: BsGasOrderCountServiceImpl |
||||
* @author: HuRui |
||||
* @date: 2025/8/28 |
||||
**/ |
||||
@Service("gasOrderCountService") |
||||
public class BsGasOrderCountServiceImpl implements BsGasOrderCountService { |
||||
@Resource |
||||
private BsGasOrderMapper gasOrderMapper; |
||||
|
||||
@Override |
||||
public List<HashMap<String, Object>> countOrderRatio(Integer channelType, String startTime, Integer days) { |
||||
return gasOrderMapper.countOrderRatio(channelType,startTime,days); |
||||
} |
||||
} |
Loading…
Reference in new issue