UserLogic.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. namespace App\Http\Logic\User;
  3. use App\Exceptions\CommonException;
  4. use App\Http\Enum\ErrorEnum;
  5. use App\Http\Logic\BaseLogic;
  6. use App\Http\Utils\BaseUtil;
  7. use App\Http\Utils\WechatAccountUtil;
  8. use App\Models\UserCashModel;
  9. use App\Models\UserFinanceModel;
  10. use App\Models\UserModel;
  11. use App\Models\UserOrderCommissionModel;
  12. use Illuminate\Support\Facades\DB;
  13. class UserLogic extends BaseLogic
  14. {
  15. /**
  16. * 获取用户信息逻辑
  17. */
  18. public static function getUserInfoLogic()
  19. {
  20. $userId = BaseUtil::getUserId();
  21. $user = UserModel::query()
  22. ->where("id",$userId)
  23. ->select(["user_head_img_url","user_nickname","user_sex","user_balance","user_unreceive_balance","created_at","id"])
  24. ->first();
  25. $user["user_fans_num"] = UserModel::query()->where("invite_user_id",$userId)->count();
  26. $user["user_crash_balance"] = 0;//累计提现金额
  27. $user["user_unreceive_balance"] = UserOrderCommissionModel::query()
  28. ->where("user_id",$userId)
  29. ->where("commission_status",0)
  30. ->sum("order_commission");//带结算佣金
  31. $last_start_time = date("Y-m-",strtotime("-1 month"))."01 00:00:00";
  32. $last_end_time = date("Y-m")."-01 00:00:00";
  33. $start_time = date("Y-m")."-01 00:00:00";
  34. $end_time = date("Y-m-d H:i:s");
  35. $user["last_month_money"] = UserOrderCommissionModel::query()
  36. ->where("user_id",$userId)
  37. ->whereIn("commission_status",[0,1])
  38. ->where("created_at",">=",$last_start_time)
  39. ->where("created_at","<=",$last_end_time)
  40. ->sum("order_commission");
  41. $user["month_money"] = UserOrderCommissionModel::query()
  42. ->where("user_id",$userId)
  43. ->whereIn("commission_status",[0,1])
  44. ->where("created_at",">=",$start_time)
  45. ->where("created_at","<=",$end_time)
  46. ->sum("order_commission");
  47. return $user;
  48. }
  49. /**
  50. * 获取用户粉丝逻辑
  51. */
  52. public static function getFansListLogic()
  53. {
  54. $userId = BaseUtil::getUserId();
  55. $params = request()->all();
  56. $query = UserModel::query()
  57. ->where("invite_user_id",$userId)
  58. ->select(
  59. [
  60. "user_nickname",
  61. "user_head_img_url",
  62. "created_at"
  63. ]
  64. )->orderBy("id","desc");
  65. switch ($params["type"]){
  66. case 1:
  67. //未激活
  68. $query->where("user_order_type",0);
  69. break;
  70. case 2:
  71. //已激活
  72. $query->where("user_order_type",1);
  73. break;
  74. }
  75. $res = $query->paginate($params["page_size"]);
  76. return [
  77. "total"=>$res->total(),
  78. "list"=>$res->items()
  79. ];
  80. }
  81. /**
  82. * 收支明细逻辑
  83. */
  84. public static function getFinanceListLogic()
  85. {
  86. $userId = BaseUtil::getUserId();
  87. $params = request()->all();
  88. $query = UserFinanceModel::query()
  89. ->where("user_id",$userId);
  90. switch ($params["finance_type"]){
  91. case 1://收入
  92. $query->where("finance_type",$params["finance_type"]);
  93. break;
  94. case 2://提现
  95. $query->where("finance_type",$params["finance_type"]);
  96. break;
  97. }
  98. $res = $query
  99. ->select(
  100. [
  101. "finance_balance",
  102. "finance_type",
  103. "finance_number",
  104. "created_at",
  105. "cash_status",
  106. "id",
  107. "order_number"
  108. ]
  109. )
  110. ->paginate($params["page_size"]);
  111. return [
  112. "total"=>$res->total(),
  113. "list"=>$res->items()
  114. ];
  115. }
  116. /**
  117. * 流水详情逻辑
  118. */
  119. public static function getFinanceDetailLogic()
  120. {
  121. $userId = BaseUtil::getUserId();
  122. $params = request()->all();
  123. $finance = UserFinanceModel::query()
  124. ->where("user_id",$userId)
  125. ->select(
  126. [
  127. "finance_balance",
  128. "finance_remark",
  129. "finance_number",
  130. "created_at",
  131. "cash_status",
  132. "order_number"
  133. ]
  134. )->find($params["id"]);
  135. return $finance;
  136. }
  137. /**
  138. * 提现逻辑
  139. */
  140. public static function getCashLogic()
  141. {
  142. $params = request()->all();
  143. $crashBalance = $params["amount"];
  144. $userId = BaseUtil::getUserId();
  145. $user = UserModel::query()->find($userId);
  146. if($user->user_balance < $crashBalance){
  147. throw new CommonException(ErrorEnum::ERROR_CASH_ENUGH);
  148. }
  149. $key = "cashMoeny:".$userId;
  150. BaseUtil::setRedisLock($key,60);
  151. //开启事务
  152. DB::beginTransaction();
  153. //1.修改用户可提现金额
  154. $user = UserModel::query()->lockForUpdate()->find($userId);
  155. $userBalance = $user->user_balance;
  156. $user->user_balance -= $crashBalance;
  157. $userRes = $user->save();
  158. if(!$userRes){
  159. DB::rollBack();
  160. throw new CommonException(ErrorEnum::ERROR_CASH);
  161. }
  162. //2.写入流水记录
  163. $financeRes = UserFinanceModel::query()->insertGetId(
  164. [
  165. "user_id"=>$userId,
  166. "user_before_balance"=>$userBalance,
  167. "user_after_balance"=>$userBalance-$crashBalance,
  168. "finance_balance"=>$crashBalance,
  169. "finance_remark"=>"用户提现",
  170. "finance_type"=>2,
  171. "finance_number"=>time(),
  172. "created_at"=>date("Y-m-d H:i:s"),
  173. "updated_at"=>date("Y-m-d H:i:s"),
  174. ]
  175. );
  176. if(!$financeRes){
  177. DB::rollBack();
  178. throw new CommonException(ErrorEnum::ERROR_CASH);
  179. }
  180. //3.写入提现记录
  181. $cashRes = UserCashModel::query()->insert(
  182. [
  183. "user_id"=>$userId,
  184. "user_finance_id"=>$financeRes,
  185. "cash_money"=>$crashBalance,
  186. "cash_status"=>0,
  187. "check_status"=>1,
  188. "check_remark"=>"用户提现",
  189. "created_at"=>date("Y-m-d H:i:s"),
  190. "updated_at"=>date("Y-m-d H:i:s"),
  191. "cash_order_number"=>BaseUtil::getOrderNumber($userId)
  192. ]
  193. );
  194. if(!$cashRes){
  195. DB::rollBack();
  196. throw new CommonException(ErrorEnum::ERROR_CASH);
  197. }
  198. DB::commit();
  199. //发送企微通知
  200. BaseUtil::sendBaoJing("","用户【".$user["id"]."】提现,金额:".$crashBalance,"用户提现申请");
  201. }
  202. /**
  203. * 获取用户推广二维码逻辑
  204. */
  205. public static function getRecommendUrlLogic()
  206. {
  207. $userId = BaseUtil::getUserId();
  208. $params = request()->all();
  209. $app = WechatAccountUtil::getApp($params["code"]);
  210. return $app->qrcode->forever($userId);
  211. }
  212. }