| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- <?php
- declare (strict_types=1);
- namespace app\model\api;
- use library\basic\BaseModel;
- use think\Model;
- /**
- * @mixin \think\Model
- */
- class RechargeOrder extends BaseModel
- {
- /**
- * 生成充值订单号
- * @param int $uid 用户UID
- * @return string
- */
- public function mkOrderSn($uid)
- {
- return 'RO' . time() . rand(1000, 9000) . $uid;
- }
- /**
- * 创建充值订单
- * @param int $uid 用户UID
- * @param int $rechargeId 充值配置ID(0为自定义充值)
- * @param float $price 充值金额
- * @param int $integral 获得积分
- * @param int $giveIntegral 赠送积分
- * @param string $payType 支付方式:wxpay/alipay/system
- * @param int $adminId 管理员ID(后台充值)
- * @param string $remark 备注
- * @param float $discount 折扣金额
- * @return string|null 订单号
- */
- public function createOrder($uid, $rechargeId, $price, $integral, $giveIntegral, $payType = 'wxpay', $adminId = 0, $remark = '', $discount = 0)
- {
- $orderSn = $this->mkOrderSn($uid);
- $totalIntegral = $integral + $giveIntegral;
-
- $bool = $this->insert([
- 'order_id' => $orderSn,
- 'uid' => $uid,
- 'recharge_id' => $rechargeId,
- 'price' => $price,
- 'integral' => $integral,
- 'give_integral' => $giveIntegral,
- 'total_integral' => $totalIntegral,
- 'pay_type' => $payType,
- 'paid' => 0,
- 'pay_time' => 0,
- 'remark' => $remark,
- 'admin_id' => $adminId,
- 'discount_amount' => $discount,
- 'status' => 0,
- 'add_time' => time(),
- 'update_time' => time()
- ]);
-
- return $bool ? $orderSn : null;
- }
- /**
- * 根据订单号获取订单
- * @param string $orderSn 订单号
- * @return array|null
- */
- public function getByOrderSn($orderSn)
- {
- return $this->where('order_id', $orderSn)->find();
- }
- /**
- * 根据ID获取订单
- * @param int $id 订单ID
- * @return array|null
- */
- public function getById($id)
- {
- return $this->where('id', $id)->find();
- }
- /**
- * 更新订单支付状态
- * @param string $orderSn 订单号
- * @param string $paySn 支付流水号
- * @param string $payJson 支付返回信息
- * @return bool
- */
- public function updatePayStatus($orderSn, $paySn, $payJson = '')
- {
- return $this->where('order_id', $orderSn)->update([
- 'paid' => 1,
- 'pay_sn' => $paySn,
- 'pay_json' => $payJson,
- 'pay_time' => time(),
- 'status' => 1,
- 'update_time' => time()
- ]);
- }
- /**
- * 获取用户充值订单列表
- * @param int $uid 用户UID
- * @param array $where 查询条件
- * @param string $field 查询字段
- * @param int $page 页码
- * @param int $pageCount 每页数量
- * @param string $order 排序
- * @return array
- */
- public function getUserList($uid, $where = [], $field = '*', $page = 1, $pageCount = 20, $order = 'add_time desc')
- {
- $where['uid'] = $uid;
- $data = $this
- ->where($where)
- ->field($field)
- ->order($order)
- ->paginate(['list_rows' => $pageCount, 'page' => $page])
- ->toArray();
- return [$data['total'], $data['data']];
- }
- /**
- * 获取充值订单列表(后台)
- * @param array $where 查询条件
- * @param string $field 查询字段
- * @param int $page 页码
- * @param int $pageCount 每页数量
- * @param string $order 排序
- * @return array
- */
- public function getAdminList($where = [], $field = '*', $page = 1, $pageCount = 20, $order = 'add_time desc')
- {
- $data = $this
- ->alias('ro')
- ->field($field)
- ->leftJoin('user u', 'u.uid = ro.uid')
- ->when(!empty($where), function ($query) use ($where) {
- if (!empty($where['order_id'])) {
- $query->where('ro.order_id', $where['order_id']);
- }
- if (!empty($where['uid'])) {
- $query->where('ro.uid', $where['uid']);
- }
- if (!empty($where['nickname'])) {
- $query->whereLike('u.nickname', "%{$where['nickname']}%");
- }
- if (!empty($where['mobile'])) {
- $query->where('u.mobile', $where['mobile']);
- }
- if (!empty($where['pay_type'])) {
- $query->where('ro.pay_type', $where['pay_type']);
- }
- if (!empty($where['paid'])) {
- $query->where('ro.paid', $where['paid']);
- }
- if (!empty($where['status'])) {
- $query->where('ro.status', $where['status']);
- }
- if (!empty($where['time']) && !empty($where['time'][0]) && !empty($where['time'][1])) {
- $startTime = strtotime($where['time'][0]);
- $endTime = strtotime($where['time'][1]);
- $query->whereBetween('ro.add_time', "{$startTime},{$endTime}");
- }
- })
- ->order($order)
- ->paginate(['list_rows' => $pageCount, 'page' => $page])
- ->toArray();
- return [$data['total'], $data['data']];
- }
- /**
- * 关闭订单
- * @param string $orderSn 订单号
- * @return bool
- */
- public function closeOrder($orderSn)
- {
- return $this->where('order_id', $orderSn)->update([
- 'status' => -1,
- 'update_time' => time()
- ]);
- }
- }
|