2021与蓝度共同重构项目,服务端
zhanzhiqin
2022-10-20 4e76b9737e036c18e8a9e840dc443615ffcec348
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
package com.sandu.ximon.admin.service;
 
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sandu.common.execption.BusinessException;
import com.sandu.common.service.impl.BaseServiceImpl;
import com.sandu.common.util.SpringContextHolder;
import com.sandu.ximon.admin.param.C3mChargingChargeParam;
import com.sandu.ximon.admin.utils.StoreOperationRecordsUtils;
import com.sandu.ximon.dao.domain.C3mCharging;
import com.sandu.ximon.dao.domain.C3mChargingCharge;
import com.sandu.ximon.dao.domain.PoleBinding;
import com.sandu.ximon.dao.mapper.C3mChargingChargeMapper;
import com.sandu.ximon.dao.mapper.C3mChargingMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.time.LocalTime;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * @Author liuhaonan
 * @Date 2022/3/10 13:58
 * @Version 1.0
 * c3充电桩费率相关
 */
@Service
@AllArgsConstructor
public class C3mChargingChargeService extends BaseServiceImpl<C3mChargingChargeMapper, C3mChargingCharge> {
    private final C3mChargingChargeMapper c3mChargingChargeMapper;
    private final PoleBindingService bindingService;
    private final C3mChargingMapper chargingMapper;
 
    /**
     * 修改费率
     *
     * @param chargeEntities
     * @param
     * @return
     */
    public boolean updateCharge(List<C3mChargingCharge> chargeEntities) {
        //  删除原本存在的费率
        Integer c3Id = chargeEntities.get(0).getC3Id();
 
        if (c3Id == null) {
            throw new BusinessException("c3Id不能为空");
        }
        c3mChargingChargeMapper.deleteCharge(c3Id);
        Long timestamp = new Date().getTime();
 
        C3ChargingService chargingService = SpringContextHolder.getBean(C3ChargingService.class);
        C3mCharging byId = chargingService.getById(c3Id);
        if (byId == null) {
            throw new BusinessException("c3Id不存在");
        }
        String s = chargingService.setRate(byId.getC3Mac(), chargeEntities,false);
        if (!"操作成功".equals(s)) {
            throw new BusinessException("设置失败,失败原因: " + s);
        }
        //  将费率添加至数据库
        for (C3mChargingCharge c3m : chargeEntities) {
            c3m.setTimestamp(timestamp);
            save(c3m);
        }
 
        /**
         * c3充电桩费率更改 日志记录开始
         */
        String content = "c3充电桩费率更改:" + JSON.toJSONString(chargeEntities);
        StoreOperationRecordsUtils.storeOperationData(null, null, "c3充电桩费率更改", content);
        /**
         * c3充电桩费率更改 日志记录结束
         */
        return true;
    }
 
    /**
     * 插入费率
     *
     * @param c3
     * @return
     */
    public boolean initCharge(C3mChargingChargeParam c3) {
        C3mChargingCharge charge = new C3mChargingCharge();
        BeanUtil.copyProperties(c3, charge);
        charge.setTimestamp(new Date().getTime());
//        c3mChargingChargeMapper.insertCharge(charge);
 
 
        return save(charge);
    }
 
    /**
     * 自动插入默认费率
     *
     * @param c3Id
     */
    public void initCharge(Integer c3Id) {
        List<C3mChargingCharge> chargeList = getChargeByC3Id(c3Id);
        if (chargeList.size() == 0) {
            //  不存在费率时,生成单个费率并插入数据库
            C3mChargingCharge c3mChargeEntity = new C3mChargingCharge();/*C3mChargingCharge.getInitInstance(c3Id);*/
            c3mChargeEntity.setC3Id(c3Id);
            c3mChargeEntity.setHour(0);
            c3mChargeEntity.setMin(0);
            c3mChargeEntity.setCharge(8.8);
            c3mChargeEntity.setTimestamp(new Date().getTime());
            c3mChargingChargeMapper.insertCharge(c3mChargeEntity);
        }
    }
 
 
    public List<C3mChargingCharge> getChargeByC3Id(Integer c3Id) {
        //  查找数据库所有费率
        List<C3mChargingCharge> list = c3mChargingChargeMapper.getChargeByC3Id(c3Id);
        //  判断list为单条直接返回
        if (list.size() < 2) {
            return list;
        }
        //  将时分进行排序
        Collections.sort(list, new Comparator<C3mChargingCharge>() {
            @Override
            public int compare(C3mChargingCharge o1, C3mChargingCharge o2) {
                int i = o2.getHour().compareTo(o1.getHour());
                if (i == 0) {
                    return o2.getMin().compareTo(o1.getMin());
                }
                return i;
            }
 
            @Override
            public boolean equals(Object obj) {
                return false;
            }
        });
        //  list翻转
        Collections.reverse(list);
        //  时间段字符拼接
        for (int i = 1; i < list.size(); i++) {
            C3mChargingCharge pre = list.get(i - 1);
            C3mChargingCharge now = list.get(i);
            pre.setStrTime(
                    pre.getHour() + "点" + pre.getMin() + "分 到 " +
                            now.getHour() + "点" + now.getMin() + "分"
            );
            if (i == list.size() - 1) {
                pre = now;
                now = list.get(0);
                if (now.getHour() < pre.getHour()) {
                    pre.setStrTime(
                            pre.getHour() + "点" + pre.getMin() + "分 到 " +
                                    "隔日" + now.getHour() + "点" + now.getMin() + "分"
                    );
                } else {
                    pre.setStrTime(
                            pre.getHour() + "点" + pre.getMin() + "分 到 " +
                                    now.getHour() + "点" + now.getMin() + "分"
                    );
                }
            }
        }
        return list;
 
    }
 
    /**
     * 根据灯杆id查找费率
     *
     * @param poleId
     * @return
     */
    public List<C3mChargingCharge> getChargeByPoleId(Long poleId) {
        PoleBinding one = bindingService.getOne(Wrappers.lambdaQuery(PoleBinding.class).eq(PoleBinding::getDeviceType, 2).eq(PoleBinding::getPoleId, poleId));
        if (one == null) {
            throw new BusinessException("未找到绑定关系");
        }
        Long c3Id = chargingMapper.getAllByC3Mac(one.getDeviceCode()).getC3Id();
        return getChargeByC3Id(c3Id.intValue());
    }
 
    public boolean deleteC3mCharging(Integer c3mId) {
        return c3mChargingChargeMapper.deleteCharge(c3mId);
    }
 
    /**
     * 根据c3Id查找费率  并计算花费
     *
     * @param c3Id
     * @return
     */
    public BigDecimal getCostByC3id(Long c3Id, Integer power) {
        BigDecimal chargeDecimal = getchargeDecimalByC3id(c3Id);
        BigDecimal powerDecimal = new BigDecimal(power);
        BigDecimal result = chargeDecimal.multiply(powerDecimal);
        return result;
    }
 
 
    /**
     * 根据c3Id查找费率
     *
     * @param c3Id
     * @return
     */
    public BigDecimal getchargeDecimalByC3id(Long c3Id) {
        List<C3mChargingCharge> list = list(Wrappers.lambdaQuery(C3mChargingCharge.class).eq(C3mChargingCharge::getC3Id, c3Id));
 
        //只有一条费率时,直接返回
        if (list.size() == 1) {
            return BigDecimal.valueOf(list.get(0).getCharge());
        } else {//费率大于一条时候,计算费率
 
            //排序费率(根据时间排序)
            List<C3mChargingCharge> collect =
                    list.stream().sorted(Comparator.comparing(C3mChargingCharge::getHour).thenComparing(C3mChargingCharge::getMin)).collect(Collectors.toList());
            System.out.println("-----    " + collect);
 
            //获取当前时间
            LocalTime localTime = LocalTime.now();
//            LocalTime localTime = LocalTime.of(18, 00);
            System.out.println("time"+localTime);
 
            C3mChargingCharge temp1 = collect.get(collect.size() - 1);
            if (localTime.isAfter(LocalTime.of(temp1.getHour(), temp1.getMin()))) {
                return BigDecimal.valueOf(temp1.getCharge());
            }
 
            C3mChargingCharge temp2= collect.get(0);
            if (localTime.isBefore(LocalTime.of(temp2.getHour(), temp2.getMin()))) {
                return BigDecimal.valueOf(temp1.getCharge());
            }
 
 
            for (int i = 0; i < collect.size(); i++) {
                int x = i;
                int y = i + 1;
                if (y == collect.size()) {
                    y = 0;
                }
                LocalTime startTime = LocalTime.of(collect.get(x).getHour(), collect.get(x).getMin());
                LocalTime endTime = LocalTime.of(collect.get(y).getHour(), collect.get(y).getMin());
 
                //当前时间等于费率时间段的开始时间
                if (localTime.equals(startTime)) {
                    return BigDecimal.valueOf(collect.get(x).getCharge());
                }
 
                //当前时间在费率时间段内
                if (localTime.isAfter(startTime) && localTime.isBefore(endTime)) {
                    return BigDecimal.valueOf(collect.get(x).getCharge());
                }
            }
        }
 
        return BigDecimal.valueOf(8.8);
    }
}