UserLogic.php 7.7 KB

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