|
@@ -2,14 +2,10 @@
|
|
|
|
|
|
namespace app\mobile\logic;
|
|
|
|
|
|
-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;
|
|
@@ -17,288 +13,6 @@ use think\response\Json;
|
|
|
class PayLogic extends BaseLogic
|
|
|
{
|
|
|
|
|
|
- //下单
|
|
|
- public static function getPrepayId(array $data = []): json
|
|
|
- {
|
|
|
-
|
|
|
- Db::startTrans();
|
|
|
-
|
|
|
- try {
|
|
|
-
|
|
|
- $rs = AddrModel::field('id')
|
|
|
- ->where(['is_del' => CommonModel::$del_normal, 'id' => $data['addr_id'], 'uid' => self::$aid])
|
|
|
- ->findOrEmpty()
|
|
|
- ->isEmpty();
|
|
|
- if ($rs) throw new Exception('该地址不存在');
|
|
|
-
|
|
|
- $amount = '0';//支付金额
|
|
|
- $num = array_column($data['list'], 'num', 'id');//
|
|
|
- $date = date('Y-m-d H:i:s');
|
|
|
-
|
|
|
- $masterOrderCode = make_no('MS');
|
|
|
-
|
|
|
- //主订单数据
|
|
|
- $insert_master_order = [
|
|
|
- 'masterOrderCode' => $masterOrderCode,
|
|
|
- 'uid' => self::$aid,
|
|
|
- 'type' => $data['type'],
|
|
|
- 'createrid' => self::$aid,
|
|
|
- 'creater' => self::$aname,
|
|
|
- 'addtime' => $date,
|
|
|
- 'updaterid' => self::$aid,
|
|
|
- 'updater' => self::$aname,
|
|
|
- 'updatetime' => $date,
|
|
|
- ];
|
|
|
-
|
|
|
- //子订单数据
|
|
|
- $insert_order = [];
|
|
|
-
|
|
|
- //库存变动记录数据
|
|
|
- $insert_inventory_exchange_log = [];
|
|
|
-
|
|
|
-
|
|
|
- //兑换商品
|
|
|
- if ($data['type'] == MasterOrderModel::$type_exchange_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_exchange])
|
|
|
- ->column('a.id,a.good_code,a.good_name,a.moq,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['good_name'] . '不满足起订量,' . $item['moq']);
|
|
|
- if ($item['inventory'] < $num[$item['id']]) throw new Exception($item['good_name'] . '的库存数不足');
|
|
|
-
|
|
|
- $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']],
|
|
|
- 'price' => 0,
|
|
|
- 'total_price' => 0,
|
|
|
- 'addr_id' => $data['addr_id'],
|
|
|
- 'type' => $data['type'],
|
|
|
- 'status' => MasterOrderModel::$order_status_not_deliver,
|
|
|
- '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++;
|
|
|
- }
|
|
|
-
|
|
|
- //更新主订单表数据
|
|
|
- $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' => ''];
|
|
|
-
|
|
|
- } else {
|
|
|
-
|
|
|
- $openId = AccountModel::where(['id' => self::$aid, 'is_del' => CommonModel::$del_normal])
|
|
|
- ->value('wx_openId', '');
|
|
|
-
|
|
|
- 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,
|
|
|
- 'transaction_id' => '',
|
|
|
- 'trade_type' => '',
|
|
|
- 'notify_result' => '',
|
|
|
- 'expiretime' => $date,
|
|
|
- 'status' => MasterOrderModel::$order_status_wait_pay,
|
|
|
- ]);
|
|
|
-
|
|
|
- $res = ['is_need_pay' => 1, 'result' => $result];
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- //处理数据
|
|
|
- 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);
|
|
|
- } catch (Exception $exception) {
|
|
|
- Db::rollback();
|
|
|
- return json_show(CommonModel::$success, '下单失败,' . $exception->getMessage());
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
|
|
|
//微信支付成功后的异步通知及订单处理
|
|
|
public static function Notify()
|