初始上传
This commit is contained in:
177
addon/giftcard/model/order/GiftCardOrder.php
Executable file
177
addon/giftcard/model/order/GiftCardOrder.php
Executable file
@@ -0,0 +1,177 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace addon\giftcard\model\order;
|
||||
|
||||
use app\model\BaseModel;
|
||||
|
||||
class GiftCardOrder extends BaseModel
|
||||
{
|
||||
|
||||
public $order_status_list = [
|
||||
'topay' => '待支付',
|
||||
'complete' => '已完成',
|
||||
'close' => '已关闭',
|
||||
];
|
||||
|
||||
public $pay_type = [
|
||||
'ONLINE_PAY' => '在线支付',
|
||||
'BALANCE' => '余额支付',
|
||||
'offlinepay' => '线下支付'
|
||||
];
|
||||
|
||||
|
||||
/**
|
||||
* 获取支付方式
|
||||
*/
|
||||
public function getPayType()
|
||||
{
|
||||
//获取订单基础的其他支付方式
|
||||
$pay_type = $this->pay_type;
|
||||
//获取当前所有在线支付方式
|
||||
$onlinepay = event('PayType', []);
|
||||
if (!empty($onlinepay)) {
|
||||
foreach ($onlinepay as $k => $v) {
|
||||
$pay_type[ $v[ 'pay_type' ] ] = $v[ 'pay_type_name' ];
|
||||
}
|
||||
}
|
||||
return $pay_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取礼品卡订单信息
|
||||
* @param $condition
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getOrderInfo($condition, $field = '*', $alias = '', $join = [])
|
||||
{
|
||||
$info = model('giftcard_order')->getInfo($condition, $field, $alias, $join);
|
||||
return $this->success($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取礼品卡订单
|
||||
* @param $condition
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getOrderSum($condition, $field = '*', $alias = '', $join = [])
|
||||
{
|
||||
$info = model('giftcard_order')->getSum($condition, $field, $alias, $join);
|
||||
return $this->success($info);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取礼品卡订单列表
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param null $limit
|
||||
* @return array
|
||||
*/
|
||||
public function getOrderList($condition = [], $field = '*', $order = '', $limit = null)
|
||||
{
|
||||
$list = model('giftcard_order')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取礼品卡订单分页列表
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getOrderPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*', $alias = '', $join = [])
|
||||
{
|
||||
$list = model('giftcard_order')->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单详情分页列表
|
||||
* @param $params
|
||||
* @return array
|
||||
*/
|
||||
public function getOrderDetailPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*', $alias = '', $join = [])
|
||||
{
|
||||
$list = $this->getOrderPageList($condition, $page, $page_size, $order, $field, $alias, $join)[ 'data' ] ?? [];
|
||||
if (!empty($list[ 'list' ])) {
|
||||
$order_id_array = [];
|
||||
foreach ($list['list'] as $k => $v){
|
||||
$order_id_array[] = $v['order_id'];
|
||||
}
|
||||
$order_ids = implode(',', $order_id_array);
|
||||
$order_goods_list = model('giftcard_order_goods')->getList( [['order_id', 'in', $order_ids]], '*');
|
||||
foreach ($list[ 'list' ] as $k => $v) {
|
||||
|
||||
foreach ($order_goods_list as $ck => $cv) {
|
||||
$order_goods_list[ $ck ][ 'num' ] = numberFormat($order_goods_list[ $ck ][ 'num' ]);
|
||||
if($v['order_id'] == $cv['order_id'])
|
||||
{
|
||||
$list[ 'list' ][ $k ][ 'order_goods_list' ][] = $cv;
|
||||
}
|
||||
}
|
||||
$list[ 'list' ][ $k ] = $this->tran($v);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单详情
|
||||
* @param $params
|
||||
* @return array
|
||||
*/
|
||||
public function getOrderDetail($params)
|
||||
{
|
||||
$order_id = $params[ 'order_id' ];
|
||||
$site_id = $params[ 'site_id' ];
|
||||
$member_id = $params[ 'member_id' ] ?? 0;
|
||||
$condition = array (
|
||||
[ 'go.order_id', '=', $order_id ]
|
||||
);
|
||||
if ($site_id > 0) $condition[] = [ 'go.site_id', '=', $site_id ];
|
||||
if ($member_id > 0) $condition[] = [ 'go.member_id', '=', $member_id ];
|
||||
|
||||
$order_info = $this->getOrderInfo($condition, 'go.*,m.nickname,m.headimg,m.mobile', 'go', [
|
||||
[ 'member m', 'go.member_id=m.member_id', 'left' ]
|
||||
])[ 'data' ] ?? [];
|
||||
if (empty($order_info))
|
||||
return $this->error();
|
||||
|
||||
$order_goods_model = new GiftCardOrderGoods();
|
||||
$condition = array (
|
||||
[ 'order_id', '=', $order_id ]
|
||||
);
|
||||
if ($site_id > 0) $condition[] = [ 'site_id', '=', $site_id ];
|
||||
if ($member_id > 0) $condition[] = [ 'member_id', '=', $member_id ];
|
||||
$order_goods_list = $order_goods_model->getOrderGoodsList($condition)[ 'data' ] ?? [];
|
||||
if (empty($order_goods_list))
|
||||
return $this->error();
|
||||
|
||||
$order_info[ 'order_goods_list' ] = $order_goods_list;
|
||||
$order_info = $this->tran($order_info);
|
||||
return $this->success($order_info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据字段转化翻译
|
||||
* @param $data
|
||||
* @return mixed
|
||||
*/
|
||||
public function tran($data)
|
||||
{
|
||||
$order_status = $data[ 'order_status' ] ?? '';
|
||||
if (!empty($order_status)) {
|
||||
$data[ 'order_status_name' ] = $this->order_status_list[ $order_status ];
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
}
|
||||
281
addon/giftcard/model/order/GiftCardOrderCreate.php
Executable file
281
addon/giftcard/model/order/GiftCardOrderCreate.php
Executable file
@@ -0,0 +1,281 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace addon\giftcard\model\order;
|
||||
|
||||
use addon\giftcard\model\giftcard\Media;
|
||||
use app\model\BaseModel;
|
||||
use addon\giftcard\model\giftcard\GiftCard as GiftCardModel;
|
||||
use app\model\order\Config;
|
||||
use app\model\system\Cron;
|
||||
use app\model\system\Pay;
|
||||
use app\model\system\Site;
|
||||
use think\facade\Cache;
|
||||
|
||||
/**
|
||||
* 礼品卡订单创建
|
||||
* Class GiftCardOrderCreate
|
||||
* @package addon\giftcard\model\order
|
||||
*/
|
||||
class GiftCardOrderCreate extends BaseModel
|
||||
{
|
||||
|
||||
public function create($params)
|
||||
{
|
||||
if(isset($params['num']) && $params['num'] === '') $params['num'] = 0;
|
||||
$calculate_result = $this->calculate($params);
|
||||
if ($calculate_result[ 'code' ] < 0)
|
||||
return $calculate_result;
|
||||
|
||||
$calculate_data = $calculate_result[ 'data' ];
|
||||
|
||||
$member_id = $calculate_data[ 'member_id' ];
|
||||
$site_id = $calculate_data[ 'site_id' ];
|
||||
$pay = new Pay();
|
||||
$out_trade_no = $pay->createOutTradeNo($member_id);
|
||||
$order_no = $this->createOrderNo($site_id, $member_id);
|
||||
|
||||
$site_info = $calculate_data[ 'site_info' ];
|
||||
$site_name = $site_info[ 'site_name' ];
|
||||
$card_right_type = $calculate_data[ 'card_right_type' ];
|
||||
$giftcard_info = $calculate_data[ 'giftcard_info' ];
|
||||
|
||||
$validity_type = $giftcard_info[ 'validity_type' ];
|
||||
$validity_time = $giftcard_info[ 'validity_time' ];
|
||||
$validity_day = $giftcard_info[ 'validity_day' ];
|
||||
$common_data = array (
|
||||
'order_no' => $order_no,
|
||||
'site_id' => $site_id,
|
||||
'site_name' => $site_name,
|
||||
'member_id' => $member_id,
|
||||
);
|
||||
$pay_money = $calculate_data[ 'pay_money' ];
|
||||
$order_name = $calculate_data[ 'order_name' ];
|
||||
$order_data = [
|
||||
'out_trade_no' => $out_trade_no,
|
||||
'order_from' => $calculate_data[ 'order_from' ],
|
||||
'order_from_name' => $calculate_data[ 'order_from_name' ],
|
||||
'order_status' => 'topay',
|
||||
'buyer_ip' => request()->ip(),
|
||||
'goods_money' => $calculate_data[ 'goods_money' ],
|
||||
'order_money' => $calculate_data[ 'order_money' ],
|
||||
'pay_money' => $calculate_data[ 'pay_money' ],
|
||||
'create_time' => time(),
|
||||
'order_name' => $order_name,
|
||||
// 'buyer_message' => $calculate_data['buyer_message'],
|
||||
'giftcard_id' => $calculate_data[ 'giftcard_id' ],
|
||||
'card_right_type' => $card_right_type,
|
||||
'card_cover' => $calculate_data[ 'card_cover' ],
|
||||
'is_allow_transfer' => $calculate_data[ 'is_allow_transfer' ],
|
||||
'media_id' => $calculate_data[ 'media_id' ],
|
||||
'card_price' => $calculate_data[ 'item_money' ],
|
||||
'validity_type' => $validity_type,
|
||||
'validity_time' => $validity_time,
|
||||
'validity_day' => $validity_day,
|
||||
'num' => $calculate_data[ 'num' ],
|
||||
'card_right_goods_type' => $giftcard_info[ 'card_right_goods_type' ],
|
||||
'card_right_goods_count' => $giftcard_info[ 'card_right_goods_count' ],
|
||||
];
|
||||
model('giftcard_order')->startTrans();
|
||||
//循环生成多个订单
|
||||
try {
|
||||
$order_id = model('giftcard_order')->add(array_merge($common_data, $order_data));
|
||||
$order_goods_list = $calculate_data[ 'order_goods_list' ];
|
||||
$order_goods_data_list = [];
|
||||
foreach ($order_goods_list as $k => $v) {
|
||||
$order_goods_data_list[] = array_merge($common_data, [
|
||||
'order_id' => $order_id,
|
||||
'sku_id' => $v[ 'sku_id' ],
|
||||
'sku_name' => $v[ 'sku_name' ],
|
||||
'sku_image' => $v[ 'sku_image' ],
|
||||
'sku_no' => $v[ 'sku_no' ] ?? '',
|
||||
'goods_id' => $v[ 'goods_id' ] ?? 0,
|
||||
'goods_name' => $v[ 'goods_name' ] ?? '',
|
||||
'goods_class' => $v[ 'goods_class' ] ?? '',
|
||||
'goods_class_name' => $v[ 'goods_class_name' ] ?? '',
|
||||
'price' => $v[ 'price' ],
|
||||
'num' => $v[ 'num' ],
|
||||
'card_right_type' => $card_right_type,
|
||||
'goods_money' => $v[ 'goods_money' ],
|
||||
'balance' => $v[ 'balance' ] ?? 0,
|
||||
'total_balance' => $v[ 'total_balance' ] ?? 0,
|
||||
]);
|
||||
}
|
||||
model('giftcard_order_goods')->addList($order_goods_data_list);
|
||||
//生成整体付费支付单据
|
||||
$pay_model = new Pay();
|
||||
$res = $pay_model->addPay($site_id, $out_trade_no, 'ONLINE_PAY', $order_name, $order_name, $pay_money, '', 'GiftCardOrderPayNotify', '/pages_promotion/giftcard/order_detail?order_id=' . $order_id, $order_id, $member_id);
|
||||
if($res['code'] < 0){
|
||||
model('giftcard_order')->rollback();
|
||||
return $res;
|
||||
}
|
||||
|
||||
$config_model = new Config();
|
||||
$order_config = $config_model->getOrderEventTimeConfig($site_id)[ 'data' ];
|
||||
if ($order_config[ 'value' ][ 'auto_close' ] > 0) {
|
||||
$now_time = time();
|
||||
$execute_time = $now_time + $order_config[ 'value' ][ 'auto_close' ] * 60; //自动关闭时间
|
||||
$cron_model = new Cron();
|
||||
$cron_model->addCron(1, 0, '订单自动关闭', 'GiftCardOrderClose', $execute_time, $order_id);
|
||||
}
|
||||
|
||||
model('giftcard_order')->commit();
|
||||
return $this->success($out_trade_no);
|
||||
} catch (\Exception $e) {
|
||||
model('giftcard_order')->rollback();
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function calculate($params)
|
||||
{
|
||||
if(isset($params['num']) && $params['num'] === '') $params['num'] = 0;
|
||||
$giftcard_id = $params[ 'giftcard_id' ] ?? 0;
|
||||
$site_id = $params[ 'site_id' ];
|
||||
|
||||
$site_result = $this->initSite($params);
|
||||
if ($site_result[ 'code' ] < 0)
|
||||
return $site_result;
|
||||
|
||||
$params = $site_result[ 'data' ];
|
||||
$num = $params[ 'num' ] ?? 1;
|
||||
$item_result = $this->itemCalculate($params);
|
||||
if ($item_result[ 'code' ] < 0)
|
||||
return $item_result;
|
||||
|
||||
$params = $item_result[ 'data' ];
|
||||
$goods_money = $params[ 'goods_money' ];
|
||||
$total_money = $params[ 'giftcard_info' ][ 'card_price' ] * $num;
|
||||
$order_money = $total_money;
|
||||
$pay_money = $order_money;
|
||||
$params[ 'goods_money' ] = $total_money;
|
||||
$params[ 'order_money' ] = $order_money;
|
||||
$params[ 'pay_money' ] = $pay_money;
|
||||
$params[ 'item_money' ] = $params[ 'giftcard_info' ][ 'card_price' ];
|
||||
|
||||
return $this->success($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* 单项计算
|
||||
* @param $params
|
||||
* @return array
|
||||
*/
|
||||
public function itemCalculate($params)
|
||||
{
|
||||
$giftcard_id = $params[ 'giftcard_id' ];
|
||||
$media_id = $params[ 'media_id' ];
|
||||
$site_id = $params[ 'site_id' ];
|
||||
$giftcard_model = new GiftCardModel();
|
||||
$info = $giftcard_model->getGiftcardDetail([ 'site_id' => $site_id, 'giftcard_id' => $giftcard_id ])[ 'data' ] ?? [];
|
||||
if (empty($info))
|
||||
return $this->error([], '当前礼品卡活动不存在');
|
||||
|
||||
if ($info[ 'status' ] != 1)
|
||||
return $this->error([], '当前礼品卡活动已下架');
|
||||
|
||||
$params[ 'giftcard_info' ] = $info;
|
||||
$media_model = new Media();
|
||||
$media_condition = array (
|
||||
[ 'media_id', '=', $media_id ],
|
||||
);
|
||||
$media_info = $media_model->getInfo($media_condition)[ 'data' ] ?? [];
|
||||
if (empty($media_info))
|
||||
return $this->error([], '封面图不存在');
|
||||
|
||||
$card_cover = $media_info[ 'media_path' ];
|
||||
$params[ 'card_cover' ] = $card_cover;
|
||||
$card_name = $info[ 'card_name' ];
|
||||
$params[ 'order_name' ] = $card_name;
|
||||
$goods_money = 0;
|
||||
$card_right_type = $info[ 'card_right_type' ];//卡券类型
|
||||
$params[ 'card_right_type' ] = $card_right_type;
|
||||
$params[ 'is_allow_transfer' ] = $info[ 'is_allow_transfer' ];
|
||||
$goods_item_list = [];
|
||||
switch ( $card_right_type ) {
|
||||
case 'goods'://商品
|
||||
$goods_list = $info[ 'goods_list' ];
|
||||
foreach ($goods_list as $v) {
|
||||
$item_sku_id = $v[ 'sku_id' ];
|
||||
$item_num = $v[ 'goods_num' ];
|
||||
$goods_id = $v[ 'goods_id' ];
|
||||
$item_goods_price = $v[ 'goods_price' ];
|
||||
$item_goods_money = $item_goods_price * $item_num;
|
||||
$goods_item_list[] = [
|
||||
'sku_id' => $item_sku_id,
|
||||
'goods_id' => $goods_id,
|
||||
'price' => $item_goods_price,
|
||||
'goods_money' => $item_goods_money,
|
||||
'num' => $item_num,
|
||||
'sku_name' => $v[ 'sku_info' ][ 'sku_name' ],
|
||||
'sku_image' => $v[ 'sku_info' ][ 'sku_image' ],
|
||||
'sku_no' => $v[ 'sku_info' ][ 'sku_no' ],
|
||||
'goods_class' => $v[ 'sku_info' ][ 'goods_class' ],
|
||||
'goods_class_name' => $v[ 'sku_info' ][ 'goods_class_name' ],
|
||||
'goods_name' => $v[ 'sku_info' ][ 'goods_name' ],
|
||||
];
|
||||
$goods_money = $info[ 'card_price' ];
|
||||
}
|
||||
break;
|
||||
case 'balance'://储值
|
||||
$num = 1;
|
||||
$item_goods_price = $info[ 'card_price' ];
|
||||
$item_goods_money = $item_goods_price * $num;
|
||||
$item_sku_name = $info[ 'balance' ] . '元储值卡';
|
||||
$item_sku_image = $card_cover;
|
||||
$balance = $info[ 'balance' ];
|
||||
$total_balance = $balance * $num;
|
||||
$goods_item_list[] = [
|
||||
'sku_id' => $giftcard_id,
|
||||
'price' => $item_goods_price,
|
||||
'goods_money' => $item_goods_money,
|
||||
'num' => $num,
|
||||
'sku_name' => $item_sku_name,
|
||||
'sku_image' => $item_sku_image,
|
||||
'goods_name' => $card_name,
|
||||
'balance' => $balance,
|
||||
'total_balance' => $total_balance
|
||||
];
|
||||
$goods_money += $item_goods_money;
|
||||
break;
|
||||
}
|
||||
$params[ 'order_goods_list' ] = $goods_item_list;
|
||||
$params[ 'goods_money' ] = $goods_money;
|
||||
return $this->success($params);
|
||||
}
|
||||
|
||||
|
||||
public function initSite($params)
|
||||
{
|
||||
$site_id = $params[ 'site_id' ];
|
||||
$site_model = new Site();
|
||||
$condition = array (
|
||||
[ 'site_id', '=', $site_id ]
|
||||
);
|
||||
$site_info = $site_model->getSiteInfo($condition)[ 'data' ] ?? [];
|
||||
if (empty($site_info))
|
||||
return $this->error();
|
||||
$params[ 'site_info' ] = $site_info;
|
||||
return $this->success($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成订单编号
|
||||
* @param $site_id
|
||||
* @param int $member_id
|
||||
* @return string
|
||||
*/
|
||||
public function createOrderNo($site_id, $member_id = 0)
|
||||
{
|
||||
$time_str = date('YmdHi');
|
||||
$max_no = Cache::get($site_id . '_' . $member_id . '_' . $time_str);
|
||||
if (empty($max_no)) {
|
||||
$max_no = 1;
|
||||
} else {
|
||||
$max_no = $max_no + 1;
|
||||
}
|
||||
$order_no = $time_str . sprintf('%05d', $member_id) . sprintf('%03d', $max_no);
|
||||
Cache::set($site_id . '_' . $member_id . '_' . $time_str, $max_no);
|
||||
return $order_no;
|
||||
}
|
||||
}
|
||||
54
addon/giftcard/model/order/GiftCardOrderGoods.php
Executable file
54
addon/giftcard/model/order/GiftCardOrderGoods.php
Executable file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace addon\giftcard\model\order;
|
||||
|
||||
use app\model\BaseModel;
|
||||
|
||||
|
||||
class GiftCardOrderGoods extends BaseModel
|
||||
{
|
||||
|
||||
/**
|
||||
* 获取礼品卡订单项信息
|
||||
* @param $condition
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getOrderGoodsInfo($condition, $field = '*')
|
||||
{
|
||||
$info = model('giftcard_order_goods')->getInfo($condition, $field);
|
||||
return $this->success($info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取礼品卡订单项列表
|
||||
* @param array $condition
|
||||
* @param string $field
|
||||
* @param string $order
|
||||
* @param null $limit
|
||||
* @return array
|
||||
*/
|
||||
public function getOrderGoodsList($condition = [], $field = '*', $order = '', $limit = null)
|
||||
{
|
||||
$list = model('giftcard_order_goods')->getList($condition, $field, $order, '', '', '', $limit);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取礼品卡订单项分页列表
|
||||
* @param array $condition
|
||||
* @param int $page
|
||||
* @param int $page_size
|
||||
* @param string $order
|
||||
* @param string $field
|
||||
* @return array
|
||||
*/
|
||||
public function getOrderGoodsPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
|
||||
{
|
||||
$list = model('giftcard_order_goods')->pageList($condition, $field, $order, $page, $page_size);
|
||||
return $this->success($list);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
161
addon/giftcard/model/order/GiftCardOrderOperation.php
Executable file
161
addon/giftcard/model/order/GiftCardOrderOperation.php
Executable file
@@ -0,0 +1,161 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace addon\giftcard\model\order;
|
||||
|
||||
use addon\giftcard\model\card\VirtualCard;
|
||||
use addon\giftcard\model\giftcard\CardStat;
|
||||
use app\dict\member_account\AccountDict;
|
||||
use app\model\BaseModel;
|
||||
use app\model\member\MemberAccount;
|
||||
use app\model\system\Pay;
|
||||
use app\model\system\Stat;
|
||||
|
||||
class GiftCardOrderOperation extends BaseModel
|
||||
{
|
||||
|
||||
/**
|
||||
* 只涉及订单业务交互
|
||||
* @param $params
|
||||
* @return array
|
||||
*/
|
||||
public function orderPay($params)
|
||||
{
|
||||
$out_trade_no = $params[ 'out_trade_no' ];
|
||||
$order_model = new GiftCardOrder();
|
||||
$order_condition = array (
|
||||
[ 'out_trade_no', '=', $out_trade_no ]
|
||||
);
|
||||
$order_info = $order_model->getOrderInfo($order_condition)[ 'data' ] ?? [];
|
||||
if (empty($order_info))
|
||||
return $this->error();
|
||||
|
||||
$giftcard_id = $order_info[ 'giftcard_id' ];
|
||||
$num = $order_info[ 'num' ];//礼品卡套数
|
||||
$order_goods_model = new GiftCardOrderGoods();
|
||||
$order_goods_list = $order_goods_model->getOrderGoodsList([ [ 'order_id', '=', $order_info[ 'order_id' ] ] ])[ 'data' ] ?? [];
|
||||
$total_balance = 0;
|
||||
foreach ($order_goods_list as $k => $v) {
|
||||
$total_balance += $v[ 'total_balance' ];
|
||||
}
|
||||
|
||||
//订单支付后生成礼品卡
|
||||
$virtual_card_model = new VirtualCard();
|
||||
$order_info[ 'source' ] = 'order';
|
||||
$order_info[ 'goods_list' ] = $order_goods_list;
|
||||
$order_info[ 'balance' ] = $total_balance;
|
||||
$temp_num = 0;
|
||||
model('giftcard_order')->startTrans();
|
||||
try {
|
||||
while ($temp_num < $num) {
|
||||
$virtual_card_model->addCard($order_info);
|
||||
$temp_num++;
|
||||
}
|
||||
$pay_type = $params[ 'pay_type' ];
|
||||
//订单相关操作(业务复杂后会拆开步骤)
|
||||
$pay_type_list = $order_model->getPayType();
|
||||
$data = array (
|
||||
'order_status' => 'complete',
|
||||
'pay_status' => 1,
|
||||
'pay_time' => time(),
|
||||
'pay_type' => $pay_type,
|
||||
'pay_type_name' => $pay_type_list[ $pay_type ]
|
||||
);
|
||||
model('giftcard_order')->update($data, $order_condition);
|
||||
//可能是消费奖励
|
||||
event('GiftCardOrderPay', $order_info);
|
||||
|
||||
//业务和支付的融合
|
||||
$pay_money = new Pay();
|
||||
$pay_info = $pay_money->getPayInfo($out_trade_no)[ 'data' ] ?? [];
|
||||
if (!empty($pay_info)) {
|
||||
$balance = $pay_info[ 'balance' ];
|
||||
$balance_money = $pay_info[ 'balance_money' ];
|
||||
$member_account_model = new MemberAccount();
|
||||
if ($balance > 0) {
|
||||
$use_res = $member_account_model->addMemberAccount($order_info[ 'site_id' ], $order_info[ 'member_id' ], AccountDict::balance, -$balance, 'order', $order_info[ 'order_id' ], '订单消费扣除');
|
||||
if ($use_res[ 'code' ] != 0) {
|
||||
model('giftcard_order')->rollback();
|
||||
return $use_res;
|
||||
}
|
||||
}
|
||||
if ($balance_money > 0) {
|
||||
$use_res = $member_account_model->addMemberAccount($order_info[ 'site_id' ], $order_info[ 'member_id' ], 'balance_money', -$balance_money, 'order', $order_info[ 'order_id' ], '订单消费扣除');
|
||||
if ($use_res[ 'code' ] != 0) {
|
||||
model('giftcard_order')->rollback();
|
||||
return $use_res;
|
||||
}
|
||||
}
|
||||
}
|
||||
model('giftcard_order')->commit();
|
||||
//活动增加销量
|
||||
( new CardStat() )->stat([ 'stat_type' => 'sale', 'giftcard_id' => $giftcard_id, 'num' => $num ]);
|
||||
$stat_model = new Stat();
|
||||
$stat_model->switchStat([ 'type' => 'gift_card_order', 'data' => [ 'order_id' => $order_info[ 'order_id' ], 'site_id' => $order_info[ 'site_id' ] ] ]);
|
||||
return $this->success();
|
||||
} catch (\Exception $e) {
|
||||
model('giftcard_order')->rollback();
|
||||
return $this->error('', $e->getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除礼品卡订单(伪删除)
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
public function delete($condition)
|
||||
{
|
||||
model('giftcard_order')->update([ 'is_delete' => 1 ], $condition);
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 订单关闭
|
||||
* @param $params
|
||||
* @return array
|
||||
*/
|
||||
public function close($params)
|
||||
{
|
||||
$order_id = $params[ 'order_id' ];
|
||||
$site_id = $params[ 'site_id' ] ?? 0;
|
||||
$close_cause = $params[ 'close_cause' ] ?? '长时间未支付,订单自动关闭';
|
||||
$condition = array (
|
||||
[ 'order_id', '=', $order_id ],
|
||||
[ 'order_status', '=', 'topay' ]
|
||||
);
|
||||
if ($site_id > 0) {
|
||||
$condition[] = [ 'site_id', '=', $site_id ];
|
||||
}
|
||||
$order_model = new GiftCardOrder();
|
||||
$info = $order_model->getOrderInfo($condition, 'order_status, out_trade_no, pay_status')[ 'data' ] ?? [];
|
||||
if (empty($info)) {
|
||||
return $this->error([], '订单不存在');
|
||||
}
|
||||
|
||||
if ($info[ 'order_status' ] != 'topay') {
|
||||
return $this->error([], '订单不是待支付状态');
|
||||
}
|
||||
|
||||
$data = array (
|
||||
'order_status' => 'close',
|
||||
'close_time' => time(),
|
||||
);
|
||||
if (!empty($close_cause)) {
|
||||
$data[ 'close_cause' ] = $close_cause;
|
||||
}
|
||||
//关闭支付单据(没支付的话)
|
||||
if ($info[ 'pay_status' ] == 0) {
|
||||
$pay_model = new Pay();
|
||||
$pay_result = $pay_model->closePay($info[ 'out_trade_no' ]);
|
||||
if ($pay_result[ 'code' ] < 0) {
|
||||
return $pay_result;
|
||||
}
|
||||
}
|
||||
model('giftcard_order')->update($data, $condition);
|
||||
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
}
|
||||
42
addon/giftcard/model/order/GiftCardOrderPay.php
Executable file
42
addon/giftcard/model/order/GiftCardOrderPay.php
Executable file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace addon\giftcard\model\order;
|
||||
|
||||
use app\model\BaseModel;
|
||||
use app\model\system\Pay;
|
||||
|
||||
class GiftCardOrderPay extends BaseModel
|
||||
{
|
||||
|
||||
public function resetPay($params)
|
||||
{
|
||||
$out_trade_no = $params[ 'out_trade_no' ];
|
||||
$order_condition = array (
|
||||
[ 'pay_status', '=', 0 ],
|
||||
[ 'out_trade_no', '=', $out_trade_no ]
|
||||
);
|
||||
$order_condition[] = [ 'out_trade_no', '=', $out_trade_no ];
|
||||
$giftcard_order_model = new GiftCardOrder();
|
||||
$order_info = $giftcard_order_model->getOrderInfo($order_condition, 'pay_money,site_id,member_id');
|
||||
if (empty($order_info))
|
||||
return $this->error([], '没有可支付订单!');
|
||||
|
||||
$pay_money = $order_info[ 'pay_money' ];
|
||||
$site_id = $order_info[ 'site_id' ];
|
||||
|
||||
$pay_model = new Pay();
|
||||
$result = $pay_model->closePay($out_trade_no);//关闭旧支付单据
|
||||
if ($result[ 'code' ] < 0) {
|
||||
return $result;
|
||||
}
|
||||
$member_id = $order_info[ 'member_id' ];
|
||||
$new_out_trade_no = $pay_model->createOutTradeNo($member_id ?? 0);
|
||||
$update_data = array (
|
||||
'out_trade_no' => $new_out_trade_no
|
||||
);
|
||||
model('giftcard_order')->update($update_data, $order_condition);
|
||||
$result = $pay_model->addPay($site_id, $new_out_trade_no, '', $params[ 'pay_body' ], $params[ 'pay_detail' ], $pay_money, '', 'GiftCardOrderPayNotify', '', $order_info['order_id'], $order_info['member_id']);
|
||||
return $this->success($new_out_trade_no);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user