199 lines
5.6 KiB
Go
199 lines
5.6 KiB
Go
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"`
|
|
}
|
|
|
|
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 := gormdb.GetDB()
|
|
places := []*model.Place{}
|
|
sql := "enabled = 1 "
|
|
if includeDisabled {
|
|
sql = "1 = 1 "
|
|
}
|
|
if vendorID >= 0 {
|
|
if vendorID == model.VendorIDJD {
|
|
return places, db.Where(sql + "AND jd_code <> 0 AND level >= 2").Find(&places).Error
|
|
}
|
|
return nil, ErrHaveNotImplementedYet
|
|
}
|
|
return places, db.Where(sql+"AND parent_code = ?", parentCode).Find(&places).Error
|
|
}
|
|
|
|
func UpdatePlaces(places []*model.Place, userName string) (err error) {
|
|
if len(places) == 0 {
|
|
return ErrMissingInput
|
|
}
|
|
db := gormdb.GetDB()
|
|
for _, place := range places {
|
|
params := map[string]interface{}{"enabled": place.Enabled, "updated_at": time.Now()}
|
|
if place.JdCode != 0 {
|
|
params["jd_code"] = place.JdCode
|
|
}
|
|
if place.MtpsPrice != 0 {
|
|
params["mtps_price"] = place.MtpsPrice
|
|
}
|
|
if err = db.Table("place").Where("code = ?", place.Code).Updates(params).Error; err != nil {
|
|
break
|
|
}
|
|
}
|
|
return err
|
|
}
|
|
|
|
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
|
|
WHERE`
|
|
|
|
params2 := 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)
|
|
|
|
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)
|
|
}
|
|
sql += ")"
|
|
} else {
|
|
sql += " 1 = 1"
|
|
}
|
|
|
|
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)
|
|
}
|
|
sqlCount := "SELECT COUNT(*) ct\n" + sql
|
|
type tcount struct {
|
|
Ct int
|
|
}
|
|
countInfo := []tcount{}
|
|
if err = dao.GetRows(nil, &countInfo, sqlCount, params2...); err == nil {
|
|
sqlData := "SELECT t1.*, city.name city_name, district.name district_name\n" + sql + `
|
|
ORDER BY id
|
|
LIMIT ? OFFSET ?`
|
|
if pageSize == 0 {
|
|
pageSize = model.DefPageSize
|
|
}
|
|
if offset < 0 {
|
|
offset = 0
|
|
}
|
|
params2 = append(params2, pageSize, offset)
|
|
retVal := &StoresInfo{
|
|
TotalCount: countInfo[0].Ct,
|
|
}
|
|
err = dao.GetRows(nil, &retVal.Stores, sqlData, params2...)
|
|
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 := 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
|
|
}
|