package final import ( "errors" "fmt" "zhiyuan/pkg/db" "zhiyuan/pkg/logger" "zhiyuan/pkg/utils" "github.com/gin-gonic/gin" ) type FinalMaterialProcess struct { ID int64 `json:"id" prop:"add:false"` MaterialId int64 `json:"material_id" label:"材料ID" type:"int"` CollectId int64 `json:"collect_id" label:"汇总ID" type:"int"` Explain string `json:"explain" label:"说明" type:"string" search:"like"` Pictures string `json:"pictures" label:"图片" type:"string" search:"like"` Total float64 `json:"total" label:"金额" type:"float" default:"0"` Modify string `json:"modify" type:"int" prop:"add:false" default:"0"` 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 (FinalMaterialProcess) TableName() string { return "zy_final_material_process" } func (model FinalMaterialProcess) ListPrivilege(c *gin.Context, data map[string]interface{}, s *db.Select) bool { material_id, _ := db.ToInt64(data["material_id"]) collect_id, _ := db.ToInt64(data["collect_id"]) if material_id != 0 && collect_id != 0 { s.Where = append(s.Where, fmt.Sprintf("`%s`.`material_id` = %s OR `%s`.`collect_id` = %s", model.TableName(), s.Param(material_id), model.TableName(), s.Param(collect_id))) } else if material_id != 0 { s.Where = append(s.Where, fmt.Sprintf("`%s`.`material_id` = %s", model.TableName(), s.Param(material_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 (FinalMaterialProcess) OnePrivilege(c *gin.Context, id int64) bool { return true } func (FinalMaterialProcess) AddPrivilege(c *gin.Context, data map[string]interface{}, post map[string]interface{}) error { data["created_id"] = c.GetInt("adminID") typ, _ := db.ToInt64(data["type"]) materialID, _ := db.ToInt64(post["material_id"]) collectID, _ := db.ToInt64(data["collect_id"]) //pictures := db.ToString(post["pictures"]) //total, _ := db.ToFloat64(post["total"]) state, _ := db.ToInt64(data["state"]) receipt_date, _ := db.ToInt64(post["receipt_date"]) modify := post["modify"] if materialID != 0 { var model FinalMaterial db.GetModel(map[string]interface{}{ "id": materialID, }, &model) if model.ID == 0 { return errors.New("没有权限") } s := typ + 1 datas := map[string]interface{}{ "state": s, } switch typ { case 0: if model.State != 0 { return errors.New("没有权限") } num := float64(0) total := float64(0) supplierid := int64(0) items, _ := db.GetModelMap(db.Type(FinalMaterialItemMobile{}), map[string]interface{}{"material_id": materialID, "deleted_at": 0}, nil) for _, v := range items { if matid, ok := db.ToInt64(v["mat_id"]); ok && matid != 0 { vnum, _ := db.ToFloat64(v["num"]) price, _ := db.ToFloat64(v["mat_price"]) vsupplierid, _ := db.ToInt64(v["supplier_id"]) if supplierid != 0 && supplierid != vsupplierid { return errors.New("请选择同一个材料商的材料") } num += vnum total += vnum * price supplierid = vsupplierid } } if num == 0 { return errors.New("请选择材料") } datas["total"] = total datas["supplier_id"] = supplierid if model.BudgetType == 0 && num <= model.Budget || model.BudgetType == 1 && total <= model.Budget { datas["state"] = 2 } case 1: if model.State == 1 { if state != 0 { datas["state"] = 0 } } else if model.State == 3 { datas["state"] = 2 if state == 0 { var process FinalMaterialProcess db.GetModel(map[string]interface{}{ "material_id": materialID, "type": 2, "order by": "id desc", }, &process) if process.ID == 0 { return errors.New("系统错误") } for id, num := range utils.JsonDecode(process.Modify).ToMap() { id, _ := db.ToInt64(id) db.UpdateModel(db.Type(FinalMaterialItem{}), id, map[string]interface{}{ "num": num, }) } total := float64(0) items, _ := db.GetModelMap(db.Type(FinalMaterialItemMobile{}), map[string]interface{}{"material_id": materialID, "deleted_at": 0}, nil) for _, v := range items { if matid, ok := db.ToInt64(v["mat_id"]); ok && matid != 0 { vnum, _ := db.ToFloat64(v["num"]) price, _ := db.ToFloat64(v["mat_price"]) total += vnum * price } } datas["total"] = total } } else { return errors.New("没有权限") } case 2: if model.State != 2 { return errors.New("没有权限") } if modify, ok := modify.(map[string]interface{}); ok { items := make([]FinalMaterialItem, 0) db.GetModel(map[string]interface{}{"material_id": materialID, "deleted_at": 0}, &items) modifys := make(map[int64]float64) for _, v := range items { if num, ok := modify[db.ToString(v.ID)]; ok { modifys[v.ID], _ = db.ToFloat64(num) } } if len(modifys) == 0 { return errors.New("请修改数量") } data["modify"] = utils.JsonEncode(modifys) } else { logger.Sugar.Infof("modify: %v;", modify) return errors.New("请修改数量") } case 3: if model.State != 2 { return errors.New("没有权限") } if state != 0 { datas["state"] = 0 } case 4: if model.State != 4 { return errors.New("没有权限") } if receipt_date == 0 { return errors.New("请选择配送日期") } datas["receipt_date"] = receipt_date case 5: if model.State != 5 { return errors.New("没有权限") } case 6: if model.State != 6 { return errors.New("没有权限") } if state != 0 { datas["state"] = 5 } case 7: if model.State != 7 { return errors.New("没有权限") } case 8: if model.State != 8 { return errors.New("没有权限") } if state != 0 { datas["state"] = 7 } default: return errors.New("没有权限") } err := db.UpdateModel(db.Type(model), materialID, datas) if err != nil { return errors.New("没有权限") } } 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 err := db.UpdateModel(db.Type(model), collectID, map[string]interface{}{ "state": s, }) if err != nil { return errors.New("没有权限") } err = db.UpdateModels(db.Type(FinalMaterial{}), map[string]interface{}{ "collect_id": collectID, }, map[string]interface{}{ "state": s, }) if err != nil { return errors.New("没有权限") } } else { return errors.New("没有权限") } return nil } func (FinalMaterialProcess) Page() bool { return false } func (model FinalMaterialProcess) 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`"}, }, } }