coupon-admin/app/traits/CouponTrait.php

192 lines
6.1 KiB
PHP

<?php
namespace app\traits;
use app\exception\RepositoryException;
use app\model\Account;
use app\model\CouponMain;
use app\model\UsingRule;
use think\Model;
use app\model\Coupon;
trait CouponTrait
{
/**消费者持有签到券总数
* @param string $userCode
*
*
* @return int
*/
public function consumerTotalCoupon($userCode)
{
return Coupon::where("consumer_code", $userCode)->count("id");
}
/**消费者已使用签到券总数
* @param string $userCode
* @return int
*/
public function consumerUsedTotalCoupon($userCode)
{
return Coupon::where("consumer_code", $userCode)->where("is_verificated", Coupon::is_verificated_on)->count("id");
}
/**
* 消费者已使用签到券总数
* @param string $userCode
* @return int
*/
public function consumerNotUsedTotalCoupon($userCode)
{
return Coupon::where("consumer_code", $userCode)->where("is_verificated", Coupon::is_verificated_off)->count("id");
}
/**
* 消费者已使用签到券总数
* @param string $userCode
* @return array
* @throws \Exception
*/
public function consumerCouponList($where, $page, $size)
{
return Coupon::findList($where, [], $page, $size, function ($q){
return $q->with("couponBill");
}, ["id" => "desc"]);
}
/**
* 查看某个签到券的是否可以领取 0 可以领取 1 已领取不能再领取
* @param $accountCode
* @param CouponMain $couponMain
* @return int
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getCouponReceiveStatus($accountCode,CouponMain $couponMain )
{
//如果 领取数量==总数
if($couponMain['received_count'] == $couponMain['count'] ){
return CouponMain::receive_status_over;
}
//没有领取记录就可以领取
$ReceiveCount = Coupon::where("coupon_id",$couponMain->id)
->where("consumer_code",$accountCode)
->count();
if($ReceiveCount <= 0){
return CouponMain::receive_status_default;
}else{
return CouponMain::receive_status_received;
}
//确定使用规则
//$usingRule =UsingRule::where("coupon_id",$couponMain->id)->find();
//if(empty($usingRule)){
// return CouponMain::COMMON_OFF;
//}
//单人日限量
//$todayReceivesCount = Coupon::where("coupon_id",$couponMain->id)
// ->where("consumer_code",$accountCode)
// ->whereTime("received_time","between",[date("Y-m-d 00:00:00"),date("Y-m-d 23:59:59")])
// ->count();
//if($todayReceivesCount >= $usingRule->person_day_total ){
//return CouponMain::COMMON_ON;
//}
//单人总限量
//if($ReceiveCount >= $usingRule->person_total ){
// return CouponMain::COMMON_ON;
//}
}
/**
* 查看某个签到券的是否可以领取 0 可以领取 1 已领取不能再领取
* @param Account $account
* @param CouponMain $couponMain
* @return void
* @throws RepositoryException
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getCouponReceiveStatusText(Account $account,CouponMain $couponMain )
{
//没有领取记录就可以领取
$ReceiveCount = Coupon::where("coupon_id",$couponMain->id)
->where("consumer_code",$account->user_code)->count();
if(!empty($ReceiveCount)){
throw new RepositoryException("您已经领取过了");
}
//确定使用规则
//$usingRule =UsingRule::where("coupon_id",$couponMain->id)->find();
//if(empty($usingRule)){
// throw new RepositoryException("使用规则错误,不能领取");
//}
//一天的总限量
//$dayTotalReceivesCount = Coupon::where("coupon_id",$couponMain->id)
// ->whereTime("received_time","between",[date("Y-m-d 00:00:00"),date("Y-m-d 23:59:59")])
// ->count();
//if($dayTotalReceivesCount >= $usingRule->day_total){
// throw new RepositoryException("单日领取总限量达到上限");
//}
//单人日限量
//$todayReceivesCount = Coupon::where("coupon_id",$couponMain->id)
// ->where("consumer_code",$account->user_code)
// ->whereTime("received_time","between",[date("Y-m-d 00:00:00"),date("Y-m-d 23:59:59")])
// ->count();
//if($todayReceivesCount >= $usingRule->person_day_total ){
// throw new RepositoryException("单人日限量达到上限");
//}
//单人总限量
//if($ReceiveCount >= $usingRule->person_total ){
// throw new RepositoryException("单人总限量达到上限");
//}
//白名单
if(!empty($couponMain->white_list)){
if (
//empty($account->real_name) ||
empty($account->mobile)) {
throw new RepositoryException("请先授权手机号");
}
if (false === strpos($couponMain->white_list, $account->mobile
//. "-" . $account->real_name
)) {
throw new RepositoryException("您没有在该签到券白名单内");
}
}
return ;
//领取不做限制 使用才做限制
//$time = time();
//一天的开始时间
//if(strtotime(date("Y-m-d " . $usingRule->day_start_time)) > $time){
// throw new RepositoryException("请在当天{$usingRule->day_start_time}-{$usingRule->day_start_time}领取");
//}
//一天的结束时间
//if(strtotime(date("Y-m-d ".$usingRule->day_end_time)) < $time){
// throw new RepositoryException("请在当天{$usingRule->day_start_time}-{$usingRule->day_start_time}前领取");
//}
}
}