CRMEB_PRO_M/app/dao/activity/coupon/StoreCouponUserDao.php

251 lines
9.8 KiB
PHP
Raw Permalink 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\dao\activity\coupon;
use app\dao\BaseDao;
use app\model\activity\coupon\StoreCouponUser;
/**
* Class StoreCouponUserDao
* @package app\dao\activity\coupon
*/
class StoreCouponUserDao extends BaseDao
{
/**
* 设置模型
* @return string
*/
protected function setModel(): string
{
return StoreCouponUser::class;
}
/**
* 搜索
* @param array $where
* @return \crmeb\basic\BaseModel|mixed|\think\Model
*/
public function search(array $where = [])
{
return parent::search($where)->when(isset($where['nickname']) && $where['nickname'] != '', function ($query) use ($where) {
$query->where('uid', 'IN' , function($q) use ($where) {
$q->name('user')->where('uid|real_name|nickname|account|phone', 'like', '%' . $where['nickname'] . '%')->field('uid');
});
})->when(isset($where['coupon_title']) && $where['coupon_title'] != '', function ($query) use ($where) {
$query->where('coupon_title', 'like', '%' . $where['coupon_title'] . '%');
})->when(isset($where['issue_type']) && $where['issue_type'] !== '', function ($query) use ($where) {
if($where['issue_type'] == -1) {
$time = time() + 24 * 60 * 60;
$query->where('end_time', '>=', $time);
} else {
$query->whereIn('cid', function ($query) use ($where) {
$query->name('store_coupon_issue')->where('type', $where['issue_type'])->field(['id'])->select();
});
}
});
}
/**
* 获取列表
* @param array $where
* @param string $field
* @param array $with
* @param int $page
* @param int $limit
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getList(array $where, string $field = '*', array $with = ['issue'], int $page = 0, int $limit = 0)
{
return $this->search($where)->field($field)->with($with)->page($page, $limit)->order('id desc')->select()->toArray();
}
/**
* 使用优惠券修改优惠券状态
* @param $id
* @return \think\Model|null
*/
public function useCoupon(int $id)
{
return $this->getModel()->where('id', $id)->update(['status' => 1, 'use_time' => time()]);
}
/**
* 获取指定商品id下的优惠卷
* @param array $productIds
* @param int $uid
* @param string $price
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function productIdsByCoupon(array $productIds, int $uid, string $price)
{
return $this->getModel()->whereIn('cid', function ($query) use ($productIds) {
$query->name('store_coupon_issue')->whereIn('id', function ($q) use ($productIds) {
$q->name('store_coupon_product')->whereIn('product_id', $productIds)->field('coupon_id')->select();
})->field(['id'])->select();
})->with('issue')->where(['uid' => $uid, 'status' => 0])->order('coupon_price DESC')
->where('use_min_price', '<=', $price)->select()
->where('start_time', '<=', time())->where('end_time', '>=', time())
->hidden(['status', 'is_fail'])->toArray();
}
/**
* 根据商品id获取
* @param array $cateIds
* @param int $uid
* @param string $price
* @return mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function cateIdsByCoupon(array $cateIds, int $uid, string $price)
{
return $this->getModel()->whereIn('cid', function ($query) use ($cateIds) {
$query->name('store_coupon_issue')->whereIn('category_id', $cateIds)->where('type', 1)->field('id')->select();
})->where(['uid' => $uid, 'status' => 0])->where('use_min_price', '<=', $price)
->where('start_time', '<=', time())->where('end_time', '>=', time())
->order('coupon_price DESC')->with('issue')->select()->hidden(['status', 'is_fail'])->toArray();
}
/**
* 获取当前用户可用的优惠卷
* @param array $ids
* @param int $uid
* @param string $price
* @return mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getUserCouponV1(int $uid, string $price = '0', array $ids = [])
{
return $this->getModel()->where(['uid' => $uid, 'status' => 0])->when(count($ids) != 0, function ($query) use ($ids) {
$query->whereNotIn('id', $ids);
})->whereIn('cid', function ($query) {
$query->name('store_coupon_issue')->where('type', 0)->field(['id'])->select();
})->when($price, function ($query, $price) {
$query->where('use_min_price', '<=', $price);
})
->where('start_time', '<=', time())->where('end_time', '>=', time())
->order('coupon_price DESC')->with('issue')->select()->hidden(['status', 'is_fail'])->toArray();
}
/**
* 获取当前用户可用的优惠卷
* @param array $ids
* @param int $uid
* @param string $price
* @return mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getUserCoupon(array $ids, int $uid, string $price)
{
return $this->getModel()->where(['uid' => $uid, 'status' => 0])->when(count($ids) != 0, function ($query) use ($ids) {
$query->whereNotIn('id', $ids);
})->whereIn('cid', function ($query) {
$query->name('store_coupon_issue')->where('type', 0)->field(['id'])->select();
})->where('use_min_price', '<=', $price)
->where('start_time', '<=', time())->where('end_time', '>=', time())
->order('coupon_price DESC')->with('issue')->select()->hidden(['status', 'is_fail'])->toArray();;
}
/**
* 获取当前用户所有可用的优惠卷
* @param int $uid
* @return mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getUserAllCoupon(int $uid)
{
return $this->getModel()->where(['uid' => $uid, 'status' => 0, 'is_fail' => 0])
->where('start_time', '<=', time())->where('end_time', '>=', time())
->order('coupon_price DESC')->with('issue')->select()->hidden(['status', 'is_fail'])->toArray();
}
/**
* 获取列表带排序
* @param array $where
* @param $order
* @param int $page
* @param int $limit
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getCouponListByOrder(array $where, $order, int $page = 0, int $limit = 0)
{
return $this->search($where)->with('issue')->when($page > 0 && $limit > 0, function ($qeury) use ($page, $limit) {
$qeury->page($page, $limit);
})->when($order != '', function ($query) use ($order) {
$query->order($order);
})->when(isset($where['coupon_ids']), function ($qeury) use ($where) {
$qeury->whereIn('cid', $where['coupon_ids']);
})->select()->toArray();
}
/**根据月份查询用户获得的优惠券
* @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->search($where)
->whereMonth('add_time')
->whereIn('cid', $where['couponIds'])
->field('count(id) as num,FROM_UNIXTIME(add_time, \'%Y-%m\') as time')
->group("FROM_UNIXTIME(add_time, '%Y-%m')")
->select()->toArray();
//echo $this->getModel()->getLastSql();die;
}
/**根据时间查询
* @param array $where
* @return array|\think\Model|null
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getUserCounponByMonth(array $where)
{
return $this->search($where)->whereMonth('add_time')->find();
}
/**
* 获取本月领取的优惠券
* @param $uid
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getVipCouponList($uid)
{
return $this->getModel()->where('uid', $uid)->whereMonth('add_time')->select()->toArray();
}
}