2021与蓝度共同重构项目,服务端
liuhaonan
2022-06-28 3c88ed137df2428dd43a405336cbd79011345efd
ximon-admin/src/main/java/com/sandu/ximon/admin/service/C3mChargingChargeService.java
@@ -1,11 +1,21 @@
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.ximon.admin.param.C3mChargingChargeParam;
import com.sandu.ximon.admin.utils.StoreOperationRecordsUtils;
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;
@@ -21,15 +31,17 @@
@AllArgsConstructor
public class C3mChargingChargeService extends BaseServiceImpl<C3mChargingChargeMapper, C3mChargingCharge> {
    private final C3mChargingChargeMapper c3mChargingChargeMapper;
    private final PoleBindingService bindingService;
    private final C3mChargingMapper chargingMapper;
    /**
     * 修改费率
     *
     * @param chargeEntities
     * @param userId
     * @param username
     * @param
     * @return
     */
    public boolean updateCharge(List<C3mChargingCharge> chargeEntities, Long userId, String username) {
    public boolean updateCharge(List<C3mChargingCharge> chargeEntities) {
        //  删除原本存在的费率
        c3mChargingChargeMapper.deleteCharge(chargeEntities.get(0).getC3Id());
        Long timestamp = new Date().getTime();
@@ -38,16 +50,49 @@
            c3m.setTimestamp(timestamp);
            save(c3m);
        }
//        LogService.getBean().log(userId,username,"修改C3m费率",null,
//                "{ "+ JSON.toJSONString(chargeEntities)+" }");
        /**
         * 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) {
        if (chargeList.size() == 0) {
            //  不存在费率时,生成单个费率并插入数据库
            C3mChargingCharge c3mChargeEntity = C3mChargingCharge.getInitInstance(c3Id);
            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);
        }
    }
@@ -57,7 +102,7 @@
        //  查找数据库所有费率
        List<C3mChargingCharge> list = c3mChargingChargeMapper.getChargeByC3Id(c3Id);
        //  判断list为单条直接返回
        if(list.size()<2){
        if (list.size() < 2) {
            return list;
        }
        //  将时分进行排序
@@ -65,7 +110,7 @@
            @Override
            public int compare(C3mChargingCharge o1, C3mChargingCharge o2) {
                int i = o2.getHour().compareTo(o1.getHour());
                if(i == 0){
                if (i == 0) {
                    return o2.getMin().compareTo(o1.getMin());
                }
                return i;
@@ -79,22 +124,22 @@
        //  list翻转
        Collections.reverse(list);
        //  时间段字符拼接
        for(int i=1;i<list.size();i++){
        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()+"分"
                    pre.getHour() + "点" + pre.getMin() + "分 到 " +
                            now.getHour() + "点" + now.getMin() + "分"
            );
            if(i==list.size()-1){
            if (i == list.size() - 1) {
                pre = now;
                now = list.get(0);
                if(now.getHour() < pre.getHour()){
                if (now.getHour() < pre.getHour()) {
                    pre.setStrTime(
                            pre.getHour() + "点" + pre.getMin() + "分 到 " +
                                    "隔日"+now.getHour() + "点" + now.getMin() + "分"
                                    "隔日" + now.getHour() + "点" + now.getMin() + "分"
                    );
                }else {
                } else {
                    pre.setStrTime(
                            pre.getHour() + "点" + pre.getMin() + "分 到 " +
                                    now.getHour() + "点" + now.getMin() + "分"
@@ -106,4 +151,68 @@
    }
    /**
     * 根据灯杆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) {
        List<C3mChargingCharge> list = list(Wrappers.lambdaQuery(C3mChargingCharge.class).eq(C3mChargingCharge::getC3Id, c3Id));
        //获取当前时间
        LocalTime localTime = LocalTime.now();
        //遍历费率集合  判断当前时间是否在费率时间段内
        LocalTime startTime;
        LocalTime endTime;
        //默认费率
        Double charge = 8.8;
        if (list.size() > 1) {
            //费率条数大于1时,遍历费率集合  获取当前时间段的费率
            for (int i = 0; i < list.size(); i++) {
                //获取费率时间段  转化为LocalTime
                startTime = LocalTime.of(list.get(i).getHour(), list.get(i).getMin());
                endTime = LocalTime.of(list.get(i + 1).getHour(), list.get(i + 1).getMin());
                if (i == list.size()) {
                    startTime = LocalTime.of(list.get(i).getHour(), list.get(i).getMin());
                    endTime = LocalTime.of(list.get(0).getHour(), list.get(0).getMin());
                }
                //判断当前时间是否在费率时间段内
                if (localTime.isAfter(startTime) && localTime.isBefore(endTime)) {
                    //获取费率
                    charge = list.get(i).getCharge();
                }
            }
        } else if (list.size() == 1) {
            //只有一条费率时,直接获取费率
            charge = list.get(0).getCharge();
        } else {
            //其他费率时,获取默认费率
            charge = 8.8;
        }
        //计算花费  费率*电量
        BigDecimal chargeDecimal = new BigDecimal(charge);
        BigDecimal powerDecimal = new BigDecimal(power);
        BigDecimal result = chargeDecimal.multiply(powerDecimal);
        return result;
    }
}