CouponModel.php
2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
/**
* Created by PhpStorm.
* User: yhbr
* Date: 2018/9/5
* Time: 15:03
*/
namespace app\coupons\model;
use think\Model;
use think\Db;
class CouponModel extends Model
{
/**
* 个人中心优惠券列表
* @param null $status
* @param $user_id
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function getAllDiscountCouponByUniqueIdAndByStatus($status = null, $user_id)
{
if ($status == null) {
$status = 1;
}
$map['status'] = array('eq', $status);
$map['user_id'] = array('eq', $user_id);
$res = Db::name('discount_coupon')->field('id,reduce,discount_coupon_name,deadline')->where($map)->select()->toArray();
foreach ($res as $key => $item) {
$item['deadline'] = date('Y-m-d', $item['deadline']);
$res[$key] = $item;
}
return $res;
}
/**
* 根据金额获取符合条件的满减优惠券
* @param $activity_id
* @param null $schedule_id
* @param $num
* @param $userId
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function orderCoupon($activity_id, $schedule_id = null, $num, $userId)
{
$info = Db::name('activity')->field('is_down_payment,down_price')->where(['id' => $activity_id])->find();
//如果有定金,则按照定金计算满减
if ($info['is_down_payment'] == 1) {
$overflow = $info['down_price'] * $num;
} //如果没有定金,则直接按照选中的活动批次价格计算满减
else {
$price = Db::name('activity_schedule')->where(['activity_id' => $activity_id, 'id' => $schedule_id])->value('price');
$overflow = $price * $num;
}
$map = [
'user_id' => ['eq', $userId],
'overflow' => ['elt', $overflow],
'deadline' => ['gt', time()],
'status' => ['eq', 1]
];
$coupons = Db::name('discount_coupon')->field('id as discount_coupon_id,discount_coupon_name,reduce')->where($map)->select()->toArray();
return (empty($coupons)) ? null : $coupons;
}
}