package dispatch import ( "errors" "fmt" "zhiyuan/pkg/db" "zhiyuan/services/admin" "github.com/gin-gonic/gin" ) type DispatchSiteCollect struct { ID int64 `json:"id" prop:"add:false"` ShopID int64 `json:"shop_id" label:"门店" type:"int" prop:"add:false" default:"0" search:"="` Total float64 `json:"total" label:"合计金额" type:"float" prop:"add:false" search:"="` State int64 `json:"state" label:"状态" type:"int" prop:"add:false" default:"0"` CreatedId int64 `json:"created_id" label:"创建人员" type:"int" prop:"add:false" search:"="` DeletedAt int64 `json:"deleted_at" prop:"add:false select:false"` CreatedAt int64 `json:"created_at" prop:"add:false"` UpdatedAt int64 `json:"updated_at" prop:"add:false"` db.BaseModel } func (DispatchSiteCollect) TableName() string { return "zy_dispatch_site_collect" } func (DispatchSiteCollect) AddPrivilege(c *gin.Context, data map[string]interface{}, post map[string]interface{}) error { signature := db.ToString(post["signature"]) if signature == "" { return errors.New("没有权限") } type DispatchSiteTableCollects struct { ShopId int64 `json:"shop_id" label:"门店" type:"int" prop:"select:dispatchsite.shop_id" search:"="` Total float64 `json:"total" label:"合计" type:"float" prop:"select:ROUND(sum(dispatchsiteitem.price*num))"` DispatchSiteTableCollect } if tableIds, ok := db.ToArray(post["table_ids"]); ok { models := make([]DispatchSiteTableCollects, 0) db.GetModel(map[string]interface{}{ "id in": tableIds, }, &models) if len(models) == 0 { return errors.New("没有权限") } shopId := models[0].ShopId total := float64(0) for _, v := range models { if v.ShopId != shopId { return errors.New("没有权限") } total += v.Total } data["shop_id"] = shopId data["total"] = total data["state"] = 3 data["created_id"] = c.GetInt("adminID") return nil } return errors.New("没有权限") } func (DispatchSiteCollect) AddAfter(c *gin.Context, id int64, post map[string]interface{}, data map[string]interface{}) { signature := db.ToString(post["signature"]) explain := db.ToString(post["explain"]) pictures := db.ToString(post["pictures"]) db.InsertModel(db.Type(DispatchSiteProcess{}), map[string]interface{}{ "collect_id": id, "explain": explain, "pictures": pictures, "type": 2, "signature": signature, "state": 0, "created_id": c.GetInt("adminID"), }) if tableIds, ok := db.ToArray(post["table_ids"]); ok { db.UpdateModels(db.Type(DispatchSiteTable{}), map[string]interface{}{ "id in": tableIds, }, map[string]interface{}{ "state": 3, "collect_id": id, }) } } func (model DispatchSiteCollect) ListPrivilege(c *gin.Context, data map[string]interface{}, s *db.Select) bool { if !admin.IsSuperAdmin(c.GetInt("adminID")) { adminID := s.Param(c.GetInt("adminID")) where := fmt.Sprintf("`%s`.`created_id` = %s", model.TableName(), adminID) if admin.CheckAuth([]string{"dispatch:verify"}, c.GetInt("adminID")) { info, _ := admin.GetInfoByID(c.GetInt("adminID"), nil, nil) if info == nil { return false } where = fmt.Sprintf("%s OR FIND_IN_SET(`%s`.`shop_id`, %s)", where, model.TableName(), s.Param(info.ShopIds)) } s.Where = append(s.Where, fmt.Sprintf("(%s)", where)) } if state, ok := data["state"]; ok { if n, ok := db.ToInt64(state); ok { if n != 0 { s.Having = append(s.Having, fmt.Sprintf("`%s`.`state` = %s", model.TableName(), s.Param(n))) } } } return true } func (DispatchSiteCollect) OnePrivilege(c *gin.Context, id int64) bool { return true } func (DispatchSiteCollect) Page() bool { return false } func (DispatchSiteCollect) Count() bool { return true } type DispatchSiteCollectMobile struct { CreatedName string `json:"created_name" prop:"select:admin.username"` CreatedPhone string `json:"created_phone" prop:"select:admin.phone"` ShopName string `json:"shop_name" prop:"select:shop.shop_name"` DispatchSiteCollect } func (model DispatchSiteCollectMobile) GroupBy() string { return fmt.Sprintf("`%s`.`id`", model.TableName()) } type JoinShop struct { DispatchSiteCollect } func (JoinShop) TableName() string { return "zy_shop" } func (model DispatchSiteCollectMobile) LeftJoin(data map[string]interface{}, s *db.Select) []db.JoinModel { return append(model.DispatchSiteCollect.LeftJoin(data, s), db.JoinModel{ Model: JoinShop{}, As: "shop", On: []string{"`shop`.`id` = " + model.TableName() + ".`shop_id`"}, }, db.JoinModel{ Model: JoinAdmin{}, As: "admin", On: []string{"`admin`.`id` = " + model.TableName() + ".`created_id`"}, }) }