CouponModel.php
2.7 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
71
72
73
74
75
76
77
78
79
80
81
<?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 $user_id
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function getAllDiscountCouponByUniqueIdAndByStatus($user_id)
{
$data = [];
$x = 0;
$y = 0;
$z = 0;
$map['user_id'] = array('eq', $user_id);
$data['unused'] = [];
$data['used'] = [];
$data['expired'] = [];
$res = Db::name('discount_coupon')->field('reduce,discount_coupon_name,deadline,status')->where($map)->order('status')->select()->toArray();
foreach ($res as $key => $item) {
$item['deadline'] = date('Y.m.d', $item['deadline']);
if ($item['status'] == 1) {
$data['unused'][$x] = $item;
$x++;
} elseif ($item['status'] == 2) {
$data['used'][$y] = $item;
$y++;
} else {
$data['expired'][$z] = $item;
$z++;
}
}
return $data;
}
/**
* 根据金额获取符合条件的满减优惠券
* @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;
}
}