123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- package calc
- import (
- "errors"
- "time"
- "zhiyuan/pkg/db"
- "github.com/gin-gonic/gin"
- )
- type Item struct {
- ID int64 `json:"id" prop:"add:false"`
- CalcId int64 `json:"calcId" type:"int" prop:"add" search:"="`
- Name string `json:"name" label:"项目名称" type:"string" prop:"add edit" search:"like"`
- TypeId int64 `json:"typeId" label:"产品类别" type:"int" prop:"edit" default:"0"`
- Min int64 `json:"min" label:"最小数量" type:"int" prop:"add edit" default:"0"`
- Max int64 `json:"max" label:"最大数量" type:"int" prop:"add edit" default:"0"`
- State int64 `json:"state" label:"状态" type:"int" prop:"edit" default:"0"`
- ParentId int64 `json:"parentId" type:"int" prop:"add" search:"=" default:"0"`
- OrderAt int64 `json:"order_at" prop:"add:false select:false"`
- 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"`
- Childs []Item `json:"childs" prop:"ignore"`
- db.BaseModel
- }
- func (Item) TableName() string {
- return "zy_calc_item"
- }
- func (Item) ListPrivilege(c *gin.Context, data map[string]interface{}, s *db.Select) bool {
- return true
- }
- func (Item) OnePrivilege(c *gin.Context, id int64) bool {
- return true
- }
- func (Item) AddPrivilege(c *gin.Context, data map[string]interface{}, post map[string]interface{}) error {
- if data["parentId"] != nil && data["parentId"].(int64) != 0 {
- var p Item
- db.GetModel(map[string]interface{}{
- "id": data["parentId"].(int64),
- }, &p)
- if p.ID == 0 {
- return errors.New("没有权限")
- }
- }
- if data["min"].(int64) > data["max"].(int64) {
- return errors.New("没有权限")
- }
- return nil
- }
- func (Item) EditPrivilege(c *gin.Context, id int64, data map[string]interface{}, post map[string]interface{}) error {
- return nil
- }
- func (Item) DelPrivilege(c *gin.Context, id int64) error {
- return nil
- }
- func (Item) OrderField() string {
- return "order_at"
- }
- func (Item) Page() bool {
- return false
- }
- func (Item) Count() bool {
- return false
- }
- func (Item) 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 {
- if calcId, ok := db.ToInt64(data["calcId"]); ok {
- order := time.Now().Unix()
- CopyPropertys(calc.Props, calcId, id, order)
- CopyItems(calc.Items, calcId, id, order)
- }
- }
- }
- }
- func findItems(itemId int64, list []Item, props []Property) []Item {
- items := make([]Item, 0)
- for _, v := range list {
- if v.ParentId == itemId {
- v.Props = findProps(v.ID, props)
- v.Childs = findItems(v.ID, list, props)
- items = append(items, v)
- }
- }
- return items
- }
- func GetItemModel(id int64, showAll bool) *Item {
- var item *Item
- db.GetModel(map[string]interface{}{"id": id}, &item)
- if item != nil {
- var items []Item
- var props []Property
- where := map[string]interface{}{"calcId": item.CalcId, "deleted_at": 0}
- if !showAll {
- where["state"] = 1
- }
- db.GetModel(where, &items)
- if !showAll {
- where["type !="] = 1
- }
- db.GetModel(where, &props)
- item.Props = findProps(item.ID, props)
- item.Childs = findItems(item.ID, items, props)
- }
- return item
- }
- func CopyItems(items []Item, calcId int64, parentId int64, order int64) error {
- for i, item := range items {
- _, err := item.Copy(item.Name, calcId, parentId, order+int64(len(items)-i))
- if err != nil {
- return err
- }
- }
- return nil
- }
- func (model Item) Copy(name string, calcId int64, parentId int64, order int64) (int64, error) {
- id, err := db.InsertModel(db.Type(model), map[string]interface{}{
- "calcId": calcId,
- "name": name,
- "typeId": model.TypeId,
- "min": model.Min,
- "max": model.Max,
- "state": model.State,
- "parentId": parentId,
- "order_at": order,
- })
- if err != nil {
- return 0, err
- }
- err = CopyPropertys(model.Props, calcId, id, order)
- if err != nil {
- return id, err
- }
- err = CopyItems(model.Childs, calcId, id, order)
- if err != nil {
- return id, err
- }
- return id, nil
- }
|