zjxpcyc 6 jaren geleden
bovenliggende
commit
4fa39bd257
5 gewijzigde bestanden met toevoegingen van 40 en 102 verwijderingen
  1. 7
    0
      log/common.log
  2. 12
    2
      models/customer/customer.go
  3. 1
    0
      models/customer/types.go
  4. 20
    28
      utils/pagenavi.go
  5. 0
    72
      utils/pagenavi_test.go

+ 7
- 0
log/common.log Bestand weergeven

@@ -0,0 +1,7 @@
1
+2018/08/20 15:27:18 [E] 获取客户列表失败: sql: converting Exec argument $1 type: unsupported type []interface {}, a slice of interface
2
+2018/08/20 15:36:51 [E] 获取客户列表失败: sql: converting Exec argument $1 type: unsupported type []interface {}, a slice of interface
3
+2018/08/20 15:37:19 [E] 获取客户列表失败: sql: converting Exec argument $1 type: unsupported type []interface {}, a slice of interface
4
+2018/08/20 15:44:30 [E] 获取客户列表失败: Error 1248: Every derived table must have its own alias
5
+2018/08/20 15:46:35 [E] 获取客户列表失败: Error 1248: Every derived table must have its own alias
6
+2018/08/20 15:49:09 [E] 获取客户列表失败: Error 1060: Duplicate column name 'user_id'
7
+2018/08/20 15:57:45 [E] 获取客户列表失败: sql: statement expects 2 inputs; got 1

+ 12
- 2
models/customer/customer.go Bestand weergeven

@@ -28,6 +28,8 @@ func NewCustomerDAO(ctx *utils.Context) *CustomerDAO {
28 28
 
29 29
 // CustWithWXList 客户列表
30 30
 func (m *CustomerDAO) CustWithWXList(phone string, page ...int) ([]CustWithWX, int64, error) {
31
+	org := m.ctx.Get("org").(model.SysOrg)
32
+
31 33
 	if phone == "" {
32 34
 		phone = "%"
33 35
 	} else {
@@ -49,7 +51,14 @@ func (m *CustomerDAO) CustWithWXList(phone string, page ...int) ([]CustWithWX, i
49 51
 	}
50 52
 
51 53
 	query := `
52
-		SELECT *
54
+		SELECT
55
+			t.*, s.account_info,
56
+			s.account_type,
57
+			s.mapping_id,
58
+			s.openid,
59
+			s.user_id as map_user,
60
+			s.user_type,
61
+			s.uuid
53 62
 		FROM
54 63
 			ta_customer t
55 64
 		LEFT JOIN ta_user_mapping s ON t.customer_id = s.user_id
@@ -58,13 +67,14 @@ func (m *CustomerDAO) CustWithWXList(phone string, page ...int) ([]CustWithWX, i
58 67
 		WHERE
59 68
 			t.phone like ?
60 69
 		AND t.status > ?
70
+		AND t.org_id = '` + org.OrgId + `'
61 71
 		ORDER BY
62 72
 			t.create_date DESC
63 73
 	`
64 74
 
65 75
 	var cust []CustWithWX
66 76
 
67
-	cnt, err := utils.GetPageList(m.db, &cust, []int{pageNum, offset}, query, phone, models.STATUS_DEL)
77
+	cnt, err := utils.NewPageNaviEngine(m.ctx).GetPageList(&cust, query, []int{pageNum, offset}, phone, models.STATUS_DEL)
68 78
 	if err != nil {
69 79
 		return nil, 0, err
70 80
 	}

+ 1
- 0
models/customer/types.go Bestand weergeven

@@ -9,4 +9,5 @@ type CustWithWX struct {
9 9
 	model.TaCustomer    `xorm:"extends"`
10 10
 	model.TaUserMapping `xorm:"extends"`
11 11
 	Points              int
12
+	MapUser             string
12 13
 }

+ 20
- 28
utils/pagenavi.go Bestand weergeven

@@ -1,20 +1,26 @@
1 1
 package utils
2 2
 
3 3
 import (
4
-	"errors"
5 4
 	"strconv"
6
-
7
-	"github.com/go-xorm/xorm"
8 5
 )
9 6
 
7
+type PageNaviEngine struct {
8
+	ctx *Context
9
+}
10
+
11
+func NewPageNaviEngine(ctx *Context) *PageNaviEngine {
12
+	return &PageNaviEngine{ctx: ctx}
13
+}
14
+
10 15
 // GetPageList 获取列表
11
-func GetPageList(db *xorm.Session, target interface{}, limit []int, sqlorArgs ...interface{}) (int64, error) {
12
-	if sqlorArgs == nil || len(sqlorArgs) == 0 {
13
-		return 0, errors.New("SQL 语句不存在")
16
+func (t *PageNaviEngine) GetPageList(target interface{}, sql string, limit []int, sqlArgs ...interface{}) (int64, error) {
17
+	countSQL := "select count(*) as cnt from (" + sql + ") as virtualTable"
18
+	countSQLorArgs := []interface{}{countSQL}
19
+	for _, arg := range sqlArgs {
20
+		countSQLorArgs = append(countSQLorArgs, arg)
14 21
 	}
15 22
 
16
-	countSQLorArgs := GetCountSQL(sqlorArgs...)
17
-	cntRes, err := db.Query(countSQLorArgs...)
23
+	cntRes, err := t.ctx.DB.Query(countSQLorArgs...)
18 24
 	if err != nil {
19 25
 		return 0, err
20 26
 	}
@@ -22,9 +28,9 @@ func GetPageList(db *xorm.Session, target interface{}, limit []int, sqlorArgs ..
22 28
 	cntStr := string(cntRes[0]["cnt"])
23 29
 	cnt, _ := strconv.ParseInt(cntStr, 10, 64)
24 30
 
25
-	sql, args := PackLimitToSQL(limit, sqlorArgs...)
31
+	newSQL := t.PackLimitToSQL(sql, limit)
26 32
 
27
-	err = db.SQL(sql, args...).Find(target)
33
+	err = t.ctx.DB.SQL(newSQL, sqlArgs...).Find(target)
28 34
 	if err != nil {
29 35
 		return 0, err
30 36
 	}
@@ -32,28 +38,14 @@ func GetPageList(db *xorm.Session, target interface{}, limit []int, sqlorArgs ..
32 38
 	return cnt, nil
33 39
 }
34 40
 
35
-// GetCountSQL 获取计算 Count 的相关 SQL 脚本
36
-func GetCountSQL(sqlorArgs ...interface{}) []interface{} {
37
-	sql := sqlorArgs[0].(string)
38
-
39
-	cntSQL := "select count(*) as cnt from (" + sql + ")"
40
-
41
-	newSQLorArgs := []interface{}{cntSQL}
42
-	newSQLorArgs = append(newSQLorArgs, sqlorArgs[1:])
43
-	return newSQLorArgs
44
-}
45
-
46 41
 // PackLimitToSQL 添加 limit 语句到 sql 最后
47
-func PackLimitToSQL(limit []int, sqlorArgs ...interface{}) (string, []interface{}) {
48
-	sql := sqlorArgs[0].(string)
49
-	args := sqlorArgs[1:]
50
-
42
+func (t *PageNaviEngine) PackLimitToSQL(sql string, limit []int) string {
51 43
 	switch len(limit) {
52 44
 	case 1:
53
-		return sql + " LIMIT " + strconv.Itoa(limit[0]), args
45
+		return sql + " LIMIT " + strconv.Itoa(limit[0])
54 46
 	case 2:
55
-		return sql + " LIMIT " + strconv.Itoa(limit[0]) + " OFFSET " + strconv.Itoa(limit[1]), args
47
+		return sql + " LIMIT " + strconv.Itoa(limit[0]) + " OFFSET " + strconv.Itoa(limit[1])
56 48
 	default:
57
-		return sql, args
49
+		return sql
58 50
 	}
59 51
 }

+ 0
- 72
utils/pagenavi_test.go Bestand weergeven

@@ -1,72 +0,0 @@
1
-package utils_test
2
-
3
-import (
4
-	"spaceofcheng/services/utils"
5
-	"testing"
6
-)
7
-
8
-func TestGetCountSQL(t *testing.T) {
9
-	testCases := []map[string]string{
10
-		map[string]string{
11
-			"sql":      "Select field1, field2 From table",
12
-			"expected": "select count(*) as cnt from (Select field1, field2 From table)",
13
-		},
14
-		map[string]string{
15
-			"sql": `Select 
16
-					field1, field2 From table where status = ?`,
17
-			"expected": `select count(*) as cnt from (Select 
18
-					field1, field2 From table where status = ?)`,
19
-		},
20
-	}
21
-
22
-	for inx, cs := range testCases {
23
-		sql := cs["sql"]
24
-		expected := cs["expected"]
25
-
26
-		var res []interface{}
27
-		if inx < 1 {
28
-			res = utils.GetCountSQL(sql)
29
-		} else {
30
-			res = utils.GetCountSQL(sql, inx)
31
-		}
32
-
33
-		if res == nil || len(res) == 0 {
34
-			t.Fatalf("Test GetCountSQL fail --1")
35
-		}
36
-
37
-		newSQL := res[0].(string)
38
-		if newSQL != expected {
39
-			t.Fatalf("\nTest GetCountSQL fail: \n%s\n%s", newSQL, expected)
40
-		}
41
-	}
42
-}
43
-
44
-func TestPackLimitToSQL(t *testing.T) {
45
-	testCases := []map[string]string{
46
-		map[string]string{
47
-			"sql":      "Select * From table",
48
-			"expected": "Select * From table LIMIT 1 OFFSET 0",
49
-		},
50
-		map[string]string{
51
-			"sql":      "Select * From table",
52
-			"expected": "Select * From table LIMIT 1",
53
-		},
54
-	}
55
-
56
-	for inx, cs := range testCases {
57
-		sql := cs["sql"]
58
-		expected := cs["expected"]
59
-
60
-		var sqlRes string
61
-		if inx < 1 {
62
-			sqlRes, _ = utils.PackLimitToSQL([]int{inx + 1, inx}, sql)
63
-		} else {
64
-			sqlRes, _ = utils.PackLimitToSQL([]int{inx}, sql, inx)
65
-		}
66
-
67
-		if sqlRes != expected {
68
-			t.Fatalf("\n%v: Test PackLimitToSQL fail: %s", inx+1, sqlRes)
69
-		}
70
-	}
71
-
72
-}