package provider import ( "context" "joybuywork/internal/app" "joybuywork/internal/consts/ckey" "joybuywork/internal/plugins/jsonm" "joybuywork/internal/repository/cache" "joybuywork/internal/repository/model" "joybuywork/internal/repository/mtype" "joybuywork/internal/repository/withdefault" "joybuywork/internal/utils/lockdo" "joybuywork/internal/utils/logic" "joybuywork/internal/utils/merr" "joybuywork/internal/utils/verify" ) type CompanyMade struct { logic.Context companyId int64 conf *model.CompanyMade } func NewCompanyMade(ctx context.Context, companyId int64, opts ...logic.CtxOption) (conf *CompanyMade) { conf = &CompanyMade{ companyId: companyId, } conf.BaseInit(ctx, merr.NewBE(ctx), opts...) return } type CompanyMadeStatus struct { Open bool `json:"open"` FissionTemp []string `json:"fission_temp"` } func (s *CompanyMade) OkTemp(mark string) (ok bool) { status, _ := s.GetMade(mtype.CompanyMadeMarkFissionTemp) return verify.InArray[string](mark, status.FissionTemp...) } func (s *CompanyMade) GetMade(mark string) (status CompanyMadeStatus, err error) { key := ckey.CompanyMade(s.companyId, mark) err = lockdo.ExistDo(s.Ctx, key, func(exist bool) (err error) { if exist { b, _ := app.Redis.Cache.Get(s.Ctx, key).Bytes() return jsonm.Unmarshal(b, &status) } var made *model.CompanyMade made, err = s._getMade(mark, false) if err != nil { return } status.Open = made != nil if made != nil { status.FissionTemp = made.GetFissionTemp() } b, _ := jsonm.Marshal(status) app.Redis.Cache.Set(s.Ctx, key, b.ToBytes(), lockdo.Rand2HourExp()) return }) return } func (s *CompanyMade) SetMake(mark string, params any) (err error) { made, err := s._getMade(mark, true) if err != nil { return } made.SetParams(params) err = s.GetDao().CompanyMade.WithContext(s.Ctx).Save(made) if err != nil { err = s.BE().MysqlError(err) return } s._clear(mark) return } func (s *CompanyMade) Del(mark string) (err error) { made, err := s._getMade(mark, false) if err != nil || made == nil { return } _, err = s.GetDao().CompanyMade.WithContext(s.Ctx).Delete(made) if err != nil { err = s.BE().MysqlError(err) return } s._clear(mark) return } func (s *CompanyMade) _getMade(mark string, nilCreate bool) (made *model.CompanyMade, err error) { made, err = cache.NewCompanyMade(s.Ctx).FindByUniqId(s.companyId, mark) if err != nil { err = s.BE().CacheError(err) return } if made != nil || !nilCreate { return } key := ckey.CompanyMadeStoreOnce(s.companyId, mark) lc := app.NewFollower(s.Ctx, key).Lock() defer lc.Release() if lc.Error() != nil { err = lc.Error() return } made, err = cache.NewCompanyMade(s.Ctx).FindByUniqId(s.companyId, mark) if err != nil { err = s.BE().CacheError(err) return } if made != nil { return } made = withdefault.CompanyMade(&model.CompanyMade{ CompanyID: s.companyId, Mark: mark, }) err = s.GetDao().CompanyMade.WithContext(s.Ctx).Create(made) if err != nil { err = s.BE().MysqlError(err) return } return } func (s *CompanyMade) _clear(mark string) { _ = cache.NewCompanyMade(s.Ctx).Clear(&model.CompanyMade{ CompanyID: s.companyId, Mark: mark, }) app.Redis.Cache.Del(s.Ctx, ckey.CompanyMade(s.companyId, mark)) }