Files
LinHelp/backend/src/main/java/com/linhelp/groupbuy/JdbcGroupBuyOrderStore.java

114 lines
5.1 KiB
Java
Raw Normal View History

2026-07-14 16:04:51 +08:00
package com.linhelp.groupbuy;
import com.linhelp.common.enums.DeliveryMethod;
import com.linhelp.common.enums.GroupBuyOrderStatus;
import com.linhelp.common.enums.OfflinePayStatus;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;
@Repository
@ConditionalOnBean(JdbcTemplate.class)
public class JdbcGroupBuyOrderStore implements GroupBuyOrderStore {
private final JdbcTemplate jdbcTemplate;
public JdbcGroupBuyOrderStore(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public List<GroupBuyOrderResponse> loadAll() {
return jdbcTemplate.query(
"SELECT id, order_no, tenant_id, community_id, group_buy_id, group_buy_title, cover_url, user_id, address_id, " +
"delivery_method, quantity, price_cent, amount_cent, status, offline_pay_status, remark, created_at, updated_at " +
"FROM group_buy_order WHERE deleted = 0 ORDER BY id ASC",
this::mapOrder
);
}
@Override
public void save(GroupBuyOrderResponse order) {
int updated = jdbcTemplate.update(
"UPDATE group_buy_order SET order_no = ?, tenant_id = ?, community_id = ?, group_buy_id = ?, group_buy_title = ?, cover_url = ?, " +
"user_id = ?, address_id = ?, delivery_method = ?, quantity = ?, price_cent = ?, amount_cent = ?, status = ?, " +
"offline_pay_status = ?, remark = ?, updated_at = ? WHERE id = ?",
order.getOrderNo(),
order.getTenantId(),
order.getCommunityId(),
order.getGroupBuyId(),
order.getGroupBuyTitle(),
order.getCoverUrl(),
order.getUserId(),
order.getAddressId(),
order.getDeliveryMethod().name(),
order.getQuantity(),
order.getPriceCent(),
order.getAmountCent(),
order.getStatus().name(),
order.getOfflinePayStatus().name(),
order.getRemark(),
toTimestamp(order.getUpdatedAt()),
order.getId()
);
if (updated == 0) {
jdbcTemplate.update(
"INSERT INTO group_buy_order (id, order_no, tenant_id, community_id, group_buy_id, group_buy_title, cover_url, user_id, address_id, " +
"delivery_method, quantity, price_cent, amount_cent, status, offline_pay_status, remark, created_at, updated_at) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
order.getId(),
order.getOrderNo(),
order.getTenantId(),
order.getCommunityId(),
order.getGroupBuyId(),
order.getGroupBuyTitle(),
order.getCoverUrl(),
order.getUserId(),
order.getAddressId(),
order.getDeliveryMethod().name(),
order.getQuantity(),
order.getPriceCent(),
order.getAmountCent(),
order.getStatus().name(),
order.getOfflinePayStatus().name(),
order.getRemark(),
toTimestamp(order.getCreatedAt()),
toTimestamp(order.getUpdatedAt())
);
}
}
private GroupBuyOrderResponse mapOrder(ResultSet rs, int rowNum) throws SQLException {
GroupBuyOrderResponse order = new GroupBuyOrderResponse();
order.setId(rs.getLong("id"));
order.setOrderNo(rs.getString("order_no"));
order.setTenantId(rs.getLong("tenant_id"));
order.setCommunityId(rs.getLong("community_id"));
order.setGroupBuyId(rs.getLong("group_buy_id"));
order.setGroupBuyTitle(rs.getString("group_buy_title"));
order.setCoverUrl(rs.getString("cover_url"));
order.setUserId(rs.getLong("user_id"));
long addressId = rs.getLong("address_id");
order.setAddressId(rs.wasNull() ? null : addressId);
order.setDeliveryMethod(DeliveryMethod.valueOf(rs.getString("delivery_method")));
order.setQuantity(rs.getInt("quantity"));
order.setPriceCent(rs.getInt("price_cent"));
order.setAmountCent(rs.getInt("amount_cent"));
order.setStatus(GroupBuyOrderStatus.valueOf(rs.getString("status")));
order.setOfflinePayStatus(OfflinePayStatus.valueOf(rs.getString("offline_pay_status")));
order.setRemark(rs.getString("remark"));
order.setCreatedAt(rs.getTimestamp("created_at").toLocalDateTime());
order.setUpdatedAt(rs.getTimestamp("updated_at").toLocalDateTime());
return order;
}
private Timestamp toTimestamp(LocalDateTime value) {
return value == null ? null : Timestamp.valueOf(value);
}
}