|
@@ -6,22 +6,20 @@ use app\model\AccountModel;
|
|
|
use app\model\AddrModel;
|
|
|
use app\model\CommonModel;
|
|
|
use app\model\GoodModel;
|
|
|
+use app\model\InventoryExchangeModel;
|
|
|
use app\model\MasterOrderModel;
|
|
|
use app\model\PayInfoModel;
|
|
|
use app\model\ServiceModel;
|
|
|
+use think\Exception;
|
|
|
use think\facade\Db;
|
|
|
use think\response\Json;
|
|
|
|
|
|
class PayLogic extends BaseLogic
|
|
|
{
|
|
|
|
|
|
- //微信支付预下单
|
|
|
+ //下单
|
|
|
public static function getPrepayId(array $data = []): json
|
|
|
{
|
|
|
- $openId = AccountModel::where(['id' => self::$aid, 'is_del' => CommonModel::$del_normal])
|
|
|
- ->value('wx_openId', '');
|
|
|
-
|
|
|
- if ($openId == '') return json_show(CommonModel::$error_token, '获取账户openId失败,请重新登录');
|
|
|
|
|
|
Db::startTrans();
|
|
|
|
|
@@ -37,75 +35,37 @@ class PayLogic extends BaseLogic
|
|
|
$num = array_column($data['list'], 'num', 'id');//
|
|
|
$date = date('Y-m-d H:i:s');
|
|
|
|
|
|
- $pay_code = make_no('ZF');
|
|
|
+ $masterOrderCode = make_no('MS');
|
|
|
|
|
|
- //支付信息表
|
|
|
- $pay_info_id = Db::name('pay_info')->insertGetId([
|
|
|
- 'pay_code' => $pay_code,
|
|
|
- 'wx_openId' => $openId,
|
|
|
+ //主订单数据
|
|
|
+ $insert_master_order = [
|
|
|
+ 'masterOrderCode' => $masterOrderCode,
|
|
|
'uid' => self::$aid,
|
|
|
'type' => $data['type'],
|
|
|
- 'ids' => json_encode($data['list']),
|
|
|
- 'status' => MasterOrderModel::$order_status_wait_pay,
|
|
|
'createrid' => self::$aid,
|
|
|
'creater' => self::$aname,
|
|
|
'addtime' => $date,
|
|
|
'updaterid' => self::$aid,
|
|
|
'updater' => self::$aname,
|
|
|
'updatetime' => $date,
|
|
|
- ]);
|
|
|
+ ];
|
|
|
|
|
|
+ //子订单数据
|
|
|
+ $insert_order = [];
|
|
|
|
|
|
- if ($data['type'] == CommonModel::$pay_type_service) {
|
|
|
- //购买服务
|
|
|
- $rs = ServiceModel::field('id,original_price,activity_price,title,activity_status')
|
|
|
- ->whereIn('id', array_column($data['list'], 'id'))
|
|
|
- ->where(['is_del' => CommonModel::$del_normal, 'status' => CommonModel::$status_normal])
|
|
|
- ->select()
|
|
|
- ->toArray();
|
|
|
- if (empty($rs)) throw new Exception('购买的服务不存在或已下架');
|
|
|
+ //库存变动记录数据
|
|
|
+ $insert_inventory_exchange_log = [];
|
|
|
|
|
|
- $insert_service = [];
|
|
|
- foreach ($rs as $item) {
|
|
|
- //单价
|
|
|
- if ($item['activity_status'] == ServiceModel::$activity_status_ing) $price = $rs['activity_price'];
|
|
|
- else $price = $rs['original_price'];
|
|
|
|
|
|
- $total_price = bcmul($price, $num[$item['id']] ?? 0, 2);//总价
|
|
|
- $amount = bcadd($amount, $total_price, 2);//总金额
|
|
|
+ //兑换商品
|
|
|
+ if ($data['type'] == MasterOrderModel::$type_exchange_good) {
|
|
|
|
|
|
- $insert_service[] = [
|
|
|
- 'orderCode' => make_no('FW'),
|
|
|
- 'pay_info_id' => $pay_info_id,
|
|
|
- 'uid' => self::$aid,
|
|
|
- 'service_id' => $item['id'],
|
|
|
- 'num' => $num[$item['id']] ?? 0,
|
|
|
- 'addr_id' => $data['addr_id'],
|
|
|
- 'price' => $price,
|
|
|
- 'total_price' => $total_price,
|
|
|
- 'status' => MasterOrderModel::$order_status_wait_pay,
|
|
|
- 'is_del' => CommonModel::$del_normal,
|
|
|
- 'createrid' => self::$aid,
|
|
|
- 'creater' => self::$aname,
|
|
|
- 'addtime' => $date,
|
|
|
- 'updaterid' => self::$aid,
|
|
|
- 'updater' => self::$aname,
|
|
|
- 'updatetime' => $date,
|
|
|
- ];
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- //向服务订单表写数据
|
|
|
- Db::name('order_service')->insertAll($insert_service);
|
|
|
+ $rs = GoodModel::alias('a')
|
|
|
+ ->leftJoin('inventory_exchange c', 'c.account_id=' . self::$aid . ' AND c.good_id=a.id')
|
|
|
+ ->whereIn('a.id', array_column($data['list'], 'id'))
|
|
|
+ ->where(['a.is_del' => CommonModel::$del_normal, 'a.status' => CommonModel::$status_normal, 'type' => GoodModel::$type_exchange])
|
|
|
+ ->column('a.id,a.good_code,a.good_name,a.moq,c.inventory,c.id inventory_id', 'a.id');
|
|
|
|
|
|
- } else {
|
|
|
- //购买商城商品
|
|
|
- $order_insert = [];
|
|
|
- $rs = GoodModel::field('id,price,good_name')
|
|
|
- ->whereIn('id', array_column($data['list'], 'id'))
|
|
|
- ->where(['is_del' => CommonModel::$del_normal, 'status' => CommonModel::$status_normal])
|
|
|
- ->select()
|
|
|
- ->toArray();
|
|
|
if (empty($rs)) throw new Exception('购买的商品不存在或已禁用');
|
|
|
|
|
|
$i = 0;
|
|
@@ -113,20 +73,22 @@ class PayLogic extends BaseLogic
|
|
|
$orderCode = substr($orderCode, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
|
|
|
foreach ($rs as $item) {
|
|
|
|
|
|
- $total_price = bcmul($item['price'], $num[$item['id']] ?? 0, 2);//总价
|
|
|
- $order_insert[] = [
|
|
|
+ if ($item['moq'] > $num[$item['id']]) throw new Exception($item['good_name'] . '不满足起订量,' . $item['moq']);
|
|
|
+ if ($item['inventory'] < $num[$item['id']]) throw new Exception($item['good_name'] . '的库存数不足');
|
|
|
+
|
|
|
+ $insert_order[] = [
|
|
|
+ 'masterOrderCode' => $masterOrderCode,
|
|
|
'orderCode' => $orderCode,
|
|
|
- 'pay_info_id' => $pay_info_id,
|
|
|
'company_id' => self::$company_id,
|
|
|
'card_id' => self::$card_id,
|
|
|
'uid' => self::$aid,
|
|
|
- 'good_id' => $item['id'],
|
|
|
+ 'child_id' => $item['id'],
|
|
|
'num' => $num[$item['id']],
|
|
|
- 'price' => $item['price'],
|
|
|
- 'total_price' => $total_price,
|
|
|
+ 'price' => 0,
|
|
|
+ 'total_price' => 0,
|
|
|
'addr_id' => $data['addr_id'],
|
|
|
- 'type' => MasterOrderModel::$pay_type_shopping_good,
|
|
|
- 'status' => MasterOrderModel::$order_status_wait_pay,
|
|
|
+ 'type' => $data['type'],
|
|
|
+ 'status' => MasterOrderModel::$order_status_not_deliver,
|
|
|
'is_del' => CommonModel::$del_normal,
|
|
|
'createrid' => self::$aid,
|
|
|
'creater' => self::$aname,
|
|
@@ -135,38 +97,204 @@ class PayLogic extends BaseLogic
|
|
|
'updater' => self::$aname,
|
|
|
'updatetime' => $date,
|
|
|
];
|
|
|
- $amount = bcadd($amount, $total_price, 2);
|
|
|
+
|
|
|
+ //扣减库存
|
|
|
+ $after_inventory = bcsub($item['inventory'], $num[$item['id']]);
|
|
|
+ //添加库存变动记录
|
|
|
+ $insert_inventory_exchange_log[] = [
|
|
|
+ 'inventory_exchange_id' => $item['inventory_id'],
|
|
|
+ 'before_inventory' => $item['inventory'],
|
|
|
+ 'after_inventory' => $after_inventory,
|
|
|
+ 'flag' => '-1',
|
|
|
+ 'good_id' => $item['id'],
|
|
|
+ 'good_code' => $item['good_code'],
|
|
|
+ 'good_name' => $item['good_name'],
|
|
|
+ 'source' => CommonModel::$source_account,
|
|
|
+ 'createrid' => self::$aid,
|
|
|
+ 'creater' => self::$aname,
|
|
|
+ 'addtime' => $date
|
|
|
+ ];
|
|
|
+
|
|
|
+ //扣减库存
|
|
|
+ InventoryExchangeModel::where('id', $item['inventory_id'])
|
|
|
+ ->save(['inventory' => $after_inventory, 'updaterid' => self::$aid, 'updater' => self::$aname, 'updatetime' => $date]);
|
|
|
|
|
|
$i++;
|
|
|
}
|
|
|
|
|
|
+ //更新主订单表数据
|
|
|
+ $insert_master_order = array_merge($insert_master_order, [
|
|
|
+ 'wx_openId' => '',
|
|
|
+ 'prepay_info' => '',
|
|
|
+ 'amount' => $amount,
|
|
|
+ 'transaction_id' => '',
|
|
|
+ 'trade_type' => '',
|
|
|
+ 'notify_result' => '',
|
|
|
+ 'expiretime' => $date,
|
|
|
+ 'status' => MasterOrderModel::$order_status_not_deliver,
|
|
|
+ ]);
|
|
|
|
|
|
- }
|
|
|
+ $res = ['is_need_pay' => 0, 'result' => ''];
|
|
|
|
|
|
- //获取预支付信息,
|
|
|
- //@todo body要写什么????????
|
|
|
- $result = WechatLogic::getPrepayId($openId, '', $pay_code, $amount);
|
|
|
+ } else {
|
|
|
|
|
|
+ $openId = AccountModel::where(['id' => self::$aid, 'is_del' => CommonModel::$del_normal])
|
|
|
+ ->value('wx_openId', '');
|
|
|
|
|
|
- //更新pay_info表
|
|
|
- Db::name('pay_info')
|
|
|
- ->where('id', $pay_info_id)
|
|
|
- ->update([
|
|
|
- 'prepay_info' => $result,
|
|
|
+ if ($openId == '') return json_show(CommonModel::$error_token, '获取账户openId失败,请重新登录');
|
|
|
+
|
|
|
+ switch ($data['type']) {
|
|
|
+
|
|
|
+ //服务订单
|
|
|
+ case MasterOrderModel::$type_service:
|
|
|
+ $rs = ServiceModel::field('id,original_price,activity_price,title,activity_status')
|
|
|
+ ->whereIn('id', array_column($data['list'], 'id'))
|
|
|
+ ->where(['is_del' => CommonModel::$del_normal, 'status' => CommonModel::$status_normal])
|
|
|
+ ->select()
|
|
|
+ ->toArray();
|
|
|
+ if (empty($rs)) throw new Exception('购买的服务不存在或已下架');
|
|
|
+
|
|
|
+ $i = 0;
|
|
|
+ $orderCode = make_no('QR');
|
|
|
+ $orderCode = substr($orderCode, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
|
|
|
+ foreach ($rs as $item) {
|
|
|
+ //单价
|
|
|
+ if ($item['activity_status'] == ServiceModel::$activity_status_ing) $price = $rs['activity_price'];
|
|
|
+ else $price = $rs['original_price'];
|
|
|
+
|
|
|
+ $total_price = bcmul($price, $num[$item['id']] ?? 0, 2);//总价
|
|
|
+ $amount = bcadd($amount, $total_price, 2);//总金额
|
|
|
+
|
|
|
+ $insert_order[] = [
|
|
|
+ 'masterOrderCode' => $masterOrderCode,
|
|
|
+ 'orderCode' => $orderCode,
|
|
|
+ 'company_id' => self::$company_id,
|
|
|
+ 'card_id' => self::$card_id,
|
|
|
+ 'uid' => self::$aid,
|
|
|
+ 'child_id' => $item['id'],
|
|
|
+ 'num' => $num[$item['id']] ?? 0,
|
|
|
+ 'price' => $price,
|
|
|
+ 'total_price' => $total_price,
|
|
|
+ 'addr_id' => $data['addr_id'],
|
|
|
+ 'type' => $data['type'],
|
|
|
+ 'status' => MasterOrderModel::$order_status_wait_pay,
|
|
|
+ 'is_del' => CommonModel::$del_normal,
|
|
|
+ 'createrid' => self::$aid,
|
|
|
+ 'creater' => self::$aname,
|
|
|
+ 'addtime' => $date,
|
|
|
+ 'updaterid' => self::$aid,
|
|
|
+ 'updater' => self::$aname,
|
|
|
+ 'updatetime' => $date,
|
|
|
+ ];
|
|
|
+ $i++;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ break;
|
|
|
+
|
|
|
+ //商城商品订单
|
|
|
+ case MasterOrderModel::$type_shopping_good:
|
|
|
+ $rs = GoodModel::alias('a')
|
|
|
+ ->leftJoin('inventory_exchange c', 'c.account_id=' . self::$aid . ' AND c.good_id=a.id')
|
|
|
+ ->whereIn('a.id', array_column($data['list'], 'id'))
|
|
|
+ ->where(['a.is_del' => CommonModel::$del_normal, 'a.status' => CommonModel::$status_normal, 'type' => GoodModel::$type_shopping])
|
|
|
+ ->column('a.id,a.good_code,a.good_name,a.moq,a.price,c.inventory,c.id inventory_id', 'a.id');
|
|
|
+
|
|
|
+ if (empty($rs)) throw new Exception('购买的商品不存在或已禁用');
|
|
|
+
|
|
|
+ $i = 0;
|
|
|
+ $orderCode = make_no('QR');
|
|
|
+ $orderCode = substr($orderCode, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
|
|
|
+ foreach ($rs as $item) {
|
|
|
+
|
|
|
+ if ($item['moq'] > $num[$item['id']]) throw new Exception('不满足该商品的起订量,' . $item['moq']);
|
|
|
+ if ($item['inventory'] < $num[$item['id']]) throw new Exception('库存数不足');
|
|
|
+
|
|
|
+ $total_price = bcmul($rs['price'], $num[$item['id']] ?? 0, 2);//总价
|
|
|
+ $amount = bcadd($amount, $total_price, 2);//总金额
|
|
|
+
|
|
|
+ $order_insert[] = [
|
|
|
+ 'masterOrderCode' => $masterOrderCode,
|
|
|
+ 'orderCode' => $orderCode,
|
|
|
+ 'company_id' => self::$company_id,
|
|
|
+ 'card_id' => self::$card_id,
|
|
|
+ 'uid' => self::$aid,
|
|
|
+ 'child_id' => $item['id'],
|
|
|
+ 'num' => $num[$item['id']],
|
|
|
+ 'price' => $rs['price'],
|
|
|
+ 'total_price' => $total_price,
|
|
|
+ 'addr_id' => $data['addr_id'],
|
|
|
+ 'type' => $data['type'],
|
|
|
+ 'status' => MasterOrderModel::$order_status_wait_pay,
|
|
|
+ 'is_del' => CommonModel::$del_normal,
|
|
|
+ 'createrid' => self::$aid,
|
|
|
+ 'creater' => self::$aname,
|
|
|
+ 'addtime' => $date,
|
|
|
+ 'updaterid' => self::$aid,
|
|
|
+ 'updater' => self::$aname,
|
|
|
+ 'updatetime' => $date,
|
|
|
+ ];
|
|
|
+
|
|
|
+
|
|
|
+ //扣减库存
|
|
|
+ $after_inventory = bcsub($item['inventory'], $num[$item['id']]);
|
|
|
+ //添加库存变动记录
|
|
|
+ $insert_inventory_exchange_log[] = [
|
|
|
+ 'inventory_exchange_id' => $item['inventory_id'],
|
|
|
+ 'before_inventory' => $item['inventory'],
|
|
|
+ 'after_inventory' => $after_inventory,
|
|
|
+ 'flag' => '-1',
|
|
|
+ 'good_id' => $item['id'],
|
|
|
+ 'good_code' => $item['good_code'],
|
|
|
+ 'good_name' => $item['good_name'],
|
|
|
+ 'source' => CommonModel::$source_account,
|
|
|
+ 'createrid' => self::$aid,
|
|
|
+ 'creater' => self::$aname,
|
|
|
+ 'addtime' => $date
|
|
|
+ ];
|
|
|
+
|
|
|
+ //扣减库存
|
|
|
+ InventoryExchangeModel::where('id', $item['inventory_id'])
|
|
|
+ ->save(['inventory' => $after_inventory, 'updaterid' => self::$aid, 'updater' => self::$aname, 'updatetime' => $date]);
|
|
|
+
|
|
|
+ $i++;
|
|
|
+ }
|
|
|
+
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取预支付信息,
|
|
|
+ //@todo body要写什么????????
|
|
|
+ $result = WechatLogic::getPrepayId($openId, '', $masterOrderCode, $amount);
|
|
|
+
|
|
|
+
|
|
|
+ //更新主订单表数据
|
|
|
+ $insert_master_order = array_merge($insert_master_order, [
|
|
|
+ 'wx_openId' => $openId,
|
|
|
+ 'prepay_info' => '',//预支付信息
|
|
|
'amount' => $amount,
|
|
|
- 'expiretime' => date('Y-m-d H:i:s', time() + 5 * 60),
|
|
|
+ 'transaction_id' => '',
|
|
|
+ 'trade_type' => '',
|
|
|
+ 'notify_result' => '',
|
|
|
+ 'expiretime' => $date,
|
|
|
+ 'status' => MasterOrderModel::$order_status_wait_pay,
|
|
|
]);
|
|
|
|
|
|
+ $res = ['is_need_pay' => 1, 'result' => $result];
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
- //@todo 要给前端返回什么信息?
|
|
|
- $res = ['pay_code' => $pay_code];
|
|
|
|
|
|
+ //处理数据
|
|
|
+ MasterOrderModel::create($insert_master_order)->save();
|
|
|
+ if ($insert_inventory_exchange_log) Db::name('inventory_exchange_log')->insertAll($insert_inventory_exchange_log);
|
|
|
+ if ($insert_order) Db::name('order')->insertAll($insert_order);
|
|
|
|
|
|
Db::commit();
|
|
|
- return json_show(CommonModel::$success, '微信支付预下单成功', $res);
|
|
|
+ return json_show(CommonModel::$success, '下单成功', $res);
|
|
|
} catch (Exception $exception) {
|
|
|
Db::rollback();
|
|
|
- return json_show(CommonModel::$success, '微信支付预下单失败' . $exception->getMessage());
|
|
|
+ return json_show(CommonModel::$success, '下单失败,' . $exception->getMessage());
|
|
|
}
|
|
|
|
|
|
|