| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- <?php
- /**
- * 订单相关
- * @author x.b
- * @date 2020-02-25 10:10:06
- */
- declare(strict_types=1);
- namespace App\Repositories;
- use App\Constants\ChangeLogConstant;
- use App\Model\CoinsOrderModel;
- use App\Model\Mongo\RepairOrderHistoryModel;
- use App\Exception\ApiException;
- use App\Model\CollectionsModel;
- use App\Model\PaymentTypeModel;
- use App\Model\UserModel;
- use App\Model\PaymentModel;
- use App\Model\VipCardModel;
- use App\Model\VipOrderModel;
- use App\Repositories\Service\QueueService;
- use App\Repositories\UserRepository as UserRepository;
- use Carbon\Carbon;
- use Hyperf\DbConnection\Db;
- use KZ2\Repositories\Model\Repository;
- use Psr\SimpleCache\CacheInterface;
- class OrderRepository extends Repository
- {
- public function model()
- {
- return PaymentModel::class;
- }
- /**
- * 查询支付方式/支付通道信息
- * @param array no $select 查询字段,默认*
- * @return array
- */
- public function getPaymentData($select = ['*'])
- {
- return PaymentModel::query()->select($select)->get();
- }
- /**
- * 对指定对订单-标示为查单处理状态
- * @param string yes $order_sn 订单号
- * @param int no $is_admin_action 是否已查单处理: 0-没处理 1-已处理,(默认 值为 1)
- * @return bool
- */
- public function setAdminAction($order_sn = '', $is_admin_action = 1)
- {
- $data = array(
- 'is_admin_action' => $is_admin_action
- );
- if ($is_admin_action == 1) {
- $data['admin_action_at'] = date('Y-m-d H:i:s');
- }
- $r = VipOrderModel::query()
- ->where('order_sn', $order_sn)
- ->update($data);
- if (!$r) {
- return false;
- }
- return true;
- }
- /**
- * 查询指定订单信息
- * @param string yes $order_sn 订单号
- * @return array
- */
- public function getOrderInfo($order_sn = '')
- {
- $r = VipOrderModel::query()
- ->where('order_sn', $order_sn)
- ->first();
- if (!$r) {
- return array();
- }
- return $r;
- }
- /**
- * 查询指定订单信息
- * @param string yes $order_sn 订单号
- * @return array
- */
- public function getCoinsOrderInfo($order_sn = '')
- {
- $r = CoinsOrderModel::query()
- ->where('order_sn', $order_sn)
- ->first();
- if (!$r) {
- return array();
- }
- return $r;
- }
- /**
- * 补单
- * @param string yes $order_sn 订单号
- * @param string no $trade_no 支付单号
- * @param string no $repair_remark 补单备注
- * @return bool
- *
- * 处理逻辑:vip_order 订单表标示已付款和付款日期; collections 表创建收款单记录, user表添加会员日期,
- * 成功之后,在mongodb中创建一个 补单记录。
- * 备注: 不需要去内调回调接口,不要验签!
- *
- */
- public function again($order_sn = '', $trade_no = '', $repair_remark = '')
- {
- $order_info = $this->getOrderInfo($order_sn);
- if (!$order_info) {
- return false;
- }
- $user_info = UserModel::query()
- ->where('id', $order_info['user_id'])
- ->first();
- if (!$user_info) {
- return false;
- }
- $card_info = VipCardModel::query()
- ->where('id', $order_info['vip_id'])
- ->first();
- if (!$card_info) {
- return false;
- }
- Db::beginTransaction();
- try {
- // 标示订单已付款
- $data = array(
- 'status' => 1,
- 'pay_time' => date('Y-m-d H:i:s'),
- 'trade_no' => $trade_no,
- 'repair_remark' => $repair_remark,
- );
- $result = VipOrderModel::query()
- ->where('order_sn', $order_sn)
- ->update($data);
- if (!$result) {
- throw new ApiException('标示订单已付款失败');
- }
- // 创建收款单记录
- $insertData = array(
- 'user_id' => $user_info['id'],
- 'user_code' => $user_info['code'],
- 'order_sn' => $order_sn,
- 'trade_sn' => $trade_no,
- 'order_amount' => $order_info['order_amount'],
- 'real_amount' => $order_info['real_amount'],
- 'device_type' => $user_info['device_type'],
- 'type' => 1, //购买会员
- );
- $result = CollectionsModel::create($insertData);
- if (!$result) {
- throw new ApiException('创建收款单记录失败');
- }
- // 对用户加会员天数
- $userInfo = make(UserRepository::class)
- ->getUserInfoFromCache($user_info['code']);
- if ($userInfo['is_vip'] == 'y') {
- $vipTime = Carbon::parse($userInfo['vip_expired'])
- ->addDays($card_info['day'])
- ->toDateTimeString();
- } else {
- $vipTime = Carbon::now()
- ->addDays($card_info['day'])
- ->toDateTimeString();
- }
- $package = json_decode($order_info['package_info'], true);
- $houseCount = 0;
- $userCoins = 0;
- foreach ($package['jdata']['feature'] as $packVal) {
- switch ($packVal['type']) {
- case 'house':
- $houseCount = $packVal['count'];
- break;
- }
- }
- if ($package['send_type'] == 2) {
- $userCoins = $package['send_num'];
- }
- $houseCount = $userInfo['max_open_house'] + $houseCount;
- $coins = $userInfo['coins'] + $userCoins;
- $result = UserModel::query()
- ->where('id', $user_info['id'])
- ->update(['vip_expired' => $vipTime, 'vip_level' => $order_info['vip_id'], 'max_open_house' => $houseCount, 'coins' => $coins]);
- if (!$result) {
- throw new ApiException('对用户加会员天数失败');
- }
- //创建一条mongo补单记录
- $data = array(
- '_id' => md5($order_info['user_id'] . '_' . $order_sn),
- 'order_sn' => $order_sn,
- 'trade_no' => $trade_no,
- 'user_id' => $order_info['user_id'],
- 'vip_id' => $order_info['vip_id'],
- 'vip_days' => $order_info['vip_days'],
- 'repair_remark' => $repair_remark,
- 'created_at' => date('Y-m-d H:i:s')
- );
- make(RepairOrderHistoryModel::class)->db()->insertOne($data);
- Db::commit();
- //清除用户信息redis缓存
- make(UserRepository::class)->delUserCache($user_info['code']);
- // 帐变记录
- if ($userCoins > 0) {
- $changeData = [
- 'user_uid' => $user_info['code'],
- 'currency_type' => ChangeLogConstant::CHANGE_COIN,
- 'amount' => $userCoins,
- 'way' => ChangeLogConstant::WAY_1,
- 'type' => ChangeLogConstant::TYPE_23,
- ];
- make(QueueService::class)->saveChangeLogJob($changeData);
- }
- } catch (\Exception $e) {
- Db::rollBack();
- }
- return true;
- }
- /**
- * 获取可用的自发通道
- *
- * @param $payType
- * @return
- */
- public function getPayChannel($payId)
- {
- $paymentData = PaymentModel::query()
- ->join('payment_type as pt', 'payment.uid', '=', 'pt.payment_uid')
- ->leftJoin('payment_attr as pa', 'pt.payment_attr_uid', '=', 'pa.uid')
- ->select(
- 'payment.pay_code',
- 'payment.partner_id',
- 'payment.uid',
- 'pa.code',
- 'pt.limit_type',
- 'pt.limit',
- 'pt.pay_type',
- 'pt.weights',
- 'pt.id',
- 'pt.name'
- )
- ->where(['payment.status' => 1, 'pt.status' => 1, 'pt.id' => $payId])
- ->first();
- return $paymentData;
- }
- /**
- * 金币补单
- *
- * @param $orderSn
- * @param $remark
- * @param $userId
- * @param $coins
- * @return string|void
- */
- public function coinsAgain($orderSn, $remark, $userId, $coins)
- {
- $userInfo = UserModel::query()
- ->where('id', $userId)
- ->first();
- if (!$userInfo) {
- return false;
- }
- Db::beginTransaction();
- $coinsRes = CoinsOrderModel::query()->where('order_sn', $orderSn)->update(['remark' => $remark, 'status' => 1]);
- if (!$coinsRes) {
- Db::rollBack();
- return false;
- }
- $userRes = UserModel::query()->where('id', $userId)->increment('coins', $coins);
- if (!$userRes) {
- Db::rollBack();
- return false;
- }
- Db::commit();
- //清除用户信息redis缓存
- make(UserRepository::class)->delUserCache($userInfo['code']);
- // 帐变记录
- $changeData = [
- 'user_uid' => $userInfo['code'],
- 'currency_type' => ChangeLogConstant::CHANGE_COIN,
- 'amount' => $coins,
- 'way' => ChangeLogConstant::WAY_1,
- 'type' => ChangeLogConstant::TYPE_19,
- ];
- make(QueueService::class)->saveChangeLogJob($changeData);
- return true;
- }
- }
|