package final import ( "errors" "fmt" "math" "zhiyuan/pkg/db" "zhiyuan/pkg/utils" "github.com/gin-gonic/gin" ) type FinalSiteProcess struct { ID int64 `json:"id" prop:"add:false"` TableId int64 `json:"table_id" label:"表" type:"int"` CollectId int64 `json:"collect_id" label:"汇总" type:"int"` Explain string `json:"explain" label:"说明" type:"string" search:"like"` Pictures string `json:"pictures" label:"图片" type:"string" search:"like"` Type int64 `json:"type" label:"类型" type:"int" prop:"add" default:"0"` Signature string `json:"signature" label:"签名" prop:"add" type:"string"` State int64 `json:"state" label:"状态" type:"int" prop:"add" default:"0"` CreatedId int64 `json:"created_id" label:"创建人员" type:"int" prop:"add:false" search:"="` CreatedName string `json:"created_name" prop:"add:false select:created.username"` CreatedPhone string `json:"created_phone" prop:"add:false select:created.phone"` CreatedHeadimgurl string `json:"created_headimgurl" prop:"add:false select:created.headimgurl"` 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 (FinalSiteProcess) TableName() string { return "zy_final_site_process" } func (model FinalSiteProcess) ListPrivilege(c *gin.Context, data map[string]interface{}, s *db.Select) bool { table_id, _ := db.ToInt64(data["table_id"]) collect_id, _ := db.ToInt64(data["collect_id"]) if table_id != 0 && collect_id != 0 { s.Where = append(s.Where, fmt.Sprintf("`%s`.`table_id` = %s OR `%s`.`collect_id` = %s", model.TableName(), s.Param(table_id), model.TableName(), s.Param(collect_id))) } else if table_id != 0 { s.Where = append(s.Where, fmt.Sprintf("`%s`.`table_id` = %s", model.TableName(), s.Param(table_id))) } else if collect_id != 0 { s.Where = append(s.Where, fmt.Sprintf("`%s`.`collect_id` = %s", model.TableName(), s.Param(collect_id))) } return true } func (FinalSiteProcess) OnePrivilege(c *gin.Context, id int64) bool { return true } func (FinalSiteProcess) AddPrivilege(c *gin.Context, data map[string]interface{}, post map[string]interface{}) error { data["created_id"] = c.GetInt("adminID") typ, _ := db.ToInt64(data["type"]) tableID, _ := db.ToInt64(data["table_id"]) collectID, _ := db.ToInt64(data["collect_id"]) state, _ := db.ToInt64(data["state"]) signature := db.ToString(data["signature"]) if signature == "" { return errors.New("请签名") } if tableID != 0 { var model FinalSiteTable db.GetModel(map[string]interface{}{ "id": tableID, }, &model) if model.ID == 0 || model.State != typ { return errors.New("没有权限") } s := model.State + 1 if state != 0 { s = -1 } if s > 2 { return errors.New("没有权限") } datas := map[string]interface{}{ "state": s, } if typ == 0 && state == 0 { total := float64(0) items := make([]FinalSiteItem, 0) db.GetModel(map[string]interface{}{ "table_id": tableID, "deleted_at": 0, }, &items) for _, v := range items { total = utils.FloatAdd(total, utils.FloatMul(v.Price, v.Num, 2), 2) //total += v.Price * v.Num } total = math.Round(total) datas["total"] = total if total > model.Budget { return errors.New("申请金额不能大于控额") } } err := db.UpdateModel(db.Type(model), tableID, datas) if err != nil { return errors.New("没有权限") } /*if s == -1 { tableid, err := db.InsertModel(db.Type(FinalSiteTable{}), map[string]interface{}{ "type_id": model.TypeId, "site_id": model.SiteId, "worker_id": model.WorkerId, "budget": model.Budget, "state": 0, }) if err == nil { items, _ := db.GetModelMap(db.Type(FinalTypeItem{}), map[string]interface{}{"type_id": model.TypeId}, nil) if items != nil { for _, v := range items { db.InsertModel(db.Type(FinalSiteItem{}), map[string]interface{}{ "table_id": tableid, "name": v["name"], "unit": v["unit"], "price": v["price"], "num": 0, }) } } } }*/ } else if collectID != 0 { var model FinalSiteCollect db.GetModel(map[string]interface{}{ "id": collectID, }, &model) if model.ID == 0 || model.State != typ { return errors.New("没有权限") } if state != 0 { return errors.New("没有权限") } s := model.State + 1 //if model.State == 3 { // s += 1 //} err := db.UpdateModel(db.Type(model), collectID, map[string]interface{}{ "state": s, }) if err != nil { return errors.New("没有权限") } err = db.UpdateModels(db.Type(FinalSiteTable{}), map[string]interface{}{ "collect_id": collectID, }, map[string]interface{}{ "state": s, }) if err != nil { return errors.New("没有权限") } } else { return errors.New("没有权限") } return nil } func (FinalSiteProcess) Page() bool { return false } func (model FinalSiteProcess) LeftJoin(data map[string]interface{}, s *db.Select) []db.JoinModel { return []db.JoinModel{ { Model: JoinAdmin{}, As: "created", On: []string{"`created`.`id` = " + model.TableName() + ".`created_id`"}, }, } }