382 lines
16 KiB
PHP
382 lines
16 KiB
PHP
<?php
|
|
/**
|
|
* ThinkShop商城系统 - 团队十年电商经验汇集巨献!
|
|
* =========================================================
|
|
* Copy right 2019-2029 成都云之牛科技有限公司, 保留所有权利。
|
|
* ----------------------------------------------
|
|
* 官方网址: https://www.cdcloudshop.com
|
|
* =========================================================
|
|
*/
|
|
|
|
namespace addon\blindbox\model;
|
|
|
|
use addon\blindbox\model\Blindbox as BlindboxModel;
|
|
use addon\blindbox\model\BlindboxGoods as BlindboxGoodsModel;
|
|
use app\model\BaseModel;
|
|
use app\model\member\Member;
|
|
use app\model\member\MemberAccount;
|
|
use app\model\message\Message;
|
|
use app\model\order\Config;
|
|
use app\model\system\Cron;
|
|
use app\model\system\Pay;
|
|
use think\facade\Cache;
|
|
|
|
/**
|
|
* 订单
|
|
*/
|
|
class BlindboxOrder extends BaseModel
|
|
{
|
|
// 订单待付款
|
|
const ORDER_CREATE = 0;
|
|
//已完成
|
|
const ORDER_PAY = 1;
|
|
//订单已取消
|
|
const ORDER_CANCEL = 2;
|
|
private $goods_money = 0;//商品金额
|
|
private $balance_money = 0;//余额
|
|
private $delivery_money = 0;//配送费用
|
|
private $coupon_money = 0;//优惠券金额
|
|
private $adjust_money = 0;//调整金额
|
|
private $invoice_money = 0;//发票费用
|
|
private $promotion_money = 0;//优惠金额
|
|
private $order_money = 0;//订单金额
|
|
private $pay_money = 0;//支付总价
|
|
private $goods_num = 0; //商品种数
|
|
private $member_balance_money = 0;//会员账户余额(计算过程中会逐次减少)
|
|
private $pay_type = 'ONLINE_PAY';//支付方式
|
|
private $invoice_delivery_money = 0;
|
|
private $error = 0; //是否有错误
|
|
private $error_msg = ''; //错误描述
|
|
|
|
/**
|
|
* 订单创建
|
|
* @param unknown $data
|
|
*/
|
|
public function create($data)
|
|
{
|
|
//查询出会员相关信息
|
|
$calculate_data = $this->calculate($data);
|
|
if (isset($calculate_data[ 'code' ]) && $calculate_data[ 'code' ] < 0)
|
|
return $calculate_data;
|
|
|
|
if ($this->error > 0) {
|
|
return $this->error([ 'error_code' => $this->error ], $this->error_msg);
|
|
}
|
|
|
|
if (!empty($calculate_data[ 'invoice_type' ])) {
|
|
if ($calculate_data[ 'invoice_type' ] == 1 && $calculate_data[ 'invoice_full_address' ] == "") {
|
|
//物流,同城
|
|
if ($calculate_data[ 'shop_goods_list' ][ 'delivery' ][ 'delivery_type' ] == "express" || $calculate_data[ 'shop_goods_list' ][ 'delivery' ][ 'delivery_type' ] == "local") {
|
|
$calculate_data[ 'invoice_full_address' ] = $calculate_data[ 'member_address' ][ 'full_address' ] . $calculate_data[ 'member_address' ][ 'address' ];
|
|
$calculate_data[ 'shop_goods_list' ][ 'invoice_full_address' ] = $calculate_data[ 'member_address' ][ 'full_address' ] . $calculate_data[ 'member_address' ][ 'address' ];
|
|
}
|
|
//门店
|
|
if ($calculate_data[ 'shop_goods_list' ][ 'delivery' ][ 'delivery_type' ] == "store") {
|
|
$delivery_store_info = json_decode($calculate_data[ 'shop_goods_list' ][ 'delivery_store_info' ], true);
|
|
$calculate_data[ 'invoice_full_address' ] = $delivery_store_info[ 'full_address' ];
|
|
$calculate_data[ 'shop_goods_list' ][ 'invoice_full_address' ] = $delivery_store_info[ 'full_address' ];
|
|
}
|
|
}
|
|
}
|
|
|
|
$pay = new Pay();
|
|
$out_trade_no = $pay->createOutTradeNo($data[ 'member_id' ]);
|
|
model("blindbox_order")->startTrans();
|
|
|
|
$order_info = model("blindbox_order")->getInfo([ [ 'blindbox_id', '=', $data[ 'blindbox_id' ] ], [ 'blindbox_goods_id', '=', $data[ 'blindbox_goods_id' ] ], [ 'status', '=', 1 ] ]);
|
|
if ($order_info) {
|
|
return $this->error([ 'error_code' => $this->error ], "该盲盒已被抢");
|
|
}
|
|
|
|
//循环生成多个订单
|
|
try {
|
|
//订单主表
|
|
$order_no = $this->createOrderNo($data[ 'site_id' ], $data[ 'member_id' ]);
|
|
$data_order = [
|
|
'order_number' => $order_no,
|
|
'site_id' => $data[ 'site_id' ],
|
|
'out_trade_no' => $out_trade_no,
|
|
'member_id' => $data[ 'member_id' ],
|
|
'buyer_ip' => request()->ip(),
|
|
'price' => $data[ 'price' ],
|
|
'blindbox_id' => $data[ 'blindbox_id' ],
|
|
'blindbox_goods_id' => $data[ 'blindbox_goods_id' ],
|
|
'sku_id' => $calculate_data[ 'blindbox_goods_info' ][ 'sku_id' ],
|
|
'num' => $data[ 'goods_num' ],
|
|
'create_time' => time(),
|
|
'order_from' => $data[ 'order_from' ],
|
|
'order_from_name' => $data[ 'order_from_name' ],
|
|
'is_invoice' => $data[ 'is_invoice' ],
|
|
'invoice_type' => $data[ 'invoice_type' ],
|
|
'invoice_title' => $data[ 'invoice_title' ],
|
|
'taxpayer_number' => $data[ 'taxpayer_number' ],
|
|
'invoice_content' => $data[ 'invoice_content' ],
|
|
'invoice_full_address' => $data[ 'invoice_full_address' ],
|
|
'is_tax_invoice' => $data[ 'is_tax_invoice' ],
|
|
'invoice_email' => $data[ 'invoice_email' ],
|
|
'invoice_title_type' => $data[ 'invoice_title_type' ],
|
|
];
|
|
$order_id = model("blindbox_order")->add($data_order);
|
|
|
|
$config_model = new Config();
|
|
$balance_config = $config_model->getBalanceConfig($data[ 'site_id' ]);
|
|
//扣除余额(统一扣除)
|
|
if ($calculate_data[ "balance_money" ] > 0 && $balance_config[ 'data' ][ 'value' ][ 'balance_show' ] == 1) {
|
|
$this->pay_type = "BALANCE";
|
|
$calculate_data[ 'order_id' ] = $order_id;
|
|
$balance_result = $this->useBalance($calculate_data, $data[ 'site_id' ]);
|
|
if ($balance_result[ "code" ] < 0) {
|
|
model("blindbox_order")->rollback();
|
|
return $balance_result;
|
|
}
|
|
} else {
|
|
$this->pay_money = $data[ 'price' ];
|
|
}
|
|
//生成支付单据
|
|
$pay_body = '购买盲盒';
|
|
$pay->addPay($data[ 'site_id' ], $out_trade_no, $this->pay_type, $pay_body, $pay_body, $this->pay_money, '', 'BlindboxGoodsOrderPayNotify', '');
|
|
$this->addOrderCronClose($order_id, $data[ 'site_id' ]);//增加关闭订单自动事件
|
|
//订单生成的消息
|
|
$message_model = new Message();
|
|
$message_model->sendMessage([ 'keywords' => "ORDER_CREATE", 'order_id' => $order_id, 'site_id' => $data[ 'site_id' ] ]);
|
|
|
|
model("blindbox_order")->commit();
|
|
return $this->success($out_trade_no);
|
|
} catch (\Exception $e) {
|
|
model("blindbox_order")->rollback();
|
|
return $this->error('', $e->getMessage() . $e->getFile() . $e->getLine());
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* 订单计算
|
|
* @param unknown $data
|
|
*/
|
|
public function calculate($data)
|
|
{
|
|
$blindbox_model = new BlindboxModel();
|
|
$blindbox_goods_model = new BlindboxGoodsModel();
|
|
$data = $this->initMemberAccount($data);//初始化会员账户
|
|
$blindbox_data = $blindbox_model->getBlindboxInfo([ [ 'site_id', '=', $data[ 'site_id' ] ], [ 'blindbox_id', '=', $data[ 'blindbox_id' ] ], [ 'blindbox_status', '=', 1 ] ])[ 'data' ] ?? [];
|
|
if (empty($blindbox_data)) {
|
|
return $this->error([], "未查询到盲盒活动");
|
|
}
|
|
$blindbox_goods_data = $blindbox_goods_model->getBlindboxGoodsInfo([ [ 'site_id', '=', $data[ 'site_id' ] ], [ 'id', '=', $data[ 'blindbox_goods_id' ] ] ])[ 'data' ] ?? [];
|
|
if (!empty($blindbox_goods_data)) {
|
|
if ($blindbox_goods_data[ 'status' ] == 1) {
|
|
return $this->error([], "该盲盒已被别的用户开启");
|
|
}
|
|
}
|
|
$data[ "blindbox_info" ] = $blindbox_data;
|
|
$data[ "blindbox_goods_info" ] = $blindbox_goods_data;
|
|
//余额付款
|
|
if ($data[ 'is_balance' ] > 0 && $blindbox_data[ 'is_balance' ] == 1) {
|
|
$this->member_balance_money = $data[ "member_account" ][ "balance_total" ] ?? 0;
|
|
$balance_money = $data[ 'member_account' ][ 'balance' ] - $data[ 'price' ];
|
|
if ($balance_money > 0) {
|
|
$this->balance_money = $data[ 'price' ];
|
|
} else {
|
|
$this->balance_money = $data[ 'member_account' ][ 'balance' ];
|
|
}
|
|
}
|
|
|
|
//总结计算
|
|
$data[ 'delivery_money' ] = $this->delivery_money;
|
|
$data[ 'coupon_money' ] = $this->coupon_money;
|
|
$data[ 'adjust_money' ] = $this->adjust_money;
|
|
$data[ 'invoice_money' ] = $this->invoice_money;
|
|
$data[ 'invoice_delivery_money' ] = $this->invoice_delivery_money;
|
|
$data[ 'promotion_money' ] = $this->promotion_money;
|
|
$data[ 'order_money' ] = $this->order_money;
|
|
$data[ 'balance_money' ] = $this->balance_money;
|
|
$data[ 'pay_money' ] = $this->pay_money;
|
|
$data[ 'goods_money' ] = $this->goods_money;
|
|
$data[ 'goods_num' ] = $this->goods_num;
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* 初始化会员账户
|
|
* @param $data
|
|
* @return mixed
|
|
*/
|
|
public function initMemberAccount($data)
|
|
{
|
|
$member_model = new Member();
|
|
$member_info_result = $member_model->getMemberDetail($data[ "member_id" ], $data[ "site_id" ]);
|
|
$member_info = $member_info_result[ "data" ];
|
|
|
|
if (!empty($member_info)) {
|
|
if (!empty($member_info[ "pay_password" ])) {
|
|
$is_pay_password = 1;
|
|
} else {
|
|
$is_pay_password = 0;
|
|
}
|
|
unset($member_info[ "pay_password" ]);
|
|
$member_info[ "is_pay_password" ] = $is_pay_password;
|
|
$data[ 'member_account' ] = $member_info;
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* 生成订单编号
|
|
*
|
|
* @param array $site_id
|
|
*/
|
|
public function createOrderNo($site_id, $member_id = 0)
|
|
{
|
|
$time_str = date('YmdHis');
|
|
$max_no = Cache::handler()->incr($site_id . "_" . $member_id . "_" . $time_str,1);
|
|
Cache::handler()->expire($site_id . "_" . $member_id . "_" . $time_str,120);
|
|
$order_no = $time_str . $member_id . sprintf("%03d", $max_no);
|
|
return $order_no;
|
|
}
|
|
|
|
/**
|
|
* 使用余额
|
|
* @param $data
|
|
* @param $site_id
|
|
* @param string $from_type
|
|
* @return array
|
|
*/
|
|
public function useBalance($data, $site_id, $from_type = 'order')
|
|
{
|
|
$this->pay_type = "BALANCE";
|
|
$member_model = new Member();
|
|
$balance_money = $data[ "member_account" ][ "balance_money" ]; //现金余额
|
|
$balance = $data[ "member_account" ][ "balance" ]; //储值余额
|
|
$member_account_model = new MemberAccount();
|
|
$surplus_banance = $data[ "balance_money" ];
|
|
//优先扣除储值余额
|
|
if ($balance > 0) {
|
|
if ($balance >= $surplus_banance) {
|
|
$real_balance = $surplus_banance;
|
|
} else {
|
|
$real_balance = $balance;
|
|
}
|
|
$result = $member_account_model->addMemberAccount($site_id, $data[ "member_id" ], "balance", -$real_balance, $from_type, $data[ 'order_id' ], "订单消费扣除");
|
|
$surplus_banance -= $real_balance;
|
|
}
|
|
|
|
if ($surplus_banance > 0) {
|
|
$result = $member_account_model->addMemberAccount($site_id, $data[ "member_id" ], "balance_money", -$surplus_banance, $from_type, $data[ 'order_id' ], "订单消费扣除");
|
|
}
|
|
$this->pay_money = $data[ "balance_money" ] - ( $real_balance + $surplus_banance ); //计算出实际支付金额
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* 增加订单自动关闭事件
|
|
* @param $order_id
|
|
*/
|
|
public function addOrderCronClose($order_id, $site_id)
|
|
{
|
|
//计算订单自动关闭时间
|
|
$config_model = new Config();
|
|
$order_config_result = $config_model->getOrderEventTimeConfig($site_id);
|
|
$order_config = $order_config_result[ "data" ];
|
|
$now_time = time();
|
|
if ($order_config[ "value" ][ "auto_close" ] > 0) {
|
|
$execute_time = $now_time + $order_config[ "value" ][ "auto_close" ] * 60; //自动关闭时间
|
|
$cron_model = new Cron();
|
|
$cron_model->addCron(1, 0, "订单自动关闭", "BlindboxGoodsOrderClose", $execute_time, $order_id);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 盲盒订单详情
|
|
* @param array $condition
|
|
* @param string $field
|
|
* @param string $alias
|
|
* @param array $join
|
|
* @return array
|
|
*/
|
|
public function getOrderInfo($condition = [], $field = '*', $alias = 'a', $join = [])
|
|
{
|
|
$res = model('blindbox_order')->getInfo($condition, $field, $alias, $join);
|
|
|
|
return $this->success($res);
|
|
}
|
|
|
|
/**
|
|
* 订单线上支付回调
|
|
* @param $data
|
|
* @return array
|
|
*/
|
|
public function orderOnlinePay($data)
|
|
{
|
|
$out_trade_no = $data[ "out_trade_no" ];
|
|
$order_info = model("blindbox_order")->getInfo([ [ 'out_trade_no', '=', $out_trade_no ] ]);
|
|
if ($order_info[ 'status' ] != self::ORDER_CREATE) {
|
|
return $this->success();
|
|
}
|
|
|
|
$order_model = new \app\model\order\Order();
|
|
$pay_type_list = $order_model->getPayType();
|
|
model('blindbox_order')->startTrans();
|
|
try {
|
|
//修改订单状态
|
|
$order_data = [
|
|
'status' => self::ORDER_PAY,
|
|
'pay_time' => time(),
|
|
'pay_type' => $data[ 'pay_type' ],
|
|
'pay_type_name' => $pay_type_list[ $data[ 'pay_type' ] ],
|
|
];
|
|
|
|
model("blindbox_order")->update($order_data, [ [ 'order_id', '=', $order_info[ 'order_id' ] ] ]);
|
|
$cron_model = new Cron();
|
|
$cron_model->deleteCron([ [ 'event', '=', 'BlindboxGoodsOrderClose' ], [ 'relate_id', '=', $order_info[ 'order_id' ] ] ]);
|
|
|
|
//支付成功减库存
|
|
model('blindbox')->setInc([ [ 'blindbox_id', '=', $order_info[ 'blindbox_id' ] ] ], 'blindbox_num');
|
|
model('blindbox')->setDec([ [ 'blindbox_id', '=', $order_info[ 'blindbox_id' ] ] ], 'blindbox_inventory');
|
|
model('blindbox_goods')->update([ 'member_id' => $order_info[ 'member_id' ], 'status' => 1 ], [ [ 'id', '=', $order_info[ 'blindbox_goods_id' ] ] ]);
|
|
$group_data = [
|
|
'blindbox_id' => $order_info[ 'blindbox_id' ],
|
|
'blindbox_goods_id' => $order_info[ 'blindbox_goods_id' ],
|
|
'member_id' => $order_info[ 'member_id' ],
|
|
'site_id' => $order_info[ 'site_id' ],
|
|
'sku_id' => $order_info[ 'sku_id' ],
|
|
'create_time' => time(),
|
|
'order_id' => $order_info[ 'order_id' ],
|
|
];
|
|
|
|
event('GiftCardOrderPay', $order_info);
|
|
|
|
model('blindbox_member_group')->add($group_data);
|
|
|
|
model('blindbox_order')->commit();
|
|
return $this->success();
|
|
} catch (\Exception $e) {
|
|
model('blindbox_order')->rollback();
|
|
return $this->error('', $e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 关闭盲盒订单
|
|
* @param $order_id
|
|
* @return array|\multitype
|
|
*/
|
|
public function cronCloseOrder($order_id)
|
|
{
|
|
$order_info = model('blindbox_order')->getInfo([ [ 'order_id', '=', $order_id ] ], 'status,card_id,num');
|
|
if (!empty($order_info)) {
|
|
|
|
if ($order_info[ 'status' ] == 0) {
|
|
$res = model('blindbox_order')->update([ 'status' => 2 ], [ [ 'order_id', '=', $order_id ] ]);
|
|
|
|
return $this->success($res);
|
|
} else {
|
|
return $this->error("", "该订单已支付或关闭");
|
|
}
|
|
} else {
|
|
return $this->error("", "该订单不存在");
|
|
}
|
|
}
|
|
|
|
|
|
} |