123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- <?php
- /**
- *
- * @author: xaboy<365615158@qq.com>
- * @day: 2018/01/05
- */
- namespace app\models\user;
- use crmeb\basic\BaseModel;
- use crmeb\services\MiniProgramService;
- use crmeb\services\WechatService;
- use crmeb\traits\ModelTrait;
- /**
- * TODO 用户充值
- * Class UserRecharge
- * @package app\models\user
- */
- class UserRecharge extends BaseModel
- {
- /**
- * 数据表主键
- * @var string
- */
- protected $pk = 'id';
- /**
- * 模型名称
- * @var string
- */
- protected $name = 'user_recharge';
- use ModelTrait;
- protected $insert = ['add_time'];
- protected function setAddTimeAttr()
- {
- return time();
- }
- /**
- * 创建充值订单
- * @param $uid
- * @param $price
- * @param string $recharge_type
- * @param int $paid
- * @return UserRecharge|bool|\think\Model
- */
- public static function addRecharge($uid, $price, $recharge_type = 'weixin', $give_price = 0, $paid = 0, $mer_id = '')
- {
- $order_id = self::getNewOrderId($uid);
- if (!$order_id) return self::setErrorInfo('订单生成失败!');
- $add_time = time();
- return self::create(compact('order_id', 'uid', 'price', 'recharge_type', 'paid', 'add_time', 'give_price', 'mer_id'));
- }
- /**
- * 生成充值订单号
- * @param int $uid
- * @return bool|string
- */
- public static function getNewOrderId($uid = 0)
- {
- if (!$uid) return false;
- $count = (int)self::where('uid', $uid)->where('add_time', '>=', strtotime(date("Y-m-d")))->where('add_time', '<', strtotime(date("Y-m-d", strtotime('+1 day'))))->count();
- return 'wx' . date('YmdHis', time()) . (10000 + $count + $uid);
- }
- /**
- * 充值js支付
- * @param $orderInfo
- * @param bool $mer_id
- * @return array|string
- * @throws \think\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public static function jsPay($orderInfo, $mer_id = false)
- {
- return MiniProgramService::jsPay(WechatUser::uidToOpenid($orderInfo['uid']), $orderInfo['order_id'], $orderInfo['price'], 'user_recharge', '用户充值', '', 'JSAPI', [], $mer_id);
- }
- /**
- * 微信H5支付
- * @param $orderInfo
- * @return mixed
- */
- public static function wxH5Pay($orderInfo)
- {
- return WechatService::paymentPrepare(null, $orderInfo['order_id'], $orderInfo['price'], 'user_recharge', '用户充值', '', 'MWEB');
- }
- /**
- * 公众号支付
- * @param $orderInfo
- * @return array|string
- * @throws \Exception
- */
- public static function wxPay($orderInfo)
- {
- return WechatService::jsPay(WechatUser::uidToOpenid($orderInfo['uid'], 'openid'), $orderInfo['order_id'], $orderInfo['price'], 'user_recharge', '用户充值');
- }
- /**
- * //TODO用户充值成功后
- * @param $orderId
- */
- public static function rechargeSuccess($orderId)
- {
- $order = self::where('order_id', $orderId)->where('paid', 0)->find();
- if (!$order) return false;
- $user = User::getUserInfo($order['uid']);
- self::beginTrans();
- $price = bcadd($order['price'], $order['give_price'], 2);
- $res1 = self::where('order_id', $order['order_id'])->update(['paid' => 1, 'pay_time' => time()]);
- $mark = '成功充值余额' . floatval($order['price']) . '元' . ($order['give_price'] ? ',赠送' . $order['give_price'] . '元' : '');
- $res2 = UserBill::income('用户余额充值', $order['uid'], 'now_money', 'recharge', $order['price'], $order['id'], $user['now_money'], $mark, 1, $order['mer_id']);
- $res3 = User::edit(['now_money' => bcadd($user['now_money'], $price, 2)], $order['uid'], 'uid');
- $res = $res1 && $res2 && $res3;
- self::checkTrans($res);
- // MiniProgramService::profit_sharing_finish($order['transaction_id'], $order['order_id'], $order['mer_id']);
- event('RechargeSuccess', [$order]);
- return $res;
- }
- public static function rechargeTest($orderId)
- {
- $order = self::where('order_id', $orderId)->where('paid', 0)->find();
- if (!$order) return false;
- $user = User::getUserInfo($order['uid']);
- $price = bcadd($order['price'], $order['give_price'], 2);
- $res1 = self::where('order_id', $order['order_id'])->update(['paid' => 1, 'pay_time' => time()]);
- $mark = '成功充值余额' . floatval($order['price']) . '元' . ($order['give_price'] ? ',赠送' . $order['give_price'] . '元' : '');
- $res2 = UserBill::income('用户余额充值', $order['uid'], 'now_money', 'recharge', $order['price'], $order['id'], $user['now_money'], $mark, 1, $order['mer_id']);
- event('RechargeSuccess', [$order]);
- }
- /**
- * 导入佣金到余额
- * @param $uid 用户uid
- * @param $price 导入金额
- * @return bool
- * @throws \think\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public static function importNowMoney($uid, $price, $mer_id = '')
- {
- $user = User::getUserInfo($uid);
- self::beginTrans();
- try {
- $broken_time = intval(sys_config('extract_time'));
- $search_time = time() - 86400 * $broken_time;
- //返佣 +
- $brokerage_commission = UserBill::where(['uid' => $uid, 'category' => 'now_money', 'type' => 'brokerage'])
- ->where('add_time', '>', $search_time)
- ->where('pm', 1)
- ->sum('number');
- //退款退的佣金 -
- $refund_commission = UserBill::where(['uid' => $uid, 'category' => 'now_money', 'type' => 'brokerage'])
- ->where('add_time', '>', $search_time)
- ->where('pm', 0)
- ->sum('number');
- $broken_commission = bcsub($brokerage_commission, $refund_commission, 2);
- if ($broken_commission < 0)
- $broken_commission = 0;
- $commissionCount = bcsub($user['brokerage_price'], $broken_commission, 2);
- if ($price > $commissionCount) return self::setErrorInfo('转入金额不能大于可提现佣金!');
- $res1 = User::bcInc($uid, 'now_money', $price, 'uid');
- $res3 = User::bcDec($uid, 'brokerage_price', $price, 'uid');
- $res2 = UserBill::expend('用户佣金转入余额', $uid, 'now_money', 'recharge', $price, 0, $user['now_money'], '成功转入余额' . floatval($price) . '元');
- $extractInfo = [
- 'uid' => $uid,
- 'real_name' => $user['nickname'],
- 'extract_type' => 'balance',
- 'extract_price' => $price,
- 'balance' => bcsub($user['brokerage_price'], $price, 2),
- 'add_time' => time(),
- 'status' => 1,
- 'mer_id' => $mer_id
- ];
- $res4 = UserExtract::create($extractInfo);
- $res = $res2 && $res1 && $res3;
- self::checkTrans($res);
- if ($res) {
- event('ImportNowMoney', [$uid, $price]);
- }
- return $res;
- } catch (\Exception $e) {
- self::rollbackTrans();
- return self::setErrorInfo($e->getMessage());
- }
- }
- /**
- * 查找用户充值金额记录
- * @param $where
- * @return array
- */
- public static function getUserRechargeList($where)
- {
- $model = self::setWhere($where, null, 'a', 'u.nickname')->join('user u', 'u.uid=a.uid')->field(['a.*', 'u.nickname', 'u.avatar']);
- $list = $model->page($where['page'], $where['limit'])->select();
- $list = count($list) ? $list->toArray() : [];
- foreach ($list as &$item) {
- switch ($item['recharge_type']) {
- case 'routine':
- $item['_recharge_type'] = '小程序充值';
- break;
- case 'weixin':
- $item['_recharge_type'] = '公众号充值';
- break;
- default:
- $item['_recharge_type'] = '其他充值';
- break;
- }
- $item['_pay_time'] = $item['pay_time'] ? date('Y-m-d H:i:s', $item['pay_time']) : '暂无';
- $item['_add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '暂无';
- $item['paid_type'] = $item['paid'] ? '已支付' : '未支付';
- }
- $count = self::setWhere($where, null, 'a', 'u.nickname')->join('user u', 'u.uid=a.uid')->count();
- return compact('list', 'count');
- }
- /**
- * 设置查询条件
- * @param $where
- * @param null $model
- * @param string $alias
- * @param string $join
- * @return $this
- */
- public static function setWhere($where, $model = null, $alias = '', $join = '')
- {
- $model = is_null($model) ? new self() : $model;
- if ($alias) {
- $model = $model->alias('a');
- $alias .= '.';
- }
- if ($where['data']) $model = self::getModelTime($where, $model, "{$alias}add_time");
- if ($where['paid'] != '') $model = $model->where("{$alias}paid", $where['paid']);
- if ($where['nickname']) $model = $model->where("{$alias}uid|{$alias}order_id" . ($join ? '|' . $join : ''), 'LIKE', "%$where[nickname]%");
- if (isset($where['mer_id']) && !empty($where['mer_id'])) $model = $model->where("{$alias}mer_id", $where['mer_id']);
- return $model->order("{$alias}add_time desc");
- }
- //导出数据
- public static function exportData($where)
- {
- return self::setWhere($where, null, 'a', 'u.nickname')->join('user u', 'u.uid=a.uid')->field(['a.*', 'u.nickname', 'u.avatar'])->select()->toArray();
- }
- /**
- * 获取用户充值数据
- * @param $where
- * @return array
- */
- public static function getDataList($where)
- {
- return [
- [
- 'name' => '充值总金额',
- 'field' => '元',
- 'count' => self::setWhere($where, null, 'a', 'u.nickname')->join('user u', 'u.uid=a.uid')->sum('a.price'),
- 'className' => 'logo-yen',
- 'col' => 6,
- ],
- [
- 'name' => '充值退款金额',
- 'field' => '元',
- 'count' => self::setWhere($where, null, 'a', 'u.nickname')->join('user u', 'u.uid=a.uid')->sum('a.refund_price'),
- 'className' => 'logo-usd',
- 'col' => 6,
- ],
- [
- 'name' => '小程序充值金额',
- 'field' => '元',
- 'count' => self::setWhere($where, null, 'a', 'u.nickname')->where('a.recharge_type', 'routine')->join('user u', 'u.uid=a.uid')->sum('a.price'),
- 'className' => 'logo-bitcoin',
- 'col' => 6,
- ],
- [
- 'name' => '公众号充值金额',
- 'field' => '元',
- 'count' => self::setWhere($where, null, 'a', 'u.nickname')->where('a.recharge_type', 'weixin')->join('user u', 'u.uid=a.uid')->sum('a.price'),
- 'className' => 'ios-bicycle',
- 'col' => 6,
- ],
- ];
- }
- }
|