* @day: 2019/12/07
*/
namespace app\models\member;
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 MemberCheck extends BaseModel
{
/**
* 数据表主键
* @var string
*/
protected $pk = 'id';
/**
* 模型名称
* @var string
*/
protected $name = 'member_check';
protected static $payType = ['weixin' => '微信支付', 'yue' => '余额支付'];
use ModelTrait;
public static function valid()
{
return self::where('paid', 1);
}
public static function getValidList($where)
{
$data = self::valid()
->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 = self::valid()->count();
return compact('count', 'data');
}
/**
* @param $where
* @return array
*/
public static function getList($where)
{
$model = self::where('paid', $where['paid']);
if (isset($where['search']) && $where['search'] != '') $model = $model->where('name|iacard', "%{$where['search']}%");
$data = $model->page((int)$where['page'], (int)$where['limit'])->order('status asc,add_time desc')->select()->each(function ($item) {
$item['_add_time'] = date('Y-m-d H:i:s', $item['add_time']);
$item['_valid_time'] = date('Y-m-d H:i:s', $item['valid_time']);
$item['_pay_time'] = date('Y-m-d H:i:s', $item['pay_time']);
$item['_pay_time'] = date('Y-m-d H:i:s', $item['pay_time']);
$item['_type'] = $item['type'] == 1 ? "个人会员" : "企业会员";
$item['grade_info'] = MemberGrade::where('grade=' . $item['grade'])->where('type=' . $item['type'])->find();
$item['grade_name'] = $item['grade_info']['name'];
$item['status_txt'] = ($item['status'] == 0 ? "待审核" : ($item['status'] == 1 ? "通过" : "驳回"));
if ($item['status'] == 1) $item['status_txt'] .= '
有效期至:
' . date('Y-m-d', $item['valid_time']);
});
$count = $model->count();
return compact('count', 'data');
}
public static function createOrder($param, $uid, $pay_type, $from)
{
$type = $param['type'] ?? '';
$name = $param['name'] ?? '';
$idcard = $param['id'] ?? '';
$phone = $param['phone'] ?? '';
$location = $param['location'] ?? '';
$detail_address = $param['address'] ?? '';
$job = $param['occupation'] ?? '';
$job_year = $param['careerYears'] ?? '';
$saver = $param['sponsor'] ?? '';
$logo = $param['img'] ?? '';
$grade = $param['grade'] ?? '';
$location = explode('/', $location);
$province = $location[0] ?? '';
$city = $location[1] ?? '';
$area = $location[2] ?? '';
self::beginTrans();
try {
if (!array_key_exists($pay_type, self::$payType)) return self::setErrorInfo('选择支付方式有误!', true);
$userInfo = User::getUserInfo($uid);
if (!$userInfo) return self::setErrorInfo('用户不存在!', true);
if (!$grade || !$uid || !$type || !$name || !$idcard || !$phone || !$province || !$city || !$area || !$detail_address || !$job || !$job_year || !$saver || !$logo) {
return self::setErrorInfo('参数不完整', true);
}
$member = MemberGrade::where(['type' => $type, 'grade' => $grade])->find();
if (!$member) self::setErrorInfo('会员类型不存在', true);
if (self::where('idcard', $idcard)->where('paid', 1)->where('status', 'in', '0,1')->find())
return self::setErrorInfo('已有同身份证/企业代码的记录', true);
$add_time = time();
$pay_money = $member['price'];
$order_id = self::getOrderId();
$order = self::create(compact('pay_type', 'from', 'order_id', 'grade', 'add_time', 'pay_money', 'saver', 'logo', 'uid', 'type', 'name', 'idcard', 'phone', 'province', 'city', 'area', 'detail_address', 'job', 'job_year'));
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_member', $orderInfo['pay_money'], $orderInfo['id'], $userInfo['now_money'], '微信支付' . floatval($orderInfo['pay_money']) . '元申请会员');
$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_money'])
return self::setErrorInfo(['status' => 'pay_deficiency', 'msg' => '余额不足' . floatval($orderInfo['pay_money'])]);
self::beginTrans();
$res1 = false !== User::bcDec($uid, 'now_money', $orderInfo['pay_money'], 'uid');
$res2 = UserBill::expend('申请会员', $uid, 'now_money', 'pay_member', $orderInfo['pay_money'], $orderInfo['id'], $userInfo['now_money'], '余额支付' . floatval($orderInfo['pay_money']) . '元申请会员');
$res3 = self::paySuccess($order_id, 'yue');//余额支付成功
$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()]);//订单改为支付
$now_money = \app\models\user\User::where('uid', $order['uid'])->value('now_money');
UserBill::expend('申请会员', $order['uid'], 'now_money', 'pay_money', $order['pay_money'], $order['id'], $now_money, '支付' . floatval($order['pay_money']) . '元申请会员');
return false !== $res1;
}
public static function getOrderId()
{
do {
$str = 'mem' . time() . rand(1000, 9999);
} while (self::be(['order_id' => $str]));
return $str;
}
}