Browse Source

Merge branch 'master' of https://devel.mephi.ru/dyokunev/go-asu-models

Dmitry Yu Okunev 4 years ago
parent
commit
8e68aeab8e
2 changed files with 192 additions and 2 deletions
  1. 38 2
      unit.go
  2. 154 0
      voipHelpers/unit_scope.go

+ 38 - 2
unit.go

@@ -7,7 +7,9 @@ import (
 	"github.com/xaionaro-go/extime"
 	"html/template"
 	"reflect"
+	"strconv"
 	"strings"
+	"time"
 )
 
 //reform:unit
@@ -40,7 +42,7 @@ type Unit struct {
 	isActiveReady bool `reform:"-"`
 }
 
-func (u *Unit) AfterFind() error {
+func (u *Unit) AfterFind(dbI interface{}) error {
 	if u.Name != nil {
 		*u.Name = strings.Trim(*u.Name, " ")
 	}
@@ -54,11 +56,36 @@ func (sql Unit) ActiveOnly() *UnitScope {
 	return sql.Scope().ActiveOnly()
 }
 func (sql *UnitScope) ActiveOnly() *UnitScope {
-	cond := "((CreateOrderDate IS NULL OR CreateOrderDate < NOW()) AND (CloseOrderDate IS NULL OR CloseOrderDate+24*3600 > NOW()))"
+	return sql.LatestOnly(time.Duration(0))
+}
+func (unit Unit) IsStruct() bool {
+	return true
+}
+
+func (sql Unit) LatestOnly(howLong time.Duration) *UnitScope {
+	return sql.Scope().LatestOnly(howLong)
+}
+func (sql *UnitScope) LatestOnly(howLong time.Duration) *UnitScope {
+	ts := time.Now().Add(howLong)
+	cond := fmt.Sprintf("((CreateOrderDate IS NULL OR CreateOrderDate < NOW()) AND (CloseOrderDate IS NULL OR DATE_ADD(CloseOrderDate, INTERVAL 1 DAY) > '%v'))", extime.Time(ts))
 
 	return sql.Where(cond + " OR (Id IN (SELECT ParentId FROM unit WHERE " + cond + "))")
 }
 
+func (sql Unit) ConsiderPersNumbers() *UnitScope {
+	return sql.Scope().ConsiderPersNumbers()
+}
+func (sql *UnitScope) ConsiderPersNumbers() *UnitScope {
+	return sql.Group("PersNumber DESC").Order("CreateOrderDate", "DESC")
+}
+
+func (sql Unit) ConsiderUniqueCodes() *UnitScope {
+	return sql.Scope().ConsiderUniqueCodes()
+}
+func (sql *UnitScope) ConsiderUniqueCodes() *UnitScope {
+	return sql.Group("Code DESC").Order("id", "DESC")
+}
+
 func (sql Unit) RealOnly() *UnitScope {
 	return sql.Scope().RealOnly()
 }
@@ -447,3 +474,12 @@ func (unit Unit) IsEqualsTo(compareTo Unit) bool {
 	compareTo.Flush()
 	return reflect.DeepEqual(unit, compareTo)
 }
+
+func (unit Unit) GetVoipCodeString() string {
+	codeInt, err := strconv.Atoi(unit.Code)
+	if err != nil {
+		panic(err)
+	}
+	return fmt.Sprintf("%02v %03v %02v", codeInt/100000, codeInt/100%1000, codeInt%100)
+}
+

+ 154 - 0
voipHelpers/unit_scope.go

@@ -0,0 +1,154 @@
+package voipHelpers
+
+import (
+	"fmt"
+	m "devel.mephi.ru/dyokunev/go-asu-models"
+	I "devel.mephi.ru/dyokunev/wwwvoip/app/iface"
+	vModels "devel.mephi.ru/dyokunev/wwwvoip/app/models"
+	"github.com/xaionaro/reform"
+)
+
+var (
+	ErrCannotModify error = fmt.Errorf("Modifying of this object is forbidden")
+	ErrNotImplemented error = fmt.Errorf("Not implemented (yet?)")
+)
+
+type UnitScope struct {
+	m.UnitScope
+	vModels.ItemCommon
+}
+func NewUnitScope(scope m.UnitScope) *UnitScope {
+	return &UnitScope{UnitScope: scope}
+}
+func (scope UnitScope) IsStruct() bool {
+	return false
+}
+func (scope UnitScope) BeforeSave() error {
+	return nil
+}
+func (scope UnitScope) AfterRestore() {
+	return
+}
+func (scope UnitScope) GetKeyColumnName() string {
+	panic(ErrNotImplemented)
+	return ""
+}
+func (scope UnitScope) GetReformDB() *reform.DB {
+	panic(ErrNotImplemented)
+	return nil
+}
+func (scope UnitScope) IWhere(requiredArg interface{}, in_args ...interface{}) I.Scope {
+	return NewUnitScope(*scope.Where(requiredArg, in_args...))
+}
+func (scope UnitScope) CanCache() bool {
+	return false
+}
+func (scope UnitScope) CanSave() error {
+	return ErrCannotModify
+}
+func (scope UnitScope) Create(controller I.Controller) error {
+	return ErrCannotModify
+}
+func (scope UnitScope) Destroy(controller I.Controller) error {
+	return ErrCannotModify
+}
+func (scope UnitScope) DefaultReformQuerier() I.ReformBackendQuerier {
+	panic(ErrNotImplemented)
+	return nil
+}
+func (scope UnitScope) FieldPtrByName(fieldName string) interface{} {
+	panic(ErrNotImplemented)
+	return nil
+}
+func (scope UnitScope) FieldValueByName(fieldName string) interface{} {
+	panic(ErrNotImplemented)
+	return nil
+}
+func (scope UnitScope) FlushCache() error {
+	return nil
+}
+func (scope UnitScope) GetDisplayName() string {
+	panic(ErrNotImplemented)
+	return ""
+}
+func (scope UnitScope) IAsuFindByKey(key interface{}) (I.Item, error) {
+	panic(ErrNotImplemented)
+	return nil, nil
+}
+func (scope UnitScope) IDB(db *reform.DB) I.Scope {
+	panic(ErrNotImplemented)
+	return nil
+}
+func (scope UnitScope) IDiffFieldsTo(compareItem I.Item) [][]string {
+	panic(ErrNotImplemented)
+	return nil
+}
+func (scope UnitScope) IElem() (I.Item) {
+	panic(ErrNotImplemented)
+	return nil
+}
+func (scope UnitScope) IFirst(args ...interface{}) (I.Item, error) {
+	panic(ErrNotImplemented)
+	return nil, nil
+}
+func (scope UnitScope) IFixPairByFieldName(fieldName string) I.ItemPtr {
+	panic(ErrNotImplemented)
+	return nil
+}
+func (scope UnitScope) IGetTemplatesForSimilars() (result []I.Item) {
+	panic(ErrNotImplemented)
+	return nil
+}
+func (scope UnitScope) IGroup(argsI ...interface{}) I.Scope {
+	panic(ErrNotImplemented)
+	return nil
+}
+func (scope UnitScope) IIsIdenticalTo(compareItem I.Item) bool {
+	panic(ErrNotImplemented)
+	return false
+}
+func (scope UnitScope) ILimit(limit int) I.Scope {
+	panic(ErrNotImplemented)
+	return nil
+}
+func (scope UnitScope) INewKeyMap() I.ItemKeyMap {
+	panic(ErrNotImplemented)
+	return nil
+}
+func (scope UnitScope) IOrder(argsI ...interface{}) I.Scope {
+	panic(ErrNotImplemented)
+	return nil
+}
+func (scope UnitScope) IPrepareForRender() I.ItemPtr {
+	panic(ErrNotImplemented)
+	return nil
+}
+func (scope UnitScope) ISelect(args ...interface{}) (I.Slice, error) {
+	panic(ErrNotImplemented)
+	return nil, nil
+}
+func (scope UnitScope) ISetDB(dbI interface{}) I.ItemPtr {
+	panic(ErrNotImplemented)
+	return nil
+}
+func (scope UnitScope) ISetGroup(group []string) I.Scope {
+	panic(ErrNotImplemented)
+	return nil
+}
+func (scope UnitScope) ISetOrder(order []string) I.Scope {
+	panic(ErrNotImplemented)
+	return nil
+}
+func (scope UnitScope) ISetReformScope(anotherScope reform.GormImitateScope) reform.GormImitateScope {
+	panic(ErrNotImplemented)
+	return nil
+}
+func (scope UnitScope) ISetWhere(where [][]interface{}) I.Scope {
+	panic(ErrNotImplemented)
+	return nil
+}
+func (scope UnitScope) Model(newModel I.Model) I.Scope {
+	panic(ErrNotImplemented)
+	return nil
+}
+