package com.sandu.ximon.admin.service; import cn.hutool.core.bean.BeanUtil; import com.sandu.common.service.impl.BaseServiceImpl; import com.sandu.ximon.admin.param.C3mChargingChargeParam; import com.sandu.ximon.dao.domain.C3mChargingCharge; import com.sandu.ximon.dao.mapper.C3mChargingChargeMapper; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.List; /** * @Author liuhaonan * @Date 2022/3/10 13:58 * @Version 1.0 * c3充电桩费率相关 */ @Service @AllArgsConstructor public class C3mChargingChargeService extends BaseServiceImpl { private final C3mChargingChargeMapper c3mChargingChargeMapper; /** * 修改费率 * * @param chargeEntities * @param * @return */ public boolean updateCharge(List chargeEntities) { // 删除原本存在的费率 c3mChargingChargeMapper.deleteCharge(chargeEntities.get(0).getC3Id()); Long timestamp = new Date().getTime(); // 将费率添加至数据库 for (C3mChargingCharge c3m : chargeEntities) { c3m.setTimestamp(timestamp); save(c3m); } // LogService.getBean().log(userId,username,"修改C3m费率",null, // "{ "+ JSON.toJSONString(chargeEntities)+" }"); return true; } public boolean initCharge(C3mChargingChargeParam c3) { C3mChargingCharge charge = new C3mChargingCharge(); BeanUtil.copyProperties(c3, charge); charge.setTimestamp(new Date().getTime()); // c3mChargingChargeMapper.insertCharge(charge); return save(charge); } public void initCharge(Integer c3Id) { List 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 getChargeByC3Id(Integer c3Id) { // 查找数据库所有费率 List list = c3mChargingChargeMapper.getChargeByC3Id(c3Id); // 判断list为单条直接返回 if (list.size() < 2) { return list; } // 将时分进行排序 Collections.sort(list, new Comparator() { @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; } }