| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- <?php
- /**
- *
- * @author: wuhaotian<442384644@qq.com>
- * @day: 2019/12/07
- */
- namespace app\admin\model\vote;
- use app\admin\model\user\User;
- use app\models\store\StoreOrderStatus;
- use app\models\store\StorePink;
- use app\models\user\UserBill;
- use app\models\user\UserSpread;
- use crmeb\basic\BaseModel;
- use crmeb\repositories\PaymentRepositories;
- use crmeb\traits\ModelTrait;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use think\db\Where;
- /**
- * Class UserGroup
- * @package app\admin\model\user
- */
- class VoteOrder extends BaseModel
- {
- /**
- * 数据表主键
- * @var string
- */
- protected $pk = 'id';
- /**
- * 模型名称
- * @var string
- */
- protected $name = 'vote_order';
- protected static $payType = ['weixin' => '微信支付', 'yue' => '余额支付', 'free' => '免费投票'];
- use ModelTrait;
- public static function valid()
- {
- return self::where('paid', 1);
- }
- public static function getValidList($id, $where)
- {
- $data = self::valid()
- ->where('vjid', $id)
- ->page((int)$where['page'], (int)$where['limit'])
- ->order('add_time', 'desc')->select()->each(function ($item) {
- $user = \app\admin\model\user\User::get($item['uid']);
- $item['user_name'] = $user['nickname'];
- $item['_add_time'] = date('Y-m-d H:i:s', $item['add_time']);
- $item['_pay_time'] = date('Y-m-d H:i:s', $item['pay_time']);
- });
- $count = $data->count();
- return compact('count', 'data');
- }
- public static function createOrder($uid, $id, $num, $payType)
- {
- self::beginTrans();
- try {
- if (!array_key_exists($payType, self::$payType)) return self::setErrorInfo('选择支付方式有误!', true);
- $userInfo = User::getUserInfo($uid);
- if (!$userInfo) return self::setErrorInfo('用户不存在!', true);
- $voteJoinInfo = VoteJoin::valid()->find($id);
- if (!$voteJoinInfo) return self::setErrorInfo('找不到投票对象', true);
- $voteInfo = Vote::valid()->find($voteJoinInfo['vid']);
- if (!$voteInfo) return self::setErrorInfo('找不到投票活动信息', true);
- if ($voteInfo['start_time'] > time()) {
- return app('json')->fail('活动未开始');
- }
- if ($voteInfo['end_time'] < time()) {
- return app('json')->fail('活动已结束');
- }
- $payPrice = bcmul($num, $voteInfo['price'], 2);
- $orderInfo = [
- 'uid' => $uid,
- 'vid' => $voteJoinInfo['vid'],
- 'vjid' => $id,
- 'vote_num' => $num,
- 'pay_price' => $payPrice,
- 'order_id' => self::getOrderId(),
- 'pay_type' => $payType,
- 'add_time' => time(),
- ];
- $order = self::create($orderInfo);
- if (!$order) return self::setErrorInfo('订单生成失败!', true);
- self::commitTrans();
- return $order;
- } catch (\PDOException $e) {
- self::rollbackTrans();
- return self::setErrorInfo('生成订单时SQL执行错误错误原因:' . $e->getMessage());
- } catch (\Exception $e) {
- self::rollbackTrans();
- return self::setErrorInfo('生成订单时系统错误错误原因:' . $e->getMessage());
- }
- }
- public static function jsPayPrice($order_id, $uid)
- {
- $orderInfo = self::where('uid', $uid)->where('order_id', $order_id)->find();
- if (!$orderInfo) return self::setErrorInfo('订单不存在!');
- if ($orderInfo['paid']) return self::setErrorInfo('该订单已支付!');
- $userInfo = \app\models\user\User::getUserInfo($uid);
- self::beginTrans();
- $res1 = UserBill::expend('参与投票', $uid, 'now_money', 'pay_vote', $orderInfo['pay_price'], $orderInfo['id'], $userInfo['now_money'], '微信支付' . floatval($orderInfo['pay_price']) . '元参与投票');
- $res2 = self::paySuccess($order_id, 'weixin');//微信支付为0时
- $res = $res1 && $res2;
- self::checkTrans($res);
- return $res;
- }
- public static function yuePay($order_id, $uid)
- {
- $orderInfo = self::where('uid', $uid)->where('order_id', $order_id)->find();
- if (!$orderInfo) return self::setErrorInfo('订单不存在!');
- if ($orderInfo['paid']) return self::setErrorInfo('该订单已支付!');
- $userInfo = \app\models\user\User::getUserInfo($uid);
- if ($userInfo['now_money'] < $orderInfo['pay_price'])
- return self::setErrorInfo(['status' => 'pay_deficiency', 'msg' => '余额不足' . floatval($orderInfo['pay_price'])]);
- self::beginTrans();
- $res1 = false !== User::bcDec($uid, 'now_money', $orderInfo['pay_price'], 'uid');
- $res2 = UserBill::expend('参与投票', $uid, 'now_money', 'pay_vote', $orderInfo['pay_price'], $orderInfo['id'], $userInfo['now_money'], '余额支付' . floatval($orderInfo['pay_price']) . '元参与投票');
- $res3 = self::paySuccess($order_id, 'yue');//余额支付成功
- $res = $res1 && $res2 && $res3;
- self::checkTrans($res);
- return $res;
- }
- public static function freePay($order_id, $uid)
- {
- $orderInfo = self::where('uid', $uid)->where('order_id', $order_id)->find();
- if (!$orderInfo) return self::setErrorInfo('订单不存在!');
- if ($orderInfo['paid']) return self::setErrorInfo('该订单已支付!');
- $userInfo = \app\models\user\User::getUserInfo($uid);
- if (Vote::where('id', $orderInfo['vid'])->value('day_free') < $orderInfo['vote_num'] + self::valid()->where('uid', $orderInfo['uid'])->where('vid', $orderInfo['vid'])->whereTime('pay_time', 'today')->sum('vote_num')) {
- return self::setErrorInfo(['status' => 'pay_deficiency', 'msg' => '免费票不足' . floatval($orderInfo['pay_price'])]);
- }
- self::beginTrans();
- //$res1 = false !== User::bcDec($uid, 'now_money', $orderInfo['pay_price'], 'uid');
- //$res2 = UserBill::expend('参与投票', $uid, 'now_money', 'pay_vote', $orderInfo['pay_price'], $orderInfo['id'], $userInfo['now_money'], '余额支付' . floatval($orderInfo['pay_price']) . '元参与投票');
- $res = self::paySuccess($order_id, 'free');//余额支付成功
- //$res = $res1 && $res2 && $res3;
- self::checkTrans($res);
- return $res;
- }
- /**
- * //TODO 支付成功后
- * @param $orderId
- * @param string $paytype
- * @param string $formId
- * @return bool
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function paySuccess($orderId, $paytype = 'weixin')
- {
- $order = self::where('order_id', $orderId)->find();
- $res1 = self::where('order_id', $orderId)->update(['paid' => 1, 'pay_type' => $paytype, 'pay_time' => time()]);//订单改为支付
- $res2 = VoteJoin::where('id', $order['vjid'])->inc('vote', $order['vote_num'])->update();
- $now_money = \app\models\user\User::where('uid', $order['uid'])->value('now_money');
- UserBill::expend('参与投票', $order['uid'], 'now_money', 'pay_money', $order['pay_price'], $order['id'], $now_money, '支付' . floatval($order['pay_price']) . '元参与投票');
- $res = $res1 && $res2;
- return false !== $res;
- }
- public static function getOrderId()
- {
- do {
- $str = 'vote' . time() . rand(1000, 9999);
- } while (self::be(['order_id' => $str]));
- return $str;
- }
- }
|