-
Notifications
You must be signed in to change notification settings - Fork 0
/
product_company_get.go
159 lines (150 loc) · 4.96 KB
/
product_company_get.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package ERPProduct
import (
CoreSQL "github.com/fotomxq/weeekj_core/v5/core/sql"
CoreSQLPages "github.com/fotomxq/weeekj_core/v5/core/sql/pages"
Router2SystemConfig "github.com/fotomxq/weeekj_core/v5/router2/system_config"
"time"
)
// ArgsGetProductCompanyList 获取产品供应商列表参数
type ArgsGetProductCompanyList struct {
//分页
Pages CoreSQLPages.ArgsDataList `json:"pages"`
//组织ID
// -1 跳过
OrgID int64 `db:"org_id" json:"orgID" check:"id" empty:"true"`
//产品ID
ProductID int64 `db:"product_id" json:"productID" check:"id" empty:"true"`
//供应商公司
CompanyID int64 `db:"company_id" json:"companyID" check:"id" empty:"true"`
//是否删除
IsRemove bool `db:"is_remove" json:"isRemove" check:"bool"`
}
// GetProductCompanyList 获取产品供应商列表
func GetProductCompanyList(args *ArgsGetProductCompanyList) (dataList []FieldsProductCompany, dataCount int64, err error) {
where := ""
maps := map[string]interface{}{}
where = CoreSQL.GetDeleteSQL(args.IsRemove, where)
if args.OrgID > -1 {
where = where + " AND org_id = :org_id"
maps["org_id"] = args.OrgID
}
if args.ProductID > -1 {
where = where + " AND product_id = :product_id"
maps["product_id"] = args.ProductID
}
if args.CompanyID > -1 {
where = where + " AND company_id = :company_id"
maps["company_id"] = args.CompanyID
}
tableName := "erp_product_company"
var rawList []FieldsProductCompany
dataCount, err = CoreSQL.GetListPageAndCount(
Router2SystemConfig.MainDB.DB,
&rawList,
tableName,
"id",
"SELECT id FROM "+tableName+" WHERE "+where,
where,
maps,
&args.Pages,
[]string{"id", "create_at", "update_at", "delete_at", "weight", "tip_price", "tip_tax_price"},
)
if err != nil {
return
}
for _, v := range rawList {
vData := getProductCompanyByID(v.ID)
if vData.ID < 1 {
continue
}
dataList = append(dataList, vData)
}
return
}
// GetProductCompany 获取供货商配置数据
func GetProductCompany(orgID int64, productID int64, companyID int64) (data FieldsProductCompany) {
var b bool
data, b = CheckProductCompany(orgID, productID, companyID)
if !b {
data = FieldsProductCompany{}
return
}
return
}
// CheckProductCompany 检查供应商是否供应商品,并反馈数据
func CheckProductCompany(orgID int64, productID int64, companyID int64) (data FieldsProductCompany, b bool) {
err := Router2SystemConfig.MainDB.Get(&data, "SELECT id FROM erp_product_company WHERE org_id = $1 AND product_id = $2 AND company_id = $3 AND delete_at < to_timestamp(1000000)", orgID, productID, companyID)
if err != nil {
return
}
data = getProductCompanyByID(data.ID)
b = true
return
}
// GetProductCompanyLast 获取产品的历史信息
func GetProductCompanyLast(orgID int64, productID int64, companyID int64, beforeAt time.Time) (data FieldsProductCompany) {
//如果实际数据更新时间较早,则直接获取当前数据即可
err := Router2SystemConfig.MainDB.Get(&data, "SELECT id FROM erp_product_company WHERE org_id = $1 AND product_id = $2 AND company_id = $3 AND delete_at < to_timestamp(1000000) AND update_at <= $4", orgID, productID, companyID, beforeAt)
if err == nil && data.ID > 0 {
data = getProductCompanyByID(data.ID)
return
}
return
}
// GetProductCompanyRand 随机抽取一个供货商
// randMode 随机模式,0 只要反馈即可; 1 成本价最低
func GetProductCompanyRand(productID int64, randMode int) (data FieldsProductCompany) {
dataList := getProductCompanyListByProductID(productID)
switch randMode {
case 0:
if len(dataList) > 0 {
data = dataList[0]
}
case 1:
for _, v := range dataList {
if data.ID < 0 {
v = data
continue
}
if data.TaxCostPrice > v.TaxCostPrice {
data = v
continue
}
}
}
if data.ID < 1 {
if len(dataList) > 0 {
data = dataList[0]
}
}
return
}
// getProductCompanyListByProductID 通过产品获取关联的供应商
func getProductCompanyListByProductID(productID int64) (dataList []FieldsProductCompany) {
var rawList []FieldsProductCompany
err := Router2SystemConfig.MainDB.Select(&rawList, "SELECT id FROM erp_product_company WHERE product_id = $1 AND delete_at < to_timestamp(1000000)", productID)
if err != nil || len(rawList) < 1 {
return
}
for _, v := range rawList {
vData := getProductCompanyByID(v.ID)
if vData.ID < 1 {
continue
}
dataList = append(dataList, vData)
}
return
}
// getProductCompanyByID 获取产品关联供应商
func getProductCompanyByID(id int64) (data FieldsProductCompany) {
cacheMark := getProductCompanyCacheMark(id)
if err := Router2SystemConfig.MainCache.GetStruct(cacheMark, &data); err == nil && data.ID > 0 {
return
}
err := Router2SystemConfig.MainDB.Get(&data, "SELECT id, create_at, update_at, delete_at, org_id, product_id, company_id, currency, cost_price, tax, tax_cost_price, rebate_price, tip_price, tip_tax_price, params FROM erp_product_company WHERE id = $1", id)
if err != nil {
return
}
Router2SystemConfig.MainCache.SetStruct(cacheMark, data, cacheProductCompanyTime)
return
}