@@ -1,35 +1,53 @@
package cms
import (
"math"
"strconv"
"strings"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/model"
"git.rosy.net.cn/jx-callback/business/model/dao"
)
// GetStoreSkus用
type StoreSkuNameExt struct {
model . SkuName
Skus interface { } ` orm:"-" json:"skus "`
SkusStr string ` json:"- " `
UnitPrice int ` json:"unitPrice "`
Skus [ ] map [ string ] interface { } ` orm:"-" json:"skus " `
SkusStr string ` json:"-" `
}
// GetStoreSkus用
type StoreSkuNamesInfo struct {
TotalCount int ` json:"totalCount" `
SkuNames [ ] * StoreSkuNameExt ` json:"skuNames" `
}
type StoreSkuBindInfo struct {
SkuID int ` json:"skuID" `
// UpdateStoreSku用, API调用时
type StoreSkuBindSkuInfo struct {
SubStoreID int ` json:"subStoreID" `
SkuID int ` json:"skuID" `
IsFocus int ` json:"isFocus" ` // -1: 不关注, 0: 忽略, 1: 关注
IsSale int ` json:"isSale" ` // -1: 不可售, 0: 忽略, 1: 可售
Price int ` json:"price" `
ElmID int64 ` json:"elmID" `
EbaiID int64 ` json:"ebaiID" `
}
// UpdateStoreSku用, API调用时
type StoreSkuBindInfo struct {
NameID int ` json:"nameID" `
UnitPrice int ` json:"unitPrice" `
Skus [ ] * StoreSkuBindSkuInfo ` json:"skus" `
}
type tStoreSkuBindAndSpec struct {
model . StoreSkuBind
SpecQuality float32
SpecUnit string
}
func GetStoreSkus ( storeID int , isFocused bool , keyword string , params map [ string ] interface { } , offset , pageSize int ) ( skuNamesInfo * StoreSkuNamesInfo , err error ) {
db := dao . GetDB ( )
sql := `
@@ -147,7 +165,12 @@ func GetStoreSkus(storeID int, isFocused bool, keyword string, params map[string
t1.price,
t1.img,
t1.elm_img_hash_code,
CONCAT("[", GROUP_CONCAT(DISTINCT CONCAT(' { "id":', t2.id, ',"status":', t2.status, ',"createdAt":"', CONCAT(REPLACE(t2.created_at," ","T"),"+08:00"), '","updatedAt":"', CONCAT(REPLACE(t2.updated_at," ","T"),"+08:00"), '","lastOperator":"', t2.last_operator, '","specQuality":', t2.spec_quality, ',"specUnit":"', t2.spec_unit, '","weight":', t2.weight, ',"jdID":', t2.jd_id, ',"categoryID":', t2.category_id, ',"nameID":', t2.name_id, ',"subStoreID":', IF(t4.sub_store_id IS NULL, 0, t4.sub_store_id), ',"price":', IF(t4.price IS NULL, 0, t4.price), ',"storeSkuStatus":', IF(t4.status IS NULL, 0, t4.status), "}")), "]") skus_str
CONCAT("[", GROUP_CONCAT(DISTINCT CONCAT(' { "id":', t2.id, ',"status":', t2.status, ',"createdAt":"',
CONCAT(REPLACE(t2.created_at," ","T"),"+08:00"), '","updatedAt":"', CONCAT(REPLACE(t2.updated_at," ","T"),"+08:00"),
'","lastOperator":"', t2.last_operator, '","specQuality":', t2.spec_quality, ',"specUnit":"', t2.spec_unit, '","weight":', t2.weight,
',"jdID":', t2.jd_id, ',"categoryID":', t2.category_id, ',"nameID":', t2.name_id, ',"subStoreID":', IF(t4.sub_store_id IS NULL, 0, t4.sub_store_id),
',"price":', IF(t4.price IS NULL, 0, t4.price), ',"unitPrice":', IF(t4.unit_price IS NULL, 0, t4.unit_price),
',"storeSkuStatus":', IF(t4.status IS NULL, 0, t4.status), "}")), "]") skus_str
` + sql + `
ORDER BY t1.id
LIMIT ? OFFSET ? `
@@ -176,6 +199,12 @@ func GetStoreSkus(storeID int, isFocused bool, keyword string, params map[string
if err = utils . UnmarshalUseNumber ( [ ] byte ( skuName . SkusStr ) , & skuName . Skus ) ; err != nil {
break
}
if len ( skuName . Skus ) > 0 {
skuName . UnitPrice = int ( utils . MustInterface2Int64 ( skuName . Skus [ 0 ] [ "unitPrice" ] ) )
for _ , v := range skuName . Skus {
delete ( v , "unitPrice" )
}
}
}
}
}
@@ -184,23 +213,21 @@ func GetStoreSkus(storeID int, isFocused bool, keyword string, params map[string
return skuNamesInfo , err
}
func UpdateStoreSku ( storeID int , skuBinds [ ] * StoreSkuBindInfo , userName string ) ( num int64 , err error ) {
func UpdateStoreSku ( storeID int , skuBindInfo * StoreSkuBindInfo , userName string ) ( num int64 , err error ) {
db := dao . GetDB ( )
var existBinds [ ] * model . StoreSkuBind
skuIDs := make ( [ ] int , len ( skuBinds ) )
for k , v := range skuBinds {
skuIDs [ k ] = v . SkuID
}
questions := "(" + dao . GenQuestionMarks ( len ( skuBinds ) ) + ")"
skuBinds := skuBindInfo . Skus
var existBinds [ ] * tStoreSkuBindAndSpec
if err = dao . GetRows ( db , & existBinds , `
SELECT *
FROM store_sku_bind
SELECT t1.*, t2.spec_quality, t2.spec_unit
FROM store_sku_bind t1
JOIN sku t2 ON t1.sku_id = t2.id
WHERE store_id = ?
AND sku _id IN ` + questions , storeID , skuIDs ) ; err == nil {
existBindsMap := make ( map [ int ] * model . StoreSkuB ind , len ( existBinds ) )
for _ , v := range existBinds {
existBindsMap [ v . SkuID ] = v
AND t2.name _id = ? ` , storeID , skuBindInfo . Name ID ) ; err == nil {
skuIDs := make ( [ ] int , len ( existBinds ) )
for k , v := range existBinds {
skuIDs [ k ] = v . SkuID
}
existBindsMap := make ( map [ int ] * model . StoreSkuBind , len ( existBinds ) )
dao . Begin ( db )
defer func ( ) {
if r := recover ( ) ; r != nil {
@@ -208,14 +235,44 @@ func UpdateStoreSku(storeID int, skuBinds []*StoreSkuBindInfo, userName string)
panic ( r )
}
} ( )
unitPrice := 0
if skuBindInfo . UnitPrice != 0 {
unitPrice = skuBindInfo . UnitPrice
} else {
unitPrice = existBinds [ 0 ] . UnitPrice
}
for _ , v := range existBinds {
if skuBindInfo . UnitPrice != 0 {
v . UnitPrice = unitPrice
v . Price = calStoreSkuPrice ( unitPrice , v . SpecQuality , v . SpecUnit )
v . JdSyncStatus |= model . SyncFlagPriceMask
v . ElmSyncStatus |= model . SyncFlagPriceMask
v . EbaiSyncStatus |= model . SyncFlagPriceMask
dao . WrapUpdateULEntity ( v , userName )
if _ , err2 := dao . UpdateEntity ( db , & v . StoreSkuBind ) ; err2 != nil {
dao . Rollback ( db )
return 0 , err2
}
}
existBindsMap [ v . SkuID ] = & v . StoreSkuBind
}
for _ , v := range skuBinds {
skuBind , ok := existBindsMap [ v . SkuID ]
if ! ok {
if v . IsFocus == 1 {
sku := & model . Sku { }
sku . ID = v . SkuID
if err2 := dao . GetEntity ( db , sku ) ; err2 != nil {
dao . Rollback ( db )
return 0 , err2
}
skuBind = & model . StoreSkuBind {
StoreID : storeID ,
SkuID : v . SkuID ,
SubStoreID : v . SubStoreID , // todo 这个应该从用户信息中自动获得
UnitPrice : unitPrice ,
Price : calStoreSkuPrice ( unitPrice , sku . SpecQuality , sku . SpecUnit ) ,
Status : model . StoreSkuBindStatusDontSale ,
}
dao . WrapAddIDCULDEntity ( skuBind , userName )
}
@@ -225,12 +282,6 @@ func UpdateStoreSku(storeID int, skuBinds []*StoreSkuBindInfo, userName string)
}
}
if skuBind != nil {
if v . Price != 0 {
skuBind . Price = v . Price
skuBind . JdSyncStatus |= model . SyncFlagPriceMask
skuBind . ElmSyncStatus |= model . SyncFlagPriceMask
skuBind . EbaiSyncStatus |= model . SyncFlagPriceMask
}
if v . IsSale != 0 {
if v . IsSale == 1 {
skuBind . Status = model . StoreSkuBindStatusNormal
@@ -286,3 +337,11 @@ func UpdateStoreSku(storeID int, skuBinds []*StoreSkuBindInfo, userName string)
}
return num , err
}
// 计算SKU价格, unitPrice为一斤的单价, specQuality为质量, 单位为克
func calStoreSkuPrice ( unitPrice int , specQuality float32 , specUnit string ) int {
if strings . ToLower ( specUnit ) == "kg" {
specQuality *= 1000
}
return int ( math . Round ( float64 ( float32 ( unitPrice ) * specQuality / 500 ) ) )
}