- store man almost ok.
This commit is contained in:
@@ -2,8 +2,15 @@ package cms
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"git.rosy.net.cn/jx-callback/business/jxcallback/scheduler/basesch"
|
||||
"git.rosy.net.cn/jx-callback/business/partner"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrHaveNotImplementedYet = errors.New("还没有实现")
|
||||
)
|
||||
|
||||
func GetPurchaseHandler(vendorID int) partner.IPurchasePlatformHandler {
|
||||
return basesch.FixedBaseScheduler.GetPurchasePlatformFromVendorID(vendorID)
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package cms
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
@@ -17,6 +18,9 @@ type StoreExt struct {
|
||||
model.Store
|
||||
CityName string `json:"cityName"`
|
||||
DistrictName string `json:"districtName"`
|
||||
JdID string `orm:"column(jd_id)" json:"jdID"`
|
||||
ElmID string `orm:"column(elm_id)" json:"elmID"`
|
||||
EbaiID string `orm:"column(ebai_id)" json:"ebaiID"`
|
||||
}
|
||||
|
||||
type StoresInfo struct {
|
||||
@@ -45,13 +49,13 @@ func GetPlaces(parentCode int, vendorID int, includeDisabled bool) ([]*model.Pla
|
||||
return places, dao.GetRows(db, &places, sql+"AND parent_code = ?", parentCode)
|
||||
}
|
||||
|
||||
func UpdatePlaces(places []map[string]interface{}, userName string) (err error) {
|
||||
func UpdatePlaces(places []map[string]interface{}, userName string) (num int64, err error) {
|
||||
if len(places) == 0 {
|
||||
return ErrMissingInput
|
||||
return 0, ErrMissingInput
|
||||
}
|
||||
for _, place := range places {
|
||||
if place["code"] == nil {
|
||||
return ErrMissingInput
|
||||
return 0, ErrMissingInput
|
||||
}
|
||||
placeid := &model.Place{}
|
||||
globals.SugarLogger.Debug(utils.Format4Output(place, false))
|
||||
@@ -59,11 +63,16 @@ func UpdatePlaces(places []map[string]interface{}, userName string) (err error)
|
||||
valid["updatedAt"] = time.Now()
|
||||
valid["lastOperator"] = userName
|
||||
globals.SugarLogger.Debug(valid)
|
||||
if err = dao.UpdateEntityByKV(nil, placeid, valid, utils.Params2Map("Code", place["code"])); err != nil {
|
||||
return err
|
||||
if num, err = dao.UpdateEntityByKV(nil, placeid, valid, utils.Params2Map("Code", place["code"])); err != nil {
|
||||
return num, err
|
||||
}
|
||||
}
|
||||
return err
|
||||
return num, err
|
||||
}
|
||||
|
||||
func UpdatePlace(placeCode int, payload map[string]interface{}, userName string) (num int64, err error) {
|
||||
payload["code"] = placeCode
|
||||
return UpdatePlaces([]map[string]interface{}{payload}, userName)
|
||||
}
|
||||
|
||||
func GetStores(keyword string, params map[string]interface{}, offset, pageSize int) (retVal *StoresInfo, err error) {
|
||||
@@ -71,17 +80,19 @@ func GetStores(keyword string, params map[string]interface{}, offset, pageSize i
|
||||
FROM store t1
|
||||
LEFT JOIN place city ON t1.city_code = city.code AND city.level = 2
|
||||
LEFT JOIN place district ON t1.district_code = district.code AND district.level = 3
|
||||
LEFT JOIN store_map jdm ON t1.id = jdm.store_id AND jdm.vendor_id = 0
|
||||
LEFT JOIN store_map elmm ON t1.id = elmm.store_id AND elmm.vendor_id = 2
|
||||
LEFT JOIN store_map ebaim ON t1.id = ebaim.store_id AND ebaim.vendor_id = 3
|
||||
WHERE`
|
||||
|
||||
params2 := make([]interface{}, 0)
|
||||
sqlParams := make([]interface{}, 0)
|
||||
if keyword != "" {
|
||||
keywordLike := "%" + keyword + "%"
|
||||
sql += " (t1.name LIKE ? OR t1.address LIKE ? OR t1.tel1 LIKE ? OR t1.tel2 LIKE ? OR t1.last_operator LIKE ? OR city.name LIKE ?"
|
||||
params2 = append(params2, keywordLike, keywordLike, keywordLike, keywordLike, keywordLike, keywordLike)
|
||||
sqlParams = append(sqlParams, keywordLike, keywordLike, keywordLike, keywordLike, keywordLike, keywordLike)
|
||||
|
||||
if keywordInt64, err2 := strconv.ParseInt(keyword, 10, 64); err2 == nil {
|
||||
sql += " OR t1.id = ? OR t1.city_code = ? OR t1.district_code = ? OR t1.lng = ? OR t1.lat = ?"
|
||||
params2 = append(params2, keywordInt64, keywordInt64, keywordInt64, keywordInt64, keywordInt64)
|
||||
sqlParams = append(sqlParams, keywordInt64, keywordInt64, keywordInt64, keywordInt64, keywordInt64)
|
||||
}
|
||||
sql += ")"
|
||||
} else {
|
||||
@@ -90,11 +101,11 @@ func GetStores(keyword string, params map[string]interface{}, offset, pageSize i
|
||||
|
||||
if params["id"] != nil {
|
||||
sql += " AND t1.id = ?"
|
||||
params2 = append(params2, params["id"].(int))
|
||||
sqlParams = append(sqlParams, params["id"].(int))
|
||||
}
|
||||
if params["name"] != nil {
|
||||
sql += " AND t1.name LIKE ?"
|
||||
params2 = append(params2, "%"+params["name"].(string)+"%")
|
||||
sqlParams = append(sqlParams, "%"+params["name"].(string)+"%")
|
||||
}
|
||||
if params["placeID"] != nil {
|
||||
level := 2
|
||||
@@ -106,16 +117,16 @@ func GetStores(keyword string, params map[string]interface{}, offset, pageSize i
|
||||
} else {
|
||||
sql += " AND t1.district_code = ?"
|
||||
}
|
||||
params2 = append(params2, params["placeID"].(int))
|
||||
sqlParams = append(sqlParams, params["placeID"].(int))
|
||||
}
|
||||
if params["address"] != nil {
|
||||
sql += " AND t1.address LIKE ?"
|
||||
params2 = append(params2, "%"+params["address"].(string)+"%")
|
||||
sqlParams = append(sqlParams, "%"+params["address"].(string)+"%")
|
||||
}
|
||||
if params["tel"] != nil {
|
||||
sql += " AND (t1.tel1 LIKE ? OR t1.tel2 LIKE ?)"
|
||||
params2 = append(params2, "%"+params["tel"].(string)+"%")
|
||||
params2 = append(params2, "%"+params["tel"].(string)+"%")
|
||||
sqlParams = append(sqlParams, "%"+params["tel"].(string)+"%")
|
||||
sqlParams = append(sqlParams, "%"+params["tel"].(string)+"%")
|
||||
}
|
||||
if params["fromStatus"] != nil {
|
||||
fromStatus := params["fromStatus"].(int)
|
||||
@@ -124,7 +135,30 @@ func GetStores(keyword string, params map[string]interface{}, offset, pageSize i
|
||||
toStatus = params["toStatus"].(int)
|
||||
}
|
||||
sql += " AND t1.status >= ? AND t1.status <= ?"
|
||||
params2 = append(params2, fromStatus, toStatus)
|
||||
sqlParams = append(sqlParams, fromStatus, toStatus)
|
||||
}
|
||||
if vendorStoreCond := strings.ToUpper(utils.Interface2String(params["vendorStoreCond"])); vendorStoreCond == "AND" || vendorStoreCond == "OR" {
|
||||
if vendorStoreCond == "AND" {
|
||||
sql += " AND ( 1 = 1"
|
||||
} else {
|
||||
sql += " AND ( 1 = 0"
|
||||
}
|
||||
condMap := map[string]int{
|
||||
"jdm": utils.Interface2DirectIntWithDefault(params["jdCond"], 0),
|
||||
"elmm": utils.Interface2DirectIntWithDefault(params["elmCond"], 0),
|
||||
"ebaim": utils.Interface2DirectIntWithDefault(params["ebaiCond"], 0),
|
||||
}
|
||||
for tableName, cond := range condMap {
|
||||
if cond != 0 {
|
||||
sql += " " + vendorStoreCond + " " + tableName
|
||||
}
|
||||
if cond == -1 {
|
||||
sql += ".vendor_store_id IS NULL"
|
||||
} else if cond == 1 {
|
||||
sql += ".vendor_store_id IS NOT NULL"
|
||||
}
|
||||
}
|
||||
sql += ")"
|
||||
}
|
||||
sqlCount := "SELECT COUNT(*) ct\n" + sql
|
||||
type tcount struct {
|
||||
@@ -132,8 +166,8 @@ func GetStores(keyword string, params map[string]interface{}, offset, pageSize i
|
||||
}
|
||||
countInfo := []tcount{}
|
||||
db := dao.GetDB()
|
||||
if err = dao.GetRows(db, &countInfo, sqlCount, params2...); err == nil {
|
||||
sqlData := "SELECT t1.*, city.name city_name, district.name district_name\n" + sql + `
|
||||
if err = dao.GetRows(db, &countInfo, sqlCount, sqlParams...); err == nil {
|
||||
sqlData := "SELECT t1.*, city.name city_name, district.name district_name, jdm.vendor_store_id jd_id, elmm.vendor_store_id elm_id, ebaim.vendor_store_id ebai_id\n" + sql + `
|
||||
ORDER BY id
|
||||
LIMIT ? OFFSET ?`
|
||||
if pageSize == 0 {
|
||||
@@ -142,11 +176,11 @@ func GetStores(keyword string, params map[string]interface{}, offset, pageSize i
|
||||
if offset < 0 {
|
||||
offset = 0
|
||||
}
|
||||
params2 = append(params2, pageSize, offset)
|
||||
sqlParams = append(sqlParams, pageSize, offset)
|
||||
retVal := &StoresInfo{
|
||||
TotalCount: countInfo[0].Ct,
|
||||
}
|
||||
err = dao.GetRows(db, &retVal.Stores, sqlData, params2...)
|
||||
err = dao.GetRows(db, &retVal.Stores, sqlData, sqlParams...)
|
||||
return retVal, err
|
||||
}
|
||||
return nil, err
|
||||
@@ -174,18 +208,23 @@ func GetVendorStore(vendorStoreID string, vendorID int) (retVal *StoreExt, err e
|
||||
return nil, ErrCanNotVendor
|
||||
}
|
||||
|
||||
func UpdateStore(params map[string]interface{}, userName string) (err error) {
|
||||
func UpdateStore(storeID int, payload map[string]interface{}, userName string) (num int64, err error) {
|
||||
store := &model.Store{}
|
||||
if params["id"] == nil {
|
||||
return ErrMissingInput
|
||||
store.ID = storeID
|
||||
valid := jxutils.NormalMakeMapByStructObject(payload, &model.Store{}, userName)
|
||||
db := dao.GetDB()
|
||||
globals.SugarLogger.Debug("1")
|
||||
if num, err = dao.UpdateEntityByKV(db, store, valid, nil); err == nil {
|
||||
globals.SugarLogger.Debug("2")
|
||||
dummy := &model.StoreMap{}
|
||||
_, err2 := dao.UpdateEntityByKV(db, dummy, utils.Params2Map("SyncStatus", 2), utils.Params2Map("StoreID", store.ID))
|
||||
if err = err2; err == nil {
|
||||
if err = dao.GetEntity(db, store); err == nil {
|
||||
err = SyncStore2Vendor(db, -1, store, false, userName)
|
||||
}
|
||||
}
|
||||
}
|
||||
params["lastOperator"] = userName
|
||||
params["updatedAt"] = time.Now()
|
||||
|
||||
store.ID = int(utils.MustInterface2Int64(params["id"]))
|
||||
valid, _ := jxutils.NormalFilterMapByStructObject(params, &model.Store{})
|
||||
err = dao.UpdateEntityByKV(nil, store, valid, nil)
|
||||
return err
|
||||
return num, err
|
||||
}
|
||||
|
||||
func CreateStore(store *model.Store, userName string) (id int, err error) {
|
||||
@@ -198,3 +237,57 @@ func CreateStore(store *model.Store, userName string) (id int, err error) {
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
|
||||
func SyncStore2Vendor(db *dao.DaoDB, vendorID int, store *model.Store, isForce bool, userName string) (err error) {
|
||||
storeMaps, err := GetStoreVendorMaps(db, store.ID)
|
||||
if err == nil {
|
||||
// globals.SugarLogger.Debug(utils.Format4Output(store, false))
|
||||
for _, storeMap := range storeMaps {
|
||||
if (vendorID == -1 || storeMap.VendorID == vendorID) && (isForce || storeMap.SyncStatus != 0) {
|
||||
if err = GetPurchaseHandler(storeMap.VendorID).UpdateStore(storeMap.VendorStoreID, store, userName); err == nil {
|
||||
storeMap.SyncStatus = 0
|
||||
dao.UpdateEntity(db, storeMap, "SyncStatus")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func GetStoreVendorMaps(db *dao.DaoDB, storeID int) (storeMaps []*model.StoreMap, err error) {
|
||||
err = dao.GetRows(db, &storeMaps, "SELECT * FROM store_map WHERE store_id = ?", storeID)
|
||||
return storeMaps, err
|
||||
}
|
||||
|
||||
func AddStoreVendorMap(db *dao.DaoDB, storeMap *model.StoreMap, userName string) (outStoreMap *model.StoreMap, err error) {
|
||||
store, err := GetPurchaseHandler(storeMap.VendorID).ReadStore(storeMap.VendorStoreID)
|
||||
if err == nil {
|
||||
storeMap.DeliveryType = store.DeliveryType
|
||||
outStoreMap = storeMap
|
||||
if err = dao.CreateEntity(db, storeMap); err == nil {
|
||||
|
||||
}
|
||||
}
|
||||
return outStoreMap, err
|
||||
}
|
||||
|
||||
func DeleteStoreVendorMap(db *dao.DaoDB, storeID, vendorID int, userName string) (num int64, err error) {
|
||||
storeMap := &model.StoreMap{}
|
||||
return dao.DeleteEntity(db, storeMap, utils.Params2Map("StoreID", storeID, "VendorID", vendorID))
|
||||
}
|
||||
|
||||
func UpdateStoreVendorMap(db *dao.DaoDB, storeID, vendorID int, payload map[string]interface{}, userName string) (num int64, err error) {
|
||||
if vendorStoreID := utils.Interface2String(payload["vendorStoreID"]); vendorStoreID != "" {
|
||||
store, err2 := GetPurchaseHandler(vendorID).ReadStore(vendorStoreID)
|
||||
if err = err2; err == nil {
|
||||
payload["deliveryType"] = store.DeliveryType
|
||||
}
|
||||
}
|
||||
if err == nil {
|
||||
dummy := &model.StoreMap{}
|
||||
valid := jxutils.NormalMakeMapByStructObject(payload, dummy, userName)
|
||||
globals.SugarLogger.Debug(utils.Format4Output(valid, false))
|
||||
num, err = dao.UpdateEntityByKV(db, dummy, valid, utils.Params2Map("StoreID", storeID, "VendorID", vendorID))
|
||||
}
|
||||
return num, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user