package dal import ( "database/sql" "encoding/json" "errors" "time" "xiaoniaokuaiyan.com/xiaoniao/cerror" "xiaoniaokuaiyan.com/xiaoniao/constants" "xiaoniaokuaiyan.com/xiaoniao/entity" "xiaoniaokuaiyan.com/xiaoniao/util" ) type Coupon struct { } func (cdal *Coupon) Get(id int, orderId string) (*entity.DiscountTicketDB, error) { db := util.GetSqlDB() var ( strSql string paramValue interface{} ) if id > 0 { strSql = "select * from t_discount_ticket where id = ?" paramValue = id } else { strSql = "select * from t_discount_ticket where order_id = ?" paramValue = orderId } citem := entity.DiscountTicketDB{} err := db.Get(&citem, strSql, paramValue) if err != nil { if err == sql.ErrNoRows { return nil, nil } return nil, err } return &citem, nil } func (cdal *Coupon) Save(item *entity.DiscountTicket) (*entity.DiscountTicket, error) { db := util.GetWriteSqlDB() sqlStr, kvm := util.GenerateInsertSqlFromStruct("t_discount_ticket", item) result, err := db.NamedExec(sqlStr, kvm) if err != nil { return nil, errors.New("db error") } cid, err := result.LastInsertId() if err != nil { return nil, err } item.Id = int(cid) return item, nil } func (cdal *Coupon) Update(item *entity.DiscountTicket) (*entity.DiscountTicket, error) { db := util.GetWriteSqlDB() sqlStr, kvm := util.GenerateUpdateSqlFromStruct("t_discount_ticket", item, " where id = :id") _, err := db.NamedExec(sqlStr, kvm) if err != nil { return nil, err } return item, nil } func (cdal *Coupon) Exchange(codeStr string, customId int, mobile string) (interface{}, cerror.CError) { db := util.GetWriteSqlDB() strSql := "select * from t_fcode where code_str = ?" codeItem := &entity.Fcode{} err := db.Get(codeItem, strSql, codeStr) if err != nil { return nil, cerror.New(constants.ERRORCODE_FCODE_INVILID, "F码无效") } if err := codeItem.Valid(); err != nil { return nil, err } strSql = "select count(*) as num from t_fcode_custom where cid = ? and fid = ?" var num int db.Get(&num, strSql, customId, codeItem.Id) if num > 0 { return nil, cerror.New(constants.ERRORCODE_FCODE_ALREADY_GOT, "该用户已经使用此码兑换过优惠券") } var tlist = []*entity.DiscountTicket{} ctype := constants.CouponType(codeItem.TypeId) var ticketDeadline = codeItem.TicketDeadline if codeItem.TicketValidDays.Valid && codeItem.TicketValidDays.Int64 > 0 { ticketDeadline = time.Now().Add(time.Hour * 24 * time.Duration(codeItem.TicketValidDays.Int64)).Format("2006-01-02 15:04:05") } if ctype == constants.COUPONTYPE_COMPOSITION { tt := []struct { TypeId int `json:"type_id"` Value int `json:"ticket_value"` UseMaxValue int `json:"use_max_value"` BindProducts string `json:"bind_products"` }{} err = json.Unmarshal([]byte(codeItem.Remark.String), &tt) if err != nil { return nil, cerror.New(101, "unmarshal error") } for _, t := range tt { ticket := &entity.DiscountTicket{ TypeId: t.TypeId, Mobile: mobile, Value: t.Value, Deadline: ticketDeadline, UseMaxValue: t.UseMaxValue, BindProducts: codeItem.BindProducts.String, } if t.BindProducts != "" { ticket.BindProducts = t.BindProducts } tlist = append(tlist, ticket) } } else { ticket := &entity.DiscountTicket{ TypeId: codeItem.TypeId, Mobile: mobile, Value: codeItem.TicketValue, Deadline: ticketDeadline, UseMaxValue: codeItem.UseMaxValue, BindProducts: codeItem.BindProducts.String, } tlist = append(tlist, ticket) } for _, titem := range tlist { _, err = cdal.Save(titem) if err != nil { return nil, cerror.New(500, err.Error()) } strSql = "insert into t_fcode_custom(cid, fid, qid) values(?,?,?)" _, err = db.Exec(strSql, customId, codeItem.Id, titem.Id) if err != nil { return nil, cerror.New(500, err.Error()) } } db.Exec("update t_fcode set already_use_num = already_use_num + 1 where code_str = ?", codeStr) return tlist, nil } var DefaultCouponDal = &Coupon{}