package svc import ( "context" "lone-services/pkg/redis" "lone-services/pkg/utils" "strconv" "time" "github.com/zeromicro/go-zero/core/logx" ) type ApplySvc struct { } type ApplyData struct { Uid int `json:"uid"` Id int `json:"id"` Reason string `json:"reason"` Status uint8 `json:"status"` //9为审核中,1为审核通过,2为审核驳回,0为没有申请 Time string `json:"time"` Type uint8 `json:"type"` } const ( applyProductEditKey = "apply:edit:product:" //产品redis KEY applyStoreEditKey = "apply:edit:store:" //门店redis KEY applyEditTimeOut = 60 * 24 applyMd5Key = "keys:" ) func (l *ApplySvc) GetActionApply(uId, objId int) (ApplyData, utils.Status) { key := l.getKey(applyStoreEditKey, uId, objId) var data ApplyData _, err := l.run(key, &data) return data, err } func (l *ApplySvc) getKey(key string, uId, id int) string { return key + strconv.Itoa(uId) + "_" + strconv.Itoa(id) } func (l *ApplySvc) Run(uId, objId int, Type uint8, reason string) (string, utils.Status) { data := ApplyData{ Uid: uId, Id: objId, Reason: reason, Status: utils.StatusWaitPass, Type: Type, } return l.run(applyStoreEditKey, &data) } func (l *ApplySvc) run(key string, data *ApplyData) (string, utils.Status) { redisKey := l.getKey(key, data.Uid, data.Id) ctx := context.Background() if len(data.Reason) > utils.NumberZero { data.Time = utils.Now().String() md5 := utils.MD5Encrypt(strconv.Itoa(data.Uid) + strconv.Itoa(data.Id) + data.Time) jsonStr := utils.StructToJson(data) expire := l.getOutTime() _, err := redis.Client.Set(ctx, redisKey, jsonStr, time.Duration(expire)*time.Minute).Result() if err != nil { logx.Error(err) return md5, utils.SetError(utils.Fail, err.Error()) } if data.Status == utils.StatusWaitPass { _, err = redis.Client.Set(ctx, l.getMd5key(key+applyMd5Key, md5), redisKey, time.Duration(expire)*time.Minute).Result() if err != nil { logx.Error(err) return md5, utils.SetError(utils.Fail, err.Error()) } } return md5, utils.Ok } result, err := redis.Client.Get(ctx, key).Result() if err != nil && err.Error() != redis.RedisNil { logx.Error(err) return utils.StringEmpty, utils.SetError(utils.Fail, err.Error()) } if len(result) < utils.NumberFive { return utils.StringEmpty, utils.Ok } if !utils.JsonStringToStruct(result, &data) { return utils.StringEmpty, utils.SetError(utils.Fail, "数据解析失败") } return utils.StringEmpty, utils.Ok } func (l *ApplySvc) getOutTime() int { outTime := utils.GetConfigInt("apply.time_out") if outTime <= utils.NumberZero { outTime = applyEditTimeOut } return outTime } func (l *ApplySvc) getMd5key(key, key2 string) string { return key + key2 } func (l *ApplySvc) SetActionApply(uId, objId int, Type uint8, reason string) utils.Status { data := ApplyData{ Uid: uId, Id: objId, Reason: reason, Status: utils.StatusWaitPass, Type: Type, } key, err := l.run(applyStoreEditKey, &data) if err == utils.Ok { // TODO 调取企业微信发消息 队列类型 //qErr := QueueService{}.Create(utils.WecomStream, dao.WeComJob{ // ObjId: objId, // ObjType: Type, // Type: dao.StoreApplyReview, // Ext: map[string]any{ // "reason": reason, // "key": key, // }, //}) // //if qErr != nil { // l.Logger.Error(qErr) // return utils.Fail //} } logx.Error(key) return utils.Ok } func (l *ApplySvc) DelActionApply(uId, objId int) utils.Status { key := l.getKey(applyStoreEditKey, uId, objId) _, err := redis.Client.Del(context.Background(), key).Result() if err != nil { logx.Error(err) return utils.SetError(utils.Fail, err.Error()) } return utils.Ok }