298 lines
9.8 KiB
Go
298 lines
9.8 KiB
Go
package cms
|
|
|
|
import (
|
|
"errors"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.rosy.net.cn/baseapi/utils"
|
|
"git.rosy.net.cn/jx-callback/business/jxcallback/scheduler/basesch"
|
|
"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"
|
|
)
|
|
|
|
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 {
|
|
TotalCount int `json:"totalCount"`
|
|
Stores []*StoreExt `json:"stores"`
|
|
}
|
|
|
|
var (
|
|
ErrMissingInput = errors.New("没有有效的输入参数")
|
|
ErrCanNotVendor = errors.New("vendorID参数不合法")
|
|
)
|
|
|
|
func GetPlaces(parentCode int, vendorID int, includeDisabled bool) ([]*model.Place, error) {
|
|
db := dao.GetDB()
|
|
places := []*model.Place{}
|
|
sql := "SELECT * FROM place WHERE enabled = 1 "
|
|
if includeDisabled {
|
|
sql = "1 = 1 "
|
|
}
|
|
if vendorID >= 0 {
|
|
if vendorID == model.VendorIDJD {
|
|
return places, dao.GetRows(db, &places, sql+"AND jd_code <> 0 AND level >= 2")
|
|
}
|
|
return nil, ErrHaveNotImplementedYet
|
|
}
|
|
return places, dao.GetRows(db, &places, sql+"AND parent_code = ?", parentCode)
|
|
}
|
|
|
|
func UpdatePlaces(places []map[string]interface{}, userName string) (num int64, err error) {
|
|
if len(places) == 0 {
|
|
return 0, ErrMissingInput
|
|
}
|
|
for _, place := range places {
|
|
if place["code"] == nil {
|
|
return 0, ErrMissingInput
|
|
}
|
|
placeid := &model.Place{}
|
|
globals.SugarLogger.Debug(utils.Format4Output(place, false))
|
|
valid, _ := jxutils.FilterMapByFieldList(place, []string{"jdCode", "enabled", "mtpsPrice"})
|
|
valid["updatedAt"] = time.Now()
|
|
valid["lastOperator"] = userName
|
|
globals.SugarLogger.Debug(valid)
|
|
if num, err = dao.UpdateEntityByKV(nil, placeid, valid, utils.Params2Map("Code", place["code"])); err != nil {
|
|
return num, 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) {
|
|
sql := `
|
|
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`
|
|
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 ?"
|
|
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 = ?"
|
|
sqlParams = append(sqlParams, keywordInt64, keywordInt64, keywordInt64, keywordInt64, keywordInt64)
|
|
}
|
|
sql += ")"
|
|
} else {
|
|
sql += " 1 = 1"
|
|
}
|
|
|
|
if params["id"] != nil {
|
|
sql += " AND t1.id = ?"
|
|
sqlParams = append(sqlParams, params["id"].(int))
|
|
}
|
|
if params["name"] != nil {
|
|
sql += " AND t1.name LIKE ?"
|
|
sqlParams = append(sqlParams, "%"+params["name"].(string)+"%")
|
|
}
|
|
if params["placeID"] != nil {
|
|
level := 2
|
|
if params["placeLevel"] != nil {
|
|
level = params["placeLevel"].(int)
|
|
}
|
|
if level == 2 {
|
|
sql += " AND t1.city_code = ?"
|
|
} else {
|
|
sql += " AND t1.district_code = ?"
|
|
}
|
|
sqlParams = append(sqlParams, params["placeID"].(int))
|
|
}
|
|
if params["address"] != nil {
|
|
sql += " AND t1.address LIKE ?"
|
|
sqlParams = append(sqlParams, "%"+params["address"].(string)+"%")
|
|
}
|
|
if params["tel"] != nil {
|
|
sql += " AND (t1.tel1 LIKE ? OR t1.tel2 LIKE ?)"
|
|
sqlParams = append(sqlParams, "%"+params["tel"].(string)+"%")
|
|
sqlParams = append(sqlParams, "%"+params["tel"].(string)+"%")
|
|
}
|
|
if params["fromStatus"] != nil {
|
|
fromStatus := params["fromStatus"].(int)
|
|
toStatus := fromStatus
|
|
if params["toStatus"] != nil {
|
|
toStatus = params["toStatus"].(int)
|
|
}
|
|
sql += " AND t1.status >= ? AND t1.status <= ?"
|
|
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 {
|
|
Ct int
|
|
}
|
|
countInfo := []tcount{}
|
|
db := dao.GetDB()
|
|
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 {
|
|
pageSize = model.DefPageSize
|
|
}
|
|
if offset < 0 {
|
|
offset = 0
|
|
}
|
|
sqlParams = append(sqlParams, pageSize, offset)
|
|
retVal := &StoresInfo{
|
|
TotalCount: countInfo[0].Ct,
|
|
}
|
|
err = dao.GetRows(db, &retVal.Stores, sqlData, sqlParams...)
|
|
return retVal, err
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
func GetVendorStore(vendorStoreID string, vendorID int) (retVal *StoreExt, err error) {
|
|
handler := basesch.FixedBaseScheduler.GetPurchasePlatformFromVendorID(vendorID)
|
|
if handler != nil {
|
|
result, err2 := handler.ReadStore(vendorStoreID)
|
|
if err = err2; err == nil {
|
|
retVal = &StoreExt{
|
|
Store: *result,
|
|
}
|
|
db := dao.GetDB()
|
|
if city, err2 := dao.GetPlaceByCode(db, result.CityCode); err2 == nil {
|
|
retVal.CityName = city.Name
|
|
}
|
|
if district, err2 := dao.GetPlaceByCode(db, result.DistrictCode); err2 == nil {
|
|
retVal.DistrictName = district.Name
|
|
}
|
|
return retVal, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
return nil, ErrCanNotVendor
|
|
}
|
|
|
|
func UpdateStore(storeID int, payload map[string]interface{}, userName string) (num int64, err error) {
|
|
store := &model.Store{}
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
return num, err
|
|
}
|
|
|
|
func CreateStore(store *model.Store, userName string) (id int, err error) {
|
|
store.ID = 0
|
|
store.LastOperator = userName
|
|
store.CreatedAt = time.Now()
|
|
store.UpdatedAt = store.CreatedAt
|
|
if err = dao.CreateEntity(nil, store); err == nil {
|
|
return store.ID, nil
|
|
}
|
|
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, -1)
|
|
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, vendorID int) (storeMaps []*model.StoreMap, err error) {
|
|
if vendorID == -1 {
|
|
err = dao.GetRows(db, &storeMaps, "SELECT * FROM store_map WHERE store_id = ?", storeID)
|
|
} else {
|
|
err = dao.GetRows(db, &storeMaps, "SELECT * FROM store_map WHERE store_id = ? AND vendor_id = ?", storeID, vendorID)
|
|
}
|
|
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
|
|
}
|