quanwei
2025-12-10 898043fc97d2ab8b793fd317a049b874ed207c6d
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
 
namespace app\api\model\user;
 
use app\common\model\user\Card as CardModel;
use app\api\model\product\Product as ProductModel;
use app\api\model\order\Order as OrderModel;
use app\common\model\settings\Setting as SettingModel;
use app\api\model\order\OrderProduct;
use app\common\service\product\factory\ProductFactory;
 
/**
 * 会员卡模型
 */
class Card extends CardModel
{
    public $model;
    public $user;
    /**
     * 获取列表
     */
    public function getList($data)
    {
        $model = $this;
        if (isset($data['card_name']) && $data['card_name']) {
            $model = $model->where('card_name', 'like', '%' . $data['card_name'] . '%');
        }
        $list = $model->where('is_delete', '=', 0)
            ->where('status', '=', 0)
            ->order(['sort' => 'asc', 'create_time' => 'asc'])
            ->paginate($data);
        return $list;
    }
 
    /**
     * 获取数量
     */
    public function getCount($user)
    {
        $cardCount = $this->where('is_delete', '=', 0)
            ->where('status', '=', 0)
            // ->where('type_id', '<>', 3)
            ->count();
        $myCount = (new CardRecord())->alias('r')
            ->where('c.is_delete', '=', 0)
            ->where('c.status', '=', 0)
            ->where('r.is_delete', '=', 0)
            ->where('r.pay_status', '=', 20)
            ->where('user_id', '=', $user['user_id'])
            ->join('user_card c', 'c.card_id=r.card_id')
            ->where(function ($query) {
                $query->where('expire_time', '=', 0)->whereOr('expire_time', '>', time());
            })
            ->count();
        $info['cardCount'] = $cardCount;
        $info['myCount'] = $myCount;
        return $info;
    }
 
    /**
     * 创建订单
     */
    public function createOrder($productData,$user)
    {
        foreach ($productData as $item){
            $product = ProductModel::detail($item["product_id"]);
            $this->user = $user;
            $this->model = new OrderModel;
            $this->model->transaction(function () use ($product) {
                // 创建订单事件
                $this->createOrderEvent($product);
                $data['multiple'] = 0;
                $this->model->onPaymentByBalance($this->model['order_no'],$data);
            });
        }
 
 
    }
 
    /**
     * 创建订单事件
     */
    private function createOrderEvent($product)
    {
        // 新增订单记录
        $status = $this->add($product);
        // 保存订单商品信息
        $this->saveOrderProduct($status, $product);
        // 更新商品库存 (针对下单减库存的商品)
        $productlist[0]=$product;
        ProductFactory::getFactory(10)->updateProductStock($productlist);
    }
 
    /**
     * 新增订单记录
     */
    private function add($product)
    {
        // 订单数据
        $data = [
            'user_id' => $this->user['user_id'],
            'order_no' => $this->model->orderNo(),
            'trade_no' => $this->model->orderNo(),
            'total_price' => 0,
            'order_price' => 0,
            'coupon_id' => '',
            'coupon_money' => '',
            'coupon_id_sys'=>'',
            'coupon_money_sys'=>'',
            'points_money' => 0,
            'points_num' => 0,
            'pay_price' => 0,
            'delivery_type' => 30,
            'pay_type' => 10,
            'pay_source' => '',
            'buyer_remark' => '',
            'order_source' => 10,
            'points_bonus' => 0,
            'is_agent' => 0,//分销
            'shop_supplier_id' => $product['shop_supplier_id'],
            'supplier_money' => 0,
            'sys_money' => 0,
            'app_id' => $product['app_id'],
            'room_id' => 0,
            'virtual_auto' => $product['virtual_auto'],
            'is_verify' => isset($product['is_verify'])?$product['is_verify']:0, //是否支持核销 by yj
            'is_virtual' => $product['is_virtual'], //是否是服务商品 by yj
            'product_reduce_money' => 0,
        ];
 
        //随主订单配置
        $config = SettingModel::getItem('trade');
        $closeDays = $config['order']['close_days'];
        $closeDays != 0 && $data['pay_end_time'] = time() + ((int)$closeDays * 86400);
 
        // 保存订单记录
        $this->model->save($data);
        return $this->model['order_id'];
    }
 
    /**
     * 保存订单商品信息
     */
    private function saveOrderProduct($status, $product)
    {
        // 订单商品列表
        $item = [
            'order_id' => $status,
            'user_id' => $this->user['user_id'],
            'app_id' => $product["app_id"],
            'product_id' => $product['product_id'],
            'product_name' => $product['product_name'],
            'image_id' => $product['image'][0]['image_id'],
            'deduct_stock_type' => $product['deduct_stock_type'],
            'spec_type' => $product['spec_type'],
            'spec_sku_id' => $product['product_sku']['spec_sku_id'],
            'product_sku_id' => $product['product_sku']['product_sku_id'],
            'product_attr' => empty($product['product_sku']['product_attr']) ? '' : $product['product_sku']['product_attr'],
            'content' => $product['content'],
            'product_no' => $product['product_sku']['product_no'],
            'product_price' => $product['product_sku']['product_price'],
            'line_price' => $product['product_sku']['line_price'],
            'product_weight' => $product['product_sku']['product_weight'],
            'is_user_grade' => (int)$product['is_user_grade'],
            'grade_ratio' => 0,
            'grade_product_price' => isset($product['grade_product_price'])?$product['grade_product_price']:0,
            'grade_total_money' => 0,
            'coupon_money' => 0,
            'points_money' => 0,
            'points_num' => 0,
            'points_bonus' => 0,
            'total_num' => 1,
            'total_price' => $product['product_sku']['product_price'],
            'total_pay_price' => $product['product_sku']['product_price'],
            'supplier_money' => 0,
            'is_agent' => 0,
            'is_enable_team' => $product['is_enable_team'],
            'is_ind_agent' => $product['is_ind_agent'],
            'agent_money_type' => $product['agent_money_type'],
            'first_money' => $product['first_money'],
            'second_money' => $product['second_money'],
            'third_money' => $product['third_money'],
            'fullreduce_money' => isset($product['fullreduce_money'])?$product['fullreduce_money']:0,
            'virtual_content' => $product['virtual_content'],
            'is_alone_team' => $product['is_alone_team'],
            'alone_team_equity' => $product['alone_team_equity'],
            'product_reduce_money' => $product['product_reduce_money'],
            'is_verify' => $product['is_verify'],
            'verify_type' => $product['verify_type'],
            'verify_day' => $product['verify_day'],
        ];
        // 记录订单商品来源id
        $item['product_source_id'] = isset($product['product_source_id']) ? $product['product_source_id'] : 0;
        // 记录订单商品sku来源id
        $item['sku_source_id'] = isset($product['sku_source_id']) ? $product['sku_source_id'] : 0;
        // 记录拼团类的商品来源id
        $item['bill_source_id'] = isset($product['bill_source_id']) ? $product['bill_source_id'] : 0;
        // 自定义表单数据id,直接读取最新的那条记录 by lyzflash
        $item['table_record_id'] = 0;
 
        $model = new OrderProduct();
        return $model->save($item);
    }
 
}