// AddPmpDemandAdspace insert a new PmpDemandAdspace into database and returns // last inserted Id on success. func AddPmpDemandAdspace(m *DemandAdspaceVo) (id int64, err error) { o := orm.NewOrm() fmt.Println("time string:", time.Now().String()) // generate PmpDemandAdspaceKey automatically pmpDemandAdspaceKey := lib.GetMd5String(m.RealAdspaceKey + "#" + time.Now().String()) entity := &PmpDemandAdspace{Name: m.Name, DemandId: m.DemandId, Description: m.Description, SecretKey: m.SecretKey, RealAdspaceKey: m.RealAdspaceKey, DemandAdspaceKey: pmpDemandAdspaceKey} id, err = o.Insert(entity) return }
// AddPmpAdspace insert a new PmpAdspace into database and returns // last inserted Id on success. func AddPmpAdspace(m *PmpAdspace) (id int64, err error) { o := orm.NewOrm() id, err = o.Insert(m) if err == nil { // generate PmpAdspaceKey automatically pmpAdspaceKey := lib.GetMd5String(strconv.Itoa(int(id)) + "#" + m.Name) o.Read(m) m.PmpAdspaceKey = pmpAdspaceKey _, err = o.Update(m) } return }
// add a new PmpAdspace and create the adsapce and demand mapping at the sametime func AddPmpAdspaceAndMapDemand(v *AdspaceVo) (id int64, err error) { o := orm.NewOrm() o.Begin() adspaceV := PmpAdspace{Name: v.Name, Description: v.Description, PmpAdspaceKey: v.PmpAdspaceKey, SecretKey: v.SecretKey, MediaId: v.MediaId} id, err = o.Insert(&adspaceV) if err != nil { o.Rollback() return -1, err } // generate PmpAdspaceKey automatically pmpAdspaceKey := lib.GetMd5String(strconv.Itoa(int(id)) + "#" + v.Name) var m *PmpAdspace = &PmpAdspace{} m.Id = int(id) o.Read(m) m.PmpAdspaceKey = pmpAdspaceKey _, err = o.Update(m) if err != nil { o.Rollback() return -1, err } sql := "SELECT id FROM pmp_demand_adspace WHERE demand_id = ? order by id desc limit 1" var maps []orm.Params _, err = o.Raw(sql, v.DemandId).Values(&maps) if err != nil { o.Rollback() return -1, err } var daid int if idv, ok := maps[0]["id"].(string); ok { daid, _ = strconv.Atoi(idv) } matrix := PmpAdspaceMatrix{PmpAdspaceId: int(id), DemandId: v.DemandId, DemandAdspaceId: daid} id, err = o.Insert(&matrix) if err != nil { o.Rollback() return -1, err } o.Commit() return id, nil }