Browse Source

Added functions to sync units directly to voip.mephi.ru

Dmitry Yu Okunev 4 years ago
parent
commit
b8ccbd1547
1 changed files with 34 additions and 1 deletions
  1. 34 1
      unit.go

+ 34 - 1
unit.go

@@ -7,7 +7,9 @@ import (
 	"github.com/xaionaro-go/extime"
 	"html/template"
 	"reflect"
+	"strconv"
 	"strings"
+	"time"
 )
 
 //reform:unit
@@ -54,11 +56,33 @@ 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 (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").Order("id", "DESC")
+}
+
+func (sql Unit) ConsiderUniqueCodes() *UnitScope {
+	return sql.Scope().ConsiderUniqueCodes()
+}
+func (sql *UnitScope) ConsiderUniqueCodes() *UnitScope {
+	return sql.Group("Code").Order("id", "DESC")
+}
+
 func (sql Unit) RealOnly() *UnitScope {
 	return sql.Scope().RealOnly()
 }
@@ -447,3 +471,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)
+}
+