UserRechargeCard.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. namespace app\models\user;
  3. use app\models\store\StoreRechargeCard;
  4. use app\models\store\StoreRechargeCardBrokerage;
  5. use crmeb\basic\BaseModel;
  6. use crmeb\services\MiniProgramService;
  7. use crmeb\services\WechatService;
  8. use crmeb\traits\ModelTrait;
  9. /**
  10. * TODO 用户充值
  11. * Class UserRecharge
  12. * @package app\models\user
  13. */
  14. class UserRechargeCard extends BaseModel
  15. {
  16. /**
  17. * 数据表主键
  18. * @var string
  19. */
  20. protected $pk = 'id';
  21. /**
  22. * 模型名称
  23. * @var string
  24. */
  25. protected $name = 'user_recharge_card';
  26. use ModelTrait;
  27. protected $insert = ['add_time'];
  28. protected function setAddTimeAttr()
  29. {
  30. return time();
  31. }
  32. /**
  33. * 创建充值订单
  34. * @param $uid
  35. * @param $price
  36. * @param string $recharge_type
  37. * @param int $paid
  38. * @return UserRecharge|bool|\think\Model
  39. */
  40. public static function addRecharge($uid, $card_id, $price, $give_price = 0, $paid = 0, $is_brokerage = 0, $parent_uid, $status, $mer_id = '')
  41. {
  42. $order_no = self::getNewOrderId($uid);
  43. if (!$order_no) return self::setErrorInfo('订单编号生成失败!');
  44. $add_time = time();
  45. return self::create(compact('uid', 'order_no', 'card_id', 'price', 'give_price', 'paid', 'add_time', 'is_brokerage', 'parent_uid', 'status', 'mer_id'));
  46. }
  47. /**
  48. * 生成充值订单号
  49. * @param int $uid
  50. * @return bool|string
  51. */
  52. public static function getNewOrderId($uid = 0)
  53. {
  54. if (!$uid) return false;
  55. $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();
  56. return 'wx' . date('YmdHis', time()) . (10000 + $count + $uid);
  57. }
  58. /**
  59. * 充值js支付
  60. * @param $orderInfo
  61. * @param bool $mer_id
  62. * @return array|string
  63. * @throws \think\Exception
  64. * @throws \think\db\exception\DataNotFoundException
  65. * @throws \think\db\exception\DbException
  66. * @throws \think\db\exception\ModelNotFoundException
  67. */
  68. public static function jsPay($orderInfo, $mer_id = false)
  69. {
  70. return MiniProgramService::jsPay(WechatUser::uidToOpenid($orderInfo['uid']), $orderInfo['order_no'], $orderInfo['price'], 'user_recharge_card', '用户充值', '', 'JSAPI', [], $mer_id);
  71. }
  72. /**
  73. * //TODO用户充值成功后
  74. * @param $orderNo
  75. */
  76. public static function rechargeSuccess($orderNo)
  77. {
  78. $order = self::where('order_no', $orderNo)->where('paid', 0)->find();
  79. if (!$order) return false;
  80. $user = User::getUserInfo($order['uid']);
  81. $price = bcadd($order['price'], $order['give_price'], 2);
  82. $received_price = bcmul($price, (1-$order['refund_percent']), 2);
  83. // ============================支付完成=============================
  84. // 更改订单状态
  85. self::where('id', $order['id'])->update(['paid' => 1, 'pay_time' => time()]);
  86. // 创建用户账单表记录
  87. $mark = '成功充值余额' . floatval($order['price']) . '元' . ($order['give_price'] ? ',赠送' . $order['give_price'] . '元' : '');
  88. UserBill::income('用户余额充值', $order['uid'], 'now_money', 'recharge', $order['price'], $order['id'], $user['now_money'], $mark, 1, $order['mer_id']);
  89. // 更改用户余额
  90. User::edit(['now_money' => bcadd($user['now_money'], $price, 2)], $order['uid'], 'uid');
  91. // ============================充值返现=============================
  92. // 判断是否返现充值
  93. if($order['is_brokerage']){
  94. // ======================================给自己返佣==========================================
  95. $card_brokerage = StoreRechargeCardBrokerage::where(['cid' => $order['card_id'], 'role' => 1])->find();
  96. $recharge_card = StoreRechargeCard::where('id', $order['card_id'])->find();
  97. $refund_no = UserBrokerage::getNewRefundNo();
  98. $refund_data['pay_price'] = $order['price'];
  99. $refund_data['refund_price'] = number_format($order['price'] * $card_brokerage['scale'] / 100, 2);
  100. $refund_data['refund_price'] = number_format($refund_data['refund_price'] * (100 - $recharge_card['poundage']) / 100, 2);
  101. $refund_data['refund_id'] = $refund_no;
  102. // 微信支付给自己退款
  103. $res = MiniProgramService::payOrderRefund($order['order_no'], $refund_data, $order['mer_id']);
  104. // (需要判断微信退款是否成功)
  105. if($res){
  106. // 更改订单状态
  107. $refund_price = bcadd($order['refund_price'], $refund_data['refund_price'], 2);
  108. $refund_percent = $order['refund_percent'] + $card_brokerage['scale'];
  109. self::where('id', $order['id'])->update(['refund_price' => $refund_price, 'refund_percent' => $refund_percent]);
  110. // 创建用户返佣记录
  111. UserBrokerage::addUserBrokerage($order['uid'], $order['card_id'], $order['id'], 0, $refund_no, 1, $refund_data['refund_price'], $card_brokerage['scale'], $recharge_card['poundage'], $order['mer_id']);
  112. }
  113. // =====================================给推荐人返佣=======================================
  114. // 初始化
  115. $bool = true;
  116. // 判断是否有上级id
  117. if(!$order['parent_uid']) $bool = false;
  118. if($order['parent_uid'] == $order['uid']) $bool = false;
  119. if(!$order['status'] === 1) $bool = false;
  120. $parent_orderInfo = self::getParentOrderInfo($order);
  121. $parent_order = self::getParentOrder($order);
  122. $parent_recharge_card = StoreRechargeCard::where('id', $parent_order['card_id'])->find();
  123. // 循环判断返现比例处于哪一级
  124. $brokerageScale = StoreRechargeCardBrokerage::where('cid', $order['card_id'])->where('role', 2)->order('level asc')->select();
  125. if($parent_orderInfo && $brokerageScale && $bool){
  126. // 判断上级是否有下级返现记录
  127. if(empty($parent_orderInfo)){
  128. $i = 0;
  129. }else{
  130. $count = $brokerageScale[0]['num'];
  131. if($parent_orderInfo[4] < $count){
  132. $i = 0;
  133. }else{
  134. $i = 0;
  135. $count = $brokerageScale[$i]['num'];
  136. while($count <= $parent_orderInfo[4]){
  137. if($i >= count($brokerageScale)){
  138. $bool = false;
  139. break;
  140. }
  141. $i++;
  142. $count += $brokerageScale[$i]['num'];
  143. }
  144. if($i >= count($brokerageScale)){
  145. $bool = false;
  146. }
  147. }
  148. if(isset($brokerageScale[$i]) && $parent_orderInfo[0] + $brokerageScale[$i]['scale'] > 100){
  149. $bool = false;
  150. }
  151. }
  152. if($bool){
  153. $recharge_card = StoreRechargeCard::where('id', $order['card_id'])->find();
  154. $refund_no = UserBrokerage::getNewRefundNo();
  155. $parent_refund_data['pay_price'] = $parent_order['price'];
  156. $parent_refund_data['refund_price'] = $parent_order['price'] * $brokerageScale[$i]['scale'] / 100;
  157. $parent_refund_data['refund_price'] = $parent_refund_data['refund_price'] * (1 - $parent_recharge_card['poundage']);
  158. $parent_refund_data['refund_id'] = $refund_no;
  159. // 微信支付给邀请人退款
  160. $res = MiniProgramService::payOrderRefund($parent_order['order_no'], $parent_refund_data, $parent_order['mer_id']);
  161. if($res){
  162. // 更改订单状态
  163. $refund_price = bcadd($parent_order['refund_price'], $parent_refund_data['refund_price'], 2);
  164. $refund_percent = $parent_order['refund_percent'] + $brokerageScale[$i]['scale'];
  165. self::where('id', $parent_order['id'])->update(['refund_price' => $refund_price, 'refund_percent' => $refund_percent]);
  166. // 创建用户返佣记录
  167. UserBrokerage::addUserBrokerage($parent_order['uid'], $parent_order['card_id'], $order['id'], $parent_order['id'], $refund_no, 2, $parent_refund_data['refund_price'], $brokerageScale[$i]['scale'], $parent_recharge_card['poundage'], $parent_order['mer_id']);
  168. }
  169. }else{
  170. self::where('id', $parent_order['id'])->update(['status' => 2]);
  171. }
  172. }
  173. }
  174. return true;
  175. }
  176. /**
  177. * 获取用户和推荐人返佣信息
  178. */
  179. public static function getParentOrderInfo($order)
  180. {
  181. if(!$parentOrder = self::getParentOrder($order)) return self::setErrorInfo('推荐人订单不存在');
  182. list($brokerageSelf, $brokerageOther, $countSelf, $countOther) = UserBrokerage::getUserOrderBrokerageInfo($parentOrder);
  183. $totalPercent = 0;
  184. foreach($brokerageOther as $k => $v){
  185. $totalPercent += $v['refund_percent'];
  186. }
  187. return [$totalPercent, $brokerageSelf, $brokerageOther, $countSelf, $countOther];
  188. }
  189. /**
  190. * 获取邀请人最近的一条充值订单记录
  191. */
  192. public static function getParentOrder($order)
  193. {
  194. $model = new self;
  195. $model = $model->where('uid', $order['parent_uid']);
  196. $model = $model->where('card_id', $order['card_id']);
  197. $model = $model->where('paid', 1);
  198. $model = $model->where('is_brokerage', 1);
  199. $model = $model->where('status', 1);
  200. $model = $model->where('mer_id', $order['mer_id']);
  201. $model = $model->order('add_time desc');
  202. return $model->find();
  203. }
  204. /**
  205. * 获取用户充值记录
  206. */
  207. public static function getUsereRechargeCardList($where)
  208. {
  209. $model = self::setWhere($where, null, 'a', 'u.nickname')->join('user u', 'u.uid=a.uid')->join('store_recharge_card r', 'r.id=a.card_id')->field(['a.*', 'u.nickname', 'u.avatar', 'r.title as card_title']);
  210. $list = $model->page($where['page'], $where['limit'])->select();
  211. $list = count($list) ? $list->toArray() : [];
  212. foreach ($list as &$item) {
  213. $item['_pay_time'] = $item['pay_time'] ? date('Y-m-d H:i:s', $item['pay_time']) : '暂无';
  214. $item['_add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '暂无';
  215. $item['paid_type'] = $item['paid'] ? '已支付' : '未支付';
  216. $item['_recharge_type'] = $item['is_brokerage'] ? '返现充值' : '普通充值';
  217. $item['received_price'] = bcadd($item['price'], $item['give_price'], 2);
  218. }
  219. $count = self::setWhere($where, null, 'a', 'u.nickname')->join('user u', 'u.uid=a.uid')->join('store_recharge_card r', 'r.id=a.card_id')->count();
  220. return compact('list', 'count');
  221. }
  222. /**
  223. * 设置查询条件
  224. * @param $where
  225. * @param null $model
  226. * @param string $alias
  227. * @param string $join
  228. * @return $this
  229. */
  230. public static function setWhere($where, $model = null, $alias = '', $join = '')
  231. {
  232. $model = is_null($model) ? new self() : $model;
  233. if ($alias) {
  234. $model = $model->alias('a');
  235. $alias .= '.';
  236. }
  237. if (isset($where['data']) && $where['data']) $model = self::getModelTime($where, $model, "{$alias}add_time");
  238. if (isset($where['paid']) && $where['paid'] != '') $model = $model->where("{$alias}paid", $where['paid']);
  239. if (isset($where['nickname']) && $where['nickname']) $model = $model->where("{$alias}uid|{$alias}order_no" . ($join ? '|' . $join : ''), 'LIKE', "%$where[nickname]%");
  240. if (isset($where['mer_id']) && $where['mer_id']) $model = $model->where("{$alias}mer_id", $where['mer_id']);
  241. if (isset($where['uid']) && $where['uid']) $model = $model->where("{$alias}uid", $where['uid']);
  242. return $model->order("{$alias}add_time desc");
  243. }
  244. }