2021-12-06 10:56:23 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace app\traits;
|
|
|
|
|
|
2021-12-16 06:03:35 +00:00
|
|
|
|
use app\exception\RepositoryException;
|
2021-12-06 10:56:23 +00:00
|
|
|
|
use app\model\CouponMain;
|
2021-12-23 02:23:10 +00:00
|
|
|
|
use app\model\Member as MemberModel;
|
2021-12-06 10:56:23 +00:00
|
|
|
|
use app\model\UsingRule;
|
|
|
|
|
use app\model\Coupon;
|
2021-12-23 02:23:10 +00:00
|
|
|
|
use think\Collection;
|
2021-12-21 07:01:21 +00:00
|
|
|
|
use think\facade\Db;
|
2021-12-06 10:56:23 +00:00
|
|
|
|
|
|
|
|
|
trait CouponMainTrait
|
|
|
|
|
{
|
|
|
|
|
/**
|
2022-03-31 06:42:56 +00:00
|
|
|
|
* 检查签到券状态是否可以领取
|
2021-12-06 10:56:23 +00:00
|
|
|
|
* @param CouponMain $couponMain
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function checkCouponMainReceiveStatus(CouponMain $couponMain){
|
|
|
|
|
if (empty($couponMain)) {
|
2022-03-31 06:42:56 +00:00
|
|
|
|
throw new RepositoryException("签到券不存在");
|
2021-12-06 10:56:23 +00:00
|
|
|
|
}
|
|
|
|
|
if ($couponMain->status != CouponMain::status_on) {
|
2022-03-31 06:42:56 +00:00
|
|
|
|
throw new RepositoryException("签到券已停用");
|
2021-12-06 10:56:23 +00:00
|
|
|
|
}
|
|
|
|
|
if ($couponMain->on_shelf != CouponMain::on_shelf_on) {
|
2022-03-31 06:42:56 +00:00
|
|
|
|
throw new RepositoryException("签到券已下架");
|
2021-12-06 10:56:23 +00:00
|
|
|
|
}
|
|
|
|
|
$time = time();
|
|
|
|
|
if (strtotime($couponMain->start_time) > $time) {
|
2022-03-31 06:42:56 +00:00
|
|
|
|
throw new RepositoryException("签到券还未到开始使用时间");
|
2021-12-06 10:56:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (strtotime($couponMain->end_time) < $time) {
|
2022-03-31 06:42:56 +00:00
|
|
|
|
throw new RepositoryException("签到券已结束使用");
|
2021-12-06 10:56:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($couponMain->using_count <= 0) {
|
2022-03-31 06:42:56 +00:00
|
|
|
|
throw new RepositoryException("签到券已经被领完了");
|
2021-12-06 10:56:23 +00:00
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2021-12-09 10:26:25 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2022-03-31 06:42:56 +00:00
|
|
|
|
* 统计这个签到券已经使用了多少张了
|
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
|
|
|
|
|
|
|
|
|
/**
|
2022-03-31 06:42:56 +00:00
|
|
|
|
* 获取商户没有被领取的签到券 需要扣除多少钱
|
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-15 07:04:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-03-31 06:42:56 +00:00
|
|
|
|
* 获取 发布签到券的统计
|
2021-12-15 07:04:20 +00:00
|
|
|
|
* 每条数据都走一遍
|
|
|
|
|
*
|
|
|
|
|
* */
|
|
|
|
|
public function getReleaseStatistics($startTime,$endTime,$agencyCode)
|
|
|
|
|
{
|
|
|
|
|
return CouponMain::alias("a")
|
|
|
|
|
->join("business b","a.business_code =b.code")
|
|
|
|
|
->whereTime("a.start_time", ">=", $startTime)
|
|
|
|
|
->whereTime("a.end_time", "<=", $endTime)
|
|
|
|
|
->when(!empty($agencyCode), function ($q) use ($agencyCode) {
|
|
|
|
|
$q->where("b.agency_code", $agencyCode);
|
|
|
|
|
})
|
|
|
|
|
->field("a.id,a.create_time")
|
|
|
|
|
->withAttr("create_time",function ($value){
|
|
|
|
|
return date("Y-m-d",strtotime($value));
|
|
|
|
|
})
|
|
|
|
|
->order(["a.id" => "desc"])
|
|
|
|
|
->select()
|
|
|
|
|
->toArray()
|
|
|
|
|
;
|
|
|
|
|
}
|
2021-12-14 09:34:27 +00:00
|
|
|
|
|
2021-12-15 07:04:20 +00:00
|
|
|
|
/**
|
2022-03-31 06:42:56 +00:00
|
|
|
|
* 获取 验证签到券的统计
|
2021-12-15 07:04:20 +00:00
|
|
|
|
* 每条数据都走一遍
|
|
|
|
|
*
|
|
|
|
|
* */
|
|
|
|
|
public function getSignStatistics($startTime,$endTime,$agencyCode)
|
|
|
|
|
{
|
|
|
|
|
return Coupon::alias("a")
|
|
|
|
|
->join("business b","a.business_code =b.code")
|
|
|
|
|
->whereTime("a.verificate_time", ">=", $startTime)
|
|
|
|
|
->whereTime("a.verificate_time", "<=", $endTime)
|
|
|
|
|
->where("a.is_verificated", "=", Coupon::is_verificated_on)
|
|
|
|
|
->when(!empty($agencyCode), function ($q) use ($agencyCode) {
|
|
|
|
|
$q->where("b.agency_code", $agencyCode);
|
|
|
|
|
})
|
|
|
|
|
->field("a.id,a.verificate_time")
|
|
|
|
|
->withAttr("verificate_time",function ($value){
|
|
|
|
|
return date("Y-m-d",strtotime($value));
|
|
|
|
|
})
|
|
|
|
|
->order(["a.id" => "desc"])
|
|
|
|
|
->select()
|
|
|
|
|
->toArray()
|
|
|
|
|
;
|
|
|
|
|
}
|
|
|
|
|
/**
|
2022-03-31 06:42:56 +00:00
|
|
|
|
* 获取 验证签到券的统计
|
2021-12-15 07:04:20 +00:00
|
|
|
|
* 每条数据都走一遍
|
|
|
|
|
*
|
|
|
|
|
* */
|
|
|
|
|
public function getReceiveStatistics($startTime,$endTime,$agencyCode)
|
|
|
|
|
{
|
|
|
|
|
return Coupon::alias("a")
|
|
|
|
|
->join("business b","a.business_code =b.code")
|
|
|
|
|
->whereTime("a.received_time", ">=", $startTime)
|
|
|
|
|
->whereTime("a.received_time", "<=", $endTime)
|
|
|
|
|
->when(!empty($agencyCode), function ($q) use ($agencyCode) {
|
|
|
|
|
$q->where("b.agency_code", $agencyCode);
|
|
|
|
|
})
|
|
|
|
|
->field("a.id,a.received_time")
|
|
|
|
|
->withAttr("received_time",function ($value){
|
|
|
|
|
return date("Y-m-d",strtotime($value));
|
|
|
|
|
})
|
|
|
|
|
->order(["a.id" => "desc"])
|
|
|
|
|
->select()
|
|
|
|
|
->toArray()
|
|
|
|
|
;
|
2021-12-14 09:34:27 +00:00
|
|
|
|
}
|
2021-12-16 02:34:06 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2022-03-31 06:42:56 +00:00
|
|
|
|
* 获取商家商家的签到券
|
2021-12-16 02:34:06 +00:00
|
|
|
|
* */
|
|
|
|
|
public function getBusinessOnShelfOnCount($businessCode)
|
|
|
|
|
{
|
|
|
|
|
return CouponMain::where("business_code",$businessCode)
|
|
|
|
|
->where("on_shelf",CouponMain::on_shelf_on)
|
|
|
|
|
->count();
|
|
|
|
|
}
|
2021-12-21 06:24:05 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 统计商家优惠卷情况
|
|
|
|
|
*
|
|
|
|
|
* @param $businessCode
|
|
|
|
|
* @param int $daySize 最近($daySize + 1)天内的领取量, 0表示当日领取量
|
|
|
|
|
*/
|
|
|
|
|
public function getCountBusinessOnShelf($businessCode, $daySize = 29)
|
|
|
|
|
{
|
|
|
|
|
$whereMap = [
|
|
|
|
|
['business_code', '=', $businessCode],
|
|
|
|
|
['on_shelf', '=', CouponMain::on_shelf_on]
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$totalSize = CouponMain::where($whereMap)->count();
|
|
|
|
|
$totalCountSize = CouponMain::where($whereMap)->sum('count');
|
|
|
|
|
$verificationSize = Coupon::alias('c')
|
|
|
|
|
->leftJoin('coupon_main m', 'c.coupon_id = m.id')
|
|
|
|
|
->where([
|
|
|
|
|
['m.business_code', '=', $businessCode],
|
|
|
|
|
['m.on_shelf', '=', CouponMain::on_shelf_on],
|
|
|
|
|
['c.is_verificated', '=', Coupon::is_verificated_on]
|
|
|
|
|
])
|
|
|
|
|
->count('c.id');
|
|
|
|
|
$unVerificationSize = $totalCountSize - $verificationSize;
|
|
|
|
|
$unVerificationSize = $unVerificationSize > 0 ? $unVerificationSize : 0;
|
|
|
|
|
$lastReceivedSize = Coupon::alias('c')
|
|
|
|
|
->leftJoin('coupon_main m', 'c.coupon_id = m.id')
|
|
|
|
|
->where([
|
|
|
|
|
['m.business_code', '=', $businessCode],
|
|
|
|
|
])
|
|
|
|
|
->when($daySize >= 0, function ($q) use ($daySize) {
|
|
|
|
|
$startDay = date('Y-m-d 00:00:00', strtotime('- '.$daySize.' day'));
|
|
|
|
|
$q->whereTime('c.received_time', '>=', $startDay);
|
|
|
|
|
})
|
|
|
|
|
->count('c.id');
|
2021-12-21 07:01:21 +00:00
|
|
|
|
$effectiveSize = CouponMain::where($whereMap)
|
|
|
|
|
->whereTime('start_time', '<=', date('Y-m-d H:i:s'))
|
|
|
|
|
->whereTime('end_time', '>=', date('Y-m-d H:i:s'))
|
2021-12-23 03:13:59 +00:00
|
|
|
|
->sum(Db::raw('(`count` - `received_count`)'));
|
2021-12-21 06:24:05 +00:00
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
// 发行总次数
|
|
|
|
|
'totalSize' => $totalSize,
|
|
|
|
|
// 发行优惠卷总数量
|
|
|
|
|
'totalCountSize' => $totalCountSize,
|
|
|
|
|
// 累计已核销数量
|
|
|
|
|
'verificationSize' => $verificationSize,
|
|
|
|
|
// 剩余未核销量
|
|
|
|
|
'unVerificationSize' => $unVerificationSize,
|
|
|
|
|
// 最近($daySize + 1)天内的领取量
|
|
|
|
|
'lastReceivedSize' => $lastReceivedSize,
|
2021-12-21 07:01:21 +00:00
|
|
|
|
// 有效的(进行中且未被领取)
|
|
|
|
|
'effectiveSize' => $effectiveSize,
|
2021-12-21 06:24:05 +00:00
|
|
|
|
];
|
|
|
|
|
}
|
2021-12-23 02:23:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2022-03-31 06:42:56 +00:00
|
|
|
|
* 签到券列表
|
2021-12-23 02:23:10 +00:00
|
|
|
|
* @param $whereMap
|
|
|
|
|
* @param $page
|
|
|
|
|
* @param $size
|
|
|
|
|
* @param $orders
|
|
|
|
|
* @param $auth
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function couponMainList($whereMap,$page,$size,$orders,$auth)
|
|
|
|
|
{
|
|
|
|
|
$data = [
|
|
|
|
|
'total' => 0,
|
|
|
|
|
'current' => $page,
|
|
|
|
|
'size' => $size,
|
|
|
|
|
'list' => new Collection(),
|
|
|
|
|
];
|
|
|
|
|
$rep = CouponMain::alias("a")
|
|
|
|
|
->join("business b", "a.business_code = b.code")
|
|
|
|
|
->where($whereMap)
|
|
|
|
|
->when(MemberModel::is_agency($auth['roles'] ), function ($q)use($auth) {
|
|
|
|
|
$q->where('b.agency_code', '=', $auth['business_code']);
|
2021-12-23 03:13:59 +00:00
|
|
|
|
})
|
|
|
|
|
->field("a.*,b.business_name,b.business_subtitle")
|
|
|
|
|
;
|
2021-12-23 02:23:10 +00:00
|
|
|
|
|
|
|
|
|
$data["total"] = $rep->count();
|
|
|
|
|
$data["list"] = $rep->order($orders)->select();
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
2022-01-20 05:56:21 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2022-03-31 06:42:56 +00:00
|
|
|
|
* 获取指定商圈的签到券 大屏使用
|
2022-01-20 05:56:21 +00:00
|
|
|
|
* */
|
|
|
|
|
|
|
|
|
|
public function screen($circle_id)
|
|
|
|
|
{
|
|
|
|
|
$whereMap = [];
|
|
|
|
|
$nowDate = date('Y-m-d H:i:s');
|
|
|
|
|
$whereMap[] = ['a.status', '=', CouponMain::status_on];
|
|
|
|
|
$whereMap[] = ['a.on_shelf', '=', CouponMain::on_shelf_on];
|
|
|
|
|
//$whereMap[] = ['start_time', '< TIME', $nowDate];
|
|
|
|
|
$whereMap[] = ['a.end_time', '> TIME', $nowDate];
|
|
|
|
|
$whereMap[] = ['a.using_count', '>', 0];
|
|
|
|
|
$whereMap[] = ['a.on_screen', '=', CouponMain::on_screen_yes];
|
|
|
|
|
|
|
|
|
|
return CouponMain::alias("a")
|
|
|
|
|
->join("business b" ,"a.business_code = b.code")
|
|
|
|
|
->where("b.business_circle_id",$circle_id)
|
|
|
|
|
->where($whereMap)
|
2022-05-06 06:04:12 +00:00
|
|
|
|
->field("a.business_code,a.id,a.image_url,a.name,b.business_subtitle")
|
2022-01-20 05:56:21 +00:00
|
|
|
|
->order("a.id desc")
|
|
|
|
|
->select()
|
|
|
|
|
->toArray();
|
|
|
|
|
}
|
2021-12-06 10:56:23 +00:00
|
|
|
|
}
|