coupon-admin/app/traits/CouponMainTrait.php

76 lines
2.3 KiB
PHP
Raw Normal View History

2021-12-06 10:56:23 +00:00
<?php
namespace app\traits;
use app\model\CouponMain;
use app\model\UsingRule;
use app\model\Coupon;
trait CouponMainTrait
{
/**
* 检查优惠券状态是否可以领取
* @param CouponMain $couponMain
* @return bool
*/
public function checkCouponMainReceiveStatus(CouponMain $couponMain){
if (empty($couponMain)) {
return $this->json(4001, "优惠券不存在");
}
if ($couponMain->status != CouponMain::status_on) {
return $this->json(4002, "优惠券已停用");
}
if ($couponMain->on_shelf != CouponMain::on_shelf_on) {
return $this->json(4003, "优惠券已下架");
}
$time = time();
if (strtotime($couponMain->start_time) > $time) {
return $this->json(4004, "优惠券还未发行");
}
if (strtotime($couponMain->end_time) < $time) {
return $this->json(4004, "优惠券已结束使用");
}
if ($couponMain->using_count <= 0) {
return $this->json(4004, "优惠券已经被领完了");
}
return true;
}
2021-12-09 10:26:25 +00:00
/**
* 统计这个优惠券已经使用了多少张了
* @param $couponMainId
* @return int
*/
public function getCouponMainUsingCount($couponMainId)
{
return Coupon::where("coupon_id",$couponMainId)
->where("is_verificated",Coupon::is_verificated_on)
->count();
}
2021-12-14 09:34:27 +00:00
/**
* 获取商户没有被领取的优惠券 需要扣除多少钱
* */
public function getBusinessNotClaimedCoupon($businessCode)
{
$date = date("Y-m-d");
$totalDeductionMoney = 0;
$couponMain = CouponMain::where("business_code", $businessCode)
->where("status", CouponMain::status_on)
->where("on_shelf", CouponMain::on_shelf_on)
->whereTime("start_time", "<=", $date)
->whereTime("end_time", ">=", $date)
->whereRaw(" verification_count < count ")
->field("id,(deduction_money * (count - verification_count)) as total_deduction_money")
->select()
->toArray();
foreach ($couponMain as $item) {
$totalDeductionMoney += $item["total_deduction_money"];
}
return $totalDeductionMoney;
}
2021-12-06 10:56:23 +00:00
}