Your Name
2023-03-11 f1eb8b84e943ea9c3e56572855c5f63bbe9273d8
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
package com.java110.common.charge;
 
import com.java110.common.smartMeter.ISmartMeterFactoryAdapt;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.account.AccountDto;
import com.java110.dto.chargeMachine.ChargeMachineDto;
import com.java110.dto.chargeMachineFactory.ChargeMachineFactoryDto;
import com.java110.dto.chargeMachineOrder.ChargeMachineOrderDto;
import com.java110.dto.chargeMachineOrder.NotifyChargeOrderDto;
import com.java110.dto.chargeMachineOrder.NotifyChargePortDto;
import com.java110.dto.chargeMachineOrderAcct.ChargeMachineOrderAcctDto;
import com.java110.dto.chargeMachinePort.ChargeMachinePortDto;
import com.java110.intf.acct.IAccountInnerServiceSMO;
import com.java110.intf.common.*;
import com.java110.po.accountDetail.AccountDetailPo;
import com.java110.po.chargeMachineOrder.ChargeMachineOrderPo;
import com.java110.po.chargeMachineOrderAcct.ChargeMachineOrderAcctPo;
import com.java110.po.chargeMachinePort.ChargeMachinePortPo;
import com.java110.utils.exception.CmdException;
import com.java110.utils.factory.ApplicationContextFactory;
import com.java110.utils.util.Assert;
import com.java110.utils.util.DateUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.util.List;
 
/**
 * 充电核心类
 */
@Service
public class ChargeCoreImpl implements IChargeCore {
 
    @Autowired
    private IAccountInnerServiceSMO accountInnerServiceSMOImpl;
 
    @Autowired
    private IChargeMachineFactoryV1InnerServiceSMO chargeMachineFactoryV1InnerServiceSMOImpl;
 
    @Autowired
    private IChargeMachineOrderV1InnerServiceSMO chargeMachineOrderV1InnerServiceSMOImpl;
 
    @Autowired
    private IChargeMachineV1InnerServiceSMO chargeMachineV1InnerServiceSMOImpl;
 
    @Autowired
    private IChargeMachinePortV1InnerServiceSMO chargeMachinePortV1InnerServiceSMOImpl;
 
 
    @Autowired
    private IChargeMachineOrderAcctV1InnerServiceSMO chargeMachineOrderAcctV1InnerServiceSMOImpl;
 
    @Override
    public ResultVo startCharge(ChargeMachineDto chargeMachineDto, ChargeMachinePortDto chargeMachinePortDto, String chargeType, double duration, String orderId) {
 
        ChargeMachineFactoryDto chargeMachineFactoryDto = new ChargeMachineFactoryDto();
        chargeMachineFactoryDto.setFactoryId(chargeMachineDto.getImplBean());
        List<ChargeMachineFactoryDto> chargeMachineFactoryDtos = chargeMachineFactoryV1InnerServiceSMOImpl.queryChargeMachineFactorys(chargeMachineFactoryDto);
 
        Assert.listOnlyOne(chargeMachineFactoryDtos, "充电桩厂家不存在");
 
        IChargeFactoryAdapt chargeFactoryAdapt = ApplicationContextFactory.getBean(chargeMachineFactoryDtos.get(0).getBeanImpl(), IChargeFactoryAdapt.class);
        if (chargeFactoryAdapt == null) {
            throw new CmdException("厂家接口未实现");
        }
 
        chargeMachinePortDto = chargeFactoryAdapt.getChargePortState(chargeMachineDto, chargeMachinePortDto);
 
        if (!ChargeMachinePortDto.STATE_FREE.equals(chargeMachinePortDto.getState())) {
            throw new IllegalArgumentException("充电插槽不是空闲状态");
        }
 
        return chargeFactoryAdapt.startCharge(chargeMachineDto, chargeMachinePortDto, chargeType, duration, orderId);
    }
 
    @Override
    public ResultVo stopCharge(ChargeMachineDto chargeMachineDto, ChargeMachinePortDto chargeMachinePortDto) {
        ChargeMachineFactoryDto chargeMachineFactoryDto = new ChargeMachineFactoryDto();
        chargeMachineFactoryDto.setFactoryId(chargeMachineDto.getImplBean());
        List<ChargeMachineFactoryDto> chargeMachineFactoryDtos = chargeMachineFactoryV1InnerServiceSMOImpl.queryChargeMachineFactorys(chargeMachineFactoryDto);
 
        Assert.listOnlyOne(chargeMachineFactoryDtos, "充电桩厂家不存在");
 
        IChargeFactoryAdapt chargeFactoryAdapt = ApplicationContextFactory.getBean(chargeMachineFactoryDtos.get(0).getBeanImpl(), IChargeFactoryAdapt.class);
        if (chargeFactoryAdapt == null) {
            throw new CmdException("厂家接口未实现");
        }
 
        return chargeFactoryAdapt.stopCharge(chargeMachineDto, chargeMachinePortDto);
    }
 
    @Override
    public ChargeMachinePortDto getChargePortState(ChargeMachineDto chargeMachineDto, ChargeMachinePortDto chargeMachinePortDto) {
        ChargeMachineFactoryDto chargeMachineFactoryDto = new ChargeMachineFactoryDto();
        chargeMachineFactoryDto.setFactoryId(chargeMachineDto.getImplBean());
        List<ChargeMachineFactoryDto> chargeMachineFactoryDtos = chargeMachineFactoryV1InnerServiceSMOImpl.queryChargeMachineFactorys(chargeMachineFactoryDto);
 
        Assert.listOnlyOne(chargeMachineFactoryDtos, "充电桩厂家不存在");
 
        IChargeFactoryAdapt chargeFactoryAdapt = ApplicationContextFactory.getBean(chargeMachineFactoryDtos.get(0).getBeanImpl(), IChargeFactoryAdapt.class);
        if (chargeFactoryAdapt == null) {
            throw new CmdException("厂家接口未实现");
        }
 
        return chargeFactoryAdapt.getChargePortState(chargeMachineDto, chargeMachinePortDto);
    }
 
    @Override
    public ResponseEntity<String> finishCharge(NotifyChargeOrderDto notifyChargeOrderDto) {
 
        // todo 生成 充电订单
        ChargeMachineOrderPo chargeMachineOrderPo = new ChargeMachineOrderPo();
        chargeMachineOrderPo.setEndTime(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_A));
        chargeMachineOrderPo.setState(ChargeMachineOrderDto.STATE_FINISH);
        chargeMachineOrderPo.setOrderId(notifyChargeOrderDto.getOrderId());
        chargeMachineOrderV1InnerServiceSMOImpl.updateChargeMachineOrder(chargeMachineOrderPo);
 
        ChargeMachineDto chargeMachineDto = new ChargeMachineDto();
        chargeMachineDto.setMachineCode(notifyChargeOrderDto.getMachineCode());
        List<ChargeMachineDto> chargeMachineDtos = chargeMachineV1InnerServiceSMOImpl.queryChargeMachines(chargeMachineDto);
 
        Assert.listOnlyOne(chargeMachineDtos, "充电桩 不存在");
 
        // todo 插槽是否空闲
 
        ChargeMachinePortDto chargeMachinePortDto = new ChargeMachinePortDto();
        chargeMachinePortDto.setMachineId(chargeMachineDtos.get(0).getMachineId());
        chargeMachinePortDto.setPortCode(notifyChargeOrderDto.getPortCode());
        chargeMachinePortDto.setState(ChargeMachinePortDto.STATE_FREE);
        List<ChargeMachinePortDto> chargeMachinePortDtos = chargeMachinePortV1InnerServiceSMOImpl.queryChargeMachinePorts(chargeMachinePortDto);
        Assert.listOnlyOne(chargeMachinePortDtos, "插槽忙线");
 
        ChargeMachinePortPo chargeMachinePortPo = new ChargeMachinePortPo();
        chargeMachinePortPo.setPortId(chargeMachinePortDtos.get(0).getPortId());
        chargeMachinePortPo.setState(ChargeMachinePortDto.STATE_FREE);
        chargeMachinePortV1InnerServiceSMOImpl.updateChargeMachinePort(chargeMachinePortPo);
 
        return new ResponseEntity<>("{\n" +
                "\"code\" : 200,\n" +
                "\"msg\" : \"success\"\n" +
                "}", HttpStatus.OK);
    }
 
    @Override
    public ResponseEntity<String> heartbeat(NotifyChargeOrderDto notifyChargeOrderDto) {
        return new ResponseEntity<>("{\n" +
                "\"code\" : 200,\n" +
                "\"msg\" : \"success\"\n" +
                "}", HttpStatus.OK);
    }
 
    @Override
    public ResponseEntity<String> chargeHeartBeat(NotifyChargeOrderDto notifyChargeOrderDto) {
        ChargeMachineDto chargeMachineDto = new ChargeMachineDto();
        chargeMachineDto.setMachineCode(notifyChargeOrderDto.getMachineCode());
        List<ChargeMachineDto> chargeMachineDtos = chargeMachineV1InnerServiceSMOImpl.queryChargeMachines(chargeMachineDto);
 
        Assert.listOnlyOne(chargeMachineDtos, "充电桩 不存在");
 
        ChargeMachineFactoryDto chargeMachineFactoryDto = new ChargeMachineFactoryDto();
        chargeMachineFactoryDto.setFactoryId(chargeMachineDtos.get(0).getImplBean());
        List<ChargeMachineFactoryDto> chargeMachineFactoryDtos = chargeMachineFactoryV1InnerServiceSMOImpl.queryChargeMachineFactorys(chargeMachineFactoryDto);
 
        Assert.listOnlyOne(chargeMachineFactoryDtos, "充电桩厂家不存在");
 
        IChargeFactoryAdapt chargeFactoryAdapt = ApplicationContextFactory.getBean(chargeMachineFactoryDtos.get(0).getBeanImpl(), IChargeFactoryAdapt.class);
        if (chargeFactoryAdapt == null) {
            throw new CmdException("厂家接口未实现");
        }
 
        List<NotifyChargePortDto> portDtos = chargeFactoryAdapt.getChargeHeartBeatParam(notifyChargeOrderDto);
 
        for (NotifyChargePortDto notifyChargePortDto : portDtos) {
            doDealChargePort(notifyChargePortDto, notifyChargeOrderDto, chargeMachineDtos.get(0), chargeFactoryAdapt);
        }
 
        return new ResponseEntity<>("{\n" +
                "\"code\" : 200,\n" +
                "\"msg\" : \"success\"\n" +
                "}", HttpStatus.OK);
    }
 
    /**
     * 处理充电 扣款问题
     *
     * @param notifyChargePortDto
     * @param notifyChargeOrderDto
     */
    private void doDealChargePort(NotifyChargePortDto notifyChargePortDto,
                                  NotifyChargeOrderDto notifyChargeOrderDto,
                                  ChargeMachineDto chargeMachineDto,
                                  IChargeFactoryAdapt chargeFactoryAdapt) {
 
        String preEnergy = "0";
        ChargeMachineOrderDto chargeMachineOrderDto = new ChargeMachineOrderDto();
        chargeMachineOrderDto.setOrderId(notifyChargePortDto.getOrderId());
        List<ChargeMachineOrderDto> orderDtos = chargeMachineOrderV1InnerServiceSMOImpl.queryChargeMachineOrders(chargeMachineOrderDto);
 
        Assert.listOnlyOne(orderDtos, "订单不存在");
        String state = ChargeMachineOrderDto.STATE_DOING;
        if (preEnergy.equals(notifyChargePortDto.getEnergy())) {
            state = ChargeMachineOrderDto.STATE_FINISH;
        }
        updateOrderState(notifyChargePortDto, state);
 
        //todo 主动调用关闭
        if (preEnergy.equals(notifyChargePortDto.getEnergy())) {
            customStopCharge(notifyChargePortDto, chargeMachineDto, chargeFactoryAdapt, orderDtos);
        }
 
        // todo 1.0 查询上报时间是否已经 扣款,如果扣款过,那么更新 充电电量 后返回
 
        String powerTime = DateUtil.getFormatTimeString(notifyChargePortDto.getPowerTime(), DateUtil.DATE_FORMATE_STRING_A);
 
        ChargeMachineOrderAcctDto chargeMachineOrderAcctDto = new ChargeMachineOrderAcctDto();
        chargeMachineOrderAcctDto.setOrderId(notifyChargeOrderDto.getOrderId());
        chargeMachineOrderAcctDto.setPowerTime(powerTime);
        List<ChargeMachineOrderAcctDto> chargeMachineOrderAcctDtos = chargeMachineOrderAcctV1InnerServiceSMOImpl.queryChargeMachineOrderAccts(chargeMachineOrderAcctDto);
 
        if (chargeMachineOrderAcctDtos != null && chargeMachineOrderAcctDtos.size() > 0) {
            return;
        }
 
        // todo 2.0 检查账户是否余额充足,如果余额不足,则 调用停止充电 将充电订单 修改成充电完成,并且修改备注
 
        double price = Double.parseDouble(chargeMachineDto.getDurationPrice());
 
        AccountDto accountDto = new AccountDto();
        accountDto.setAcctId(orderDtos.get(0).getAcctDetailId());
        accountDto.setAcctType(AccountDto.ACCT_TYPE_CASH);
        List<AccountDto> accountDtos = accountInnerServiceSMOImpl.queryAccounts(accountDto);
 
        if (accountDtos == null || accountDtos.size() < 1) {
            throw new CmdException("请先充值,账户金额不足");
        }
 
        //todo 账户金额不足,无法支付小时费用,停止充电
        if (Double.parseDouble(accountDtos.get(0).getAmount()) < price) {
            customStopCharge(notifyChargePortDto, chargeMachineDto, chargeFactoryAdapt, orderDtos);
            updateOrderState(notifyChargePortDto, ChargeMachineOrderDto.STATE_FINISH);
            return;
        }
 
 
        // todo 3.0 账户扣款
        AccountDetailPo accountDetailPo = new AccountDetailPo();
        accountDetailPo.setAcctId(accountDtos.get(0).getAcctId());
        accountDetailPo.setObjId(accountDtos.get(0).getObjId());
        accountDetailPo.setObjType(accountDtos.get(0).getObjType());
        accountDetailPo.setAmount(price + "");
        accountDetailPo.setDetailId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_detailId));
        accountInnerServiceSMOImpl.withholdAccount(accountDetailPo);
 
        ChargeMachineOrderAcctPo chargeMachineOrderAcctPo = new ChargeMachineOrderAcctPo();
        chargeMachineOrderAcctPo.setAcctDetailId(accountDetailPo.getDetailId());
        chargeMachineOrderAcctPo.setAmount(price + "");
        chargeMachineOrderAcctPo.setCmoaId(GenerateCodeFactory.getGeneratorId("11"));
        chargeMachineOrderAcctPo.setOrderId(notifyChargePortDto.getOrderId());
        chargeMachineOrderAcctPo.setAcctId(accountDtos.get(0).getAcctId());
        chargeMachineOrderAcctPo.setStartTime(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_A));
        chargeMachineOrderAcctPo.setEndTime(DateUtil.getAddHoursStringA(DateUtil.getCurrentDate(), 1));
        chargeMachineOrderAcctPo.setRemark("一小时定时扣款");
        chargeMachineOrderAcctPo.setCommunityId(orderDtos.get(0).getCommunityId());
        chargeMachineOrderAcctPo.setEnergy(notifyChargePortDto.getEnergy());
 
        chargeMachineOrderAcctV1InnerServiceSMOImpl.saveChargeMachineOrderAcct(chargeMachineOrderAcctPo);
 
    }
 
    private void updateOrderState(NotifyChargePortDto notifyChargePortDto, String state) {
        ChargeMachineOrderPo chargeMachineOrderPo = new ChargeMachineOrderPo();
        chargeMachineOrderPo.setOrderId(notifyChargePortDto.getOrderId());
        chargeMachineOrderPo.setEnergy(notifyChargePortDto.getEnergy());
        chargeMachineOrderPo.setState(state);
 
        chargeMachineOrderV1InnerServiceSMOImpl.updateChargeMachineOrder(chargeMachineOrderPo);
    }
 
    private void customStopCharge(NotifyChargePortDto notifyChargePortDto, ChargeMachineDto chargeMachineDto, IChargeFactoryAdapt chargeFactoryAdapt, List<ChargeMachineOrderDto> orderDtos) {
        ChargeMachinePortDto chargeMachinePortDto = new ChargeMachinePortDto();
        chargeMachinePortDto.setMachineId(orderDtos.get(0).getMachineId());
        chargeMachinePortDto.setPortCode(notifyChargePortDto.getPortCode());
        List<ChargeMachinePortDto> chargeMachinePortDtos = chargeMachinePortV1InnerServiceSMOImpl.queryChargeMachinePorts(chargeMachinePortDto);
        if (chargeMachinePortDtos != null && chargeMachinePortDtos.size() > 0) {
            chargeFactoryAdapt.stopCharge(chargeMachineDto, chargeMachinePortDtos.get(0));
        }
    }
}