123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- <?php
- namespace App\Http\Logic\User;
- use App\Exceptions\CommonException;
- use App\Http\Enum\ErrorEnum;
- use App\Http\Logic\BaseLogic;
- use App\Http\Utils\BaseUtil;
- use App\Http\Utils\WechatAccountUtil;
- use App\Models\UserCashModel;
- use App\Models\UserFinanceModel;
- use App\Models\UserModel;
- use App\Models\UserOrderCommissionModel;
- use Illuminate\Support\Facades\DB;
- class UserLogic extends BaseLogic
- {
- /**
- * 获取用户信息逻辑
- */
- public static function getUserInfoLogic()
- {
- $userId = BaseUtil::getUserId();
- $user = UserModel::query()
- ->where("id",$userId)
- ->select(["user_head_img_url","user_nickname","user_sex","user_balance","user_unreceive_balance","created_at","id"])
- ->first();
- $user["user_fans_num"] = UserModel::query()->where("invite_user_id",$userId)->count();
- $user["user_crash_balance"] = 0;//累计提现金额
- $user["user_unreceive_balance"] = UserOrderCommissionModel::query()
- ->where("user_id",$userId)
- ->where("commission_status",0)
- ->sum("order_commission");//带结算佣金
- $last_start_time = date("Y-m-",strtotime("-1 month"))."01 00:00:00";
- $last_end_time = date("Y-m")."-01 00:00:00";
- $start_time = date("Y-m")."-01 00:00:00";
- $end_time = date("Y-m-d H:i:s");
- $user["last_month_money"] = UserOrderCommissionModel::query()
- ->where("user_id",$userId)
- ->whereIn("commission_status",[0,1])
- ->where("created_at",">=",$last_start_time)
- ->where("created_at","<=",$last_end_time)
- ->sum("order_commission");
- $user["month_money"] = UserOrderCommissionModel::query()
- ->where("user_id",$userId)
- ->whereIn("commission_status",[0,1])
- ->where("created_at",">=",$start_time)
- ->where("created_at","<=",$end_time)
- ->sum("order_commission");
- return $user;
- }
- /**
- * 获取用户粉丝逻辑
- */
- public static function getFansListLogic()
- {
- $userId = BaseUtil::getUserId();
- $params = request()->all();
- $query = UserModel::query()
- ->where("invite_user_id",$userId)
- ->select(
- [
- "user_nickname",
- "user_head_img_url",
- "created_at"
- ]
- )->orderBy("id","desc");
- switch ($params["type"]){
- case 1:
- //未激活
- $query->where("user_order_type",0);
- break;
- case 2:
- //已激活
- $query->where("user_order_type",1);
- break;
- }
- $res = $query->paginate($params["page_size"]);
- return [
- "total"=>$res->total(),
- "list"=>$res->items()
- ];
- }
- /**
- * 收支明细逻辑
- */
- public static function getFinanceListLogic()
- {
- $userId = BaseUtil::getUserId();
- $params = request()->all();
- $query = UserFinanceModel::query()
- ->where("user_id",$userId);
- switch ($params["finance_type"]){
- case 1://收入
- $query->where("finance_type",$params["finance_type"]);
- break;
- case 2://提现
- $query->where("finance_type",$params["finance_type"]);
- break;
- }
- $res = $query
- ->select(
- [
- "finance_balance",
- "finance_type",
- "finance_number",
- "created_at",
- "cash_status",
- "id",
- "order_number"
- ]
- )
- ->paginate($params["page_size"]);
- return [
- "total"=>$res->total(),
- "list"=>$res->items()
- ];
- }
- /**
- * 流水详情逻辑
- */
- public static function getFinanceDetailLogic()
- {
- $userId = BaseUtil::getUserId();
- $params = request()->all();
- $finance = UserFinanceModel::query()
- ->where("user_id",$userId)
- ->select(
- [
- "finance_balance",
- "finance_remark",
- "finance_number",
- "created_at",
- "cash_status",
- "order_number"
- ]
- )->find($params["id"]);
- return $finance;
- }
- /**
- * 提现逻辑
- */
- public static function getCashLogic()
- {
- $params = request()->all();
- $crashBalance = $params["amount"];
- $userId = BaseUtil::getUserId();
- $user = UserModel::query()->find($userId);
- if($user->user_balance < $crashBalance){
- throw new CommonException(ErrorEnum::ERROR_CASH_ENUGH);
- }
- $key = "cashMoeny:".$userId;
- BaseUtil::setRedisLock($key,60);
- //开启事务
- DB::beginTransaction();
- //1.修改用户可提现金额
- $user = UserModel::query()->lockForUpdate()->find($userId);
- $userBalance = $user->user_balance;
- $user->user_balance -= $crashBalance;
- $userRes = $user->save();
- if(!$userRes){
- DB::rollBack();
- throw new CommonException(ErrorEnum::ERROR_CASH);
- }
- //2.写入流水记录
- $financeRes = UserFinanceModel::query()->insertGetId(
- [
- "user_id"=>$userId,
- "user_before_balance"=>$userBalance,
- "user_after_balance"=>$userBalance-$crashBalance,
- "finance_balance"=>$crashBalance,
- "finance_remark"=>"用户提现",
- "finance_type"=>2,
- "finance_number"=>time(),
- "created_at"=>date("Y-m-d H:i:s"),
- "updated_at"=>date("Y-m-d H:i:s"),
- ]
- );
- if(!$financeRes){
- DB::rollBack();
- throw new CommonException(ErrorEnum::ERROR_CASH);
- }
- //3.写入提现记录
- $cashRes = UserCashModel::query()->insert(
- [
- "user_id"=>$userId,
- "user_finance_id"=>$financeRes,
- "cash_money"=>$crashBalance,
- "cash_status"=>0,
- "check_status"=>1,
- "check_remark"=>"用户提现",
- "created_at"=>date("Y-m-d H:i:s"),
- "updated_at"=>date("Y-m-d H:i:s"),
- "cash_order_number"=>BaseUtil::getOrderNumber($userId)
- ]
- );
- if(!$cashRes){
- DB::rollBack();
- throw new CommonException(ErrorEnum::ERROR_CASH);
- }
- DB::commit();
- //发送企微通知
- BaseUtil::sendBaoJing("","用户【".$user["id"]."】提现,金额:".$crashBalance,"用户提现申请");
- }
- /**
- * 获取用户推广二维码逻辑
- */
- public static function getRecommendUrlLogic()
- {
- $userId = BaseUtil::getUserId();
- $params = request()->all();
- $app = WechatAccountUtil::getApp($params["code"]);
- return $app->qrcode->forever($userId);
- }
- }
|