cloudroam
2024-10-11 bb85777893c0a112d6b69b016d409ac37940d20c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package com.mzl.flower.service.impl.report;
 
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mzl.flower.config.exception.ValidationException;
import com.mzl.flower.dto.request.report.QueryOrderDTO;
import com.mzl.flower.dto.request.report.QueryPartnerOrderDTO;
import com.mzl.flower.dto.request.report.QuerySupplierDTO;
import com.mzl.flower.dto.response.report.*;
import com.mzl.flower.dto.response.report.OrderDetailReportResultVO;
import com.mzl.flower.dto.response.report.OrderPartnerReportResultVO;
import com.mzl.flower.dto.response.report.OrderReportCalendarBO;
import com.mzl.flower.dto.response.report.OrderReportResultVO;
import com.mzl.flower.mapper.report.OrderReportMapper;
import com.mzl.flower.service.BaseService;
import com.mzl.flower.service.calendar.CalendarService;
import com.mzl.flower.service.report.OrderReportService;
import com.mzl.flower.utils.ExcelExportUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
 
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
 
@Slf4j
@Service
public class OrderReportServiceImpl extends BaseService implements OrderReportService {
 
    @Autowired
    private CalendarService calendarService;
 
    @Autowired
    private OrderReportMapper orderReportMapper;
 
 
 
    @Override
    public Page<OrderReportResultVO> getSalePage(Page page, QueryOrderDTO dto) {
 
        List<OrderReportCalendarBO> calendarBOList= calendarService.getOrderDatePage(page,dto);
 
        final List<OrderReportResultVO> list = calendarBOList.stream().map(calendarBO -> {
            if(null!=dto.getPartnerId()) calendarBO.setPartnerId(dto.getPartnerId());
            OrderReportResultVO vo2=orderReportMapper.getOrderDateReport(calendarBO);
            if(null==vo2) vo2=new OrderReportResultVO();
            vo2.setOrderDate(calendarBO.getCalDate());
 
            return vo2;
        }).collect(Collectors.toList());
 
        page.setRecords(list);
        return page;
    }
 
    @Override
    public Page<OrderDetailReportResultVO> getOrderDetailPage(Page page, QueryOrderDTO dto) {
        OrderReportCalendarBO orderReportCalendarBO = new OrderReportCalendarBO();
        if (StringUtils.isEmpty(dto.getCalDate())) {
            throw new ValidationException("日期不能为空");
        }
        LocalDateTime orderTime = dto.getCalDate().atStartOfDay();
        orderReportCalendarBO.setPartnerId(dto.getPartnerId());
        orderReportCalendarBO.setStartDate(calculateStartTime(orderTime));
        orderReportCalendarBO.setEndDate(calculateEndTime(orderTime));
        List<OrderDetailReportResultVO> list = orderReportMapper.getOrderDetailReport(orderReportCalendarBO, page);
        page.setRecords(list);
        return page;
    }
 
    @Override
    public OrderReportResultVO getSaleStatis(QueryOrderDTO dto) {
        // 计算开始时间和结束时间
 
        OrderReportCalendarBO bo=new OrderReportCalendarBO();
        if(null!=dto.getPartnerId()) bo.setPartnerId(dto.getPartnerId());
        bo.setStartDate(calculateStartTime(dto.getStartDate()));
        bo.setEndDate(calculateEndTime(dto.getEndDate()));
        OrderReportResultVO vo2=orderReportMapper.getOrderDateReport(bo);
 
        return vo2;
    }
 
    @Override
    public void exportSalesList(HttpServletResponse response, QueryOrderDTO dto) {
 
        List<OrderReportCalendarBO> calendarBOList= calendarService.getOrderDateList(dto);
        final List<OrderReportResultVO> list = calendarBOList.stream().map(calendarBO -> {
            if(null!=dto.getPartnerId()) calendarBO.setPartnerId(dto.getPartnerId());
            OrderReportResultVO vo2=orderReportMapper.getOrderDateReport(calendarBO);
            if(null==vo2) vo2=new OrderReportResultVO();
            vo2.setOrderDate(calendarBO.getCalDate());
 
            return vo2;
        }).collect(Collectors.toList());
 
        String[] rowsName = new String[]{"序号","下单日期", "销售额", "花农底价", "平台区间加价", "平台加价", "平台区域加价"
                , "合伙人加价", "合伙人区间加价", "会员折扣","优惠券","降级扣款","缺货扣款","补货扣款","售后扣合伙人款","售后扣花农款",
                "售后扣平台款","总包干费","实际销售扎数","利润","结算状态",};
        List<Object[]> dataList = new ArrayList<>();
        int sn = 1;
        for (OrderReportResultVO o : list) {
            Object[] objs = new Object[rowsName.length];
            int a = 0;
            objs[a++] = sn; // 序号
            objs[a++] = format(o.getOrderDate(), "yyyy-MM-dd"); // 下单日期
            objs[a++] = o.getOrderTotal(); // 销售额
            objs[a++] = o.getOrderSupplierPriceAmount(); // 花农底价
            objs[a++] = o.getOrderMarkupOneAmount(); // 平台区间加价
            objs[a++] = o.getOrderMarkupTwoAmount(); // 平台加价
            objs[a++] = o.getPlatformAreaFeeAmount(); //平台区域加价
            objs[a++] = o.getOrderMarkupPartnerAmount(); //合伙人加价
            objs[a++] = o.getPartnerSectionFeeAmount(); //合伙人区间加价
            objs[a++] = o.getOrderPriceDiscountAmount();//会员折扣
            objs[a++] = o.getOrderCouponAmountTotal(); // 优惠券
            objs[a++] = o.getOrderCheckFee();//降级扣款
            objs[a++] = o.getOrderLackFeeSupplier(); //缺货扣款
            objs[a++] = o.getOrderReplaceFee(); //补货扣款
            objs[a++] = o.getOrderFeePartner(); //售后扣合伙人款
            objs[a++] = o.getOrderFeeSupplier(); // 售后扣花农款
            objs[a++] = o.getOrderFeePlatform(); //售后扣平台款
            objs[a++] = o.getPartnerTotalFeeAmount(); // 总包干费
            objs[a++] = o.getRealSaleNum(); // 实际销售扎数
            objs[a++] = o.getProfitFeeAmount(); //利润
            objs[a++] = o.getSettleStatus(); // 结算状态
            dataList.add(objs);
            sn++;
        }
 
        ExcelExportUtil excelExportUtil = new ExcelExportUtil("财务报表", rowsName, dataList, response);
        try {
            response.addHeader("filename", URLEncoder.encode("财务报表.xls", "UTF-8"));
            response.addHeader("Access-Control-Expose-Headers", "filename");
            excelExportUtil.export();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
 
    @Override
    public void exportOrderDetail(HttpServletResponse response, QueryOrderDTO dto) {
 
        if (StringUtils.isEmpty(dto.getCalDate())) {
            throw new ValidationException("日期不能为空");
        }
        OrderReportCalendarBO orderReportCalendarBO = new OrderReportCalendarBO();
        LocalDateTime orderTime = dto.getCalDate().atStartOfDay();
        orderReportCalendarBO.setPartnerId(dto.getPartnerId());
        orderReportCalendarBO.setStartDate(calculateStartTime(orderTime));
        orderReportCalendarBO.setEndDate(calculateEndTime(orderTime));
        List<OrderDetailReportResultVO> odrs = orderReportMapper.getOrderDetailReport(orderReportCalendarBO, null);
        String[] rowsName = new String[]{"序号","订单号", "下单用户", "收货人地址", "合伙人", "下单时间", "订单金额"
                , "花农底价", "平台区间加价", "平台加价", "平台区域加价", "合伙人加价", "合伙人区间加价", "会员折扣"
                , "优惠券", "降级扣款", "缺货扣款", "补货扣款", "售后扣合伙人款", "售后扣花农款", "售后扣平台款", "总包干费"
                , "销售扎数", "利润", "结算状态"};
        List<Object[]> dataList = new ArrayList<>();
        int sn = 1;
        for (OrderDetailReportResultVO o : odrs) {
            Object[] objs = new Object[rowsName.length];
            int a = 0;
            objs[a++] = sn;
            objs[a++] = o.getOrderNo();
            objs[a++] = o.getCustomer();
            objs[a++] = o.getAddress();
            objs[a++] = o.getPartnerName();
            objs[a++] = o.getOrderDate();
            objs[a++] = o.getOrderTotal();
            objs[a++] = o.getOrderSupplierPriceAmount();
            objs[a++] = o.getOrderMarkupOneAmount();
            objs[a++] = o.getOrderMarkupTwoAmount();
            objs[a++] = o.getPlatformAreaFeeAmount();
            objs[a++] = o.getOrderMarkupPartnerAmount();
            objs[a++] = o.getPartnerSectionFeeAmount();
            objs[a++] = o.getOrderPriceDiscountAmount();
            objs[a++] = o.getOrderCouponAmountTotal();
            objs[a++] = o.getOrderCheckFee();
            objs[a++] = o.getOrderLackFeeSupplier();
            objs[a++] = o.getOrderReplaceFee();
            objs[a++] = o.getOrderFeePartner();
            objs[a++] = o.getOrderFeeSupplier();
            objs[a++] = o.getOrderFeePlatform();
            objs[a++] = o.getPartnerTotalFeeAmount();
            objs[a++] = o.getRealSaleNum();
            objs[a++] = o.getProfitFeeAmount();
            objs[a++] = o.getSettleStatus();
            dataList.add(objs);
 
            sn++;
        }
        ExcelExportUtil excelExportUtil = new ExcelExportUtil("订单结算明细", rowsName, dataList, response);
        try {
            response.addHeader("filename", URLEncoder.encode("订单结算明细.xls", "UTF-8"));
            response.addHeader("Access-Control-Expose-Headers", "filename");
            excelExportUtil.export();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
 
    }
 
    @Override
    public Page<OrderPartnerReportResultVO> getPartnerSalePage(Page page, QueryPartnerOrderDTO dto) {
        Page<OrderPartnerReportResultVO> result=orderReportMapper.getPartnerOrderDateReportPage(page,dto);
        return result;
    }
 
    @Override
    public OrderPartnerReportResultVO getPartnerSaleStatis(QueryPartnerOrderDTO dto) {
        OrderPartnerReportResultVO vo2=orderReportMapper.getPartnerOrderDateReportStatis(dto);
        return vo2;
    }
 
    @Override
    public void exportPartnerSalesList(HttpServletResponse response, QueryPartnerOrderDTO dto) {
        List<OrderPartnerReportResultVO> list = orderReportMapper.getPartnerOrderDateReportList(dto);
 
 
        String[] rowsName = new String[]{"序号","下单日期", "合伙人ID", "合伙人","底价"
                , "合伙人加价",  "会员折扣","优惠券","售后扣合伙人款","总包干费","实际销售扎数"};
        List<Object[]> dataList = new ArrayList<>();
        int sn = 1;
        for (OrderPartnerReportResultVO o : list) {
            Object[] objs = new Object[rowsName.length];
            int a = 0;
            objs[a++] = sn; // 序号
            objs[a++] = o.getOrderDate(); // 下单日期
            objs[a++] = o.getPartnerId(); // 合伙人ID
            objs[a++] = o.getPartnerName(); // 合伙人
            objs[a++] = o.getOrderPartnerPriceAmount(); // 合伙人底价
            objs[a++] = o.getOrderMarkupPartnerAmount(); //合伙人加价
            objs[a++] = o.getOrderPriceDiscountAmount();//会员折扣
            objs[a++] = o.getOrderCouponAmountTotal(); // 优惠券
            objs[a++] = o.getOrderFeePartner(); //售后扣合伙人款
            objs[a++] = o.getPartnerTotalFeeAmount(); // 总包干费
            objs[a++] = o.getRealSaleNum(); // 实际销售扎数
            dataList.add(objs);
            sn++;
        }
 
        ExcelExportUtil excelExportUtil = new ExcelExportUtil("合伙人财务报表", rowsName, dataList, response);
        try {
            response.addHeader("filename", URLEncoder.encode("合伙人财务报表.xls", "UTF-8"));
            response.addHeader("Access-Control-Expose-Headers", "filename");
            excelExportUtil.export();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
 
    @Override
    public Page<OrderSupplierReportResultVO> getOrderSupplierPage(Page page, QuerySupplierDTO dto) {
        SupplierReportCalendarBO supplierReportCalendarBO = new SupplierReportCalendarBO();
        if (StringUtils.isEmpty(dto.getStartDate()) || StringUtils.isEmpty(dto.getEndDate())) {
            throw new ValidationException("日期不能为空");
        }
        supplierReportCalendarBO.setSupplierId(dto.getSupplierId());
        supplierReportCalendarBO.setStartDate(dto.getStartDate());
        supplierReportCalendarBO.setEndDate(dto.getEndDate());
        List<OrderSupplierReportResultVO> list = orderReportMapper.getOrderSupplierPageReport(supplierReportCalendarBO, page);
        page.setRecords(list);
        return page;
    }
 
    @Override
    public OrderSupplierReportResultVO getOrderSupplierClout(QuerySupplierDTO dto) {
        SupplierReportCalendarBO supplierReportCalendarBO = new SupplierReportCalendarBO();
        if (StringUtils.isEmpty(dto.getStartDate()) || StringUtils.isEmpty(dto.getEndDate())) {
            throw new ValidationException("日期不能为空");
        }
        supplierReportCalendarBO.setSupplierId(dto.getSupplierId());
        supplierReportCalendarBO.setStartDate(dto.getStartDate());
        supplierReportCalendarBO.setEndDate(dto.getEndDate());
        OrderSupplierReportResultVO orderSupplierCountReport = orderReportMapper.getOrderSupplierCountReport(supplierReportCalendarBO);
        return orderSupplierCountReport;
    }
 
 
    @Override
    public void exportSupplierList(HttpServletResponse response, QuerySupplierDTO dto) {
        SupplierReportCalendarBO supplierReportCalendarBO = new SupplierReportCalendarBO();
        if (StringUtils.isEmpty(dto.getStartDate()) || StringUtils.isEmpty(dto.getEndDate())) {
            throw new ValidationException("日期不能为空");
        }
        supplierReportCalendarBO.setSupplierId(dto.getSupplierId());
        supplierReportCalendarBO.setStartDate(dto.getStartDate());
        supplierReportCalendarBO.setEndDate(dto.getEndDate());
        List<OrderSupplierReportResultVO> list = orderReportMapper.getOrderSupplierReport(supplierReportCalendarBO);
 
        String[] rowsName = new String[]{"序号","日期", "供应商ID", "供应商","花农底价"
                , "降级扣款",  "缺货扣款(缺货+补货)","售后扣花农款","售后扣合伙人款","实际销售扎数","结算费用","订单状态"};
        List<Object[]> dataList = new ArrayList<>();
        int sn = 1;
        for (OrderSupplierReportResultVO o : list) {
            Object[] objs = new Object[rowsName.length];
            int a = 0;
            objs[a++] = sn; // 序号
            objs[a++] = format(o.getOrderDate(), "yyyy-MM-dd"); // 日期
            objs[a++] = o.getSupplierId(); // 供应商ID
            objs[a++] = o.getSupplierName(); // 供应商
            objs[a++] = o.getOrderSupplierPriceAmount(); // 花农底价
            objs[a++] = o.getOrderCheckFee(); //降级扣款
            objs[a++] = o.getOrderLackFeeSupplier();//缺货扣款
            objs[a++] = o.getSalesFeeSupplier(); // 售后扣花农款
            objs[a++] = o.getRealSaleNum(); // 实际销售扎数
            objs[a++] = o.getProfitFeeAmount(); // 结算费用
            objs[a++] = o.getSettleStatus(); // 订单状态
            dataList.add(objs);
            sn++;
        }
 
        ExcelExportUtil excelExportUtil = new ExcelExportUtil("花农结算报表", rowsName, dataList, response);
        try {
            response.addHeader("filename", URLEncoder.encode("花农结算报表.xls", "UTF-8"));
            response.addHeader("Access-Control-Expose-Headers", "filename");
            excelExportUtil.export();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
    // 计算 startDate 的前一天 17:00:00
    public static LocalDateTime calculateStartTime(LocalDateTime startDateTime) {
        // 获取 LocalDate 部分
        LocalDateTime previousDay = startDateTime.minusDays(1).with(LocalTime.of(17, 0, 0));
        return previousDay;
    }
 
    // 计算 endDate 当天的 17:00:00
    public static LocalDateTime calculateEndTime(LocalDateTime endDateTime) {
        // 将时间部分固定为 17:00:00
        LocalDateTime endOfDay = endDateTime.with(LocalTime.of(17, 0, 0));
        return endOfDay;
    }
}