Пример #1
0
// 根据主机查询商户编号
func (this *PartnerQuery) QueryPartnerIdByHost(host string) int {
	//  $ 获取合作商ID
	// $ hostname : 域名
	// *.wdian.net  二级域名
	// www.dc1.com  顶级域名

	var partnerId int
	var err error

	reg := getHostRegexp()
	if reg.MatchString(host) {
		matches := reg.FindAllStringSubmatch(host, 1)
		usr := matches[0][1]
		err = this.Connector.ExecScalar(`SELECT id FROM pt_partner WHERE usr=?`, &partnerId, usr)
	} else {
		err = this.Connector.ExecScalar(
			`SELECT id FROM pt_partner INNER JOIN pt_siteconf
					 ON pt_siteconf.partner_id = pt_partner.id
					 WHERE host=?`, &partnerId, host)
	}

	if err != nil {
		log.PrintErr(err)
	}
	return partnerId
}
Пример #2
0
// 获取商户的域名
func (this *partnerService) GetPartnerMajorHost(partnerId int) string {
	pt, err := this._partnerRep.GetPartner(partnerId)
	if err != nil {
		log.PrintErr(err)
	}
	return pt.GetMajorHost()
}
Пример #3
0
func (this *partnerService) DeleteShop(partnerId, shopId int) error {
	pt, err := this._partnerRep.GetPartner(partnerId)
	if err != nil {
		log.PrintErr(err)
	}
	return pt.DeleteShop(shopId)
}
Пример #4
0
func (this *saleRep) GetCategories(partnerId int) []*sale.ValueCategory {
	var e []*sale.ValueCategory = []*sale.ValueCategory{}
	err := this.Connector.GetOrm().Select(&e, "partner_id=? ORDER BY id ASC", partnerId)
	if err != nil {
		log.PrintErr(err)
	}
	return e
}
Пример #5
0
func (this *partnerService) GetShopValueById(partnerId, shopId int) *partner.ValueShop {
	pt, err := this._partnerRep.GetPartner(partnerId)
	if err != nil {
		log.PrintErr(err)
	}
	v := pt.GetShop(shopId).GetValue()
	return &v
}
Пример #6
0
func (this *partnerService) GetSiteConf(partnerId int) *partner.SiteConf {
	pt, err := this._partnerRep.GetPartner(partnerId)
	if err != nil {
		log.PrintErr(err)
	}
	conf := pt.GetSiteConf()
	return &conf
}
Пример #7
0
// 获取指定的商品快照
func (this *saleRep) GetGoodsSnapshot(id int) *sale.GoodsSnapshot {
	var e *sale.GoodsSnapshot = new(sale.GoodsSnapshot)
	err := this.Connector.GetOrm().Get(id, e)
	if err != nil {
		log.PrintErr(err)
		e = nil
	}
	return e
}
Пример #8
0
func (this *promotionRep) GetCouponBinds(couponId int) []promotion.ValueCouponBind {
	var arr []promotion.ValueCouponBind = []promotion.ValueCouponBind{}
	err := this.Connector.GetOrm().SelectByQuery(arr,
		"SELECT * FROM pm_coupon_bind WHERE coupon_id = ?", couponId)
	if err != nil {
		log.PrintErr(err)
	}
	return arr
}
Пример #9
0
// 根据Key获取商品快照
func (this *saleRep) GetGoodsSnapshotByKey(key string) *sale.GoodsSnapshot {
	var e *sale.GoodsSnapshot = new(sale.GoodsSnapshot)
	err := this.Connector.GetOrm().GetBy(e, "key=?", key)
	if err != nil {
		log.PrintErr(err)
		e = nil
	}
	return e
}
Пример #10
0
// 获取最新的商品快照
func (this *saleRep) GetLatestGoodsSnapshot(goodsId int) *sale.GoodsSnapshot {
	var e *sale.GoodsSnapshot = new(sale.GoodsSnapshot)
	err := this.Connector.GetOrm().GetBy(e, "goods_id=? ORDER BY id DESC", goodsId)
	if err != nil {
		log.PrintErr(err)
		e = nil
	}
	return e
}
Пример #11
0
func (this *Shopping) GetOrderByNo(orderNo string) (shopping.IOrder, error) {
	val, err := this._rep.GetOrderByNo(this._partnerId, orderNo)

	if err != nil {
		log.PrintErr(err)
		return nil, errors.New("订单不存在")
	}
	return this.CreateOrder(val, nil), err
}
Пример #12
0
func (this *oldPromotionRep) GetCouponTakes(couponId int) []promotion.ValueCouponTake {
	var arr []promotion.ValueCouponTake = []promotion.ValueCouponTake{}

	err := this.Connector.GetOrm().SelectByQuery(&arr,
		"SELECT * FROM pm_coupon_take WHERE coupon_id =?", couponId)
	if err != nil {
		log.PrintErr(err)
	}

	return arr
}
Пример #13
0
func (this *partnerRep) GetValueShop(partnerId, shopId int) *partner.ValueShop {
	var v *partner.ValueShop = new(partner.ValueShop)
	err := this.Connector.GetOrm().Get(shopId, v)
	if err == nil &&
		v.PartnerId == partnerId {
		return v
	} else {
		log.PrintErr(err)
	}
	return nil
}
Пример #14
0
// 生成订单
func (this *Shopping) BuildOrder(memberId int, couponCode string) (shopping.IOrder, shopping.ICart, error) {
	order, m, cart, err := this.ParseShoppingCart(memberId)
	if err != nil {
		return order, cart, err
	}

	if len(couponCode) != 0 {
		var coupon promotion.ICouponPromotion
		var result bool
		var val = order.GetValue()
		cp := this._promRep.GetCouponByCode(
			this._partnerId, couponCode)

		// 如果优惠券不存在
		if cp == nil {
			log.PrintErr(err)
			return order, cart, errors.New("优惠券无效")
		}

		coupon = cp.(promotion.ICouponPromotion)
		result, err = coupon.CanUse(m, val.Fee)
		if result {
			if coupon.CanTake() {
				_, err = coupon.GetTake(memberId)
				//如果未占用,则占用
				if err != nil {
					err = coupon.Take(memberId)
				}
			} else {
				_, err = coupon.GetBind(memberId)
			}
			if err != nil {
				log.PrintErr(err)
				return order, cart, errors.New("优惠券无效")
			}
			err = order.ApplyCoupon(coupon) //应用优惠券
		}
	}

	return order, cart, err
}
Пример #15
0
func (this *partnerRep) GetShopsOfPartner(partnerId int) []*partner.ValueShop {
	shops := []*partner.ValueShop{}
	err := this.Connector.GetOrm().SelectByQuery(&shops,
		"SELECT * FROM pt_shop WHERE partner_id=?", partnerId)

	if err != nil {
		log.PrintErr(err)
		return nil
	}

	return shops
}
Пример #16
0
// 提交订单,返回订单号。如有错误则返回
func (this *Order) Submit() (string, error) {
	if this.GetDomainId() != 0 {
		return "", errors.New("订单不允许重复提交!")
	}

	if this.cart == nil || len(this.cart.GetValue().Items) == 0 {
		return "", errors.New("购物车为空!")
	}

	v := this.value
	v.CreateTime = time.Now().Unix()
	v.UpdateTime = v.CreateTime
	v.ItemsInfo = this.cart.GetSummary()
	v.OrderNo = this._shopping.GetFreeOrderNo()

	// 应用优惠券
	var err error
	var t *promotion.ValueCouponTake
	var b *promotion.ValueCouponBind
	for _, c := range this.GetCoupons() {
		if c.CanTake() {
			t, err = c.GetTake(v.MemberId)
			if err == nil {
				err = c.ApplyTake(t.Id)
			}
		} else {
			b, err = c.GetBind(v.MemberId)
			if err == nil {
				err = c.UseCoupon(b.Id)
			}
		}
		if err != nil {
			log.PrintErr(err)
			err = errors.New("Code 105:优惠券使用失败")
			return "", err
		}
	}

	// 保存订单
	id, err := this.shoppingRep.SaveOrder(this._shopping.GetAggregateRootId(), v)
	v.Id = id
	if err == nil {
		var oc *shopping.OrderCoupon = new(shopping.OrderCoupon)
		for _, c := range this.GetCoupons() {
			oc.Clone(c, v.Id, v.Fee)
			// 绑定订单与优惠券
			this.shoppingRep.SaveOrderCouponBind(oc)
		}
	}

	return v.OrderNo, err
}
Пример #17
0
func (this *partnerService) GetShopsOfPartner(partnerId int) []*partner.ValueShop {
	pt, err := this._partnerRep.GetPartner(partnerId)
	if err != nil {
		log.PrintErr(err)
	}
	shops := pt.GetShops()
	sv := make([]*partner.ValueShop, len(shops))
	for i, v := range shops {
		vv := v.GetValue()
		sv[i] = &vv
	}
	return sv
}
Пример #18
0
func (this *saleRep) GetCategory(partnerId, id int) *sale.ValueCategory {
	var e *sale.ValueCategory = new(sale.ValueCategory)
	err := this.Connector.GetOrm().Get(id, e)
	if err != nil {
		log.PrintErr(err)
		return nil
	}

	if e.PartnerId != partnerId {
		return nil
	}

	return e
}
Пример #19
0
// 提交订单,返回订单号。如有错误则返回
func (this *Order) Submit() (string, error) {
	if this.GetDomainId() != 0 {
		return "", errors.New("订单不允许重复提交!")
	}

	if this._cart == nil || len(this._cart.GetValue().Items) == 0 {
		return "", errors.New("购物车为空!")
	}

	v := this._value
	v.CreateTime = time.Now().Unix()
	v.UpdateTime = v.CreateTime
	v.ItemsInfo = this._cart.GetSummary()
	v.OrderNo = this._shopping.GetFreeOrderNo()

	// 应用优惠券
	var err error
	var t *promotion.ValueCouponTake
	var b *promotion.ValueCouponBind
	for _, c := range this.GetCoupons() {
		if c.CanTake() {
			t, err = c.GetTake(v.MemberId)
			if err == nil {
				err = c.ApplyTake(t.Id)
			}
		} else {
			b, err = c.GetBind(v.MemberId)
			if err == nil {
				err = c.UseCoupon(b.Id)
			}
		}
		if err != nil {
			log.PrintErr(err)
			err = errors.New("Code 105:优惠券使用失败")
			return "", err
		}
	}

	// 保存订单
	id, err := this.saveOrderOnSubmit()
	v.Id = id
	if err == nil {
		this.bindCouponOnSubmit()
		this._cart.Destroy() // 销毁购物车
	}
	return v.OrderNo, err
}
Пример #20
0
func (this *saleRep) GetOnShelvesGoodsByCategoryId(partnerId, categoryId, num int) (e []*sale.ValueGoods) {
	var sql string
	if num <= 0 {
		sql = fmt.Sprintf(`SELECT * FROM gs_goods INNER JOIN gs_category ON gs_goods.category_id=gs_category.id
		WHERE partner_id=%d AND gs_category.id=%d AND on_shelves=1`, partnerId, categoryId)
	} else {
		sql = fmt.Sprintf(`SELECT * FROM gs_goods INNER JOIN gs_category ON gs_goods.category_id=gs_category.id
		WHERE partner_id=%d AND gs_category.id=%d AND on_shelves=1 LIMIT 0,%d`, partnerId, categoryId, num)
	}

	e = []*sale.ValueGoods{}
	err := this.Connector.GetOrm().SelectByQuery(&e, sql)
	if err != nil {
		log.PrintErr(err)
		return nil
	}
	return e
}
Пример #21
0
func (this *partnerService) SaveShop(partnerId int, v *partner.ValueShop) (int, error) {
	pt, err := this._partnerRep.GetPartner(partnerId)
	if err != nil {
		log.PrintErr(err)
		return 0, err
	}
	var shop partner.IShop
	if v.Id > 0 {
		shop = pt.GetShop(v.Id)
		if shop == nil {
			return 0, errors.New("门店不存在")
		}
	} else {
		shop = pt.CreateShop(v)
	}
	err = shop.SetValue(v)
	if err != nil {
		return 0, err
	}
	return shop.Save()
}
Пример #22
0
func (this *memberService) updateMember(v *member.ValueMember) (int, error) {
	m, err := this._memberRep.GetMember(v.Id)
	if err != nil {
		log.PrintErr(err)
		return -1, errors.New("no such member")
	}
	mv := m.GetValue()

	//更新
	mv.Name = v.Name
	mv.Address = v.Address
	mv.Birthday = v.Birthday
	mv.Email = v.Email
	mv.Phone = v.Phone
	mv.Sex = v.Sex
	mv.Qq = v.Qq

	if v.Avatar != "" {
		mv.Avatar = v.Avatar
	}

	m.SetValue(&mv)
	return m.Save()
}
Пример #23
0
// 完成订单
func (this *Order) Complete() error {
	now := time.Now().Unix()
	v := this._value
	m, err := this._memberRep.GetMember(v.MemberId)
	if err == nil {
		var ptl partner.IPartner
		ptl, err = this._partnerRep.GetPartner(v.PartnerId)
		if err != nil {
			log.Println("供应商异常!", v.PartnerId)
			log.PrintErr(err)
			return err
		}

		pv := ptl.GetValue()
		if pv.ExpiresTime < time.Now().Unix() {
			return errors.New("您的账户已经过期!")
		}

		//返现比例
		saleConf := ptl.GetSaleConf()
		back_fee := v.Fee * saleConf.CashBackPercent

		//将此次消费记入会员账户
		this.updateShoppingMemberAccount(ptl, m,
			back_fee*saleConf.CashBackMemberPercent, now)

		//todo: 增加阶梯的返积分,比如订单满30送100积分
		backIntegral := int(v.Fee)*saleConf.IntegralBackNum +
			saleConf.IntegralBackExtra

		//判断是否满足升级条件
		if backIntegral != 0 {
			err = m.AddIntegral(v.PartnerId, enum.INTEGRAL_TYPE_ORDER,
				backIntegral, fmt.Sprintf("订单返积分%d个", backIntegral))
			if err != nil {
				return err
			}
		}

		// 增加经验
		if EXP_BIT == 0 {
			fv := infrastructure.GetApp().
				Config().GetFloat(variable.EXP_BIT)
			EXP_BIT = float32(fv)
		}

		if EXP_BIT == 0 {
			log.Println("[WANNING]:Exp_bit not set!")
		}

		err = m.AddExp(int(v.Fee * EXP_BIT))
		if err != nil {
			return err
		}

		// 三级返现
		this.backFor3R(ptl, m, back_fee, now)

		this._value.Status = enum.ORDER_COMPLETED
		this._value.IsSuspend = 0
		this._value.UpdateTime = now

		_, err := this.Save()

		if err == nil {
			err = this.AppendLog(enum.ORDER_LOG_SETUP, false, "订单已完成")
		}
	}
	return err
}
Пример #24
0
// 完成订单
func (this *Order) Complete() error {
	now := time.Now().Unix()
	v := this._value
	m := this._memberRep.GetMember(v.MemberId)
	if m == nil {
		return member.ErrNoSuchMember
	}
	var err error
	var ptl partner.IPartner
	ptl, err = this._partnerRep.GetPartner(v.PartnerId)
	if err != nil {
		log.Println("供应商异常!", v.PartnerId)
		log.PrintErr(err)
		return err
	}

	pv := ptl.GetValue()
	if pv.ExpiresTime < time.Now().Unix() {
		return errors.New("您的账户已经过期!")
	}

	// 增加经验
	if EXP_BIT == 0 {
		fv := infrastructure.GetApp().Config().GetFloat(variable.EXP_BIT)
		if fv <= 0 {
			panic("[WANNING]:Exp_bit not set!")
		}
		EXP_BIT = float32(fv)
	}
	if err = m.AddExp(int(v.Fee * EXP_BIT)); err != nil {
		return err
	}

	// 更新账户
	updateAccountForOrder(m, this)

	//******* 返现到账户  ************
	var back_fee float32
	saleConf := ptl.GetSaleConf()
	if saleConf.CashBackPercent > 0 {
		back_fee = v.Fee * saleConf.CashBackPercent

		//将此次消费记入会员账户
		this.updateShoppingMemberBackFee(ptl, m,
			back_fee*saleConf.CashBackMemberPercent, now)

		//todo: 增加阶梯的返积分,比如订单满30送100积分
		backIntegral := int(v.Fee)*saleConf.IntegralBackNum +
			saleConf.IntegralBackExtra

		// 赠送积分
		if backIntegral != 0 {
			err = m.AddIntegral(v.PartnerId, enum.INTEGRAL_TYPE_ORDER,
				backIntegral, fmt.Sprintf("订单返积分%d个", backIntegral))
			if err != nil {
				return err
			}
		}
	}

	this._value.Status = enum.ORDER_COMPLETED
	this._value.IsSuspend = 0
	this._value.UpdateTime = now

	_, err = this.Save()

	if err == nil {
		err = this.AppendLog(enum.ORDER_LOG_SETUP, false, "订单已完成")
		// 处理返现促销
		this.handleCashBackPromotions(ptl, m)
		// 三级返现
		if back_fee > 0 {
			this.backFor3R(ptl, m, back_fee, now)
		}
	}
	return err
}