liurunyu
6 天以前 f1c648e7ac1b4c437e7f0c9a69130cb443ca46fd
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
package com.dy.pipIrrTerminal.general;
 
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.dy.pipIrrGlobal.daoSe.SeGeneralMapper;
import com.dy.pipIrrGlobal.daoSe.SePaymentMethodMapper;
import com.dy.pipIrrGlobal.pojoSe.SePaymentMethod;
import com.dy.pipIrrGlobal.voSe.VoTransactionStatistics;
import com.dy.pipIrrTerminal.general.qo.QoSummary;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.utils.PojoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
 
/**
 * @author ZhuBaoMin
 * @date 2025-06-25 11:22
 * @LastEditTime 2025-06-25 11:22
 * @Description
 */
 
@Slf4j
@Service
public class GeneralSv {
    @Autowired
    private SeGeneralMapper seGeneralMapper;
 
    @Autowired
    private SePaymentMethodMapper sePaymentMethodMapper;
 
    /**
     * 充值机用获取交易统计记录
     * @param vo
     * @return
     * @throws ParseException
     */
    public Map getSummaries(QoSummary vo) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
 
        Integer pageSize = vo.getPageSize();
        Integer pageCurr = vo.getPageCurr();
        // 起始索引位置
        Integer startIndex = (pageCurr - 1) * pageSize;
 
        // 获取各支付方式ID
        Long paymentId_cash = 0L;
        Long paymentId_pos = 0L;
        Long paymentId_transfer = 0L;
        List<SePaymentMethod> list_payment = sePaymentMethodMapper.getPaymentMethods();
        for (int i = 0; i < list_payment.size(); i++) {
            SePaymentMethod paymentMethod = list_payment.get(i);
            if(paymentMethod.getName().equals("现金")) {
                paymentId_cash = paymentMethod.getId();
            }
            if(paymentMethod.getName().equals("POS机")) {
                paymentId_pos = paymentMethod.getId();
            }
            if(paymentMethod.getName().equals("银行转账")) {
                paymentId_transfer = paymentMethod.getId();
            }
        }
 
        // 接收传入参数:交易起止日期、收银员ID
        JSONArray array_paymentSums = new JSONArray();
        Date timeStart = dateFormat.parse(vo.getTimeStart());
        Date timeStop = dateFormat.parse(vo.getTimeStop());
        Long cashierId = vo.cashierId;
 
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(timeStart);
        while (calendar.getTime().before(timeStop) || calendar.getTime().equals(timeStop)) {
            String tradeDate = dateFormat.format(calendar.getTime()) ;
            Float receivedCash = Optional.ofNullable(seGeneralMapper.getPaymentSums(tradeDate, cashierId, paymentId_cash)).orElse(0f);
            Float receivedPos = Optional.ofNullable(seGeneralMapper.getPaymentSums(tradeDate, cashierId,paymentId_pos)).orElse(0f);
            Float receivedTransfer = Optional.ofNullable(seGeneralMapper.getPaymentSums(tradeDate, cashierId, paymentId_transfer)).orElse(0f);
 
            JSONObject job = new JSONObject();
            job.put("tradeDate", tradeDate);
            job.put("receivedCash", receivedCash);
            job.put("receivedPos", receivedPos);
            job.put("receivedTransfer", receivedTransfer);
            array_paymentSums.add(job);
            calendar.add(Calendar.DAY_OF_MONTH, 1);
        }
 
        // 生成查询参数
        Map<String, Object> params = (Map<String, Object>) PojoUtils.generalize(vo) ;
 
        /**
         * 根据收银员编号、交易日期查询汇总记录
         * 遍历汇总记录查询结果,计算笔数合计、实收金额合计、赠送金额合计、返还金额合计
         */
        List<VoTransactionStatistics> list = Optional.ofNullable(seGeneralMapper.getSummaries(params)).orElse(new ArrayList<>());
        if(list.size() == 0) {
            return new HashMap();
        }
 
        Integer totalCount = 0;
        Float totalReceived = 0f;
        Float totalGift = 0f;
        Float totalRefund = 0f;
        JSONArray array= JSONArray.parseArray(JSON.toJSONString(list));
        for(int i = 0; i < array.size(); i++) {
            JSONObject job_summary = array.getJSONObject(i);
            totalCount = totalCount +  Optional.ofNullable(job_summary.getInteger("count")).orElse(0);
            totalReceived = totalReceived + Optional.ofNullable(job_summary.getFloat("received")).orElse(0f);
            totalGift = totalGift + Optional.ofNullable(job_summary.getFloat("gift")).orElse(0f);
            totalRefund = totalRefund + Optional.ofNullable(job_summary.getFloat("refundAmount")).orElse(0f);
        }
 
        Map map_result = new HashMap();
        map_result.put("totalCount", totalCount);
        map_result.put("totalReceived", totalReceived);
        map_result.put("totalGift", totalGift);
        map_result.put("totalRefund", totalRefund);
 
        // 分页格式组装记录列表
        Map map_record = new HashMap();
        map_record.put("itemTotal", list.size());
        map_record.put("pageCurr", pageCurr);
        map_record.put("pageSize", pageSize);
        map_record.put("pageTotal", (int)Math.ceil((double)list.size() / pageSize));
        JSONArray array_record = (JSONArray) JSON.toJSON(list);
        JSONArray array_record_page = (JSONArray)JSON.toJSON(array_record.subList(startIndex, Math.min(startIndex + pageSize, array_record.size())));
        map_record.put("list", array_record_page);
 
        // 分页格式组装支付方式列表
        Map map_paymentSums = new HashMap();
        map_paymentSums.put("itemTotal", array_paymentSums.size());
        map_paymentSums.put("pageCurr", pageCurr);
        map_paymentSums.put("pageSize", pageSize);
        map_paymentSums.put("pageTotal", (int)Math.ceil((double)array_paymentSums.size() / pageSize));
        JSONArray array_paymentSums_page = (JSONArray) JSON.toJSON(array_paymentSums.subList(startIndex, Math.min(startIndex + pageSize, array_paymentSums.size())));
        map_paymentSums.put("list", array_paymentSums_page);
 
 
        map_result.put("records", map_record);
        map_result.put("paymentSums", map_paymentSums);
 
        return map_result;
    }
}