364 lines
11 KiB
Go
364 lines
11 KiB
Go
package cms
|
||
|
||
import (
|
||
"math"
|
||
"strconv"
|
||
"strings"
|
||
|
||
"git.rosy.net.cn/baseapi/utils"
|
||
"git.rosy.net.cn/jx-callback/business/jxutils"
|
||
"git.rosy.net.cn/jx-callback/business/model"
|
||
"git.rosy.net.cn/jx-callback/business/model/dao"
|
||
"git.rosy.net.cn/jx-callback/globals"
|
||
)
|
||
|
||
// GetStoreSkus用
|
||
type StoreSkuNameExt struct {
|
||
model.SkuName
|
||
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"`
|
||
}
|
||
|
||
// UpdateStoreSku用,API调用时
|
||
type StoreSkuBindSkuInfo struct {
|
||
SkuID int `json:"skuID"`
|
||
IsSale int `json:"isSale"` // -1:不可售,0:忽略,1:可售
|
||
|
||
ElmID int64 `json:"elmID"`
|
||
EbaiID int64 `json:"ebaiID"`
|
||
}
|
||
|
||
// UpdateStoreSku用,API调用时
|
||
type StoreSkuBindInfo struct {
|
||
NameID int `json:"nameID"`
|
||
UnitPrice int `json:"unitPrice"`
|
||
IsFocus int `json:"isFocus"` // -1:不关注,0:忽略,1:关注
|
||
SubStoreID int `json:"subStoreID"`
|
||
Skus []*StoreSkuBindSkuInfo `json:"skus"`
|
||
}
|
||
|
||
type tStoreSkuBindAndSpec struct {
|
||
model.StoreSkuBind
|
||
SpecQuality float32
|
||
SpecUnit string
|
||
SkuNamePrice int
|
||
RealSkuID int `orm:"column(real_sku_id)"`
|
||
}
|
||
|
||
func GetStoreSkus(storeID int, isFocused bool, keyword string, params map[string]interface{}, offset, pageSize int) (skuNamesInfo *StoreSkuNamesInfo, err error) {
|
||
db := dao.GetDB()
|
||
sql := `
|
||
FROM sku_name t1
|
||
JOIN sku t2 ON t1.id = t2.name_id AND t2.deleted_at = '1970-01-01 00:00:00'
|
||
LEFT JOIN store_sku_bind t4 ON t4.sku_id = t2.id AND t4.deleted_at = '1970-01-01 00:00:00' AND t4.store_id = ?
|
||
WHERE t1.deleted_at = '1970-01-01 00:00:00'
|
||
`
|
||
if isFocused {
|
||
sql += " AND t4.sku_id IS NOT NULL"
|
||
} else {
|
||
sql += " AND t4.sku_id IS NULL"
|
||
}
|
||
sqlParams := []interface{}{
|
||
storeID,
|
||
}
|
||
if keyword != "" {
|
||
keywordLike := "%" + keyword + "%"
|
||
sql += " AND (t1.name LIKE ? OR t1.prefix LIKE ? OR t1.comment LIKE ?"
|
||
sqlParams = append(sqlParams, keywordLike, keywordLike, keywordLike)
|
||
|
||
if keywordInt64, err2 := strconv.ParseInt(keyword, 10, 64); err2 == nil {
|
||
sql += " OR t2.jd_id = ? OR t4.id = ?"
|
||
sqlParams = append(sqlParams, keywordInt64, keywordInt64)
|
||
}
|
||
sql += ")"
|
||
}
|
||
|
||
if params["nameID"] != nil {
|
||
sql += " AND t1.id = ?"
|
||
sqlParams = append(sqlParams, params["nameID"].(int))
|
||
}
|
||
if params["categoryID"] != nil {
|
||
cat := &model.SkuCategory{}
|
||
cat.ID = params["categoryID"].(int)
|
||
if err = dao.GetEntity(db, cat); err != nil {
|
||
return nil, err
|
||
}
|
||
if cat.Level == 1 {
|
||
sql += " AND t1.category_id IN (SELECT id FROM sku_category WHERE parent_id = ?)"
|
||
} else {
|
||
sql += " AND t1.category_id = ?"
|
||
}
|
||
sqlParams = append(sqlParams, cat.ID)
|
||
}
|
||
if params["jdID"] != nil {
|
||
sql += " AND t1.jd_id = ?"
|
||
sqlParams = append(sqlParams, params["jdID"].(int))
|
||
}
|
||
if params["name"] != nil {
|
||
sql += " AND t1.name LIKE ?"
|
||
sqlParams = append(sqlParams, "%"+params["name"].(string)+"%")
|
||
}
|
||
if params["prefix"] != nil {
|
||
sql += " AND t1.prefix LIKE ?"
|
||
sqlParams = append(sqlParams, "%"+params["prefix"].(string)+"%")
|
||
}
|
||
if params["unit"] != nil {
|
||
sql += " AND t1.unit = ?"
|
||
sqlParams = append(sqlParams, params["unit"].(string))
|
||
}
|
||
if params["skuID"] != nil {
|
||
skuID, ok := params["skuID"].(int)
|
||
if ok {
|
||
sql += " AND t2.id = ?"
|
||
sqlParams = append(sqlParams, skuID)
|
||
} else {
|
||
skuIDs := params["skuID"].([]interface{})
|
||
sql += " AND t2.id IN (" + dao.GenQuestionMarks(len(skuIDs)) + ")"
|
||
sqlParams = append(sqlParams, skuIDs)
|
||
}
|
||
}
|
||
if params["fromStatus"] != nil {
|
||
fromStatus := params["fromStatus"].(int)
|
||
toStatus := fromStatus
|
||
if params["toStatus"] != nil {
|
||
toStatus = params["toStatus"].(int)
|
||
}
|
||
sql += " AND t2.status >= ? AND t2.status <= ? AND t4.status >= ? AND t4.status <= ?"
|
||
sqlParams = append(sqlParams, fromStatus, toStatus, fromStatus, toStatus)
|
||
}
|
||
sql += `
|
||
GROUP BY
|
||
t1.id,
|
||
t1.created_at,
|
||
t1.updated_at,
|
||
t1.last_operator,
|
||
t1.deleted_at,
|
||
t1.prefix,
|
||
t1.name,
|
||
t1.comment,
|
||
t1.brand_id,
|
||
t1.category_id,
|
||
t1.is_global,
|
||
t1.unit,
|
||
t1.price,
|
||
t1.img,
|
||
t1.elm_img_hash_code
|
||
`
|
||
sqlData := `
|
||
SELECT
|
||
SQL_CALC_FOUND_ROWS
|
||
t1.id,
|
||
t1.created_at,
|
||
t1.updated_at,
|
||
t1.last_operator,
|
||
t1.deleted_at,
|
||
t1.prefix,
|
||
t1.name,
|
||
t1.comment,
|
||
t1.brand_id,
|
||
t1.category_id,
|
||
t1.is_global,
|
||
t1.unit,
|
||
t1.price,
|
||
t1.img,
|
||
t1.elm_img_hash_code,
|
||
CONCAT("[", GROUP_CONCAT(DISTINCT CONCAT('{"id":', t2.id, ',"status":', t2.status, ',"createdAt":"',
|
||
CONCAT(REPLACE(t4.created_at," ","T"),"+08:00"), '","updatedAt":"', CONCAT(REPLACE(t4.updated_at," ","T"),"+08:00"),
|
||
'","lastOperator":"', t4.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 ?`
|
||
if pageSize == 0 {
|
||
pageSize = model.DefPageSize
|
||
}
|
||
if offset < 0 {
|
||
offset = 0
|
||
}
|
||
sqlParams = append(sqlParams, pageSize, offset)
|
||
skuNamesInfo = &StoreSkuNamesInfo{}
|
||
// globals.SugarLogger.Debug(sqlData)
|
||
dao.Begin(db)
|
||
defer func() {
|
||
if r := recover(); r != nil {
|
||
dao.Rollback(db)
|
||
panic(r)
|
||
}
|
||
}()
|
||
if err = dao.GetRows(db, &skuNamesInfo.SkuNames, sqlData, sqlParams...); err == nil {
|
||
countInfo := &struct{ Ct int }{}
|
||
if err = dao.GetRow(db, countInfo, "SELECT FOUND_ROWS() ct"); err == nil {
|
||
skuNamesInfo.TotalCount = countInfo.Ct
|
||
for _, skuName := range skuNamesInfo.SkuNames {
|
||
if skuName.SkusStr != "" {
|
||
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")
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
dao.Commit(db)
|
||
return skuNamesInfo, err
|
||
}
|
||
|
||
func UpdateStoreSku(storeID int, skuBindInfo *StoreSkuBindInfo, userName string) (num int64, err error) {
|
||
return UpdateStoreSkus(storeID, []*StoreSkuBindInfo{skuBindInfo}, userName)
|
||
}
|
||
|
||
func UpdateStoreSkus(storeID int, skuBindInfos []*StoreSkuBindInfo, userName string) (num int64, err error) {
|
||
db := dao.GetDB()
|
||
needSyncIDMap := make(map[int]int)
|
||
dao.Begin(db)
|
||
defer func() {
|
||
if r := recover(); r != nil {
|
||
dao.Rollback(db)
|
||
panic(r)
|
||
}
|
||
}()
|
||
for _, skuBindInfo := range skuBindInfos {
|
||
inSkuBinds := skuBindInfo.Skus
|
||
var allBinds []*tStoreSkuBindAndSpec
|
||
if err = dao.GetRows(db, &allBinds, `
|
||
SELECT t2.*, t1.id real_sku_id, t1.spec_quality, t1.spec_unit, t3.price sku_name_price
|
||
FROM sku t1
|
||
LEFT JOIN store_sku_bind t2 ON t2.sku_id = t1.id AND store_id = ?
|
||
JOIN sku_name t3 ON t1.name_id = t3.id
|
||
WHERE t1.name_id = ?
|
||
`, storeID, skuBindInfo.NameID); err == nil {
|
||
globals.SugarLogger.Debug(len(allBinds))
|
||
inSkuBinsMap := make(map[int]*StoreSkuBindSkuInfo, len(inSkuBinds))
|
||
for _, v := range inSkuBinds {
|
||
inSkuBinsMap[v.SkuID] = v
|
||
}
|
||
unitPrice := 0
|
||
if skuBindInfo.UnitPrice != 0 {
|
||
unitPrice = skuBindInfo.UnitPrice
|
||
} else {
|
||
unitPrice = allBinds[0].UnitPrice
|
||
if unitPrice == 0 {
|
||
unitPrice = allBinds[0].SkuNamePrice
|
||
}
|
||
}
|
||
for _, v := range allBinds {
|
||
inSkuBind := inSkuBinsMap[v.SkuID]
|
||
var skuBind *model.StoreSkuBind
|
||
// globals.SugarLogger.Debug(ok)
|
||
if v.ID == 0 {
|
||
if skuBindInfo.IsFocus == 1 {
|
||
skuBind = &model.StoreSkuBind{
|
||
StoreID: storeID,
|
||
SkuID: v.RealSkuID,
|
||
SubStoreID: skuBindInfo.SubStoreID, // todo 这个应该从用户信息中自动获得
|
||
UnitPrice: unitPrice,
|
||
Price: calStoreSkuPrice(unitPrice, v.SpecQuality, v.SpecUnit),
|
||
Status: model.StoreSkuBindStatusDontSale, // 缺省不可售?
|
||
}
|
||
if inSkuBind != nil && inSkuBind.IsSale == 1 {
|
||
skuBind.Status = model.StoreSkuBindStatusNormal
|
||
}
|
||
setStoreSkuBindStatus(skuBind, model.SyncFlagNewMask)
|
||
dao.WrapAddIDCULDEntity(skuBind, userName)
|
||
globals.SugarLogger.Debug(utils.Format4Output(skuBind, false))
|
||
if err = dao.CreateEntity(db, skuBind); err != nil {
|
||
dao.Rollback(db)
|
||
return 0, err
|
||
}
|
||
num = 1
|
||
}
|
||
} else {
|
||
skuBind = &v.StoreSkuBind
|
||
|
||
if skuBindInfo.IsFocus == -1 {
|
||
if num, err = dao.DeleteEntityLogically(db, skuBind, map[string]interface{}{
|
||
model.FieldStatus: model.StoreSkuBindStatusDeleted,
|
||
model.FieldJdSyncStatus: model.SyncFlagDeletedMask,
|
||
model.FieldElmSyncStatus: model.SyncFlagDeletedMask,
|
||
model.FieldEbaiSyncStatus: model.SyncFlagDeletedMask,
|
||
}, userName, nil); err != nil {
|
||
dao.Rollback(db)
|
||
return 0, err
|
||
}
|
||
} else {
|
||
needUpdate := false
|
||
if skuBindInfo.IsFocus == 1 {
|
||
skuBind.DeletedAt = utils.DefaultTimeValue
|
||
needUpdate = true
|
||
}
|
||
if inSkuBind != nil && inSkuBind.IsSale != 0 {
|
||
if inSkuBind.IsSale == 1 {
|
||
skuBind.Status = model.StoreSkuBindStatusNormal
|
||
} else {
|
||
skuBind.Status = model.StoreSkuBindStatusDontSale
|
||
}
|
||
setStoreSkuBindStatus(skuBind, model.SyncFlagSaleMask)
|
||
needUpdate = true
|
||
}
|
||
if skuBindInfo.UnitPrice != 0 { // 这里是否需要加此条件限制
|
||
skuBind.UnitPrice = unitPrice
|
||
skuBind.Price = calStoreSkuPrice(unitPrice, v.SpecQuality, v.SpecUnit)
|
||
setStoreSkuBindStatus(skuBind, model.SyncFlagPriceMask)
|
||
needUpdate = true
|
||
}
|
||
if inSkuBind != nil && inSkuBind.EbaiID != 0 {
|
||
skuBind.EbaiID = inSkuBind.EbaiID
|
||
needUpdate = true
|
||
}
|
||
if inSkuBind != nil && inSkuBind.ElmID != 0 {
|
||
skuBind.ElmID = inSkuBind.ElmID
|
||
needUpdate = true
|
||
}
|
||
if needUpdate {
|
||
setStoreSkuBindStatus(skuBind, model.SyncFlagModifiedMask)
|
||
dao.WrapUpdateULEntity(skuBind, userName)
|
||
if num, err = dao.UpdateEntity(db, skuBind); err != nil {
|
||
dao.Rollback(db)
|
||
return 0, err
|
||
}
|
||
}
|
||
}
|
||
if skuBind != nil && num == 1 {
|
||
needSyncIDMap[skuBind.ID] = 1
|
||
}
|
||
}
|
||
}
|
||
} else {
|
||
dao.Rollback(db)
|
||
return 0, err
|
||
}
|
||
}
|
||
dao.Commit(db)
|
||
skuIDs := jxutils.IntMap2List(needSyncIDMap)
|
||
err = CurVendorSync.SyncStoreSku(db, storeID, skuIDs, false, userName)
|
||
return int64(len(skuIDs)), 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)))
|
||
}
|
||
|
||
func setStoreSkuBindStatus(skuBind *model.StoreSkuBind, status int8) {
|
||
skuBind.JdSyncStatus |= status
|
||
skuBind.ElmSyncStatus |= status
|
||
skuBind.EbaiSyncStatus |= status
|
||
}
|