UserRecharge.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2018/01/05
  6. */
  7. namespace app\models\user;
  8. use crmeb\basic\BaseModel;
  9. use crmeb\services\MiniProgramService;
  10. use crmeb\services\WechatService;
  11. use crmeb\traits\ModelTrait;
  12. /**
  13. * TODO 用户充值
  14. * Class UserRecharge
  15. * @package app\models\user
  16. */
  17. class UserRecharge extends BaseModel
  18. {
  19. /**
  20. * 数据表主键
  21. * @var string
  22. */
  23. protected $pk = 'id';
  24. /**
  25. * 模型名称
  26. * @var string
  27. */
  28. protected $name = 'user_recharge';
  29. use ModelTrait;
  30. protected $insert = ['add_time'];
  31. protected function setAddTimeAttr()
  32. {
  33. return time();
  34. }
  35. /**
  36. * 创建充值订单
  37. * @param $uid
  38. * @param $price
  39. * @param string $recharge_type
  40. * @param int $paid
  41. * @return UserRecharge|bool|\think\Model
  42. */
  43. public static function addRecharge($uid, $price, $recharge_type = 'weixin', $give_price = 0, $paid = 0, $mer_id = '')
  44. {
  45. $order_id = self::getNewOrderId($uid);
  46. if (!$order_id) return self::setErrorInfo('订单生成失败!');
  47. $add_time = time();
  48. return self::create(compact('order_id', 'uid', 'price', 'recharge_type', 'paid', 'add_time', 'give_price', 'mer_id'));
  49. }
  50. /**
  51. * 生成充值订单号
  52. * @param int $uid
  53. * @return bool|string
  54. */
  55. public static function getNewOrderId($uid = 0)
  56. {
  57. if (!$uid) return false;
  58. $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();
  59. return 'wx' . date('YmdHis', time()) . (10000 + $count + $uid);
  60. }
  61. /**
  62. * 充值js支付
  63. * @param $orderInfo
  64. * @param bool $mer_id
  65. * @return array|string
  66. * @throws \think\Exception
  67. * @throws \think\db\exception\DataNotFoundException
  68. * @throws \think\db\exception\DbException
  69. * @throws \think\db\exception\ModelNotFoundException
  70. */
  71. public static function jsPay($orderInfo, $mer_id = false)
  72. {
  73. return MiniProgramService::jsPay(WechatUser::uidToOpenid($orderInfo['uid']), $orderInfo['order_id'], $orderInfo['price'], 'user_recharge', '用户充值', '', 'JSAPI', [], $mer_id);
  74. }
  75. /**
  76. * 微信H5支付
  77. * @param $orderInfo
  78. * @return mixed
  79. */
  80. public static function wxH5Pay($orderInfo)
  81. {
  82. return WechatService::paymentPrepare(null, $orderInfo['order_id'], $orderInfo['price'], 'user_recharge', '用户充值', '', 'MWEB');
  83. }
  84. /**
  85. * 公众号支付
  86. * @param $orderInfo
  87. * @return array|string
  88. * @throws \Exception
  89. */
  90. public static function wxPay($orderInfo)
  91. {
  92. return WechatService::jsPay(WechatUser::uidToOpenid($orderInfo['uid'], 'openid'), $orderInfo['order_id'], $orderInfo['price'], 'user_recharge', '用户充值');
  93. }
  94. /**
  95. * //TODO用户充值成功后
  96. * @param $orderId
  97. */
  98. public static function rechargeSuccess($orderId)
  99. {
  100. $order = self::where('order_id', $orderId)->where('paid', 0)->find();
  101. if (!$order) return false;
  102. $user = User::getUserInfo($order['uid']);
  103. self::beginTrans();
  104. $price = bcadd($order['price'], $order['give_price'], 2);
  105. $res1 = self::where('order_id', $order['order_id'])->update(['paid' => 1, 'pay_time' => time()]);
  106. $mark = '成功充值余额' . floatval($order['price']) . '元' . ($order['give_price'] ? ',赠送' . $order['give_price'] . '元' : '');
  107. $res2 = UserBill::income('用户余额充值', $order['uid'], 'now_money', 'recharge', $order['price'], $order['id'], $user['now_money'], $mark, 1, $order['mer_id']);
  108. $res3 = User::edit(['now_money' => bcadd($user['now_money'], $price, 2)], $order['uid'], 'uid');
  109. $res = $res1 && $res2 && $res3;
  110. self::checkTrans($res);
  111. // MiniProgramService::profit_sharing_finish($order['transaction_id'], $order['order_id'], $order['mer_id']);
  112. event('RechargeSuccess', [$order]);
  113. return $res;
  114. }
  115. public static function rechargeTest($orderId)
  116. {
  117. $order = self::where('order_id', $orderId)->where('paid', 0)->find();
  118. if (!$order) return false;
  119. $user = User::getUserInfo($order['uid']);
  120. $price = bcadd($order['price'], $order['give_price'], 2);
  121. $res1 = self::where('order_id', $order['order_id'])->update(['paid' => 1, 'pay_time' => time()]);
  122. $mark = '成功充值余额' . floatval($order['price']) . '元' . ($order['give_price'] ? ',赠送' . $order['give_price'] . '元' : '');
  123. $res2 = UserBill::income('用户余额充值', $order['uid'], 'now_money', 'recharge', $order['price'], $order['id'], $user['now_money'], $mark, 1, $order['mer_id']);
  124. event('RechargeSuccess', [$order]);
  125. }
  126. /**
  127. * 导入佣金到余额
  128. * @param $uid 用户uid
  129. * @param $price 导入金额
  130. * @return bool
  131. * @throws \think\Exception
  132. * @throws \think\db\exception\DataNotFoundException
  133. * @throws \think\db\exception\ModelNotFoundException
  134. * @throws \think\exception\DbException
  135. */
  136. public static function importNowMoney($uid, $price, $mer_id = '')
  137. {
  138. $user = User::getUserInfo($uid);
  139. self::beginTrans();
  140. try {
  141. $broken_time = intval(sys_config('extract_time'));
  142. $search_time = time() - 86400 * $broken_time;
  143. //返佣 +
  144. $brokerage_commission = UserBill::where(['uid' => $uid, 'category' => 'now_money', 'type' => 'brokerage'])
  145. ->where('add_time', '>', $search_time)
  146. ->where('pm', 1)
  147. ->sum('number');
  148. //退款退的佣金 -
  149. $refund_commission = UserBill::where(['uid' => $uid, 'category' => 'now_money', 'type' => 'brokerage'])
  150. ->where('add_time', '>', $search_time)
  151. ->where('pm', 0)
  152. ->sum('number');
  153. $broken_commission = bcsub($brokerage_commission, $refund_commission, 2);
  154. if ($broken_commission < 0)
  155. $broken_commission = 0;
  156. $commissionCount = bcsub($user['brokerage_price'], $broken_commission, 2);
  157. if ($price > $commissionCount) return self::setErrorInfo('转入金额不能大于可提现佣金!');
  158. $res1 = User::bcInc($uid, 'now_money', $price, 'uid');
  159. $res3 = User::bcDec($uid, 'brokerage_price', $price, 'uid');
  160. $res2 = UserBill::expend('用户佣金转入余额', $uid, 'now_money', 'recharge', $price, 0, $user['now_money'], '成功转入余额' . floatval($price) . '元');
  161. $extractInfo = [
  162. 'uid' => $uid,
  163. 'real_name' => $user['nickname'],
  164. 'extract_type' => 'balance',
  165. 'extract_price' => $price,
  166. 'balance' => bcsub($user['brokerage_price'], $price, 2),
  167. 'add_time' => time(),
  168. 'status' => 1,
  169. 'mer_id' => $mer_id
  170. ];
  171. $res4 = UserExtract::create($extractInfo);
  172. $res = $res2 && $res1 && $res3;
  173. self::checkTrans($res);
  174. if ($res) {
  175. event('ImportNowMoney', [$uid, $price]);
  176. }
  177. return $res;
  178. } catch (\Exception $e) {
  179. self::rollbackTrans();
  180. return self::setErrorInfo($e->getMessage());
  181. }
  182. }
  183. /**
  184. * 查找用户充值金额记录
  185. * @param $where
  186. * @return array
  187. */
  188. public static function getUserRechargeList($where)
  189. {
  190. $model = self::setWhere($where, null, 'a', 'u.nickname')->join('user u', 'u.uid=a.uid')->field(['a.*', 'u.nickname', 'u.avatar']);
  191. $list = $model->page($where['page'], $where['limit'])->select();
  192. $list = count($list) ? $list->toArray() : [];
  193. foreach ($list as &$item) {
  194. switch ($item['recharge_type']) {
  195. case 'routine':
  196. $item['_recharge_type'] = '小程序充值';
  197. break;
  198. case 'weixin':
  199. $item['_recharge_type'] = '公众号充值';
  200. break;
  201. default:
  202. $item['_recharge_type'] = '其他充值';
  203. break;
  204. }
  205. $item['_pay_time'] = $item['pay_time'] ? date('Y-m-d H:i:s', $item['pay_time']) : '暂无';
  206. $item['_add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '暂无';
  207. $item['paid_type'] = $item['paid'] ? '已支付' : '未支付';
  208. }
  209. $count = self::setWhere($where, null, 'a', 'u.nickname')->join('user u', 'u.uid=a.uid')->count();
  210. return compact('list', 'count');
  211. }
  212. /**
  213. * 设置查询条件
  214. * @param $where
  215. * @param null $model
  216. * @param string $alias
  217. * @param string $join
  218. * @return $this
  219. */
  220. public static function setWhere($where, $model = null, $alias = '', $join = '')
  221. {
  222. $model = is_null($model) ? new self() : $model;
  223. if ($alias) {
  224. $model = $model->alias('a');
  225. $alias .= '.';
  226. }
  227. if ($where['data']) $model = self::getModelTime($where, $model, "{$alias}add_time");
  228. if ($where['paid'] != '') $model = $model->where("{$alias}paid", $where['paid']);
  229. if ($where['nickname']) $model = $model->where("{$alias}uid|{$alias}order_id" . ($join ? '|' . $join : ''), 'LIKE', "%$where[nickname]%");
  230. if (isset($where['mer_id']) && !empty($where['mer_id'])) $model = $model->where("{$alias}mer_id", $where['mer_id']);
  231. return $model->order("{$alias}add_time desc");
  232. }
  233. //导出数据
  234. public static function exportData($where)
  235. {
  236. return self::setWhere($where, null, 'a', 'u.nickname')->join('user u', 'u.uid=a.uid')->field(['a.*', 'u.nickname', 'u.avatar'])->select()->toArray();
  237. }
  238. /**
  239. * 获取用户充值数据
  240. * @param $where
  241. * @return array
  242. */
  243. public static function getDataList($where)
  244. {
  245. return [
  246. [
  247. 'name' => '充值总金额',
  248. 'field' => '元',
  249. 'count' => self::setWhere($where, null, 'a', 'u.nickname')->join('user u', 'u.uid=a.uid')->sum('a.price'),
  250. 'className' => 'logo-yen',
  251. 'col' => 6,
  252. ],
  253. [
  254. 'name' => '充值退款金额',
  255. 'field' => '元',
  256. 'count' => self::setWhere($where, null, 'a', 'u.nickname')->join('user u', 'u.uid=a.uid')->sum('a.refund_price'),
  257. 'className' => 'logo-usd',
  258. 'col' => 6,
  259. ],
  260. [
  261. 'name' => '小程序充值金额',
  262. 'field' => '元',
  263. 'count' => self::setWhere($where, null, 'a', 'u.nickname')->where('a.recharge_type', 'routine')->join('user u', 'u.uid=a.uid')->sum('a.price'),
  264. 'className' => 'logo-bitcoin',
  265. 'col' => 6,
  266. ],
  267. [
  268. 'name' => '公众号充值金额',
  269. 'field' => '元',
  270. 'count' => self::setWhere($where, null, 'a', 'u.nickname')->where('a.recharge_type', 'weixin')->join('user u', 'u.uid=a.uid')->sum('a.price'),
  271. 'className' => 'ios-bicycle',
  272. 'col' => 6,
  273. ],
  274. ];
  275. }
  276. }