801 lines
27 KiB
Go
801 lines
27 KiB
Go
package dao
|
||
|
||
import (
|
||
"sort"
|
||
"time"
|
||
|
||
"git.rosy.net.cn/baseapi/utils"
|
||
"git.rosy.net.cn/jx-callback/business/jxutils"
|
||
"git.rosy.net.cn/jx-callback/business/model"
|
||
"git.rosy.net.cn/jx-callback/globals"
|
||
)
|
||
|
||
// 带购物平台信息的
|
||
type StoreDetail struct {
|
||
model.Store
|
||
|
||
VendorOrgCode string `orm:"size(32)" json:"vendorOrgCode"` // 同一平台下不同的商户代码,如果只有一个,可以为空
|
||
|
||
VendorStoreID string `orm:"column(vendor_store_id);size(48)" json:"vendorStoreID"`
|
||
VendorStoreName string `json:"vendorStoreName"`
|
||
VendorStatus int `json:"vendor_status"` // 取值同Store.Status
|
||
DeliveryFeeDeductionSill int `json:"deliveryFeeDeductionSill"`
|
||
DeliveryFeeDeductionFee int `json:"deliveryFeeDeductionFee"`
|
||
SyncStatus int8 `orm:"default(2)" json:"syncStatus"`
|
||
|
||
PricePercentage int16 `orm:"default(100)" json:"pricePercentage"` // todo 厂商价格相对于本地价格的百分比,这个字段的修改会比较特殊,因为可能需要刷新厂商价格
|
||
PricePercentagePackStr string `orm:"size(4096)" json:"-"` //
|
||
PricePercentagePackObj model.PricePercentagePack `orm:"-" json:"-"`
|
||
|
||
FreightDeductionPackStr string `orm:"size(4096)" json:"-"` //
|
||
FreightDeductionPackObj *model.FreightDeductionPack `orm:"-" json:"-"`
|
||
|
||
AutoPickup int8 `orm:"default(1)" json:"autoPickup"` // 是否自动拣货
|
||
DeliveryType int8 `orm:"default(0)" json:"deliveryType"` // 配送类型
|
||
DeliveryCompetition int8 `orm:"default(1)" json:"deliveryCompetition"` // 是否支持配送竞争
|
||
IsSync int8 `orm:"default(1)" json:"isSync"` // 是否同步
|
||
|
||
DistrictName string `json:"districtName"`
|
||
CityName string `json:"cityName"`
|
||
ProvinceName string `json:"provinceName"` //省名
|
||
JdsCode int `json:"jdsCode"` //京东商城地址代码
|
||
|
||
IsAutoOrder int8 `json:"isAutoOrder"` // 平台是否自动接单,-1:否,0:未知,1:是
|
||
MarketManName string `json:"marketManName"` //市场负责人
|
||
OperatorName string `json:"operatorName"` //运营负责人
|
||
OperatorName2 string `json:"operatorName2"`
|
||
OperatorName3 string `json:"operatorName3"`
|
||
|
||
JdStoreLevel string `json:"jdStoreLevel"` //京东门店等级
|
||
IsOrder int `json:"isOrder"` //是否是下预订单门店
|
||
|
||
YbAppID string `orm:"column(yb_app_id)" json:"ybAppID"`
|
||
YbAppKey string `json:"ybAppKey"`
|
||
YbStorePrefix string `json:"ybStorePrefix"`
|
||
}
|
||
|
||
// 带快递门店信息的
|
||
type StoreDetail2 struct {
|
||
model.Store
|
||
|
||
VendorID int `orm:"column(vendor_id)" json:"vendorID"`
|
||
VendorStoreID string `orm:"column(vendor_store_id)" json:"vendorStoreID"` // 这个其实是京西快递门店ID的概念
|
||
CourierStatus int `json:"courierStatus"`
|
||
AuditStatus int `json:"auditStatus"`
|
||
|
||
DistrictName string `json:"districtName"`
|
||
CityName string `json:"cityName"`
|
||
}
|
||
|
||
type CityBrankBranch struct {
|
||
CityCode int
|
||
PayeeBankBranchName string `orm:"size(255)" json:"payeeBankBranchName"` // 开户支行
|
||
PayeeBankCode string `orm:"size(8)" json:"payeeBankCode"` // 开户行代码
|
||
}
|
||
|
||
type StorePriceScore struct {
|
||
StoreID int `orm:"column(store_id)" json:"storeID"`
|
||
StoreName string `json:"storeName"`
|
||
StoreScore float64 `json:"storeScore"`
|
||
CityName string `json:"cityName"`
|
||
DirectDownCount int `json:"directDownCount"`
|
||
SecKillCount int `json:"secKillCount"`
|
||
}
|
||
|
||
type StorePriceScoreEx struct {
|
||
StorePriceScoreList []*StorePriceScore `json:"storePriceScoreList"`
|
||
TotalCount int `json:"totalCount"`
|
||
}
|
||
|
||
func (s *StoreDetail) GetPricePerentage(price int) (pricePercentage int) {
|
||
return pricePercentage
|
||
}
|
||
|
||
func getStoreDetail(db *DaoDB, storeID, vendorID int, vendorStoreID string) (storeDetail *StoreDetail, err error) {
|
||
sql := `
|
||
SELECT t1.*,
|
||
t2.vendor_store_id, t2.status vendor_status, t2.delivery_fee_deduction_sill, t2.delivery_fee_deduction_fee, t2.sync_status, t2.vendor_org_code,
|
||
t2.price_percentage, t2.auto_pickup, t2.delivery_type, t2.delivery_competition, t2.is_sync, t2.vendor_store_name, t2.is_order, t2.yb_app_id, t2.yb_app_key, t2.yb_store_prefix,
|
||
t3.value price_percentage_pack_str,
|
||
t4.value freight_deduction_pack_str,
|
||
province.name province_name,
|
||
district.name district_name,
|
||
district.jds_code jds_code,
|
||
city.name city_name,
|
||
IF(mm.name <> '', mm.name, mm.user_id2) market_man_name,
|
||
IF(om.name <> '', om.name, om.user_id2) operator_name,
|
||
IF(om2.name <> '', om2.name, om2.user_id2) operator_name2,
|
||
IF(om3.name <> '', om3.name, om3.user_id2) operator_name3
|
||
FROM store t1
|
||
LEFT JOIN store_map t2 ON t1.id = t2.store_id AND t2.vendor_id = ? AND t2.deleted_at = ?
|
||
LEFT JOIN place city ON city.code = t1.city_code
|
||
LEFT JOIN place district ON district.code = t1.district_code
|
||
LEFT JOIN place province ON city.parent_code = province.code
|
||
LEFT JOIN new_config t3 ON t3.key = t2.price_percentage_pack AND t3.type = ? AND t3.deleted_at = ?
|
||
LEFT JOIN new_config t4 ON t4.key = t2.freight_deduction_pack AND t4.type = ? AND t4.deleted_at = ?
|
||
LEFT JOIN user mm ON mm.mobile <> '' AND mm.mobile = t1.market_man_phone
|
||
LEFT JOIN user om ON om.mobile <> '' AND om.mobile = t1.operator_phone
|
||
LEFT JOIN user om2 ON om2.mobile <> '' AND om2.mobile = t1.operator_phone2
|
||
LEFT JOIN user om3 ON om3.mobile <> '' AND om3.mobile = t1.operator_phone3
|
||
WHERE t1.deleted_at = ?
|
||
`
|
||
sqlParams := []interface{}{
|
||
vendorID,
|
||
utils.DefaultTimeValue,
|
||
model.ConfigTypePricePack,
|
||
utils.DefaultTimeValue,
|
||
model.ConfigTypeFreightPack,
|
||
utils.DefaultTimeValue,
|
||
utils.DefaultTimeValue,
|
||
}
|
||
// if vendorID != model.VendorIDJX {
|
||
// sql += " AND t2.id IS NOT NULL"
|
||
// }
|
||
if storeID > 0 {
|
||
sql += " AND t1.id = ?"
|
||
sqlParams = append(sqlParams, storeID)
|
||
}
|
||
if vendorStoreID != "" {
|
||
sql += " AND t2.vendor_store_id = ?"
|
||
sqlParams = append(sqlParams, vendorStoreID)
|
||
}
|
||
if err = GetRow(db, &storeDetail, sql, sqlParams...); err == nil {
|
||
storeDetail.PricePercentagePackObj = PricePercentagePack2Obj(storeDetail.PricePercentagePackStr)
|
||
storeDetail.FreightDeductionPackObj = FreightDeductionPack2Obj(storeDetail.FreightDeductionPackStr)
|
||
// if storeDetail.VendorStoreID == "" {
|
||
// storeDetail.VendorStatus = storeDetail.Status
|
||
// storeDetail.PricePercentage = model.DefVendorPricePercentage
|
||
// storeDetail.AutoPickup = 1
|
||
// storeDetail.DeliveryType = model.StoreDeliveryTypeByStore
|
||
// storeDetail.DeliveryCompetition = 1
|
||
// if vendorID == model.VendorIDJX {
|
||
// storeDetail.IsSync = 1
|
||
// }
|
||
// }
|
||
return storeDetail, nil
|
||
}
|
||
return nil, err
|
||
}
|
||
|
||
func GetStoreDetail(db *DaoDB, storeID, vendorID int) (storeDetail *StoreDetail, err error) {
|
||
return getStoreDetail(db, storeID, vendorID, "")
|
||
}
|
||
|
||
func GetStoreDetailByVendorStoreID(db *DaoDB, vendorStoreID string, vendorID int) (storeDetail *StoreDetail, err error) {
|
||
if vendorID != model.VendorIDJX {
|
||
return getStoreDetail(db, 0, vendorID, vendorStoreID)
|
||
}
|
||
if storeDetail, err = getStoreDetail(db, int(utils.Str2Int64WithDefault(vendorStoreID, 0)), vendorID, ""); err == nil {
|
||
storeDetail.VendorStoreID = vendorStoreID
|
||
}
|
||
return storeDetail, err
|
||
}
|
||
|
||
// 这个返回的地点信息是城市
|
||
func GetStoreDetail2(db *DaoDB, storeID int, vendorStoreID string, vendorID int) (storeDetail *StoreDetail2, err error) {
|
||
sql := `
|
||
SELECT t1.*,
|
||
city.name city_name, district.name district_name,
|
||
t3.vendor_store_id, t3.vendor_id, t3.status courier_status, t3.audit_status
|
||
FROM store t1
|
||
LEFT JOIN place city ON city.code = t1.city_code
|
||
LEFT JOIN place district ON district.code = t1.district_code
|
||
LEFT JOIN store_courier_map t3 ON t3.store_id = t1.id AND t3.vendor_id = ? AND t3.deleted_at = ?
|
||
WHERE t1.deleted_at = ?`
|
||
sqlParams := []interface{}{
|
||
vendorID,
|
||
utils.DefaultTimeValue,
|
||
utils.DefaultTimeValue,
|
||
}
|
||
if storeID != 0 {
|
||
sql += " AND t1.id = ?"
|
||
sqlParams = append(sqlParams, storeID)
|
||
}
|
||
if vendorStoreID != "" {
|
||
sql += " AND t3.vendor_store_id = ?"
|
||
sqlParams = append(sqlParams, vendorStoreID)
|
||
}
|
||
err = GetRow(db, &storeDetail, sql, sqlParams...)
|
||
return storeDetail, err
|
||
}
|
||
|
||
func GetStoreCourierList(db *DaoDB, storeIDs []int, status, auditStatus int) (courierStoreList []*model.StoreCourierMap, err error) {
|
||
sql := `
|
||
SELECT t1.*
|
||
FROM store_courier_map t1
|
||
WHERE t1.deleted_at = ?
|
||
`
|
||
sqlParams := []interface{}{
|
||
utils.DefaultTimeValue,
|
||
}
|
||
if len(storeIDs) > 0 {
|
||
sql += " AND t1.store_id IN (" + GenQuestionMarks(len(storeIDs)) + ")"
|
||
sqlParams = append(sqlParams, storeIDs)
|
||
}
|
||
if status != model.StoreStatusAll {
|
||
sql += " AND t1.status = ?"
|
||
sqlParams = append(sqlParams, status)
|
||
}
|
||
if auditStatus != model.StoreAuditStatusAll {
|
||
sql += " AND t1.audit_status = ?"
|
||
sqlParams = append(sqlParams, auditStatus)
|
||
}
|
||
if err = GetRows(db, &courierStoreList, sql, sqlParams...); err == nil {
|
||
return courierStoreList, nil
|
||
}
|
||
return nil, err
|
||
}
|
||
|
||
func GetStoresMapList2(db *DaoDB, vendorIDs, storeIDs, storeStatuss []int, status, isSync int, pricePack string, mustDirty bool) (storeMapList []*model.StoreMap, err error) {
|
||
sql := `
|
||
SELECT t1.*
|
||
FROM store_map t1
|
||
JOIN store t2 ON t2.id = t1.store_id AND t2.deleted_at = ?
|
||
WHERE t1.deleted_at = ?
|
||
`
|
||
sqlParams := []interface{}{
|
||
utils.DefaultTimeValue,
|
||
utils.DefaultTimeValue,
|
||
}
|
||
if len(vendorIDs) > 0 {
|
||
sql += " AND t1.vendor_id IN (" + GenQuestionMarks(len(vendorIDs)) + ")"
|
||
sqlParams = append(sqlParams, vendorIDs)
|
||
}
|
||
if len(storeIDs) > 0 {
|
||
sql += " AND t1.store_id IN (" + GenQuestionMarks(len(storeIDs)) + ")"
|
||
sqlParams = append(sqlParams, storeIDs)
|
||
}
|
||
if len(storeStatuss) > 0 {
|
||
sql += " AND t2.status IN (" + GenQuestionMarks(len(storeStatuss)) + ")"
|
||
sqlParams = append(sqlParams, storeStatuss)
|
||
}
|
||
if status != model.StoreStatusAll {
|
||
sql += " AND t1.status = ?"
|
||
sqlParams = append(sqlParams, status)
|
||
}
|
||
if isSync != model.StoreIsSyncAll {
|
||
sql += " AND t1.is_sync = ?"
|
||
sqlParams = append(sqlParams, isSync)
|
||
}
|
||
if pricePack != "" {
|
||
sql += " AND t1.price_percentage_pack = ?"
|
||
sqlParams = append(sqlParams, pricePack)
|
||
}
|
||
if mustDirty {
|
||
sql += " AND t1.sync_status <> 0"
|
||
}
|
||
sql += " ORDER BY t1.store_id DESC, t1.vendor_id"
|
||
if err = GetRows(db, &storeMapList, sql, sqlParams...); err == nil {
|
||
return storeMapList, nil
|
||
}
|
||
return nil, err
|
||
}
|
||
|
||
func SetStoresMapSyncStatus(db *DaoDB, vendorIDs, storeIDs []int, syncStatus int8) (err error) {
|
||
sql := `
|
||
UPDATE store_map t1
|
||
JOIN store t2 ON t2.id = t1.store_id AND t2.deleted_at = ?
|
||
SET t1.sync_status = t1.sync_status | ?
|
||
WHERE t1.deleted_at = ?
|
||
`
|
||
sqlParams := []interface{}{
|
||
utils.DefaultTimeValue,
|
||
syncStatus,
|
||
utils.DefaultTimeValue,
|
||
}
|
||
if len(vendorIDs) > 0 {
|
||
sql += " AND t1.vendor_id IN (" + GenQuestionMarks(len(vendorIDs)) + ")"
|
||
sqlParams = append(sqlParams, vendorIDs)
|
||
}
|
||
if len(storeIDs) > 0 {
|
||
sql += " AND t1.store_id IN (" + GenQuestionMarks(len(storeIDs)) + ")"
|
||
sqlParams = append(sqlParams, storeIDs)
|
||
}
|
||
_, err = ExecuteSQL(db, sql, sqlParams...)
|
||
return err
|
||
}
|
||
|
||
func GetStoresMapList(db *DaoDB, vendorIDs, storeIDs, storeStatuss []int, status, isSync int, pricePack string) (storeMapList []*model.StoreMap, err error) {
|
||
return GetStoresMapList2(db, vendorIDs, storeIDs, storeStatuss, status, isSync, pricePack, false)
|
||
}
|
||
|
||
func StoreMapList2Map(storeMapList []*model.StoreMap) (storeMapMap map[int][]*model.StoreMap) {
|
||
storeMapMap = make(map[int][]*model.StoreMap)
|
||
for _, v := range storeMapList {
|
||
storeMapMap[v.StoreID] = append(storeMapMap[v.StoreID], v)
|
||
}
|
||
return storeMapMap
|
||
}
|
||
|
||
func StoreCourierList2Map(storeCourierList []*model.StoreCourierMap) (storeCourierMap map[int][]*model.StoreCourierMap) {
|
||
storeCourierMap = make(map[int][]*model.StoreCourierMap)
|
||
for _, v := range storeCourierList {
|
||
storeCourierMap[v.StoreID] = append(storeCourierMap[v.StoreID], v)
|
||
}
|
||
return storeCourierMap
|
||
}
|
||
|
||
// 此函数在检测到一个门店的所有平台状态一样,且不为StoreStatusOpened时,
|
||
// 将平台门店状态全部改为StoreStatusOpened,则把京西门店状态改为之前那个统一的平台门店状态
|
||
func FormalizeStoreStatus(db *DaoDB, storeID, storeStatus int) (err error) {
|
||
sql := `
|
||
SELECT DISTINCT t1.status
|
||
FROM store_map t1
|
||
WHERE t1.deleted_at = ? AND t1.store_id = ?
|
||
`
|
||
sqlParams := []interface{}{
|
||
utils.DefaultTimeValue,
|
||
storeID,
|
||
}
|
||
var statusList []int
|
||
if err = GetRows(db, &statusList, sql, sqlParams...); err == nil {
|
||
if len(statusList) == 1 {
|
||
if statusList[0] != model.StoreStatusOpened {
|
||
Begin(db)
|
||
defer func() {
|
||
if r := recover(); r != nil || err != nil {
|
||
Rollback(db)
|
||
if r != nil {
|
||
panic(r)
|
||
}
|
||
}
|
||
}()
|
||
if storeStatus != statusList[0] {
|
||
store := &model.Store{}
|
||
store.ID = storeID
|
||
if _, err = UpdateEntityLogically(db, store, map[string]interface{}{
|
||
model.FieldStatus: statusList[0],
|
||
}, model.AdminName, nil); err != nil {
|
||
return err
|
||
}
|
||
}
|
||
if _, err = UpdateEntityLogically(db, &model.StoreMap{}, map[string]interface{}{
|
||
model.FieldStatus: model.StoreStatusOpened,
|
||
}, model.AdminName, map[string]interface{}{
|
||
model.FieldStoreID: storeID,
|
||
model.FieldDeletedAt: utils.DefaultTimeValue,
|
||
}); err != nil {
|
||
return err
|
||
}
|
||
Commit(db)
|
||
}
|
||
}
|
||
}
|
||
return err
|
||
}
|
||
|
||
func GetVendorStoreSnapshot(db *DaoDB, snapshotAt time.Time) (snapshotList []*model.VendorStoreSnapshot, err error) {
|
||
sql := `
|
||
SELECT t1.*
|
||
FROM vendor_store_snapshot t1
|
||
LEFT JOIN store t2 ON t2.id = t1.store_id
|
||
WHERE t1.snapshot_at = ?
|
||
ORDER BY t2.city_code, t1.store_id, t1.vendor_id`
|
||
err = GetRows(db, &snapshotList, sql, snapshotAt)
|
||
return snapshotList, err
|
||
}
|
||
|
||
func DeleteVendorStoreSnapshot(db *DaoDB, minSnapshotAt time.Time) (err error) {
|
||
_, err = ExecuteSQL(db, `
|
||
DELETE t1
|
||
FROM vendor_store_snapshot t1
|
||
WHERE t1.snapshot_at < ?
|
||
`, minSnapshotAt)
|
||
return err
|
||
}
|
||
|
||
func GetRebindPrinterStoreList(db *DaoDB) (storeList []*model.Store, err error) {
|
||
err = GetRows(db, &storeList, `
|
||
SELECT *
|
||
FROM store t1
|
||
WHERE t1.deleted_at = ? AND printer_vendor_id >= ? AND printer_bind_info <> ''
|
||
`, utils.DefaultTimeValue, model.VendorIDPrinterBegin)
|
||
return storeList, err
|
||
}
|
||
|
||
// 容错用
|
||
type tPricePercentageItemFloat struct {
|
||
BeginPrice float64 `json:"beginPrice"` // 起始价格区间(包括)
|
||
PricePercentage float64 `json:"pricePercentage"` // 调价比例
|
||
PriceAdd float64 `json:"priceAdd"` // 调价额定值
|
||
}
|
||
|
||
func PricePercentagePack2Obj(packStr string) (obj model.PricePercentagePack) {
|
||
if packStr != "" {
|
||
var floatObj []*tPricePercentageItemFloat
|
||
if err := utils.UnmarshalUseNumber([]byte(packStr), &floatObj); err == nil {
|
||
if len(floatObj) > 0 {
|
||
obj = make(model.PricePercentagePack, len(floatObj))
|
||
for k, v := range floatObj {
|
||
if v.PricePercentage >= 500 || v.PricePercentage <= 80 {
|
||
return nil
|
||
}
|
||
obj[k] = &model.PricePercentageItem{
|
||
BeginPrice: int(v.BeginPrice),
|
||
PricePercentage: int(v.PricePercentage),
|
||
PriceAdd: int(v.PriceAdd),
|
||
}
|
||
}
|
||
sort.Sort(obj)
|
||
}
|
||
}
|
||
}
|
||
return obj
|
||
}
|
||
|
||
func AddStoreCategoryMap(db *DaoDB, storeID, categoryID int, vendorID int, vendorCategoryID string, status int8, userName string) (err error) {
|
||
storeCat := &model.StoreSkuCategoryMap{
|
||
StoreID: storeID,
|
||
CategoryID: categoryID,
|
||
MtwmSyncStatus: model.SyncFlagNewMask,
|
||
EbaiSyncStatus: model.SyncFlagNewMask,
|
||
YbSyncStatus: model.SyncFlagNewMask,
|
||
}
|
||
storeCat.DeletedAt = utils.DefaultTimeValue
|
||
if err = GetEntity(db, storeCat, model.FieldStoreID, model.FieldCategoryID, model.FieldDeletedAt); err != nil && !IsNoRowsError(err) {
|
||
return err
|
||
}
|
||
if vendorID == model.VendorIDMTWM {
|
||
storeCat.MtwmID = vendorCategoryID
|
||
storeCat.MtwmSyncStatus = status
|
||
} else if vendorID == model.VendorIDEBAI {
|
||
storeCat.EbaiID = utils.Str2Int64WithDefault(vendorCategoryID, 0)
|
||
storeCat.EbaiSyncStatus = status
|
||
} else if vendorID == model.VendorIDYB {
|
||
storeCat.YbID = utils.Str2Int64WithDefault(vendorCategoryID, 0)
|
||
storeCat.YbSyncStatus = status
|
||
} else if vendorID == model.VendorIDJDShop {
|
||
storeCat.JdsID = utils.Str2Int64WithDefault(vendorCategoryID, 0)
|
||
storeCat.JdsSyncStatus = status
|
||
} else {
|
||
panic("unsupported vendor")
|
||
}
|
||
if storeCat.ID == 0 {
|
||
WrapAddIDCULDEntity(storeCat, userName)
|
||
if err = CreateEntity(db, storeCat); IsDuplicateError(err) {
|
||
err = nil
|
||
}
|
||
} else {
|
||
WrapUpdateULEntity(storeCat, userName)
|
||
_, err = UpdateEntity(db, storeCat)
|
||
}
|
||
return err
|
||
}
|
||
|
||
func GetOpenedStoreCouriersByStoreID(db *DaoDB, storeID, vendorID int) (storeMaps []*model.StoreCourierMap, err error) {
|
||
if db == nil {
|
||
db = GetDB()
|
||
}
|
||
if err = utils.CallFuncLogError(func() error {
|
||
sql := `
|
||
SELECT *
|
||
FROM store_courier_map
|
||
WHERE store_id = ? AND status = ? AND deleted_at = ?
|
||
`
|
||
sqlParams := []interface{}{
|
||
storeID,
|
||
model.StoreStatusOpened,
|
||
utils.DefaultTimeValue,
|
||
}
|
||
if vendorID != -1 {
|
||
sql += " AND vendor_id = ?"
|
||
sqlParams = append(sqlParams, vendorID)
|
||
}
|
||
return GetRows(db, &storeMaps, sql, sqlParams...)
|
||
}, "GetStoreCouriersByStoreID storeID:%d, vendorID:%d", storeID, vendorID); err != nil {
|
||
return nil, err
|
||
}
|
||
return storeMaps, nil
|
||
}
|
||
|
||
func GetStoreList(db *DaoDB, idList, cityCodes, statuss []int, mobileList []string, shortRoleName string) (storeList []*model.Store, err error) {
|
||
sql := `
|
||
SELECT t1.*
|
||
FROM store t1
|
||
WHERE t1.deleted_at = ?`
|
||
sqlParams := []interface{}{
|
||
utils.DefaultTimeValue,
|
||
}
|
||
if len(idList) > 0 {
|
||
sql += " AND t1.id IN (" + GenQuestionMarks(len(idList)) + ")"
|
||
sqlParams = append(sqlParams, idList)
|
||
}
|
||
if len(cityCodes) > 0 {
|
||
sql += " AND t1.city_code IN (" + GenQuestionMarks(len(cityCodes)) + ")"
|
||
sqlParams = append(sqlParams, cityCodes)
|
||
}
|
||
if len(statuss) > 0 {
|
||
sql += " AND t1.status IN (" + GenQuestionMarks(len(statuss)) + ")"
|
||
sqlParams = append(sqlParams, statuss)
|
||
}
|
||
if len(mobileList) > 0 {
|
||
sql += " AND (t1.tel1 IN (" + GenQuestionMarks(len(mobileList)) + ") OR t1.tel2 IN (" + GenQuestionMarks(len(mobileList)) + "))"
|
||
sqlParams = append(sqlParams, mobileList, mobileList)
|
||
}
|
||
if shortRoleName != "" {
|
||
sql += " AND (t1.market_man_role = ? OR t1.operator_role = ? OR t1.operator_role2 = ? OR t1.operator_role3 = ?)"
|
||
sqlParams = append(sqlParams, shortRoleName, shortRoleName, shortRoleName, shortRoleName)
|
||
}
|
||
err = GetRows(db, &storeList, sql, sqlParams...)
|
||
return storeList, err
|
||
}
|
||
|
||
func GetCityBankBranches(db *DaoDB, cityCode int, bankCode string) (list []*CityBrankBranch, err error) {
|
||
sql := `
|
||
SELECT payee_bank_code, city_code, payee_bank_branch_name
|
||
FROM store
|
||
WHERE payee_bank_branch_name <> ''`
|
||
sqlParams := []interface{}{}
|
||
if cityCode > 0 {
|
||
sql += " AND city_code = ?"
|
||
sqlParams = append(sqlParams, cityCode)
|
||
} else {
|
||
sql += " AND city_code <> 0"
|
||
}
|
||
if bankCode != "" {
|
||
sql += " AND payee_bank_code = ?"
|
||
sqlParams = append(sqlParams, bankCode)
|
||
} else {
|
||
sql += " AND payee_bank_code <> ''"
|
||
}
|
||
sql += " GROUP BY 1,2,3;"
|
||
err = GetRows(db, &list, sql, sqlParams...)
|
||
return list, err
|
||
}
|
||
|
||
func FreightDeductionPack2Obj(packStr string) (obj *model.FreightDeductionPack) {
|
||
if packStr != "" {
|
||
if err := utils.UnmarshalUseNumber([]byte(packStr), &obj); err == nil {
|
||
sort.Sort(obj)
|
||
}
|
||
}
|
||
return obj
|
||
}
|
||
|
||
func GetStoreMapsListWithoutDisabled(db *DaoDB, vendorIDs []int, status int) (storeMapList []*model.StoreMap, err error) {
|
||
sql := `
|
||
SELECT *
|
||
FROM store_map
|
||
WHERE 1=1
|
||
`
|
||
sqlParams := []interface{}{}
|
||
if len(vendorIDs) > 0 {
|
||
sql += " AND vendor_id in (" + GenQuestionMarks(len(vendorIDs)) + ")"
|
||
sqlParams = append(sqlParams, vendorIDs)
|
||
}
|
||
if status != model.StoreStatusAll {
|
||
sql += " AND status != ?"
|
||
sqlParams = append(sqlParams, status)
|
||
}
|
||
err = GetRows(db, &storeMapList, sql, sqlParams...)
|
||
return storeMapList, err
|
||
}
|
||
|
||
func GetStorePriceScore(db *DaoDB, storeIDs, vendorIDs []int, fromScore, toScore, sort int, snapDate time.Time, offset, pageSize int) (StorePriceScore []*StorePriceScore, totalCount int, err error) {
|
||
sql := `
|
||
SELECT SQL_CALC_FOUND_ROWS a.store_id, score store_score, e.name city_name, b.name store_name, t2.direct_down_count, t2.sec_kill_count
|
||
FROM store_price_score_snapshot a
|
||
JOIN store b ON b.id = a.store_id
|
||
JOIN place e ON e.code = b.city_code
|
||
LEFT JOIN (SELECT t1.store_id,count(t1.type = ? OR NULL) direct_down_count, count(t1.type = ? OR NULL) sec_kill_count
|
||
FROM(
|
||
SELECT a.store_id, a.sku_id,d.type
|
||
FROM store_sku_bind a
|
||
LEFT JOIN act_store_sku b ON a.store_id = b.store_id AND b.sku_id = a.sku_id
|
||
LEFT JOIN act_map c ON c.act_id = b.act_id
|
||
LEFT JOIN act d ON d.id = c.act_id
|
||
WHERE 1=1
|
||
AND d.status = ?
|
||
`
|
||
sqlParams := []interface{}{
|
||
model.ActSkuDirectDown, model.ActSkuSecKill, model.ActStatusCreated,
|
||
}
|
||
if len(storeIDs) > 0 {
|
||
sql += " AND a.store_id IN(" + GenQuestionMarks(len(storeIDs)) + ")"
|
||
sqlParams = append(sqlParams, storeIDs)
|
||
}
|
||
if len(vendorIDs) > 0 {
|
||
sql += " AND c.vendor_id IN(" + GenQuestionMarks(len(vendorIDs)) + ")"
|
||
sqlParams = append(sqlParams, vendorIDs)
|
||
}
|
||
sql += `
|
||
AND NOW() BETWEEN d.begin_at AND d.end_at
|
||
AND a.status = ?
|
||
AND a.deleted_at = ? AND b.deleted_at = ? AND c.deleted_at = ? AND d.deleted_at = ?
|
||
GROUP BY 1,2,3)t1
|
||
GROUP BY 1)t2 ON t2.store_id = a.store_id
|
||
WHERE 1=1
|
||
`
|
||
sqlParams = append(sqlParams, model.StoreSkuBindStatusNormal, utils.DefaultTimeValue, utils.DefaultTimeValue, utils.DefaultTimeValue, utils.DefaultTimeValue)
|
||
if fromScore != 0 || toScore != 0 {
|
||
sql += " AND a.score BETWEEN ? AND ?"
|
||
sqlParams = append(sqlParams, fromScore, toScore)
|
||
}
|
||
if len(storeIDs) > 0 {
|
||
sql += " AND a.store_id IN(" + GenQuestionMarks(len(storeIDs)) + ")"
|
||
sqlParams = append(sqlParams, storeIDs)
|
||
}
|
||
if !utils.IsTimeZero(snapDate) {
|
||
sql += " AND a.snapshot_at = ?"
|
||
sqlParams = append(sqlParams, snapDate)
|
||
}
|
||
if sort == 1 {
|
||
sql += " ORDER BY a.score"
|
||
} else if sort == -1 {
|
||
sql += " ORDER BY a.score DESC"
|
||
} else if sort == 2 {
|
||
sql += " ORDER BY t2.direct_down_count"
|
||
} else if sort == -2 {
|
||
sql += " ORDER BY t2.direct_down_count DESC"
|
||
} else if sort == 3 {
|
||
sql += " ORDER BY t2.sec_kill_count"
|
||
} else if sort == -3 {
|
||
sql += " ORDER BY t2.sec_kill_count DESC"
|
||
}
|
||
sql += " LIMIT ? OFFSET ?"
|
||
sqlParams = append(sqlParams, pageSize, offset)
|
||
Begin(db)
|
||
defer Commit(db)
|
||
if err = GetRows(db, &StorePriceScore, sql, sqlParams...); err == nil {
|
||
totalCount = GetLastTotalRowCount(db)
|
||
}
|
||
return StorePriceScore, totalCount, err
|
||
}
|
||
|
||
func GetStorePriceScoreSnapshot(db *DaoDB, snapDate time.Time) (storePriceScoreSnapshot []*model.StorePriceScoreSnapshot, err error) {
|
||
sql := `
|
||
SELECT c.store_id,ROUND(count(c.unit_price * IF(d.pay_percentage < 50 , 70, d.pay_percentage) / 100 <= a.mid_unit_price or NULL)/count(*)*100,2) score
|
||
FROM price_refer_snapshot a
|
||
JOIN store_sku_bind c ON c.sku_id = a.sku_id AND c.deleted_at = ?
|
||
JOIN store d ON c.store_id = d.id AND d.city_code = a.city_code AND d.deleted_at = ? AND d.status != ?
|
||
WHERE 1=1
|
||
`
|
||
sqlParams := []interface{}{
|
||
utils.DefaultTimeValue,
|
||
utils.DefaultTimeValue, model.StoreStatusDisabled,
|
||
}
|
||
if !utils.IsTimeZero(snapDate) {
|
||
sql += " AND a.snapshot_at = ?"
|
||
sqlParams = append(sqlParams, snapDate)
|
||
}
|
||
sql += `
|
||
GROUP BY c.store_id
|
||
`
|
||
err = GetRows(db, &storePriceScoreSnapshot, sql, sqlParams...)
|
||
return storePriceScoreSnapshot, err
|
||
}
|
||
|
||
func SetStoreMapSyncStatus(db *DaoDB, vendorIDs, storeIDs []int, syncStatus int) (num int64, err error) {
|
||
globals.SugarLogger.Debugf("SetStoreMapSyncStatus, vendorIDs:%v, storeIDs:%v", vendorIDs, storeIDs)
|
||
|
||
sql := `
|
||
UPDATE store_map t1
|
||
SET t1.sync_status = t1.sync_status | ?
|
||
`
|
||
sqlParams := []interface{}{
|
||
syncStatus,
|
||
}
|
||
sql += " WHERE t1.is_sync <> 0 AND t1.deleted_at = ? AND t1.sync_status & ? = 0"
|
||
sqlParams = append(sqlParams, utils.DefaultTimeValue, model.SyncFlagDeletedMask)
|
||
if len(vendorIDs) > 0 {
|
||
sql += " AND t1.vendor_id IN (" + GenQuestionMarks(len(vendorIDs)) + ")"
|
||
sqlParams = append(sqlParams, vendorIDs)
|
||
}
|
||
if len(storeIDs) > 0 {
|
||
sql += " AND t1.store_id IN (" + GenQuestionMarks(len(storeIDs)) + ")"
|
||
sqlParams = append(sqlParams, storeIDs)
|
||
}
|
||
return ExecuteSQL(db, sql, sqlParams...)
|
||
}
|
||
|
||
func GetOrderNotifyPhones(db *DaoDB, storeID int) (phoneList []string) {
|
||
store := &model.Store{}
|
||
store.ID = storeID
|
||
if err := GetEntity(db, store); err == nil {
|
||
if store.SMSNotify != 0 {
|
||
telMap := make(map[string]int)
|
||
for _, v := range []string{store.Tel1, store.Tel2} {
|
||
if v != "" {
|
||
telMap[v] = 1
|
||
}
|
||
}
|
||
phoneList = jxutils.StringMap2List(telMap)
|
||
}
|
||
}
|
||
return phoneList
|
||
}
|
||
|
||
func GetStoreLinkStores(db *DaoDB, storeID int) (storeList []*model.Store, err error) {
|
||
sql := `
|
||
SELECT t1.*
|
||
FROM store t1
|
||
WHERE t1.link_store_id = ? AND t1.deleted_at = ?
|
||
`
|
||
sqlParams := []interface{}{
|
||
storeID,
|
||
utils.DefaultTimeValue,
|
||
}
|
||
err = GetRows(db, &storeList, sql, sqlParams...)
|
||
return storeList, err
|
||
}
|
||
|
||
func GetRealLinkStoreID(db *DaoDB, linkStoreID int) (realLinkStoreID int, err error) {
|
||
realLinkStoreID = linkStoreID
|
||
if linkStoreID != 0 {
|
||
store := &model.Store{}
|
||
store.ID = linkStoreID
|
||
if err = GetEntity(db, store); err == nil {
|
||
if store.LinkStoreID != 0 {
|
||
realLinkStoreID = store.LinkStoreID
|
||
} else {
|
||
realLinkStoreID = linkStoreID
|
||
}
|
||
}
|
||
}
|
||
return realLinkStoreID, err
|
||
}
|
||
|
||
// func GetStoreCategoryMap(db *DaoDB, parentID, storeID, categoryID int) (storeCatMaps []*StoreCatMap, err error) {
|
||
// sql := `
|
||
// SELECT a.*, b.level, b.parent_id, b.seq, b.name
|
||
// FROM store_category_map a
|
||
// JOIN sku_category b ON b.id = a.category_id
|
||
// AND a.deleted_at = ?
|
||
// AND b.deleted_at = ?
|
||
// `
|
||
// sqlParams := []interface{}{
|
||
// utils.DefaultTimeValue,
|
||
// utils.DefaultTimeValue,
|
||
// }
|
||
// if parentID != -1 {
|
||
// sql += " AND b.parent_id = ?"
|
||
// sqlParams = append(sqlParams, parentID)
|
||
// }
|
||
// if storeID > 0 {
|
||
// sql += " AND a.store_id = ?"
|
||
// sqlParams = append(sqlParams, storeID)
|
||
// }
|
||
// if categoryID > 0 {
|
||
// sql += " AND a.category_id = ?"
|
||
// sqlParams = append(sqlParams, categoryID)
|
||
// }
|
||
// err = GetRows(db, &storeCatMaps, sql, sqlParams)
|
||
// if err != nil {
|
||
// return nil, err
|
||
// }
|
||
// return storeCatMaps, err
|
||
// }
|
||
|
||
func InsertStoreCategories(db *DaoDB, userName string, storeID int) (err error) {
|
||
sql := `
|
||
INSERT INTO store_category_map
|
||
(created_at, updated_at, last_operator, deleted_at, store_id, category_id, store_category_name, store_category_seq)
|
||
SELECT ?, ?, ?, ?, ?, id, name, seq
|
||
FROM sku_category
|
||
WHERE deleted_at = ?
|
||
AND is_exd_spec = ?
|
||
`
|
||
sqlParams := []interface{}{
|
||
time.Now(), time.Now(), userName, utils.DefaultTimeValue, storeID,
|
||
utils.DefaultTimeValue, model.NO,
|
||
}
|
||
_, err = ExecuteSQL(db, sql, sqlParams)
|
||
return err
|
||
}
|
||
|
||
func DeleteStoreCategroies(db *DaoDB, userName string, storeID int) (err error) {
|
||
sql := `
|
||
UPDATE store_category_map
|
||
SET deleted_at = ?, last_operator = ?
|
||
WHERE deleted_at <> ?
|
||
AND store_id = ?
|
||
`
|
||
sqlParams := []interface{}{
|
||
time.Now(), userName,
|
||
utils.DefaultTimeValue,
|
||
storeID,
|
||
}
|
||
_, err = ExecuteSQL(db, sql, sqlParams)
|
||
return err
|
||
}
|