Files
jx-callback/business/jxstore/cms/cms.go
2019-01-05 14:39:23 +08:00

147 lines
4.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package cms
import (
"crypto/md5"
"fmt"
"strconv"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/jxcallback/scheduler"
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
"git.rosy.net.cn/jx-callback/business/jxutils/tasksch"
"git.rosy.net.cn/jx-callback/business/model"
"git.rosy.net.cn/jx-callback/business/model/dao"
"git.rosy.net.cn/jx-callback/globals"
"git.rosy.net.cn/jx-callback/globals/api"
"github.com/qiniu/api.v7/storage"
)
const (
qiniuTokenExpires = 300 // 七牛TOKEN有效时间5分钟
)
var (
serviceInfo map[string]interface{}
)
func InitServiceInfo(version, buildDate, gitCommit string) {
serviceInfo = map[string]interface{}{
"version": version,
"buildDate": buildDate,
"gitCommit": gitCommit,
"metaData": map[string]interface{}{
"skuNamePrefix": model.SkuNamePrefixNames,
"skuNameUnit": model.UnitNames,
"skuSpecUnit": model.SpecUnitNames,
"skuStatus": model.SkuStatusName,
"storeDeliveryRangeType": model.DeliveryRangeTypeName,
"storeDeliveryType": model.DeliveryTypeName,
"storeStatus": model.StoreStatusName,
"categoryType": model.CategoryTypeName,
"vendorName": model.VendorChineseNames,
"orderStatus": model.OrderStatusName,
"waybillStatus": model.WaybillStatusName,
"bankName": model.BankName,
"promotionStatusName": model.PromotionStatusName,
"orderTypeName": model.OrderTypeName,
"storeDeliveryTypeName": scheduler.StoreDeliveryTypeName,
"taskStatusName": tasksch.TaskStatusName,
"opRequestTypeName": model.RequestTypeName,
"opRequestStatusName": model.RequestStatusName,
"storeMsgSendStatusName": model.StoreMsgSendStatusName,
},
}
Init()
}
func GetServiceInfo(ctx *jxcontext.Context) interface{} {
return serviceInfo
}
func GetQiniuUploadToken(ctx *jxcontext.Context, suffix, hashCode string) (upTokenInfo map[string]interface{}, err error) {
imgURL := ""
if hashCode != "" {
db := dao.GetDB()
if skuName, err := dao.GetSkuNameByHashCode(db, hashCode); err == nil {
imgURL = skuName.Img
}
}
putPolicy := storage.PutPolicy{
Scope: globals.QiniuBucket,
Expires: qiniuTokenExpires,
}
upTokenInfo = map[string]interface{}{
"token": putPolicy.UploadToken(api.QiniuAPI),
"expires": putPolicy.Expires,
"fileName": genPicFileName(suffix),
"hit": imgURL != "",
"img": imgURL,
}
return upTokenInfo, err
}
func GetPlaces(ctx *jxcontext.Context, 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(ctx *jxcontext.Context, 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.UpdateEntityLogically(nil, placeid, valid, userName, utils.Params2Map("Code", place["code"])); err != nil {
return num, err
}
}
return num, err
}
func UpdatePlace(ctx *jxcontext.Context, placeCode int, payload map[string]interface{}, userName string) (num int64, err error) {
payload["code"] = placeCode
return UpdatePlaces(ctx, []map[string]interface{}{payload}, userName)
}
func GetCoordinateDistrictCode(ctx *jxcontext.Context, lng, lat float64) (code int, err error) {
return api.AutonaviAPI.GetCoordinateDistrictCode(lng, lat), nil
}
/////
func genPicFileName(suffix string) string {
return fmt.Sprintf("%x%s", md5.Sum([]byte(utils.GetUUID()+suffix)), suffix)
}