无关风月
2024-08-16 e05fe75a9f48455692aef753bbfbb39542349017
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
package com.ruoyi.chargingPile.controller;
 
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.chargingPile.api.dto.TAccountingStrategyDTO;
import com.ruoyi.chargingPile.api.model.*;
import com.ruoyi.chargingPile.api.query.TAccountingStrategyQuery;
import com.ruoyi.chargingPile.api.vo.TAccountingStrategyDetailVO;
import com.ruoyi.chargingPile.api.vo.TAccountingStrategyVO;
import com.ruoyi.chargingPile.service.ISiteService;
import com.ruoyi.chargingPile.service.TAccountingStrategyDetailService;
import com.ruoyi.chargingPile.service.TAccountingStrategyService;
import com.ruoyi.chargingPile.service.TChargingPileService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.PageInfo;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.log.enums.OperatorType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.time.LocalTime;
import java.util.List;
 
/**
 * <p>
 * 计费策略 前端控制器
 * </p>
 *
 * @author xiaochen
 * @since 2024-08-06
 */
@Api(tags = "计费策略")
@RestController
@RequestMapping("/t-accounting-strategy")
public class TAccountingStrategyController {
 
    private final TAccountingStrategyService accountingStrategyService;
    private final TAccountingStrategyDetailService accountingStrategyDetailService;
    @Autowired
    private TChargingPileService chargingPileService;
    @Autowired
    private ISiteService siteService;
 
    @Resource
    private TokenService tokenService;
 
    @Autowired
    public TAccountingStrategyController(TAccountingStrategyService accountingStrategyService, TAccountingStrategyDetailService accountingStrategyDetailService) {
        this.accountingStrategyService = accountingStrategyService;
        this.accountingStrategyDetailService = accountingStrategyDetailService;
    }
    
    /**
     * 查询计费策略列表
     */
    @ApiOperation(tags = {"后台-计费策略"},value = "查询计费策略分页列表")
    @PostMapping(value = "/pageList")
    public AjaxResult<PageInfo<TAccountingStrategyVO>> pageList(@RequestBody TAccountingStrategyQuery query) {
        return AjaxResult.ok(accountingStrategyService.pageList(query));
    }
 
    /**
     * 查询计费策略明细列表
     */
    @ApiOperation(tags = {"后台-计费策略", "管理后台-站点管理", "管理后台-充电桩信息"},value = "查询计费策略明细列表")
    @GetMapping(value = "/queryAccountingStrategyDetailByStrategyId")
    public AjaxResult<List<TAccountingStrategyDetailVO>> queryAccountingStrategyDetailByStrategyId(@RequestParam("strategyId") Integer strategyId) {
        List<TAccountingStrategyDetailVO> list = accountingStrategyDetailService.queryAccountingStrategyDetailByStrategyId(strategyId);
        list.forEach(detail -> {
            detail.setTotalPrice(detail.getElectrovalence().add(detail.getElectrovalence()));
        });
        return AjaxResult.ok(list);
    }
 
    /**
     * 添加计费策略管理
     */
    @Log(title = "添加计费策略", businessType = BusinessType.INSERT,operatorType = OperatorType.MANAGE)
    @ApiOperation(tags = {"后台-计费策略", "管理后台-站点管理"},value = "添加计费策略")
    @PostMapping(value = "/add")
    public AjaxResult<Integer> add(@RequestBody TAccountingStrategyDTO dto) {
        accountingStrategyService.save(dto);
        // 添加明细
        dto.getAccountingStrategyDetails().forEach(detail -> detail.setAccountingStrategyId(dto.getId()));
        accountingStrategyDetailService.saveBatch(dto.getAccountingStrategyDetails());
        // TODO 硬件 同步策略到充电桩
        return AjaxResult.ok(dto.getId());
    }
 
    /**
     * 修改计费策略
     */
    @Log(title = "修改计费策略", businessType = BusinessType.UPDATE,operatorType = OperatorType.MANAGE)
    @ApiOperation(tags = {"后台-计费策略", "管理后台-站点管理"},value = "修改计费策略")
    @PostMapping(value = "/update")
    public AjaxResult<Boolean> update(@RequestBody TAccountingStrategyDTO dto) {
        // 删除计费策略明细信息
        accountingStrategyDetailService.remove(Wrappers.lambdaQuery(TAccountingStrategyDetail.class)
                .eq(TAccountingStrategyDetail::getAccountingStrategyId, dto.getId()));
        accountingStrategyService.updateById(dto);
        // 添加明细
        dto.getAccountingStrategyDetails().forEach(detail -> detail.setAccountingStrategyId(dto.getId()));
        // TODO 硬件 同步策略到充电桩
        return AjaxResult.ok(accountingStrategyDetailService.saveBatch(dto.getAccountingStrategyDetails()));
    }
 
    /**
     * 查看计费策略详情
     */
    @ApiOperation(tags = {"后台-计费策略", "管理后台-站点管理"},value = "查看计费策略详情")
    @GetMapping(value = "/getDetailById")
    public AjaxResult<TAccountingStrategy> getDetailById(@RequestParam("id") Integer id) {
        return AjaxResult.ok(accountingStrategyService.getById(id));
    }
 
    /**
     * 删除计费策略
     */
    @Log(title = "删除计费策略", businessType = BusinessType.DELETE,operatorType = OperatorType.MANAGE)
    @ApiOperation(tags = {"后台-计费策略"},value = "删除计费策略")
    @DeleteMapping(value = "/deleteById")
    public AjaxResult<Boolean> deleteById(@RequestParam("id") Integer id) {
        // 刪除计费策略明细信息
        accountingStrategyDetailService.remove(Wrappers.lambdaQuery(TAccountingStrategyDetail.class)
                .eq(TAccountingStrategyDetail::getAccountingStrategyId, id));
        return AjaxResult.ok(accountingStrategyService.removeById(id));
    }
 
    /**
     * 批量删除计费策略
     */
    @Log(title = "批量删除计费策略", businessType = BusinessType.DELETE,operatorType = OperatorType.MANAGE)
    @ApiOperation(tags = {"后台-计费策略"},value = "批量删除计费策略")
    @DeleteMapping(value = "/deleteByIds")
    public AjaxResult<Boolean> deleteByIds(@RequestBody List<Integer> ids) {
        // 刪除计费策略明细信息
        accountingStrategyDetailService.remove(Wrappers.lambdaQuery(TAccountingStrategyDetail.class)
                .in(TAccountingStrategyDetail::getAccountingStrategyId, ids));
        return AjaxResult.ok(accountingStrategyService.removeByIds(ids));
    }
    
    
    @ApiOperation(tags = {"管理后台-站点管理"},value = "获取平台添加的计费策略")
    @DeleteMapping(value = "/getPlatformAccountingStrategy")
    public AjaxResult<List<TAccountingStrategyVO>> getPlatformAccountingStrategy(){
        List<TAccountingStrategyVO> list = accountingStrategyService.getPlatformAccountingStrategy();
        return AjaxResult.success(list);
    }
 
 
 
    @ApiOperation(tags = {"后台-申请表单-计费模板审核"},value = "查询计费审核分页列表")
    @PostMapping(value = "/auth/pageList")
    public AjaxResult authPageList(@RequestBody TAccountingStrategyQuery query) {
        Long userid = tokenService.getLoginUser().getUserid();
        Page<TAccountingStrategy> page = accountingStrategyService.lambdaQuery()
                .in(TAccountingStrategy::getFirstUserId, userid)
                .or().in(TAccountingStrategy::getTwoUserId, userid)
                .page(Page.of(query.getPageCurr(), query.getPageSize()));
 
        List<TAccountingStrategy> records = page.getRecords();
        for (TAccountingStrategy record : records) {
            if (record.getAuditStatus()==2){
                if (record.getFirstUserId().equals(userid)){
                    record.setAuditStatus(3);
                }
 
            }
 
        }
 
 
        return AjaxResult.ok(accountingStrategyService.pageList(query));
    }
 
    /**
     * 小程序远程调用 根据会员折扣、预付金额 计算服务费
     * @return
     */
    @PostMapping(value = "/getServiceMoney")
    public R<BigDecimal> getServiceMoney(@RequestParam("param") String param){
        // 桩编号 + 是否是会员 + 内部会员折扣金额 + 普通会员最高折扣金额 + 预付金额
        String[] split = param.split("-");
        // 根据桩编号 查询电站
        TChargingPile one = chargingPileService.lambdaQuery().eq(TChargingPile::getNumber, split[0]).one();
        Site byId = siteService.getById(one.getSiteId());
        // 计费策略
        TAccountingStrategy one1 = accountingStrategyService.lambdaQuery().eq(TAccountingStrategy::getSiteId, byId.getId())
                .eq(TAccountingStrategy::getAuditStatus, 3).one();
        // 会员折扣
        BigDecimal discount = one1.getDiscount();
        List<TAccountingStrategyDetail> list = accountingStrategyDetailService.lambdaQuery().eq(TAccountingStrategyDetail::getAccountingStrategyId, one1.getId())
                .list();
        // 当前时间属于哪个计费策略
        LocalTime now = LocalTime.now();
        // 电价
        BigDecimal electronic = new BigDecimal(BigInteger.ZERO);
        // 最终服务费
        BigDecimal serviceMoney = new BigDecimal(BigInteger.ZERO);
        // 预付金额
        BigDecimal beforeMoney = new BigDecimal(split[4]);
        // 普通会员最高折扣金额
        BigDecimal discountMoney = new BigDecimal(split[3]);
        // 内部会员折扣
        BigDecimal discountInner = new BigDecimal(split[2]);
        // 最终服务费
        BigDecimal res = new BigDecimal(BigInteger.ZERO);
        for (TAccountingStrategyDetail tAccountingStrategyDetail : list) {
            if (now.isAfter(LocalTime.parse(tAccountingStrategyDetail.getStartTime())) && now.isBefore(LocalTime.parse(tAccountingStrategyDetail.getEndTime()))){
                electronic = tAccountingStrategyDetail.getElectrovalence();
                // 非会员下的服务费
                BigDecimal multiply = beforeMoney.multiply(tAccountingStrategyDetail.getServiceCharge()).divide(electronic, 2, RoundingMode.HALF_UP);
                if (byId.getBusinessCategory() == 1){
                    // 直营才享受会员折扣
                    // 根据电价和预付金额 是否有会员折扣 计算最终服务费
                    if (split[1].equals("1")){
                        // 抵扣金额
                        BigDecimal multiply1 = multiply.multiply(discount);
                        if (multiply1.compareTo(discountMoney)>0){
                            multiply1 = discountMoney;
                        }
                        // 普通最终服务费
                        res = multiply.subtract(multiply1);
                    }
                    if (split[1].equals("2")){
                        // 内部会员折扣 最终服务费
                        res = discountInner.multiply(multiply);
                    }
                }else{
                    // 非直营
                    res = multiply;
                }
                break;
            }
        }
 
 
        return R.ok(res);
    }
}