126 lines
3.1 KiB
Go
126 lines
3.1 KiB
Go
package db
|
|
|
|
import "context"
|
|
|
|
type ListCustomerConditionParam struct {
|
|
TimeBegin string
|
|
TimeEnd string
|
|
IsTitle bool
|
|
Title string
|
|
Status int16
|
|
Category int32
|
|
Source int32
|
|
PageID int32
|
|
PageSize int32
|
|
}
|
|
|
|
type CustomerView struct {
|
|
Customer
|
|
// 字符串ID
|
|
SID string `json:"sid"`
|
|
// 类别
|
|
CategoryName string `json:"category_name"`
|
|
// 来源
|
|
SourceName string `json:"source_name"`
|
|
CreatedName string `json:"created_name"`
|
|
UpdatedName string `json:"updated_name"`
|
|
}
|
|
|
|
func (store *SQLStore) ListCustomerCondition(ctx context.Context, arg *ListCustomerConditionParam) ([]*CustomerView, int64, error) {
|
|
query, args, err := BuildQuery(`
|
|
SELECT COUNT(1)
|
|
FROM customers
|
|
WHERE created_at BETWEEN @start AND @end
|
|
{{if ne .status 9999}}AND status = @status{{end}}
|
|
{{if ne .category 9999}}AND category = @category{{end}}
|
|
{{if ne .source 9999}}AND source = @source{{end}}
|
|
{{if .isTitle}}AND name LIKE @title{{end}};
|
|
`, map[string]interface{}{
|
|
"start": arg.TimeBegin,
|
|
"end": arg.TimeEnd,
|
|
"status": arg.Status,
|
|
"category": arg.Category,
|
|
"source": arg.Source,
|
|
"isTitle": arg.IsTitle,
|
|
"title": arg.Title,
|
|
})
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
var total int64
|
|
if err := store.db.QueryRow(ctx, query, args...).Scan(&total); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
if total <= 0 {
|
|
return nil, 0, nil
|
|
}
|
|
|
|
query, args, err = BuildQuery(`
|
|
SELECT id, name,
|
|
category, COALESCE((SELECT name FROM categories WHERE id = customers.category), '') as category_name,
|
|
source, COALESCE((SELECT name FROM categories WHERE id = customers.source), '') as source_name,
|
|
address, contact_name, contact_phone, status, sort, created_at, updated_at,
|
|
created_by, COALESCE((SELECT username FROM sys_user WHERE id = customers.created_by), '') as created_name,
|
|
updated_by, COALESCE((SELECT username FROM sys_user WHERE id = customers.updated_by), '') as updated_name
|
|
FROM customers
|
|
WHERE created_at BETWEEN @start AND @end
|
|
{{if ne .status 9999}}AND status = @status{{end}}
|
|
{{if ne .category 9999}}AND category = @category{{end}}
|
|
{{if ne .source 9999}}AND source = @source{{end}}
|
|
{{if .isTitle}}AND name LIKE @title{{end}}
|
|
ORDER BY id DESC
|
|
LIMIT @limit OFFSET @offset;
|
|
`, map[string]interface{}{
|
|
"start": arg.TimeBegin,
|
|
"end": arg.TimeEnd,
|
|
"status": arg.Status,
|
|
"category": arg.Category,
|
|
"source": arg.Source,
|
|
"isTitle": arg.IsTitle,
|
|
"title": arg.Title,
|
|
"limit": arg.PageSize,
|
|
"offset": (arg.PageID - 1) * arg.PageSize,
|
|
})
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
rows, err := store.db.Query(ctx, query, args...)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
defer rows.Close()
|
|
items := []*CustomerView{}
|
|
for rows.Next() {
|
|
var i CustomerView
|
|
if err := rows.Scan(
|
|
&i.SID,
|
|
&i.Name,
|
|
&i.Category,
|
|
&i.CategoryName,
|
|
&i.Source,
|
|
&i.SourceName,
|
|
&i.Address,
|
|
&i.ContactName,
|
|
&i.ContactPhone,
|
|
&i.Status,
|
|
&i.Sort,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.CreatedBy,
|
|
&i.CreatedName,
|
|
&i.UpdatedBy,
|
|
&i.UpdatedName,
|
|
); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
items = append(items, &i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
return items, total, nil
|
|
}
|