diff --git a/app/controller/api/Coupon.php b/app/controller/api/Coupon.php index ed849a0..f7bc673 100644 --- a/app/controller/api/Coupon.php +++ b/app/controller/api/Coupon.php @@ -493,10 +493,29 @@ class Coupon extends Base return $this->json(4001, "商家已禁用"); } - if($business->balance < $coupon->couponMain->deduction_money){ - return $this->json(4001, "商家余额不足"); + // 指定时间段内发布的优惠券个数 + $TimeListModeReleaseCount = BusinessRepository::getInstance() + ->getTimeListModeReleaseCount($business["code"],$business["time_limit_start"],$business["time_limit_end"]); + + if ( + $business["model"] == BusinessModel::model_ordinary + || + ( + $business["model"] == BusinessModel::model_time_limit + && + ( + strtotime($business["time_limit_end"]) < $time + || + $TimeListModeReleaseCount >= $business["time_limit_release_count"] + ) + ) + ) { + if ($business['balance'] < $coupon->couponMain->deduction_money) { + return $this->json(4001, "商家余额不足"); + } } + $deductionMoney = $coupon->couponMain->deduction_money; $returnConsumerMoney = 0;//需要返回提示的金额 @@ -629,8 +648,24 @@ class Coupon extends Base //4. 商家扣钱 普通商家才扣钱 否则不扣钱 - if($business->model == BusinessModel::model_ordinary){ - $business->save(["balance"=>Db::raw("balance - " . $deductionMoney)]); + if( + $business["model"] == BusinessModel::model_ordinary + || + ( + $business["model"] == BusinessModel::model_time_limit + && + ( + strtotime($business["time_limit_end"]) < $time + || + $TimeListModeReleaseCount >= $business["time_limit_release_count"] + ) + ) + ){ + + $business->save([ + "balance" => Db::raw("balance - " . $deductionMoney,), + "model" => BusinessModel::model_ordinary + ]); } $payment = WechatPay::getInstance(); @@ -845,6 +880,7 @@ class Coupon extends Base return $this->json(4001, "商家已禁用"); } $data = input(); + $time = time(); Config::load("extra/distribution_proportion","distribution_proportion"); Config::load("extra/distribution_proportion_user","distribution_proportion_user"); @@ -866,8 +902,6 @@ class Coupon extends Base } } - - //验证通过 不管是商家还是工作人员 都可以发布优惠券 $couponMain = [ "name" => $data['name'] ?? '', @@ -984,7 +1018,23 @@ class Coupon extends Base //未领取的优惠券 $NotClaimedMoney = CouponRepository::getInstance()->getBusinessNotClaimedCoupon($account->business["code"]); - if($account->business["model"] == BusinessModel::model_ordinary) { + // 指定时间段内发布的优惠券个数 + $TimeListModeReleaseCount = BusinessRepository::getInstance() + ->getTimeListModeReleaseCount($account->business["code"],$account->business["time_limit_start"],$account->business["time_limit_end"]); + + if( + $account->business["model"] == BusinessModel::model_ordinary + || + ( + $account->business["model"] == BusinessModel::model_time_limit + && + ( + (strtotime($account->business["time_limit_end"]) < $time) + || + ($TimeListModeReleaseCount >= $account->business["time_limit_release_count"]) + ) + ) + ) { if ($account->business["balance"] < ($totalMoney + $NotClaimedMoney)) { return $this->json(4001, '商家余额不足'); } @@ -994,6 +1044,18 @@ class Coupon extends Base try { //CouponRepository::getInstance()->releaseCouponMain($couponMain, $totalMoney, $usingRule); CouponRepository::getInstance()->releaseCouponMain($couponMain); + + + if($account->business["model"] == BusinessModel::model_time_limit){ + //如果到了时间 或者发布数量到达规定 更改商家模式 + if( strtotime($account->business["time_limit_end"] ) < $time + || + (($TimeListModeReleaseCount + 1) >= $account->business["time_limit_release_count"]) + ){ + $account->business->save(["model"=>BusinessModel::model_ordinary]); + } + } + Db::commit(); return $this->json(); } catch (RepositoryException $e) { diff --git a/app/controller/manager/Business.php b/app/controller/manager/Business.php index 5b67653..b0ea82a 100644 --- a/app/controller/manager/Business.php +++ b/app/controller/manager/Business.php @@ -230,8 +230,13 @@ class Business extends Base // 'intro|介绍' => 'require', // 'business_circle_id|商圈' => 'require', 'background|背景图' => 'require', + 'model|商家模式' => 'require', //'score|评分' => 'require|in:1,2,3,4,5', 'reason|驳回原因' => 'max:100', + + 'time_limit_start|时限商家模式【开始时间】' => 'requireIf:model,'.BusinessModel::model_time_limit.'|date', + 'time_limit_end|时限商家模式【结束时间】' => 'requireIf:model,'.BusinessModel::model_time_limit.'|date|after:'.date("Y-m-d",strtotime("+1 day")), + 'time_limit_release_count|时限商家模式可发布优惠券个数' => 'requireIf:model,'.BusinessModel::model_time_limit.'|>:0', ]); if ($validate !== true) { @@ -253,10 +258,12 @@ class Business extends Base if (empty($business)) { return $this->error("商家不存在"); } - $this->data["item"] = $business; - $this->data["type"] = CategoryModel::getByGroup(); - $this->data["businessCircle"] = BusinessCircleModel::getList(); - $this->data["model"] = BusinessModel::allModel(); + $this->data["item"] = $business; + $this->data["type"] = CategoryModel::getByGroup(); + $this->data["businessCircle"] = BusinessCircleModel::getList(); + $this->data["model"] = BusinessModel::allModel(); + $this->data["model_ordinary"] = BusinessModel::model_ordinary; + $this->data["model_time_limit"] = BusinessModel::model_time_limit; return $this->view(); } diff --git a/app/controller/manager/Coupon.php b/app/controller/manager/Coupon.php index 03b8fe2..ed79c5f 100644 --- a/app/controller/manager/Coupon.php +++ b/app/controller/manager/Coupon.php @@ -178,8 +178,8 @@ class Coupon extends Base //$usingRule = input("using_rule/a", []); - $business = BusinessRepository::getInstance()->getBusinessAccount($data["business_code"], true); - + $business = BusinessRepository::getInstance()->getBusinessAccount($data["business_code"], true); + $time = time(); if (empty($business)) { return $this->json(4001, '商家不存在'); } @@ -311,8 +311,23 @@ class Coupon extends Base //未领取的优惠券 $NotClaimedMoney = CouponRepository::getInstance()->getBusinessNotClaimedCoupon($business["code"]); + // 指定时间段内发布的优惠券个数 + $TimeListModeReleaseCount = BusinessRepository::getInstance() + ->getTimeListModeReleaseCount($business["code"],$business["time_limit_start"],$business["time_limit_end"]); - if($business["model"] == BusinessModel::model_ordinary){ + if( + $business["model"] == BusinessModel::model_ordinary + || + ( + $business["model"] == BusinessModel::model_time_limit + && + ( + strtotime($business["time_limit_end"]) < $time + || + $TimeListModeReleaseCount >= $business["time_limit_release_count"] + ) + ) + ){ if ($business["balance"] < ($totalMoney + $NotClaimedMoney)) { return $this->json(4001, '商家余额不足'); } @@ -328,6 +343,17 @@ class Coupon extends Base try { //CouponRepository::getInstance()->releaseCouponMain($data, $totalMoney, $usingRule); CouponRepository::getInstance()->releaseCouponMain($data); + + if($business["model"] == BusinessModel::model_time_limit){ + //如果到了时间 或者发布数量到达规定 更改商家模式 + if( strtotime($business["time_limit_end"] ) < $time + || + (($TimeListModeReleaseCount+1) >= $business["time_limit_release_count"]) + ){ + $business->save(["model"=>BusinessModel::model_ordinary]); + } + } + Db::commit(); return $this->json(); } catch (RepositoryException $e) { diff --git a/app/model/Business.php b/app/model/Business.php index 6d3e6d2..d8c6821 100644 --- a/app/model/Business.php +++ b/app/model/Business.php @@ -13,8 +13,7 @@ class Business extends Base const state_consumer = 4;//未认证 const model_ordinary = 0;//普通商家 - const model_annual_fee = 1;//年费商家 - const model_zero = 2;//0元商家 + const model_time_limit = 1;//时限商家 /** * @remarks 代理商、平台商、平台代理商、渠道商等词组均描述的是代理商,因此文案统一为【代理商】 */ @@ -23,8 +22,7 @@ class Business extends Base { return [ self::model_ordinary=>"普通商家", - self::model_annual_fee=>"年费商家", - self::model_zero=>"0元商家", + self::model_time_limit=>"时限商家", ]; } public function category() diff --git a/app/repository/BusinessRepository.php b/app/repository/BusinessRepository.php index b45a873..9cd9bdc 100644 --- a/app/repository/BusinessRepository.php +++ b/app/repository/BusinessRepository.php @@ -228,4 +228,18 @@ class BusinessRepository extends Repository ->where("a.agency_code", $agencyCode) ->sum("b.count"); } + + /** + * 统计某个时间段内 商家发布的优惠券个数 + * @param $businessId + * @param $startTime + * @param $endTime + */ + public function getTimeListModeReleaseCount($businessCode,$startTime,$endTime) + { + return CouponMain::where("business_code",$businessCode) + ->whereBetweenTime("create_time",$startTime,$endTime) + ->order("id") + ->count(); + } } \ No newline at end of file diff --git a/view/manager/business/business_detail.html b/view/manager/business/business_detail.html index a3c2774..2cc8d91 100644 --- a/view/manager/business/business_detail.html +++ b/view/manager/business/business_detail.html @@ -219,10 +219,14 @@ +
+ 时限商家 当商家在有效期内发布优惠券次数达到设置次数,或者超出有效期,商家类型自动变成普通商家 +
+
- {foreach $model as $key=> $value} {/foreach} @@ -230,6 +234,38 @@
+ +
+ +
+ 0} + value="{$item.time_limit_end??''}" + {/if} + class="layui-input"> +
+
+ +
+ +
+ 0} + value="{$item.time_limit_end??''}" + {/if} + class="layui-input"> +
+
+ +
+ +
+ +
+
+ +
@@ -303,10 +339,38 @@ \ No newline at end of file