Dmitry Yu Okunev 5 years ago
parent
commit
b575a49433
2 changed files with 81 additions and 3 deletions
  1. 19 0
      person.go
  2. 62 3
      unit.go

+ 19 - 0
person.go

@@ -67,3 +67,22 @@ func (people People) GetEmpGUIDs() (empGUIDs []int) {
 
 	return
 }
+
+func (person *Person) PrepareFormulars() *Person {
+	var err error
+	person.formulars, err = FormularSQL.Select(Formular{EmpGUID: person.EmpGUID})
+	if err != nil {
+		panic(err)
+	}
+
+	person.formularsReady = true
+	return person
+}
+
+func (person Person) GetFormulars() Formulars {
+	if !person.formularsReady {
+		panic("person is not ready for method GetFormulars: it's required to call PrepareFormulars method, first")
+	}
+	return person.formulars
+}
+

+ 62 - 3
unit.go

@@ -6,6 +6,8 @@ import (
 	"fmt"
 	"github.com/xaionaro-go/extime"
 	"html/template"
+	"strings"
+	"reflect"
 )
 
 //reform:unit
@@ -25,7 +27,7 @@ type Unit struct {
 	ChiefName       *string      `reform:"ChiefName"       view:"readonly"`
 	PersNumber      *int         `reform:"PersNumber"      view:"readonly"`
 	Code            string       `reform:"Code"            view:"readonly"`
-	SortNumber      *string      `reform:"SortNumber"      view:"readonly"`
+	SortNumber      *int         `reform:"SortNumber"      view:"readonly"`
 	ChiefTabN       *string      `reform:"ChiefTabN"       view:"readonly"`
 
 	children      []*Unit `reform:"-"`
@@ -38,11 +40,44 @@ type Unit struct {
 	isActiveReady bool `reform:"-"`
 }
 
+func (u *Unit) AfterFind() error {
+	if u.Name != nil {
+		*u.Name = strings.Trim(*u.Name, " ")
+	}
+	if u.BriefName != nil {
+		*u.BriefName = strings.Trim(*u.BriefName, " ")
+	}
+	return nil
+}
+
 func (sql Unit) ActiveOnly() *UnitScope {
 	return sql.Scope().ActiveOnly()
 }
 func (sql *UnitScope) ActiveOnly() *UnitScope {
-	return sql.Where("(CreateOrderDate IS NULL OR CreateOrderDate < NOW()) AND (CloseOrderDate IS NULL OR CloseOrderDate+24*3600 > NOW())")
+	cond := "((CreateOrderDate IS NULL OR CreateOrderDate < NOW()) AND (CloseOrderDate IS NULL OR CloseOrderDate+24*3600 > NOW()))"
+
+	return sql.Where(cond+" OR (Id IN (SELECT ParentId FROM unit WHERE "+cond+"))")
+}
+
+func (sql Unit) RealOnly() *UnitScope {
+	return sql.Scope().RealOnly()
+}
+func (sql *UnitScope) RealOnly() *UnitScope {
+	return sql.Where("LENGTH(Code) = 6")
+}
+
+func (sql Unit) UnhiddenOnly() *UnitScope {
+	return sql.Scope().UnhiddenOnly()
+}
+func (sql *UnitScope) UnhiddenOnly() *UnitScope {
+	return sql.Where("IsHidden=1")
+}
+
+func (sql Unit) NonEmptyOnly() *UnitScope {
+	return sql.Scope().UnhiddenOnly()
+}
+func (sql *UnitScope) NonEmptyOnly() *UnitScope {
+	return sql.Where("Id IN (SELECT * FROM formular WHERE (StartDate IS NULL OR StartDate < NOW()) AND (EndDate IS NULL OR EndDate+24*3600 > NOW()))")
 }
 
 // TODO: Remove this "VIEW" from this model
@@ -177,7 +212,7 @@ func (u *Unit) PrepareIsActive() *Unit {
 
 func (u Unit) IsActive() bool {
 	if !u.isActiveReady {
-		panic("unit is not ready for method IsActive: it's required to call PrepareIsReady method, first")
+		panic("unit is not ready for method IsActive: it's required to call PrepareIsActive method, first")
 	}
 	return u.isActive
 }
@@ -236,3 +271,27 @@ func GetUnitsHeadedBy(empGUID int) (units Units) {
 
 	return
 }
+
+func (units Units) GetUnitIds() (unitIds []int) {
+	for _, unit := range units {
+		unitIds = append(unitIds, unit.Id)
+	}
+
+	return
+}
+
+func (unit Unit) Flush() {
+	unit.children = []*Unit{}
+	unit.childrenReady = false
+	unit.formulars = Formulars{}
+	unit.formularsReady = false
+	unit.isActive = false
+	unit.isActiveReady = false
+}
+
+func (unit Unit) IsEqualsTo(compareTo Unit) bool {
+	unit.Flush()
+	compareTo.Flush()
+	return reflect.DeepEqual(unit, compareTo)
+}
+