<?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);
|
}
|
|
}
|