CRMEB_PRO_M/app/services/activity/coupon/StoreCouponUserServices.php

762 lines
31 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace app\services\activity\coupon;
use app\services\activity\promotions\StorePromotionsServices;
use app\services\BaseServices;
use app\services\product\brand\StoreBrandServices;
use app\services\product\product\StoreProductRelationServices;
use app\services\user\UserServices;
use app\dao\activity\coupon\StoreCouponUserDao;
use app\services\product\category\StoreProductCategoryServices;
use crmeb\utils\Arr;
/**
* Class StoreCouponUserServices
* @package app\services\activity\coupon
* @mixin StoreCouponUserDao
*/
class StoreCouponUserServices extends BaseServices
{
/**
* StoreCouponUserServices constructor.
* @param StoreCouponUserDao $dao
*/
public function __construct(StoreCouponUserDao $dao)
{
$this->dao = $dao;
}
/**
* 下单页面显示可用优惠券
* @param $uid
* @param $cartGroup
* @param $price
* @return array
*/
public function getUseableCouponList(int $uid, array $cartGroup)
{
$userCoupons = $this->dao->getUserAllCoupon($uid);
$result = [];
if ($userCoupons) {
$cartInfo = $cartGroup['valid'];
$promotions = $cartGroup['promotions'] ?? [];
$promotionsList = [];
if ($promotions) {
$promotionsList = array_combine(array_column($promotions, 'id'), $promotions);
}
$isOverlay = function ($cart) use ($promotionsList) {
$productInfo = $cart['productInfo'] ?? [];
if (!$productInfo) {
return false;
}
if (isset($cart['promotions_id']) && $cart['promotions_id']) {
foreach ($cart['promotions_id'] as $key => $promotions_id) {
$promotions = $promotionsList[$promotions_id] ?? [];
if ($promotions && $promotions['promotions_type'] != 4) {
$overlay = is_string($promotions['overlay']) ? explode(',', $promotions['overlay']) : $promotions['overlay'];
if (!in_array(5, $overlay)) {
return false;
}
}
}
}
return true;
};
/** @var StoreProductCategoryServices $storeCategoryServices */
$storeCategoryServices = app()->make(StoreProductCategoryServices::class);
/** @var StoreBrandServices $storeBrandServices */
$storeBrandServices = app()->make(StoreBrandServices::class);
foreach ($userCoupons as $coupon) {
$price = 0;
$count = 0;
switch ($coupon['applicable_type']) {
case 0:
foreach ($cartInfo as $cart) {
if (!$isOverlay($cart)) continue;
$price = bcadd((string)$price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2), 2);
$count++;
}
break;
case 1://品类券
$cateGorys = $storeCategoryServices->getAllById((int)$coupon['category_id']);
if ($cateGorys) {
$cateIds = array_column($cateGorys, 'id');
foreach ($cartInfo as $cart) {
if (!$isOverlay($cart)) continue;
if (isset($cart['productInfo']['cate_id']) && array_intersect(explode(',', $cart['productInfo']['cate_id']), $cateIds)) {
$price = bcadd((string)$price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2), 2);
$count++;
}
}
}
break;
case 2:
foreach ($cartInfo as $cart) {
if (!$isOverlay($cart)) continue;
if (isset($cart['product_id']) && in_array($cart['product_id'], explode(',', $coupon['product_id']))) {
$price = bcadd((string)$price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2), 2);
$count++;
}
}
break;
case 3://品牌
$brands = $storeBrandServices->getAllById((int)$coupon['brand_id']);
if ($brands) {
$brandIds = array_column($brands, 'id');
foreach ($cartInfo as $cart) {
if (!$isOverlay($cart)) continue;
if (isset($cart['productInfo']['brand_id']) && in_array($cart['productInfo']['brand_id'], $brandIds)) {
$price = bcadd((string)$price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2), 2);
$count++;
}
}
}
break;
}
if ($count && $coupon['use_min_price'] <= $price) {
$coupon_data['id'] = $coupon['id'];
$coupon_data['cid'] = $coupon['cid'];
$coupon_data['type'] = $coupon['applicable_type'];
$coupon_data['applicable_type'] = $coupon['applicable_type'];
$coupon_data['receive_type'] = $coupon['receive_type'];
$coupon_data['start_time'] = $coupon['start_time'] ? date('Y-m-d', $coupon['start_time']) : date('Y-m-d', $coupon['add_time']);
$coupon_data['add_time'] = date('Y-m-d', $coupon['add_time']);
$coupon_data['end_time'] = date('Y-m-d', $coupon['end_time']);
$coupon_data['title'] = $coupon['coupon_title'];
$coupon_data['use_min_price'] = floatval($coupon['use_min_price']);
$coupon_data['coupon_type'] = $coupon['coupon_type'];
$coupon_data['coupon_price'] = floatval($coupon['coupon_price']);
$result[] = $coupon_data;
}
}
}
return $result;
}
/**
* 获取列表
* @param array $where
* @return array
*/
public function issueLog(array $where)
{
[$page, $limit] = $this->getPageValue();
$list = $this->dao->getList($where, 'uid,add_time', ['userInfo'], $page, $limit);
foreach ($list as &$item) {
$item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
}
$count = $this->dao->count($where);
return compact('list', 'count');
}
/**
* 获取列表
* @param array $where
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function systemPage(array $where)
{
[$page, $limit] = $this->getPageValue();
$list = $this->dao->getList($where, '*', ['issue'], $page, $limit);
$count = 0;
if ($list) {
/** @var UserServices $userServices */
$userServices = app()->make(UserServices::class);
$userAll = $userServices->getColumn([['uid', 'IN', array_column($list, 'uid')]], 'uid,nickname', 'uid');
foreach ($list as &$item) {
$item['nickname'] = $userAll[$item['uid']]['nickname'] ?? '';
}
$count = $this->dao->count($where);
}
return compact('list', 'count');
}
/**
* 获取用户优惠券
* @param int $id
* @param int $status
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getUserCouponList(int $id, int $status = -1)
{
[$page, $limit] = $this->getPageValue();
$where = ['uid' => $id];
if ($status != -1) $where['status'] = $status;
$list = $this->dao->getList($where, '*', ['issue'], $page, $limit);
foreach ($list as &$item) {
$item['_add_time'] = date('Y-m-d H:i:s', $item['add_time']);
$item['_end_time'] = date('Y-m-d H:i:s', $item['end_time']);
if (!$item['coupon_time']) {
$item['coupon_time'] = ceil(($item['end_use_time'] - $item['start_use_time']) / '86400');
}
}
$count = $this->dao->count($where);
return compact('list', 'count');
}
/**
* 恢复优惠券
* @param int $id
* @return bool|mixed
*/
public function recoverCoupon(int $id)
{
$status = $this->dao->value(['id' => $id], 'status');
if ($status) return $this->dao->update($id, ['status' => 0, 'use_time' => 0]);
else return true;
}
/**
* 过期优惠卷失效
*/
public function checkInvalidCoupon()
{
$this->dao->update([['end_time', '<', time()], ['status', '=', '0']], ['status' => 2]);
}
/**
* 获取用户有效优惠劵数量
* @param int $uid
* @return int
*/
public function getUserValidCouponCount(int $uid)
{
$this->checkInvalidCoupon();
return $this->dao->getCount(['uid' => $uid, 'status' => 0]);
}
/**
* 下单页面显示可用优惠券
* @param int $uid
* @param array $cartGroup
* @param int $store_id
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getUsableCouponList(int $uid, array $cartGroup, int $store_id = 0)
{
$userCoupons = $this->dao->getUserAllCoupon($uid);
$result = [];
if ($userCoupons) {
$cartInfo = $cartGroup['valid'];
$promotions = $cartGroup['promotions'] ?? [];
$promotionsList = [];
if($promotions){
$promotionsList = array_combine(array_column($promotions, 'id'), $promotions);
}
//验证是否适用门店
$isApplicableStore = function ($couponInfo) use ($store_id) {
if ($store_id && isset($couponInfo['applicable_type']) && isset($couponInfo['applicable_store_id'])) {
$applicable_store_id = is_array($couponInfo['applicable_store_id']) ? $couponInfo['applicable_store_id'] : explode(',', $couponInfo['applicable_store_id']);
//活动不适用该门店
if ($couponInfo['applicable_type'] == 0 || ($couponInfo['applicable_type'] == 2 && !in_array($store_id, $applicable_store_id))) {
return false;
}
}
return true;
};
$isOverlay = function ($cart) use ($promotionsList) {
$productInfo = $cart['productInfo'] ?? [];
if (!$productInfo) {
return false;
}
//门店独立商品 不使用优惠券
$isBranchProduct = isset($productInfo['type']) && isset($productInfo['pid']) && $productInfo['type'] == 1 && !$productInfo['pid'];
if ($isBranchProduct) {
return false;
}
if (isset($cart['promotions_id']) && $cart['promotions_id']) {
foreach ($cart['promotions_id'] as $key => $promotions_id) {
$promotions = $promotionsList[$promotions_id] ?? [];
if ($promotions && $promotions['promotions_type'] != 4){
$overlay = is_string($promotions['overlay']) ? explode(',', $promotions['overlay']) : $promotions['overlay'];
if (!in_array(5, $overlay)) {
return false;
}
}
}
}
return true;
};
/** @var StoreProductCategoryServices $storeCategoryServices */
$storeCategoryServices = app()->make(StoreProductCategoryServices::class);
/** @var StoreBrandServices $storeBrandServices */
$storeBrandServices = app()->make(StoreBrandServices::class);
foreach ($userCoupons as $coupon) {
if (!$isApplicableStore($coupon)) {//不适用门店跳过
continue;
}
$price = 0;
$count = 0;
switch ($coupon['coupon_applicable_type']) {
case 0:
foreach ($cartInfo as $cart) {
if (!$isOverlay($cart)) continue;
$price = bcadd((string)$price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2), 2);
$count++;
}
break;
case 1://品类券
$cateGorys = $storeCategoryServices->getAllById((int)$coupon['category_id']);
if ($cateGorys) {
$cateIds = array_column($cateGorys, 'id');
foreach ($cartInfo as $cart) {
if (!$isOverlay($cart)) continue;
if (isset($cart['productInfo']['cate_id']) && array_intersect(explode(',', $cart['productInfo']['cate_id']), $cateIds)) {
$price = bcadd((string)$price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2), 2);
$count++;
}
}
}
break;
case 2://商品
foreach ($cartInfo as $cart) {
if (!$isOverlay($cart)) continue;
$product_id = isset($cart['productInfo']['pid']) && $cart['productInfo']['pid'] ? $cart['productInfo']['pid'] : ($cart['product_id'] ?? 0);
if ($product_id && in_array($product_id, explode(',', $coupon['product_id']))) {
$price = bcadd((string)$price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2), 2);
$count++;
}
}
break;
case 3://品牌
$brands = $storeBrandServices->getAllById((int)$coupon['brand_id']);
if ($brands) {
$brandIds = array_column($brands, 'id');
foreach ($cartInfo as $cart) {
if (!$isOverlay($cart)) continue;
if (isset($cart['productInfo']['brand_id']) && in_array($cart['productInfo']['brand_id'], $brandIds)) {
$price = bcadd((string)$price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2), 2);
$count++;
}
}
}
break;
}
if ($count && $coupon['use_min_price'] <= $price) {
$coupon['start_time'] = $coupon['start_time'] ? date('Y/m/d', $coupon['start_time']) : date('Y/m/d', $coupon['add_time']);
$coupon['add_time'] = date('Y/m/d', $coupon['add_time']);
$coupon['end_time'] = date('Y/m/d', $coupon['end_time']);
$coupon['title'] = $coupon['coupon_title'];
$coupon['type'] = $coupon['coupon_applicable_type'];
$coupon['use_min_price'] = floatval($coupon['use_min_price']);
$coupon['coupon_price'] = floatval($coupon['coupon_price']);
$result[] = $coupon;
}
}
}
return $result;
}
/**
* 下单页面显示可用优惠券
* @param $uid
* @param $cartGroup
* @param $price
* @return array
*/
public function getOldUsableCouponList(int $uid, array $cartGroup)
{
$cartPrice = $cateIds = [];
$productId = Arr::getUniqueKey($cartGroup['valid'], 'product_id');
foreach ($cartGroup['valid'] as $value) {
$cartPrice[] = bcmul((string)$value['truePrice'], (string)$value['cart_num'], 2);
}
$maxPrice = count($cartPrice) ? max($cartPrice) : 0;
if ($productId) {
/** @var StoreProductRelationServices $storeProductRelationServices */
$storeProductRelationServices = app()->make(StoreProductRelationServices::class);
$cateId = $storeProductRelationServices->productIdByCateId($productId);
if ($cateId) {
/** @var StoreProductCategoryServices $cateServices */
$cateServices = app()->make(StoreProductCategoryServices::class);
$catePids = $cateServices->cateIdByPid($cateId);
$cateIds = array_merge($cateId, $catePids);
} else {
$cateIds = $cateId;
}
}
$productCouponList = $this->dao->productIdsByCoupon($productId, $uid, (string)$maxPrice);
$cateCouponList = $this->dao->cateIdsByCoupon($cateIds, $uid, (string)$maxPrice);
$list = array_merge($productCouponList, $cateCouponList);
$couponIds = Arr::getUniqueKey($list, 'id');
$sumCartPrice = array_sum($cartPrice);
$list1 = $this->dao->getUserCoupon($couponIds, $uid, (string)$sumCartPrice);
$list = array_merge($list, $list1);
foreach ($list as &$item) {
$item['add_time'] = date('Y/m/d', $item['add_time']);
$item['end_time'] = date('Y/m/d', $item['end_time']);
$item['title'] = $item['coupon_title'];
$item['type'] = $item['coupon_applicable_type'] ?? 0;
}
return $list;
}
/**
* 用户领取优惠券
* @param $uid
* @param $issueCouponInfo
* @param string $type
* @return mixed
*/
public function addUserCoupon($uid, $issueCouponInfo, string $type = 'get')
{
$data = [];
$data['cid'] = $issueCouponInfo['id'];
$data['uid'] = $uid;
$data['coupon_title'] = $issueCouponInfo['title'];
$data['coupon_price'] = $issueCouponInfo['coupon_price'];
$data['use_min_price'] = $issueCouponInfo['use_min_price'];
$data['add_time'] = time();
if ($issueCouponInfo['coupon_time']) {
$data['start_time'] = $data['add_time'];
$data['end_time'] = $data['add_time'] + $issueCouponInfo['coupon_time'] * 86400;
} else {
$data['start_time'] = $issueCouponInfo['start_use_time'];
$data['end_time'] = $issueCouponInfo['end_use_time'];
}
$data['type'] = $type;
return $this->dao->save($data);
}
/**会员领取优惠券
* @param $uid
* @param $issueCouponInfo
* @param string $type
* @return mixed
*/
public function addMemberUserCoupon($uid, $issueCouponInfo, $type = 'get')
{
$data = [];
$data['cid'] = $issueCouponInfo['id'];
$data['uid'] = $uid;
$data['coupon_title'] = $issueCouponInfo['title'];
$data['coupon_price'] = $issueCouponInfo['coupon_price'];
$data['use_min_price'] = $issueCouponInfo['use_min_price'];
$data['add_time'] = time();
$data['start_time'] = strtotime(date('Y-m-d 00:00:00', time()));
$data['end_time'] = strtotime(date('Y-m-d 23:59:59', strtotime('+30 day')));
$data['type'] = $type;
return $this->dao->save($data);
}
/**
* 获取用户已领取的优惠卷
* @param int $uid
* @param $type
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getUserCounpon(int $uid, $types, $type)
{
$where = [];
$where['uid'] = $uid;
switch ($types) {
case 1:
$where['status'] = 1;
break;
case 2:
$where['status'] = 2;
break;
default:
$where['status'] = 0;
break;
}
$where['issue_type'] = $type;
[$page, $limit] = $this->getPageValue();
$list = $this->dao->getCouponListByOrder($where, 'status ASC,add_time DESC', $page, $limit);
/** @var StoreProductCategoryServices $categoryServices */
$categoryServices = app()->make(StoreProductCategoryServices::class);
$category = $categoryServices->getColumn([], 'pid,cate_name', 'id');
/** @var StoreBrandServices $storeBrandServices */
$storeBrandServices = app()->make(StoreBrandServices::class);
$brand = $storeBrandServices->getColumn([], 'id,pid,brand_name', 'id');
foreach ($list as &$item) {
$item['applicable_type'] = $item['coupon_applicable_type'];
if ($item['category_id'] && isset($category[$item['category_id']])) {
$item['category_type'] = $category[$item['category_id']]['pid'] == 0 ? 1 : 2;
$item['category_name'] = $category[$item['category_id']]['cate_name'];
} else {
$item['category_type'] = '';
$item['category_name'] = '';
}
if ($item['brand_id'] && isset($brand[$item['brand_id']])) {
$item['category_name'] = $brand[$item['brand_id']]['brand_name'];
} else {
$item['brand_name'] = '';
}
}
return $list ? $this->tidyCouponList($list) : [];
}
/**
* 我的优惠券数量
* @param int $uid
* @return array
* @throws \think\db\exception\DbException
*/
public function getUserCounponNum(int $uid): array
{
$data['not_used'] = $this->dao->count(['uid' => $uid, 'status' => 0]); // 未使用
$data['used'] = $this->dao->count(['uid' => $uid, 'status' => 1]);// 已使用
$data['expired'] = $this->dao->count(['uid' => $uid, 'status' => 2]);// 已过期
return $data;
}
/**
* 格式化优惠券
* @param $couponList
* @return mixed
*/
public function tidyCouponList($couponList)
{
$time = time();
foreach ($couponList as &$coupon) {
if ($coupon['status'] == '已使用') {
$coupon['_type'] = 0;
$coupon['_msg'] = '已使用';
$coupon['pc_type'] = 0;
$coupon['pc_msg'] = '已使用';
} else if ($coupon['status'] == '已过期') {
$coupon['is_fail'] = 1;
$coupon['_type'] = 0;
$coupon['_msg'] = '已过期';
$coupon['pc_type'] = 0;
$coupon['pc_msg'] = '已过期';
} else if ($coupon['end_time'] < $time) {
$coupon['is_fail'] = 1;
$coupon['_type'] = 0;
$coupon['_msg'] = '已过期';
$coupon['pc_type'] = 0;
$coupon['pc_msg'] = '已过期';
} else if ($coupon['start_time'] > $time) {
$coupon['_type'] = 0;
$coupon['_msg'] = '未开始';
$coupon['pc_type'] = 1;
$coupon['pc_msg'] = '未开始';
} else {
if ($coupon['start_time'] + 3600 * 24 > $time) {
$coupon['_type'] = 2;
$coupon['_msg'] = '立即使用';
$coupon['pc_type'] = 1;
$coupon['pc_msg'] = '可使用';
} else {
$coupon['_type'] = 1;
$coupon['_msg'] = '立即使用';
$coupon['pc_type'] = 1;
$coupon['pc_msg'] = '可使用';
}
}
$coupon['add_time'] = $coupon['_add_time'] = $coupon['start_time'] ? date('Y/m/d', $coupon['start_time']) : date('Y/m/d', $coupon['add_time']);
$coupon['end_time'] = $coupon['_end_time'] = date('Y/m/d', $coupon['end_time']);
$coupon['use_min_price'] = floatval($coupon['use_min_price']);
$coupon['coupon_price'] = floatval($coupon['coupon_price']);
}
return $couponList;
}
/**
* 获取会员优惠券列表
* @param $uid
* @return array|mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getMemberCoupon($uid)
{
if (!$uid) return [];
[$page, $limit] = $this->getPageValue();
if (!$page && !$limit) {
$page = 0;
$limit = 4;
}
/** @var StoreCouponIssueServices $couponIssueService */
$couponIssueService = app()->make(StoreCouponIssueServices::class);
$couponWhere['category'] = 2;
$couponInfo = $couponIssueService->getMemberCouponIssueList($couponWhere, $page, $limit, ['used' => function ($query) use ($uid) {
$query->where('uid', $uid);
}]);
if ($couponInfo) {
$time = time();
foreach ($couponInfo as $k => &$coupon) {
$coupon['type_name'] = $couponIssueService->_couponType[$coupon['type']];
$coupon['is_use'] = $uid ? isset($coupon['used']) : false;
if (isset($coupon['used']) && $coupon['used']) {
if ($coupon['used']['status'] == '已使用') {
$coupon['used']['_type'] = 0;
$coupon['used']['_msg'] = '已使用';
$coupon['used']['pc_type'] = 0;
$coupon['used']['pc_msg'] = '已使用';
} else if ($coupon['used']['status'] == '已过期') {
$coupon['used']['is_fail'] = 1;
$coupon['used']['_type'] = 0;
$coupon['used']['_msg'] = '已过期';
$coupon['used']['pc_type'] = 0;
$coupon['used']['pc_msg'] = '已过期';
} else if ($coupon['used']['end_time'] < $time) {
$coupon['used']['is_fail'] = 1;
$coupon['used']['_type'] = 0;
$coupon['used']['_msg'] = '已过期';
$coupon['used']['pc_type'] = 0;
$coupon['used']['pc_msg'] = '已过期';
} else if ($coupon['used']['start_time'] > $time) {
$coupon['used']['_type'] = 0;
$coupon['used']['_msg'] = '未开始';
$coupon['used']['pc_type'] = 1;
$coupon['used']['pc_msg'] = '未开始';
} else {
if ($coupon['used']['start_time'] + 3600 * 24 > $time) {
$coupon['used']['_type'] = 2;
} else {
$coupon['used']['_type'] = 1;
}
$coupon['used']['_msg'] = '立即使用';
$coupon['used']['pc_type'] = 1;
$coupon['used']['pc_msg'] = '可使用';
}
$coupon['used']['end_time'] = $coupon['used']['_end_time'] = date('Y/m/d', $coupon['used']['end_time']);
$coupon['used']['_add_time'] = $coupon['used']['start_time'] ? date('Y/m/d', $coupon['used']['start_time']) : date('Y/m/d', $coupon['add_time']);
}
$coupon['add_time'] = date('Y/m/d', $coupon['add_time']);
$coupon['start_use_time'] = date('Y/m/d', $coupon['start_use_time']);
$coupon['end_use_time'] = date('Y/m/d', $coupon['end_use_time']);
$coupon['use_min_price'] = floatval($coupon['use_min_price']);
$coupon['coupon_price'] = floatval($coupon['coupon_price']);
}
}
return $couponInfo ?: [];
}
/**
* 根据月分组看会员发放优惠券情况
* @param array $where
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function memberCouponUserGroupBymonth(array $where)
{
return $this->dao->memberCouponUserGroupBymonth($where);
}
/**
* 会员券失效
* @param $coupon_user
* @return false|mixed
*/
public function memberCouponIsFail($coupon_user)
{
if (!$coupon_user) return false;
if ($coupon_user['use_time'] == 0) {
return $this->dao->update($coupon_user['id'], ['is_fail' => 1, 'status' => 2]);
}
}
/**
* 根据id查询会员优惠劵
* @param int $id
* @param string $filed
* @param array $with
* @return array|false|\think\Model|null
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getCouponUserOne(int $id, string $filed = '', array $with = [])
{
if (!$id) return false;
return $this->dao->getOne(['id' => $id], $filed, $with);
}
/**根据时间查询用户优惠券
* @param array $where
* @return array|bool|\think\Model|null
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getUserCounponByMonth(array $where)
{
if (!$where) return false;
return $this->dao->getUserCounponByMonth($where);
}
/**
* 检查付费会员是否领取了会员券
* @param $uid
* @param $vipCouponIds
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function checkHave($uid, $vipCouponIds)
{
$list = $this->dao->getVipCouponList($uid);
$have = [];
foreach ($list as $item) {
if ($vipCouponIds && in_array($item['cid'], $vipCouponIds)) {
$have[$item['cid']] = true;
} else {
$have[$item['cid']] = false;
}
}
return $have;
}
/**
* 使用优惠券验证
* @param int $couponId
* @param int $uid
* @param array $cartInfo
* @param array $promotions
* @param int $store_id
* @return bool
*/
public function useCoupon(int $couponId, int $uid, array $cartInfo, array $promotions = [], int $store_id = 0)
{
if (!$couponId || !$uid || !$cartInfo) {
return true;
}
/** @var StorePromotionsServices $promotionsServices */
$promotionsServices = app()->make(StorePromotionsServices::class);
try {
[$couponInfo, $couponPrice] = $promotionsServices->useCoupon($couponId, $uid, $cartInfo, $promotions, $store_id);
} catch (\Throwable $e) {
$couponInfo = [];
$couponPrice = 0;
}
if ($couponInfo) {
$this->dao->useCoupon($couponId);
}
return true;
}
}