- cms store management.
This commit is contained in:
@@ -2,14 +2,26 @@ package cms
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
"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/gormdb"
|
||||
)
|
||||
|
||||
type StoreExt struct {
|
||||
*model.Store
|
||||
CityName string `json:"cityName"`
|
||||
DistrictName string `json:"districtName"`
|
||||
}
|
||||
|
||||
var (
|
||||
ErrMissingInput = errors.New("没有有效的输入参数")
|
||||
ErrCanNotVendor = errors.New("vendorID参数不合法")
|
||||
)
|
||||
|
||||
func GetPlaces(parentCode int, vendorID int, includeDisabled bool) ([]*model.Place, error) {
|
||||
@@ -47,3 +59,145 @@ func UpdatePlaces(places []*model.Place, userName string) (err error) {
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func GetStores(params map[string]interface{}, offset, pageSize int) (retVal []*StoreExt, err error) {
|
||||
sql := `
|
||||
SELECT t1.*, city.name city_name, district.name district_name
|
||||
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
|
||||
WHERE 1 = 1`
|
||||
|
||||
params2 := make([]interface{}, 0)
|
||||
if params["id"] != nil {
|
||||
sql += " AND t1.id = ?"
|
||||
params2 = append(params2, params["id"].(int))
|
||||
}
|
||||
if params["name"] != nil {
|
||||
sql += " AND t1.name LIKE ?"
|
||||
params2 = append(params2, "%"+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 = ?"
|
||||
}
|
||||
params2 = append(params2, params["placeID"].(int))
|
||||
}
|
||||
if params["address"] != nil {
|
||||
sql += " AND t1.address LIKE ?"
|
||||
params2 = append(params2, "%"+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)+"%")
|
||||
}
|
||||
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 <= ?"
|
||||
params2 = append(params2, fromStatus, toStatus)
|
||||
}
|
||||
sql += `
|
||||
ORDER BY id
|
||||
LIMIT ? OFFSET ?`
|
||||
if pageSize == 0 {
|
||||
pageSize = model.DefPageSize
|
||||
}
|
||||
if offset < 0 {
|
||||
offset = 0
|
||||
}
|
||||
params2 = append(params2, pageSize, offset)
|
||||
|
||||
err = dao.GetRows(nil, &retVal, sql, params2...)
|
||||
return retVal, err
|
||||
}
|
||||
|
||||
func SearchStores(keyword string, fromStatus, toStatus int, offset, pageSize int) (retVal []*StoreExt, err error) {
|
||||
sql := `
|
||||
SELECT t1.*, city.name city_name, district.name district_name
|
||||
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
|
||||
WHERE t1.name LIKE ? OR t1.address LIKE ? OR t1.tel1 LIKE ? OR t1.tel2 LIKE ? OR t1.last_operator LIKE ?`
|
||||
keywordLike := "%" + keyword + "%"
|
||||
params := []interface{}{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 = ?"
|
||||
params = append(params, keywordInt64, keywordInt64, keywordInt64, keywordInt64, keywordInt64)
|
||||
}
|
||||
|
||||
sql += " AND t1.status >= ? AND t1.status <= ?"
|
||||
params = append(params, fromStatus, toStatus)
|
||||
|
||||
sql += `
|
||||
ORDER BY id
|
||||
LIMIT ? OFFSET ?`
|
||||
if pageSize == 0 {
|
||||
pageSize = model.DefPageSize
|
||||
}
|
||||
if offset < 0 {
|
||||
offset = 0
|
||||
}
|
||||
params = append(params, pageSize, offset)
|
||||
|
||||
err = dao.GetRows(nil, &retVal, sql, params...)
|
||||
return retVal, 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 := gormdb.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(params map[string]interface{}, userName string) (err error) {
|
||||
store := &model.Store{}
|
||||
if params["id"] == nil {
|
||||
return ErrMissingInput
|
||||
}
|
||||
params["lastOperator"] = userName
|
||||
params["updatedAt"] = time.Now()
|
||||
|
||||
store.ID = int(utils.MustInterface2Int64(params["id"]))
|
||||
valid, _ := jxutils.FilterMapByStructObject(params, &model.Store{})
|
||||
err = dao.UpdateEntity(nil, store, valid)
|
||||
return 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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user