123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374 |
- <?php
- 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\OrderModel;
- use app\model\ServiceModel;
- use think\facade\Db;
- use think\Exception;
- use think\response\Json;
- class OrderLogic extends BaseLogic
- {
- //下单
- public static function add(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,
- 'remark' => $data['remark'],
- 'addr_id' => $data['addr_id'],
- ];
- //子订单数据
- $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'],
- 'child_title' => $item['good_name'],
- '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'],
- 'child_title' => $item['title'],
- '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);//总金额
- $insert_order[] = [
- 'masterOrderCode' => $masterOrderCode,
- 'orderCode' => $orderCode,
- 'company_id' => self::$company_id,
- 'card_id' => self::$card_id,
- 'uid' => self::$aid,
- 'child_id' => $item['id'],
- 'child_title' => $item['good_name'],
- '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' => ''];
- }
- //处理数据
- 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::$error_param, '下单失败,' . $exception->getMessage());
- }
- }
- //订单列表
- public static function list(array $data = []): Json
- {
- $db = MasterOrderModel::where(['uid' => self::$aid]);
- if ($data['status'] != '') $db->where('status', $data['status']);
- $count = $db->count('id');
- $list = $db
- ->field('id,masterOrderCode,amount,status,addtime,type')
- ->append(['order_list'])
- ->withAttr('order_list', function ($val, $da) {
- return OrderModel::field('id,orderCode,child_title,price,num,total_price')
- ->where(['is_del' => CommonModel::$del_normal, 'masterOrderCode' => $da['masterOrderCode'], 'uid' => self::$aid])
- ->order('id')
- ->select()
- ->toArray();
- })
- ->page($data['page'], $data['size'])
- ->order('id', 'desc')
- ->select()
- ->toArray();
- return json_show(CommonModel::$success, '获取订单列表成功', ['count' => $count, 'list' => $list]);
- }
- //订单详情
- public static function info(int $id = 0): Json
- {
- $rs = MasterOrderModel::alias('a')
- ->field('a.id,a.masterOrderCode,a.amount,a.status,a.addtime,c.contactor,c.mobile,c.addr,c.addr_code,a.transaction_id,a.remark,a.type')
- ->leftJoin('addr c', 'c.id=a.addr_id')
- ->append(['order_list', 'express_data'])
- ->withAttr('order_list', function ($val, $da) {
- return OrderModel::field('id,orderCode,child_title,child_cover,price,num,total_price,post_code')
- ->where(['is_del' => CommonModel::$del_normal, 'masterOrderCode' => $da['masterOrderCode'], 'uid' => self::$aid])
- ->order('id')
- ->select()
- ->toArray();
- })
- ->withAttr('addr', function ($val, $da) {
- return $da['addr_code'] ? get_addr_name($da['addr_code']) . $val : $val;
- })->where('a.id', $id)
- ->findOrEmpty()
- ->toArray();
- return $rs ? json_show(CommonModel::$success, '获取订单详情成功', $rs) : json_show(CommonModel::$error_param, '该订单不存在');
- }
- //物流信息
- public static function express(string $post_code = ''): Json
- {
- $res = Db::name('express_data')
- ->where('post_code', $post_code)
- ->field(true)
- ->withAttr('post_data', function ($val) {
- return json_decode($val, true);
- })
- ->findOrEmpty();
- return json_show(CommonModel::$success, '获取快递单号的物流信息成功', $res);
- }
- }
|