|
@@ -36,17 +36,17 @@ class UserCouponService extends CrudService {
|
|
|
async getCoupon({ coupon_id }) {
|
|
|
const coupon = await this.couponModel.findById(coupon_id);
|
|
|
if (!coupon) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到优惠券');
|
|
|
- const canGet = parseInt(coupon.num) - 1 < 0;
|
|
|
+ const canGet = this.ctx.minus(coupon.num, 1) < 0;
|
|
|
if (canGet) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '您手慢了,没抢到');
|
|
|
const res = await this.checkCouponCanGet(coupon);
|
|
|
if (!res.result) return res;
|
|
|
const customer = _.get(this.ctx, 'user._id');
|
|
|
if (!customer) throw new BusinessError(ErrorCode.NOT_LOGIN, '未找到用户信息');
|
|
|
- const data = { coupon: coupon_id, customer };
|
|
|
+ const data = { coupon: coupon_id, customer, shop: _.get(coupon, 'shop') };
|
|
|
try {
|
|
|
// 领券=>减券库存
|
|
|
this.tran.insert('UserCoupon', data);
|
|
|
- this.tran.update('Coupon', coupon_id, { num: parseInt(coupon.num) - 1 });
|
|
|
+ this.tran.update('Coupon', coupon_id, { num: this.ctx.minus(coupon.num, 1) });
|
|
|
await this.tran.run();
|
|
|
} catch (error) {
|
|
|
console.log(error);
|
|
@@ -263,7 +263,7 @@ class UserCouponService extends CrudService {
|
|
|
const { discount_type, discount_config } = coupon;
|
|
|
const { limit, min, max } = discount_config;
|
|
|
// 符合条件的商品的总价
|
|
|
- const goodsTotal = goodsList.reduce((p, n) => p + (parseFloat(n.goods_total) || 0), 0);
|
|
|
+ const goodsTotal = goodsList.reduce((p, n) => this.ctx.plus(p, n.goods_total), 0);
|
|
|
if (limit !== 'nolimit') {
|
|
|
// 有最低消费的限制,就需要看看下满足条件的商品够不够这个金额
|
|
|
// 消费下限 大于 满足优惠券使用的商品的消费总额,则不能使用这个券,消费的钱不够
|
|
@@ -285,11 +285,12 @@ class UserCouponService extends CrudService {
|
|
|
if (!max) outLimit = false;
|
|
|
else {
|
|
|
// 有上限,则需要计算
|
|
|
- const percent = 1 - parseFloat(min) / 10;
|
|
|
+ // const percent = 1 - parseFloat(min) / 10;
|
|
|
+ const percent = this.ctx.minus(1, this.ctx.divide(min, 10));
|
|
|
// 计算实际折扣 减了 多少钱
|
|
|
- const discountMoney = _.floor(goodsTotal * percent, 2);
|
|
|
+ const discountMoney = this.ctx.multiply(goodsTotal, percent);
|
|
|
// 最后看下 不限制减的钱 是不是 超过了 上限
|
|
|
- outLimit = discountMoney > parseFloat(max);
|
|
|
+ outLimit = this.ctx.minus(discountMoney, max) > 0;
|
|
|
// 超过了,就将上限的金额拿去按比例分配
|
|
|
if (outLimit) discountRealMoney = parseFloat(max);
|
|
|
}
|
|
@@ -316,23 +317,19 @@ class UserCouponService extends CrudService {
|
|
|
const g = goodsList[i];
|
|
|
const { goods_total } = g;
|
|
|
const id = _.get(g, 'goodsSpec._id');
|
|
|
- const obj = { original: goods_total };
|
|
|
+ let dm = 0;
|
|
|
+ console.log(goods_total, goodsTotal);
|
|
|
// 除了最后一个商品,其余的商品均按比例分配
|
|
|
if (i < goodsList.length - 1) {
|
|
|
- const percent = _.floor(goods_total / goodsTotal);
|
|
|
- const discountMoney = _.floor(discountRealMoney * percent, 2);
|
|
|
- const realPay = _.floor(goods_total - discountMoney, 2);
|
|
|
- obj.realPay = realPay;
|
|
|
- obj.discountMoney = discountMoney;
|
|
|
- allReadyDiscount += discountMoney;
|
|
|
+ const percent = this.ctx.divide(goods_total, goodsTotal);
|
|
|
+ dm = this.ctx.multiply(discountRealMoney, percent);
|
|
|
+ allReadyDiscount += dm;
|
|
|
} else {
|
|
|
// 最后一个商品是剩余的可分配的钱
|
|
|
- const discountMoney = _.floor(discountRealMoney - allReadyDiscount, 2);
|
|
|
- const realPay = _.floor(goods_total - discountMoney, 2);
|
|
|
- obj.realPay = realPay;
|
|
|
- obj.discountMoney = discountMoney;
|
|
|
+ dm = this.ctx.minus(discountRealMoney, allReadyDiscount);
|
|
|
}
|
|
|
- result[id] = obj;
|
|
|
+ console.log(dm);
|
|
|
+ result[id] = dm;
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
@@ -345,19 +342,15 @@ class UserCouponService extends CrudService {
|
|
|
discountPercentForEachGoods(coupon, goodsList) {
|
|
|
const { discount_config } = coupon;
|
|
|
const { min } = discount_config;
|
|
|
- const percent = parseFloat(min) / 10;
|
|
|
+ const percent = this.ctx.multiply(min, 10);
|
|
|
const result = {};
|
|
|
for (const g of goodsList) {
|
|
|
const { goods_total } = g;
|
|
|
const id = _.get(g, 'goodsSpec._id');
|
|
|
- const realPay = _.floor(goods_total * percent, 2);
|
|
|
- const discountMoney = goods_total - realPay;
|
|
|
- const obj = {
|
|
|
- original: goods_total,
|
|
|
- realPay,
|
|
|
- discountMoney,
|
|
|
- };
|
|
|
- result[id] = obj;
|
|
|
+ const realPay = this.ctx.multiply(goods_total, percent);
|
|
|
+ const discountMoney = this.ctx.minus(goods_total, realPay);
|
|
|
+ console.log(discountMoney);
|
|
|
+ result[id] = discountMoney;
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
@@ -397,7 +390,7 @@ class UserCouponService extends CrudService {
|
|
|
const r = orderInfo.find(f => ObjectId(f.id).equals(goodsSpec._id));
|
|
|
if (r) obj.buy_num = r.buy_num;
|
|
|
if (obj.buy_num) {
|
|
|
- obj.goods_total = parseInt(obj.buy_num) * parseFloat(_.get(obj.goodsSpec, 'sell_money'));
|
|
|
+ obj.goods_total = this.ctx.multiply(obj.buy_num, _.get(obj.goodsSpec, 'sell_money'));
|
|
|
}
|
|
|
return obj;
|
|
|
});
|