package calc import ( "time" "zhiyuan/pkg/db" "github.com/gin-gonic/gin" ) type Calculate struct { ID int64 `json:"id" prop:"add:false"` Name string `json:"name" label:"产品名称" type:"string" prop:"add edit" search:"like"` State int64 `json:"state" label:"状态" type:"int" prop:"edit" default:"0" search:"="` Template int64 `json:"template" label:"模板" type:"int" prop:"edit" default:"0" search:"="` DeletedAt int64 `json:"deleted_at" prop:"add:false select:false"` CreatedAt int64 `json:"created_at" prop:"add:false select:false"` UpdatedAt int64 `json:"updated_at" prop:"add:false select:false"` Props []Property `json:"props" prop:"ignore"` Items []Item `json:"items" prop:"ignore"` db.BaseModel } func (Calculate) TableName() string { return "zy_calculate" } func (Calculate) ListPrivilege(c *gin.Context, data map[string]interface{}, s *db.Select) bool { return true } func (Calculate) OnePrivilege(c *gin.Context, id int64) bool { return true } func (Calculate) AddPrivilege(c *gin.Context, data map[string]interface{}, post map[string]interface{}) error { return nil } func (Calculate) EditPrivilege(c *gin.Context, id int64, data map[string]interface{}, post map[string]interface{}) error { return nil } func (Calculate) DelPrivilege(c *gin.Context, id int64) error { return nil } func (Calculate) Page() bool { return true } func (Calculate) Count() bool { return true } func (Calculate) AddAfter(c *gin.Context, id int64, post map[string]interface{}, data map[string]interface{}) { if use, ok := post["use"].(float64); ok && use != 0 { calc := GetCalculateModel(int64(use), true) if calc != nil { order := time.Now().Unix() CopyPropertys(calc.Props, id, 0, order) CopyItems(calc.Items, id, 0, order) } } } func filterProps(list []Property) []Property { props := make([]Property, 0) for _, v := range list { if v.State == 1 { if v.Type == 1 { v.Value = "" } props = append(props, v) } } return props } func filterItems(list []Item) []Item { items := make([]Item, 0) for _, v := range list { if v.State == 1 { v.Props = filterProps(v.Props) v.Childs = filterItems(v.Childs) items = append(items, v) } } return items } func (model Calculate) FilterShow() Calculate { calc := model calc.Props = filterProps(calc.Props) calc.Items = filterItems(calc.Items) return calc } func GetCalculateModel(id int64, showAll bool) *Calculate { var calc *Calculate db.GetModel(map[string]interface{}{"id": id}, &calc) if calc != nil { var items []Item var props []Property where := map[string]interface{}{"calcId": id, "deleted_at": 0} if !showAll { where["state"] = 1 } db.GetModel(where, &items) if !showAll { where["type !="] = 1 } db.GetModel(where, &props) calc.Props = findProps(0, props) calc.Items = findItems(0, items, props) } return calc } func (model Calculate) Copy(name string) (int64, error) { id, err := db.InsertModel(db.Type(model), map[string]interface{}{ "name": name, }) if err != nil { return 0, err } order := time.Now().Unix() err = CopyPropertys(model.Props, id, 0, order) if err != nil { return id, err } err = CopyItems(model.Items, id, 0, order) if err != nil { return id, err } return id, nil }