Files
opsx-licence/backend/internal/service/customer/customer_service.go
T
Logan.Li 4ae318335c feat: 完成 opsx-licence 许可证管理平台全栈实现
基于 opsx-web-lite 框架(Go+Gin+Vue3+Element Plus)构建许可证管理服务器,
业务域参考 Licence 服务器 Web 版。

【后端 - 4层架构】
- 认证:单 Admin 表 + JWT(HS256)
- 核心授权域:Product / Customer / License / Activation
  · License 生成/验证/激活/机器绑定/心跳/续期/吊销
  · 客户端契约接口:/api/v1/license/{verify,activate,deactivate,heartbeat}
- 定价订单:Plan / Order / 支付网关配置
- 防重复购买:CustomerEntitlement + 幂等支付 webhook + 手动发放
- 分析审计:Dashboard 统计 / Analytics / AuditLog / Setting 管理

【前端】
- Dashboard(ECharts 图表)、产品、客户、License、计划、订单、权益、日志、设置
- 9 个完整 CRUD 业务页面 + 登录/布局

【部署】
- SQLite + 单二进制(//go:embed 嵌入前端)
- 开发:./run.sh start(前端5181/后端9001)
- 生产:./run.sh build && ./run.sh binary start
- 默认账号:admin/admin

Author: Logan.Li
2026-08-07 15:02:00 +08:00

147 lines
4.0 KiB
Go

package customer
import (
"backend/internal/dto"
"backend/internal/model"
"errors"
"gorm.io/gorm"
)
// CustomerService 客户业务逻辑层 // Logan.Li
type CustomerService struct {
db *gorm.DB
}
// NewCustomerService 创建 service 实例 // Logan.Li
func NewCustomerService(db *gorm.DB) *CustomerService {
return &CustomerService{db: db}
}
// List 分页查询客户 // Logan.Li
func (s *CustomerService) List(page, pageSize int, keyword string) ([]dto.CustomerResponse, int64, error) {
var customers []model.Customer
var total int64
query := s.db.Model(&model.Customer{})
if keyword != "" {
query = query.Where("name LIKE ? OR email LIKE ? OR company LIKE ?", "%"+keyword+"%", "%"+keyword+"%", "%"+keyword+"%")
}
query.Count(&total)
if err := query.Order("created_at DESC").
Offset((page - 1) * pageSize).Limit(pageSize).
Find(&customers).Error; err != nil {
return nil, 0, err
}
results := make([]dto.CustomerResponse, 0, len(customers))
for _, c := range customers {
results = append(results, toCustomerResponse(c))
}
return results, total, nil
}
// GetAll 获取全部客户(下拉选项用) // Logan.Li
func (s *CustomerService) GetAll() ([]dto.CustomerResponse, error) {
var customers []model.Customer
if err := s.db.Where("status = ?", 1).Order("created_at DESC").Find(&customers).Error; err != nil {
return nil, err
}
results := make([]dto.CustomerResponse, 0, len(customers))
for _, c := range customers {
results = append(results, toCustomerResponse(c))
}
return results, nil
}
// GetByID 根据 ID 获取客户 // Logan.Li
func (s *CustomerService) GetByID(id uint) (*dto.CustomerResponse, error) {
var customer model.Customer
if err := s.db.First(&customer, id).Error; err != nil {
return nil, errors.New("客户不存在")
}
resp := toCustomerResponse(customer)
return &resp, nil
}
// Create 创建客户 // Logan.Li
func (s *CustomerService) Create(req dto.CustomerCreate) (*dto.CustomerResponse, error) {
status := 1
if req.Status != nil {
status = *req.Status
}
customer := model.Customer{
Name: req.Name,
Company: req.Company,
Email: req.Email,
Phone: req.Phone,
Address: req.Address,
Notes: req.Notes,
Status: status,
}
if err := s.db.Create(&customer).Error; err != nil {
return nil, errors.New("创建客户失败")
}
resp := toCustomerResponse(customer)
return &resp, nil
}
// Update 更新客户 // Logan.Li
func (s *CustomerService) Update(id uint, req dto.CustomerUpdate) (*dto.CustomerResponse, error) {
var customer model.Customer
if err := s.db.First(&customer, id).Error; err != nil {
return nil, errors.New("客户不存在")
}
updates := map[string]interface{}{
"name": req.Name,
"company": req.Company,
"email": req.Email,
"phone": req.Phone,
"address": req.Address,
"notes": req.Notes,
}
if req.Status != nil {
updates["status"] = *req.Status
}
if err := s.db.Model(&customer).Updates(updates).Error; err != nil {
return nil, errors.New("更新客户失败")
}
s.db.First(&customer, id)
resp := toCustomerResponse(customer)
return &resp, nil
}
// Delete 删除客户(软删除) // Logan.Li
func (s *CustomerService) Delete(id uint) error {
var count int64
s.db.Model(&model.License{}).Where("customer_id = ?", id).Count(&count)
if count > 0 {
return errors.New("该客户下存在 License,无法删除")
}
return s.db.Delete(&model.Customer{}, id).Error
}
// UpdateStatus 切换客户状态 // Logan.Li
func (s *CustomerService) UpdateStatus(id uint, status int) error {
return s.db.Model(&model.Customer{}).Where("id = ?", id).Update("status", status).Error
}
// toCustomerResponse 模型转响应 DTO // Logan.Li
func toCustomerResponse(c model.Customer) dto.CustomerResponse {
return dto.CustomerResponse{
ID: c.ID,
Name: c.Name,
Company: c.Company,
Email: c.Email,
Phone: c.Phone,
Address: c.Address,
Notes: c.Notes,
Status: c.Status,
CreatedAt: c.CreatedAt.Format("2006-01-02 15:04:05"),
UpdatedAt: c.UpdatedAt.Format("2006-01-02 15:04:05"),
}
}