package service import ( "database/sql" "errors" "fmt" "time" "xiaoniaokuaiyan.com/xiaoniao/cerror" "xiaoniaokuaiyan.com/xiaoniao/constants" "xiaoniaokuaiyan.com/xiaoniao/dal" "xiaoniaokuaiyan.com/xiaoniao/entity" "xiaoniaokuaiyan.com/xiaoniao/util" ) type CouponService struct { dal.ICoupon } func (csrv *CouponService) AddCoupon(couponItem *entity.DiscountTicket) (interface{}, error) { if couponItem.Value <= 0 { return nil, errors.New("ticket value must bigger than zero") } if !util.IsMobile(couponItem.Mobile) { return nil, errors.New("wrong mobile value") } _, err := time.Parse("2006-01-02", couponItem.Deadline) if err != nil { return nil, errors.New("wrong deadline value") } dateStr := time.Now().Format("2006-01-02") if dateStr > couponItem.Deadline { return nil, errors.New("invalid deadline") } ctype := constants.CouponType(couponItem.TypeId) if ctype == constants.COUPONTYPE_LIMIT && couponItem.UseMaxValue <= 0 { return nil, errors.New("ticket UseMaxValue must bigger than zero") } return csrv.ICoupon.Save(couponItem) } func (csrv *CouponService) GetCoupon(id int) (interface{}, error) { return csrv.ICoupon.Get(id, "") } func (csrv *CouponService) ListCoupon(mobile string, status, pageIndex, pageSize int) (interface{}, error) { if !util.IsMobile(mobile) { return nil, errors.New("invalid mobile") } nowStr := time.Now().Format("2006-01-02") orderStr := "order by deadline desc" whereStr := " where mobile = ? and status = ?" params := []interface{}{ mobile, status, } //未使用 if status == 0 { orderStr = "order by deadline asc" whereStr += " and deadline >= ? " params = append(params, nowStr) } else if status == 2 { //已过期 whereStr += " and deadline < ? " params[1] = 0 params = append(params, nowStr) } whereStr += " and is_delete='N' " dataList := &[]entity.DiscountTicketDB{} return dal.List(dataList, "t_discount_ticket", "select * from t_discount_ticket ", orderStr, whereStr, params, dal.Pager{PageIndex: pageIndex, PageSize: pageSize}) } func (csrv *CouponService) Exchange(codeStr string, coustomId int, mobile string) (interface{}, cerror.CError) { return csrv.ICoupon.Exchange(codeStr, coustomId, mobile) } func (csrv *CouponService) QueryFcode(codeStr string) (interface{}, error) { db := util.GetSqlDB() codeItem := &entity.Fcode{} err := db.Get(codeItem, "select * from t_fcode where code_str = ?", codeStr) if err != nil { if err == sql.ErrNoRows { return nil, nil } return nil, err } if err = codeItem.Valid(); err != nil { return nil, err } return codeItem, nil } func (csrv *CouponService) GetCouponByOrderId(orderId string) (interface{}, error) { return csrv.Get(0, orderId) } func (csrv *CouponService) CouponActList(mobile string, pid int) (interface{}, error) { db := util.GetSqlDB() sql := `select a.id,a.name,a.end_time,a.value,a.use_max_value,a.bind_products,a.type_id,count(t.id) as count from t_discount_activity a left join t_discount_ticket t on a.id = t.discount_activity_id and t.mobile = ?` where := " where a.is_delete = 0 and a.is_putaway=0 and a.end_time>CURRENT_DATE" if pid > 0 { where += fmt.Sprintf(" and (a.bind_products='' or a.bind_products is null or JSON_CONTAINS(a.bind_products,JSON_OBJECT('pid','%d')) )", pid) } group := " group by a.id " order := " order by a.created_at desc limit 20" var list []entity.DiscountActDB err := db.Select(&list, sql+where+group+order, mobile) if list == nil || len(list) == 0 { return nil, err } return list, nil } const coupon_lock = "COUPON:LOCK" func (csrv *CouponService) CouponReceive(id int, mobile string) (interface{}, error) { db := util.GetSqlDB() //查询是否存在 var count int db.Get(&count, "select count(1) from t_discount_ticket where mobile = ? and discount_activity_id = ?", mobile, id) if count > 0 { return nil, fmt.Errorf("1::exists discount_activity_id :%d mobile:%s", id, mobile) } //考虑加锁 err := util.SetLock(fmt.Sprintf("%s:%d:%s", coupon_lock, id, mobile)) if err != nil { return nil, fmt.Errorf("5::someone get lock by id:%d mobile:%s", id, mobile) } //defer 去掉锁 defer util.DelLock(fmt.Sprintf("%s:%d:%s", coupon_lock, id, mobile)) tmp := entity.DiscountActDB{} sql := `select a.id,a.name,a.end_time,a.value,a.use_max_value,a.bind_products,a.type_id,count(t.id) as count from t_discount_activity a left join t_discount_ticket t on a.id = t.discount_activity_id and t.mobile = ? where a.id = ? and a.is_delete = 0 and a.is_putaway=0 and a.end_time>CURRENT_DATE ` err = db.Get(&tmp, sql, mobile, id) if err != nil { return nil, fmt.Errorf("2:: not ") } if tmp.Count > 0 { return nil, fmt.Errorf("3:: ") } sql = "INSERT INTO t_discount_ticket(type_id, value, mobile, use_max_value, deadline,bind_products,discount_activity_id ) VALUES (?,?,?,?,?,?,?);" _, err = db.Exec(sql, tmp.TypeId, tmp.Value, mobile, tmp.UseMaxValue, tmp.EndTime, tmp.BindProducts, id) if err != nil { return nil, errors.New("4::db error") } return nil, nil }