quanwei
18 hours ago c441dea81bd86bdfb12dff35821fed51f4cc91c2
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
 
namespace app\operations\controller\plus\coupon;
 
use app\operations\controller\Controller;
use app\operations\model\plus\coupon\Coupon as CouponModel;
use app\operations\model\plus\coupon\UserCoupon as UserCouponModel;
use app\operations\model\user\Grade as GradeModel;
use app\operations\model\product\Product as ProductModel;
use app\operations\model\product\Category as CategoryModel;
/**
 * 优惠券控制器
 */
class Coupon extends Controller
{
    /* @var CouponModel $model */
    private $model;
 
    /**
     * 构造方法
     */
    public function initialize()
    {
        $this->model = new CouponModel;
    }
 
    /**
     * 优惠券列表
     */
    public function index()
    {
        $list = $this->model->getList($this->postData());
        return $this->renderSuccess('', compact('list'));
    }
 
    /**
     * 添加优惠券
     */
    public function add()
    {
        $data = $this->postData();
        // 新增记录
        if ($this->model->add($data)) {
            return $this->renderSuccess('添加成功');
        }
        return $this->renderError('添加失败');
    }
 
    /**
     * 优惠券详情
     */
    public function couponDetail()
    {
        $coupon_id = $this->postData('coupon_id/i');
 
        // 优惠券详情
        $detail = CouponModel::detail($coupon_id)->toArray();
        if($detail['expire_type']==20){
            $detail['active_time'][0]=date('Y-m-d H:i:s',$detail['start_time']['value']);
            $detail['active_time'][1]=date('Y-m-d H:i:s',$detail['end_time']['value']);
        }
        if($detail['apply_range'] == 20){
            $detail['product_ids'] = explode(',', $detail['product_ids']);
            $detail['product_list'] = (new ProductModel())->getListByIds($detail['product_ids']);
        }
        if($detail['apply_range'] == 30){
            $category_ids = json_decode($detail['category_ids'], true);
            $detail['category_list']['first'] = (new CategoryModel())->getListByIds($category_ids['first']);
            $detail['category_list']['second'] = (new CategoryModel())->getListByIds($category_ids['second']);
            foreach ($detail['category_list']['second'] as &$item){
                $item['parent'] = CategoryModel::detail($item['parent_id'])['name'];
            }
        }
        return $this->renderSuccess('', compact('detail'));
    }
 
    /**
     * 更新优惠券
     */
    public function edit()
    {
        $data = $this->postData();
        unset($data['state']);
        $model = new CouponModel;
        // 更新记录
        if ($model->edit($data)) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError('更新失败');
    }
 
    /**
     * 删除优惠券
     */
    public function delete($coupon_id)
    {
        $coupon_id = $this->postData('coupon_id/i');
        // 优惠券详情
        $model = new CouponModel;
        // 更新记录
        if ($model->setDelete(['coupon_id' => $coupon_id])) {
            return $this->renderSuccess('删除成功');
        }
        return $this->renderError('删除失败');
    }
 
    /**
     * 领取记录
     */
    public function receive()
    {
        $model = new UserCouponModel;
        $list = $model->getList($this->postData());
        return $this->renderSuccess('', compact('list'));
    }
 
    /**
     * 发送优惠券
     */
    public function SendCoupon()
    {
        if($this->request->isGet()){
            $model = new GradeModel;
            $list = $model->getLists();
            return $this->renderSuccess('', compact('list'));
        }
        $model = new UserCouponModel;
        if ($model->SendCoupon($this->postData())) {
            return $this->renderSuccess('发送成功');
        }
        return $this->renderError('发送失败');
    }
 
}