- GetPlaces support keyword.

This commit is contained in:
gazebo
2018-09-19 09:56:45 +08:00
parent 8593e8b44b
commit 57b60208e2
5 changed files with 110 additions and 47 deletions

View File

@@ -3,11 +3,13 @@ package cms
import (
"crypto/md5"
"fmt"
"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/model"
"git.rosy.net.cn/jx-callback/business/model/dao"
"git.rosy.net.cn/jx-callback/business/partner"
"git.rosy.net.cn/jx-callback/globals"
"git.rosy.net.cn/jx-callback/globals/api"
@@ -66,6 +68,61 @@ func GetQiniuUploadToken(suffix string) (upTokenInfo map[string]interface{}, err
return upTokenInfo, err
}
func GetPlaces(keyword string, includeDisabled bool, params map[string]interface{}) ([]*model.Place, error) {
sql := `
SELECT *
FROM place t1
WHERE 1 = 1
`
if !includeDisabled {
sql += " AND enabled = 1"
}
sqlParams := make([]interface{}, 0)
if keyword != "" {
sql += " AND (t1.name LIKE ?"
sqlParams = append(sqlParams, "%"+keyword+"%")
if keywordInt64, err2 := strconv.ParseInt(keyword, 10, 64); err2 == nil {
sql += " OR t1.code = ?"
sqlParams = append(sqlParams, keywordInt64)
}
sql += ")"
}
if params["parentCode"] != nil {
sql += " AND t1.parent_code = ?"
sqlParams = append(sqlParams, params["parentCode"])
}
if params["level"] != nil {
sql += " AND t1.level = ?"
sqlParams = append(sqlParams, params["level"])
}
sql += " ORDER BY t1.level, t1.name"
globals.SugarLogger.Debug(sql)
places := []*model.Place{}
return places, dao.GetRows(nil, &places, sql, sqlParams)
}
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{}
valid := dao.NormalMakeMapByFieldList(place, []string{"jdCode", "enabled", "mtpsPrice"}, userName)
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 genPicFileName(suffix string) string {
return fmt.Sprintf("%x%s", md5.Sum([]byte(utils.GetUUID()+suffix)), suffix)

View File

@@ -29,40 +29,6 @@ var (
ErrCanNotFindVendor = errors.New("vendorID参数不合法")
)
func GetPlaces(parentCode int, includeDisabled bool) ([]*model.Place, error) {
db := dao.GetDB()
places := []*model.Place{}
cond := map[string]interface{}{
"ParentCode": parentCode,
}
if !includeDisabled {
cond["Enabled"] = 1
}
return places, dao.GetEntities(db, &places, cond, false)
}
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{}
valid := dao.NormalMakeMapByFieldList(place, []string{"jdCode", "enabled", "mtpsPrice"}, userName)
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)
}
// todo 门店绑定信息可以考虑以数组形式返回,而不是现在这样
func GetStores(keyword string, params map[string]interface{}, offset, pageSize int) (retVal *StoresInfo, err error) {
sqlFrom := `

View File

@@ -12,6 +12,7 @@ import (
"git.rosy.net.cn/jx-callback/business/model"
"git.rosy.net.cn/jx-callback/business/model/dao"
"git.rosy.net.cn/jx-callback/business/partner"
"git.rosy.net.cn/jx-callback/globals"
"git.rosy.net.cn/jx-callback/globals/api"
)
@@ -19,6 +20,13 @@ const (
VendorStorePrefix = "好菜鲜生"
)
type tEbaiStoreInfo struct {
model.Store
VendorStoreID string `orm:"column(vendor_store_id)"`
RealLastOperator string
EbaiStoreStatus int
}
func (p *PurchaseHandler) ReadStore(vendorStoreID string) (*model.Store, error) {
baiduShopID := utils.Str2Int64(vendorStoreID)
result, err := api.EbaiAPI.ShopGet("", baiduShopID)
@@ -70,20 +78,34 @@ func (p *PurchaseHandler) ReadStore(vendorStoreID string) (*model.Store, error)
retVal.ID = int(utils.Str2Int64WithDefault(utils.Interface2String(result["shop_id"]), 0))
retVal.DeliveryRangeType = model.DeliveryRangeTypePolygon
region := result["delivery_region"].([]interface{})[0].(map[string]interface{})["region"].([]interface{})[0].([]interface{})
coords := make([]string, len(region))
for k, v := range region {
mapV := v.(map[string]interface{})
coords[k] = fmt.Sprintf("%d,%d", jxutils.StandardCoordinate2Int(utils.MustInterface2Float64(mapV["longitude"])), jxutils.StandardCoordinate2Int(utils.MustInterface2Float64(mapV["latitude"])))
}
retVal.DeliveryRange = strings.Join(coords, ";")
retVal.DeliveryRange = EbaiDeliveryRegion2Jx(result["delivery_region"])
return retVal, nil
}
return nil, err
}
func (p *PurchaseHandler) UpdateStore(storeID int, userName string) error {
return nil
func (p *PurchaseHandler) UpdateStore(storeID int, userName string) (err error) {
db := dao.GetDB()
var store tEbaiStoreInfo
sql := `
SELECT t1.*, t2.status ebai_store_status, t2.vendor_store_id,
IF(t1.updated_at > t2.updated_at, t1.last_operator, t2.last_operator) real_last_operator
FROM store t1
JOIN store_map t2 ON t1.id = t2.store_id AND t2.vendor_id = ?
WHERE t1.id = ?`
if err = dao.GetRow(db, &store, sql, model.VendorIDJD, storeID); err == nil {
params := map[string]interface{}{
"name": jxutils.ComposeStoreName(store.Name, partner.StoreNameSeparator, VendorStorePrefix),
"address": store.Address,
"coord_type": ebaiapi.CoordTypeAutonavi, // 一直用高德
}
// globals.SugarLogger.Debug(utils.Format4Output(params, false))
if globals.EnableStoreWrite {
err = api.JdAPI.UpdateStoreInfo4Open(store.VendorStoreID, store.RealLastOperator, params)
}
}
return err
}
func EbaiDeliveryType2Jx(deliveryType string) int8 {
@@ -100,3 +122,17 @@ func EbaiDeliveryType2Jx(deliveryType string) int8 {
return scheduler.StoreDeliveryTypeCrowdSourcing
}
}
func EbaiDeliveryRegion2Jx(deliveryRegion interface{}) string {
region := deliveryRegion.([]interface{})[0].(map[string]interface{})["region"].([]interface{})[0].([]interface{})
coords := make([]string, len(region))
for k, v := range region {
mapV := v.(map[string]interface{})
coords[k] = fmt.Sprintf("%d,%d", jxutils.StandardCoordinate2Int(utils.MustInterface2Float64(mapV["longitude"])), jxutils.StandardCoordinate2Int(utils.MustInterface2Float64(mapV["latitude"])))
}
return strings.Join(coords, ";")
}
func JxDeliveryRegion2Ebai(coords string) string {
return ""
}

View File

@@ -97,6 +97,8 @@ func (p *PurchaseHandler) UpdateStore(storeID int, userName string) (err error)
"serviceTimeEnd1": JxOperationTime2JdOperationTime(store.CloseTime1),
"deliveryRangeType": store.DeliveryRangeType,
"coordinateType": 3, // 一直用高德
"lng": jxutils.IntCoordinate2Standard(store.Lng),
"lat": jxutils.IntCoordinate2Standard(store.Lat),
"city": store.JdCityCode,
"county": store.JdDistrictCode,
}

View File

@@ -11,16 +11,18 @@ type CmsController struct {
}
// @Title 得到地点(省,城市,区)信息
// @Description parentCode与vendorID必传入一个vendorID的意思是得到所有与这个厂商相关的城市列表。地点级别省为1市为2区为3注意直辖市也要分省与市级
// @Param token header string true "认证token"
// @Param parentCode query int true "上级地点code这个指的是国家标准CODE中国为100000北京为110000北京市为110100不是数据库中的ID"
// @Description 得到地点(省,城市,区)信息。
// @Param token header string true "认证token"// @Param keyword query string false "查询关键字(可以为空,为空表示不限制)"
// @Param keyword query string false "查询关键字(可以为空,为空表示不限制)"
// @Param parentCode query int false "上级地点code这个指的是国家标准CODE中国为100000北京为110000北京市为110100不是数据库中的ID"
// @Param level query int false "地点级别省为1市为2区为3注意直辖市也要分省与市级"
// @Param includeDisabled query bool false "是否包括禁用的城市(缺省不包括)"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /GetPlaces [get]
func (c *CmsController) GetPlaces() {
c.callGetPlaces(func(params *tCmsGetPlacesParams) (retVal interface{}, errCode string, err error) {
retVal, err = cms.GetPlaces(params.ParentCode, params.IncludeDisabled)
retVal, err = cms.GetPlaces(params.Keyword, params.IncludeDisabled, params.MapData)
return retVal, "", err
})
}