package cms import ( "encoding/json" "fmt" "git.rosy.net.cn/baseapi/platformapi/mtunionapi" "git.rosy.net.cn/baseapi/platformapi/tbunionapi" "reflect" "regexp" "strconv" "time" "git.rosy.net.cn/baseapi/platformapi/ejyapi" "git.rosy.net.cn/baseapi/utils/errlist" "git.rosy.net.cn/jx-callback/globals/api2" "git.rosy.net.cn/jx-callback/business/authz/autils" "git.rosy.net.cn/baseapi/utils" "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/api" ) const ( SendMsgTypeOpenStoreRequest = "openStoreRequest" SendMsgTypeSuggestRequest = "suggestRequest" ) type SysConfigLimit struct { ValueType reflect.Kind MinValue int64 MaxValue int64 AfterChanged func() error } var ( serviceInfo map[string]interface{} regexpMsgContentOpID = regexp.MustCompile(`"openid":"(.*?)"`) ) func InitServiceInfo(version string, buildTime time.Time, gitCommit string) { buildTimeStr := "" if !utils.IsTimeZero(buildTime) { buildTimeStr = utils.Time2Str(buildTime) } serviceInfo = map[string]interface{}{ "startupTime": utils.Time2Str(time.Now()), "version": version, "buildTime": buildTimeStr, "gitCommit": gitCommit, "metaData": map[string]interface{}{ "vendorTypeName": model.VendorTypeName, "vendorName": model.VendorChineseNames, "vendorImg": model.VendorImg, "vendorColors": model.VendorColors, "orderStatus": model.OrderStatusName, "waybillStatus": model.WaybillStatusName, "orderTypeName": model.OrderTypeName, "taskStatusName": tasksch.TaskStatusName, "storeMsgSendStatusName": model.StoreMsgSendStatusName, "shopChineseNames": model.ShopChineseNames, "printerVendorInfo": model.PrinterVendorInfo, "purchaseVendorInfo": model.PurchaseVendorInfo, "afsReasonTypeName": model.AfsReasonTypeName, "afsAppealTypeName": model.AfsAppealTypeName, "actTypeName": model.ActTypeName, "actStatusName": model.ActStatusName, "actCreateTypeName": model.ActCreateTypeName, "configTypeName": model.ConfigTypeName, "userTypeName": model.UserTypeName, "payStatusName": model.PayStatusName, "refundStatusName": model.RefundStatusName, "complaintReasons": model.ComplaintReasons, "supplementType": model.SupplementTypeName, "operateType": model.OperateTypeName, "apiFunctionName": model.ApiFunctionName, "vendorStatus": model.VendorStatus, "jobLimitCountType": `[{ "id":` + utils.Int2Str(model.JobLimitCountTypePO) + `,"value": "每人一次" },{ "id":` + utils.Int2Str(model.JobLimitCountTypePDO) + `,"value": "每人每天一次" },{ "id":` + utils.Int2Str(model.JobLimitCountTypePWO) + `,"value": "每人每周一次" },{ "id":` + utils.Int2Str(model.JobLimitCountTypeNoLimit) + `,"value": "不限次" }]`, "billTypeNames": model.BillTypeNames, "deliveryStatusName": model.DeliveryStatusName, "cashbackName": model.CashbackName, "consumeName": model.ConsumeName, "txWaybillNames": model.TxWaybillNames, "unionActTypeNames": map[int]map[int]interface{}{ model.VendorIDMTWM: map[int]interface{}{ mtunionapi.ActTypeQB: "券包推广", }, model.VendorIDTB: map[int]interface{}{ tbunionapi.TbElmActTypeBDH: "本地化", }, model.VendorIDPDD: map[int]interface{}{ 1: "进行中的活动", }, model.VendorIDJDShop: map[int]interface{}{ 2: "进行中", }, }, "unionOrderStatusName": model.UnionOrderStatusName, }, } } func GetServiceInfo(ctx *jxcontext.Context) map[string]interface{} { return serviceInfo } 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 GetCoordinateDistrictCode(ctx *jxcontext.Context, lng, lat float64) (code int, err error) { return api.AutonaviAPI.GetCoordinateDistrictCode(lng, lat), nil } func GetCoordinateCityInfo(ctx *jxcontext.Context, lng, lat float64) (name string, err error) { name, _ = api.AutonaviAPI.GetCoordinateCityInfo(lng, lat) return name, err } func SendMsg2Somebody(ctx *jxcontext.Context, mobileNum, verifyCode, msgType, msgContent string) (err error) { return err } func AddConfig(ctx *jxcontext.Context, key, configType, value string) (err error) { // if err = checkConfig(model.SyncFlagNewMask, configType, key, value); err != nil { // return err // } db := dao.GetDB() conf := &model.NewConfig{ Key: key, Type: configType, Value: value, } dao.WrapAddIDCULDEntity(conf, ctx.GetUserName()) err = dao.CreateEntity(db, conf) if configType == model.ConfigTypeSys && err == nil { // err = onSysConfigChanged(key, value) } return err } func DeleteConfig(ctx *jxcontext.Context, key, configType string) (err error) { // if err = checkConfig(model.SyncFlagDeletedMask, configType, key, ""); err != nil { // return err // } db := dao.GetDB() switch configType { case model.ConfigTypePricePack: case model.ConfigTypeFreightPack: case model.ConfigTypeBank: //todo return fmt.Errorf("暂不支持删除银行") case model.ConfigTypeRole: errList := errlist.New() userIDs, err2 := api2.RoleMan.GetRoleUserList(autils.NewRole(key, 0)) if err = err2; err == nil && len(userIDs) > 0 { userList, totalCount, err2 := dao.GetUsers(dao.GetDB(), 0, "", "", userIDs, nil, nil, 0, -1) if err = err2; err == nil && totalCount > 0 { // todo // err = fmt.Errorf("还有人员在使用角色:%s,人员信息:%s", key, utils.MustMarshal(utils.Struct2Map(userList, "compact"))) err = fmt.Errorf("还有人员在使用角色:%s,人员信息:%s", key, utils.Format4Output(userList, false)) } } errList.AddErr(err) errList.AddErr(err) err = errList.GetErrListAsOne() } if err == nil { _, err = dao.DeleteEntityLogically(db, &model.NewConfig{}, nil, ctx.GetUserName(), map[string]interface{}{ "Key": key, "Type": configType, }) } if configType == model.ConfigTypeSys && err == nil { // err = onSysConfigChanged(key, "") } return err } func UpdateConfig(ctx *jxcontext.Context, key, configType, value string) (hint string, err error) { if key == "" { return "", fmt.Errorf("修改配置必须给定key") } hint = "1" db := dao.GetDB() txDB, _ := dao.Begin(db) defer func() { if r := recover(); r != nil { dao.Rollback(db, txDB) panic(r) } }() configList, err := dao.QueryConfigs(db, key, configType, "") if err != nil { dao.Rollback(db, txDB) return "", err } if _, err = dao.UpdateEntityLogically(db, configList[0], map[string]interface{}{ "Value": value, }, ctx.GetUserName(), nil); err != nil { dao.Rollback(db, txDB) return "", err } switch configType { case model.ConfigTypePricePack: default: dao.Commit(db, txDB) } if configType == model.ConfigTypeSys && err == nil { // err = onSysConfigChanged(key, value) } return hint, err } func QueryConfigs(key, configType, keyword string) (configList []*model.NewConfig, err error) { return dao.QueryConfigs(dao.GetDB(), key, configType, keyword) } func InitStation(ctx *jxcontext.Context) (err error) { var ( db = dao.GetDB() stationMap = make(map[string]*model.StationInfo) stationEjyMap = make(map[string]*ejyapi.GetStationListResult) addList []*model.StationInfo updateList []*model.StationInfo deleteList []*model.StationInfo ) api.EjyAPI.SetTimestamp(time.Now().Unix()) if stations, err := dao.GetStationList(db); len(stations) > 0 && err == nil { for _, v := range stations { stationMap[v.StationID] = v } } if getStationListResult, err := api.EjyAPI.GetStationList(); len(getStationListResult) > 0 && err == nil { for _, v := range getStationListResult { stationEjyMap[v.StationID] = v if stationMap[v.StationID] == nil { addList = append(addList, EjyStationToStationInfo(v)) } else { updateList = append(updateList, stationMap[v.StationID]) } } } for _, v := range stationMap { if stationEjyMap[v.StationID] == nil { deleteList = append(deleteList, v) } } task := tasksch.NewParallelTask("InitStation", tasksch.NewParallelConfig().SetParallelCount(1).SetIsContinueWhenError(true), ctx, func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) { step := batchItemList[0].(int) switch step { case 0: if len(addList) > 0 { err = dao.CreateMultiEntities(db, addList) } case 1: if len(updateList) > 0 { task := tasksch.NewParallelTask("updateList", tasksch.NewParallelConfig().SetParallelCount(1).SetIsContinueWhenError(true), ctx, func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) { station := batchItemList[0].(*model.StationInfo) dao.UpdateEntity(db, station) return retVal, err }, updateList) tasksch.HandleTask(task, nil, true).Run() _, err = task.GetResult(0) } case 2: if len(deleteList) > 0 { task := tasksch.NewParallelTask("deleteList", tasksch.NewParallelConfig().SetParallelCount(1).SetIsContinueWhenError(true), ctx, func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) { station := batchItemList[0].(*model.StationInfo) dao.DeleteEntity(db, station) return retVal, err }, deleteList) tasksch.HandleTask(task, nil, true).Run() _, err = task.GetResult(0) } } return retVal, err }, []int{0, 1, 2}) tasksch.HandleTask(task, nil, true).Run() _, err = task.GetResult(0) return err } func EjyStationToStationInfo(station *ejyapi.GetStationListResult) (stationInfo *model.StationInfo) { stationInfo = &model.StationInfo{ StationID: station.StationID, StationName: station.StationName, ProvinceName: station.ProvinceName, ProvinceID: station.ProvinceID, CityName: station.CityName, Latitude: utils.Str2Float64(station.Latitude), Longitude: utils.Str2Float64(station.Longitude), Location: station.Location, StarNum: station.StarNum, Phone: station.Phone, StationPic: station.StationPic, StationBannerPic: station.StationBannerPic, District: station.District, CityID: station.CityID, StationType: station.StationType, } if station.Prices != nil { if data, err := json.Marshal(station.Prices); err == nil { stationInfo.Prices = string(data) } } if station.Adverts != nil { if data, err := json.Marshal(station.Adverts); err == nil { stationInfo.Adverts = string(data) } } return stationInfo } func GetStationList(ctx *jxcontext.Context, stationName string, cityCode int, lat, lng float64, oilCode string, sortType, offset, pageSize int) (pageInfo *model.PagedInfo, err error) { return dao.GetStationInfoList(dao.GetDB(), stationName, cityCode, lat, lng, oilCode, sortType, offset, pageSize) }