jingcai-php/app/Http/Controllers/Api/Seller/CustomerController.php

726 lines
26 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Http\Controllers\Api\Seller;
use App\Enums\BillType;
use App\Enums\BoolEnum;
use App\Enums\LottState;
use App\Enums\PayState;
use App\Enums\WithdrawState;
use App\Exceptions\JingCaiException;
use App\Http\RequestValidators\BalanceChangeValidator;
use App\Model\Config;
use App\Model\Customer\Customer;
use App\Model\Customer\CustomerBill;
use App\Model\Customer\CustomerWithdraw;
use App\Model\Order;
use App\Model\Seller\Seller;
use App\Model\Seller\Shop;
use App\Service\CustomerWalletService;
use App\Service\SellerWalletService;
use App\Utils\ThrowException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class CustomerController extends BaseController
{
/**
* @api {GET} /api/seller/customer/list 用户管理-用户列表
* @apiVersion 0.1.0
* @apiGroup 店主
*
* @apiParam {Int} type 2下单和星标用户其他全部用户
* @apiParam {Int} [agent] 1代理用户其他不生效
* @apiParam {String} [sort] 倒序register注册时间默认balance余额(全部用户生效)
* @apiParam {String} [keyword] 昵称/手机号/真是姓名搜索
*
* @apiSuccessExample {json} 返回结果
* {
* "code": 200,
* "message": "",
* "data": {
* "data": [
* {
* "id": 2, // ID
* "shop_id": 1,
* "phone": "13511111111" , // 手机号
* "name": "",
* "nickname": "", // 昵称
* "password_pay": "",
* "balance": "10000.00", // 余额
* "remark": "", // 店主备注名称
* "win_alway_num": 0,
* "win_lead_num": 0,
* "star": 1, // 1星标用户0非星标用户
* "agent": 0,
* "avatar": "",
* "seven_hit": "",
* "profit_rate": "",
* "fans_num": 0,
* "follower_num": 0,
* "pay_time": 1, // 下单时间
* "pay_money": "0.00", // 下单金额
* "lottery_state": 0,
* "lottery_state_name": "" // 订单状态
* "deleted_at": null,
* "level_name": "储备组长",
* "level_score_group": "5000"
* }
* ],
* "per_page": 20,
* "to": 1,
* "total": 1
* }
* }
*/
public function list(Request $request)
{
$size = $request->input('size');
$type = $request->input('type');
$sort = $request->input('sort');
$keyword = $request->input('keyword');
$agent = $request->input('agent');
$query = Customer::where('shop_id', $this->shopId());
if ($agent == BoolEnum::YES) {
$query->where('agent', BoolEnum::YES);
}
if ($keyword) {
$query->where(function ($query) use ($keyword) {
$query->where('name', 'like', '%' . $keyword . '%')
->orWhere('nickname', 'like', '%' . $keyword . '%')
->orWhere('phone', 'like', '%' . $keyword . '%');
});
}
// 下单和星标用户
if ($type == 2) {
$list = $query->where(function ($query) {
$query->where('star', BoolEnum::YES)
->orWhere('pay_time', '>', 0);
})
->orderBy('star', 'desc')
->orderBy('pay_time', 'desc')
->paging($size);
return $this->jsonSuccess($list);
}
$query->orderBy('star', 'desc');
if (!$sort || $sort == 'balance') {
$query->orderBy('balance', 'desc');
}
if ($sort == 'register') {
$query->orderBy('created_at', 'desc');
}
$list = $query->paging($size);
return $this->jsonSuccess($list);
}
/**
* @api {GET} /api/seller/customer/list 用户管理-用户详情
* @apiVersion 0.1.0
* @apiGroup 店主
*
* @apiParam {Int} customer_id 彩民id
* @apiSuccessExample {json} 返回结果
* {
* "code": 200,
* "message": "",
* "data": {
* "id": 2, // ID
* "shop_id": 1,
* "phone": "13511111111" , // 手机号
* "name": "",
* "nickname": "", // 昵称
* "password_pay": "",
* "balance": "10000.00", // 余额
* "remark": "", // 店主备注名称
* "win_alway_num": 0,
* "win_lead_num": 0,
* "star": 1, // 1星标用户0非星标用户
* "agent": 0,
* "avatar": "",
* "seven_hit": "",
* "profit_rate": "",
* "fans_num": 0,
* "follower_num": 0,
* "pay_time": 1, // 下单时间
* "pay_money": "0.00", // 下单金额
* "lottery_state": 0,
* "lottery_state_name": "" // 订单状态
* "deleted_at": null,
* "level_name": "储备组长",
* "level_score_group": "5000"
* "client_type_name": 注册来源
* "agentor": { // 当前用户的代理
* "id": 4,
* "avatar": "",
* "nickname": "",
* "remark": "",
* "name": "",
* "created_at": "2023-04-10T12:23:12.000000Z",
* "level_name": "储备组长",
* "level_score_group": "5000",
* "lottery_state_name": ""
* }
* }
* }
*/
public function show(Request $request)
{
$customerId = $request->input('customer_id');
ThrowException::isTrue(!$customerId, '请选择要查看的用户');
$customer = Customer::with('agentor:id,avatar,nickname,remark,name,created_at')
->where('shop_id', $this->shopId())->find($customerId);
ThrowException::isTrue(!$customer, '用户不存在');
return $this->jsonSuccess($customer);
}
/**
* @api {POST} /api/seller/customer/update 用户管理-修改用户信息
* @apiVersion 0.1.0
* @apiGroup 店主
*
* @apiParam {Int} customer_id 彩民ID
* @apiParam {Int} [star] 1标记0取消标记
* @apiParam {String} [remark] 用户备注
* @apiParam {Int} [agent] 1代理用户0非代理用户
* @apiParam {Int} [agent_brokerage] 代理佣金(设置agent时必填1-100)
* @apiParam {Int} [agent_id] 设置当前用户的上级代理
*
* @apiSuccessExample {json} 返回结果
* {
* "code": 200,
* "message": "",
* "data": []
* }
*
*/
public function update(Request $request)
{
$customerId = $request->input('customer_id');
$star = $request->input('star');
$remark = $request->input('remark');
$agent = $request->input('agent');
$agentBrokerage = $request->input('agent_brokerage');
$agentId = $request->input('agent_id');
ThrowException::isTrue(!$customerId, '用户id不能为空');
$customer = Customer::find($customerId);
ThrowException::isTrue($customer->shop_id != $this->shopId(), '非本店用户不能标记');
if (BoolEnum::hasValue($star)) {
$customer->star = $star;
}
if (BoolEnum::hasValue($agent)) {
if ($agent == BoolEnum::YES && $this->seller()->agentNum() >= Config::agentMaxNum()) {
ThrowException::run(sprintf('设置销售失败,销售人数不能超过%d位', Config::agentMaxNum()));
}
$customer->agent = $agent;
if ($agentBrokerage !== null) {
ThrowException::isTrue($agentBrokerage < 1 || $agentBrokerage > 100, '请设置正确销售佣金');
$customer->agent_brokerage = $agentBrokerage;
}
if (!$customer->agent_at) {
$customer->agent_at = date('Y-m-d H:i:s');
}
}
if ($remark !== null) {
ThrowException::isTrue(mb_strlen($remark) < 3, '备注长度至少3位');
$customer->remark = $remark;
}
if ($agentId) {
$customerAgent = Customer::find($agentId);
ThrowException::isTrue($customerAgent->agent != BoolEnum::YES, '请选择有销售权限的用户');
$customer->agent_id = $agentId;
}
$customer->save();
return $this->jsonSuccess();
}
/**
* @api {POST} /api/seller/customer/reset_password 用户管理-重置密码
* @apiVersion 0.1.0
* @apiGroup 店主
*
* @apiSuccessExample {json} 返回结果
* {
* "code": 200,
* "message": "",
* "data": []
* }
*/
public function resetPassword(Request $request)
{
$customerId = $request->input('customer_id');
$customer = Customer::where('shop_id', $this->shopId())->find($customerId);
ThrowException::isTrue(!$customer, '用户不存在');
$customer->password = Customer::encryPassword(Config::defaultPassword());
$customer->save();
return $this->jsonSuccess(Config::defaultPassword());
}
/**
* @api {POST} /api/seller/customer/reset_pay_password 用户管理-重置支付密码
* @apiVersion 0.1.0
* @apiGroup 店主
*
* @apiSuccessExample {json} 返回结果
* {
* "code": 200,
* "message": "",
* "data": []
* }
*/
public function resetPayPassword(Request $request)
{
$customerId = $request->input('customer_id');
$customer = Customer::where('shop_id', $this->shopId())->find($customerId);
ThrowException::isTrue(!$customer, '用户不存在');
$customer->password_pay = Customer::encryPassword(Config::defaultPayPassword());
$customer->save();
return $this->jsonSuccess(Config::defaultPayPassword());
}
/**
* @api {POST} /api/seller/customer/bills 用户管理-账户明细
* @apiVersion 0.1.0
* @apiGroup 店主
*
* @apiParam {Int} customer_id 彩民id
* @apiParam {String} [date_start] 开始日期(2001-01-01),默认本月第一天
* @apiParam {String} [date_end] 结束日期(2001-01-01),默认当前
* @apiParam {Int} [bill_type] bill_types中的bill_type值
* @apiParam {Int} [page] 页数
* @apiParam {Int} [size] 每页条数默认20
*
* @apiSuccessExample {json} 返回结果
* {
* "code": 200,
* "message": "",
* "data": {
* "bill_types": [ // 交易明细类型
* {
* "name": "全部",
* "bill_type": 0
* },
* {
* "name": "充值",
* "bill_type": 1
* },
* {
* "name": "投注",
* "bill_type": 2
* },
* {
* "name": "派奖",
* "bill_type": 3
* },
* {
* "name": "提款",
* "bill_type": 4
* }
* ],
* "money": "31003.00", // 如果选择了明细类型则money代表对应类型的金额总和
* "bills": {
* "current_page": 1,
* "data": [
* {
* "id": 11,
* "type": 2, //
* "customer_id": 1,
* "customer_balance": "9856.00", // 用户余额
* "ie": "-", // - 减少,+增加
* "money": "120.00", // 金额
* "recharge_id": 0,
* "order_id": null,
* "title": "充值", // 标题
* "seller_id": 0,
* "created_at": "2023-04-09T07:36:12.000000Z", // 时间
* "updated_at": "2023-04-09T07:36:12.000000Z",
* "deleted_at": null
* },
* ],
* "prev_page_url": null,
* "to": 10,
* "total": 10 // 总条数
* }
* }
* }
*/
public function bills(Request $request)
{
$customerId = $request->input('customer_id');
$billType = $request->input('bill_type');
$size = $request->input('size');
$dateStart = $request->input('date_start');
$dateEnd = $request->input('date_end');
if ($dateStart) {
$dateStart = date('Y-m-d 00:00:00', strtotime($dateStart));
}
if ($dateEnd) {
$dateEnd = date('Y-m-d 23:59:59', strtotime($dateEnd));
}
ThrowException::isTrue(!$customerId, '请选择用户');
$customer = Customer::where('shop_id', $this->shopId())->find($customerId);
ThrowException::isTrue(!$customer, '无此用户');
$query = CustomerBill::where('customer_id', $customerId);
if ($dateStart) {
$query->where('created_at', '>=', $dateStart);
}
if ($dateEnd) {
$query->where('created_at', '<=', $dateEnd);
}
if (BillType::hasValue($billType, false)) {
$query->where('type', $billType);
}
$money = $query->sum('money');
$result = $query->orderBy('id', 'desc')->paging($size);
return $this->jsonSuccess([
'bill_types' => BillType::asOptionArray(true, true),
'bills' => $result,
'money' => $money
]);
}
/**
* @api {POST} /api/seller/customer/balance_incr 用户管理-加款
* @apiVersion 0.1.0
* @apiGroup 店主
*
* @apiParam {Int} customer_id 彩民id
* @apiParam {Float} money 金额
* @apiParam {String} remark 备注
* @apiParam {Int} password_pay 支付密码
* @apiParam {Int} password_pay_confirmation 确认支付密码
*
* @apiSuccessExample {json} 返回结果
* {
* "code": 200,
* "message": "",
* "data": []
* }
*/
public function balanceIncr(Request $request) {
$errors = BalanceChangeValidator::hasErrors($request);
ThrowException::isTrue($errors, $errors);
$customerId = $request->input('customer_id');
$money = $request->input('money');
$passwordPay = $request->input('password_pay');
$remark = $request->input('remark');
ThrowException::isTrue($money <= 0, '金额不能小于0');
$seller = Seller::find($this->sellerId());
ThrowException::isTrue(!$seller->password_pay, '请先设置店铺支付密码');
ThrowException::isTrue(!Seller::checkPassword($passwordPay,$seller->password_pay), '店铺支付密码错误');
$customer = Customer::where('shop_id', $this->shopId())->where('id',$customerId)->first();
ThrowException::isTrue(!$customer, '用户不存在');
DB::beginTransaction();
try {
CustomerWalletService::sellerIncrBalance($seller,$customerId, $money, $remark);
DB::commit();
} catch (JingCaiException $exception) {
DB::rollBack();
throw $exception;
} catch (\Exception $exception) {
DB::rollBack();
throw $exception;
}
return $this->jsonSuccess();
}
/**
* @api {POST} /api/seller/customer/balance_reduce 用户管理-扣款
* @apiVersion 0.1.0
* @apiGroup 店主
*
* @apiParam {Int} customer_id 彩民id
* @apiParam {Float} money 金额
* @apiParam {String} remark 备注
* @apiParam {Int} password_pay 支付密码
* @apiParam {Int} password_pay_confirmation 确认支付密码
*
* @apiSuccessExample {json} 返回结果
* {
* "code": 200,
* "message": "",
* "data": []
* }
*/
public function balanceReduce(Request $request) {
$errors = BalanceChangeValidator::hasErrors($request);
ThrowException::isTrue($errors, $errors);
$customerId = $request->input('customer_id');
$money = $request->input('money');
$passwordPay = $request->input('password_pay');
$remark = $request->input('remark');
ThrowException::isTrue($money <= 0, '金额不能小于0');
$seller = Seller::find($this->sellerId());
ThrowException::isTrue(!$seller->password_pay, '请先设置支付密码');
ThrowException::isTrue( !Seller::checkPassword($passwordPay,$seller->password_pay), '支付密码错误');
$customer = Customer::where('shop_id', $this->shopId())->where('id',$customerId)->first();
ThrowException::isTrue(!$customer, '用户不存在');
DB::beginTransaction();
try {
CustomerWalletService::sellerReduceBalance($seller,$customerId, $money, $remark);
DB::commit();
} catch (JingCaiException $exception) {
DB::rollBack();
throw $exception;
} catch (\Exception $exception) {
DB::rollBack();
throw $exception;
}
return $this->jsonSuccess();
}
/**
* @api {GET} /api/customer/wallet/withdraw_list 用户提现-列表
* @apiVersion 0.1.0
* @apiGroup 店主
*
* @apiParam {Int} [state] 提现状态1待处理2已提现3提现失败
* @apiParam {Int} [customer_id] 彩民id
* @apiParam {Int} [size] 每页条数
*
* @apiSuccessExample {json} 返回结果
* {
* "code": 200,
* "message": "",
* "data": {
* "current_page": 1,
* "data": [
* {
* "id": 1,
* "shop_id": 1,
* "customer_id": 1,
* "freeze_id": 1,
* "money": 10, // 提现金额
* "type": 1, // 1支付宝;2银行卡
* "ali_account": "ssssss", // 阿里账号
* "bank_no": "", // 银行卡号
* "bank_area": "",
* "bank_master": "",
* "bank_branch": "",
* "state": 1, // 1待处理2成功3失败
* "seller_id": 0,
* "seller_remark": "", // 处理备注
* "remark_at": null,
* "created_at": "2023-04-25T13:03:39.000000Z",
* "updated_at": "2023-04-25T13:03:39.000000Z",
* "deleted_at": null,
* "customer": {
* "id": 1,
* "name": "发达", // 真是姓名
* "nickname": "大象", // 昵称
* "avatar": "/avatar/TM1f852RwuK2Dh6b5xcfT8q3xPOdY2I3nKMl1G5P.png", //头像
* "level_name": "储备组长", //
* "level_score_group": "5000",
* "lottery_state_name": "",
* "client_type_name": "未知"
* }
* }
* ],
* "from": 1,
* "per_page": 20,
*
* "total": 1
* }
* }
*/
public function withdrawList(Request $request) {
$size = $request->input('size');
$customerId = $request->input('customer_id');
$state = $request->input('state');
$type = $request->input('type');
$dateStart = $request->input('date_start');
$dateEnd = $request->input('date_end');
$query = CustomerWithdraw::with('customer:id,name,nickname,avatar')
->where('shop_id', $this->shopId());
if ($customerId) {
$query->where('customer_id', $customerId);
}
if ($type) {
$query->where('type', $type);
}
if (WithdrawState::hasValue($state, false)) {
$query->where('state', $state);
}
if ($dateStart) {
$dateStart = date('Y-m-d 00:00:00', strtotime($dateStart));
$query->where('created_at','>=', $dateStart);
}
if ($dateEnd) {
$dateEnd = date('Y-m-d 23:59:59', strtotime($dateEnd));
$query->where('created_at','<=', $dateEnd);
}
$list = $query->orderBy('id', 'desc')
->paging($size);
return $this->jsonSuccess($list);
}
/**
* @api {POST} /api/seller/customer/withdraw_show 用户提现-提现详情
* @apiVersion 0.1.0
* @apiGroup 店主
*
* @apiParam {Int} id 提现列表id
*
* @apiSuccessExample {json} 返回结果
* {
* "code": 200,
* "message": "",
* "data": {
* "id": 1,
* "shop_id": 1,
* "customer_id": 1,
* "freeze_id": 1,
* "money": 10, // 提现金额
* "type": 1, // 1支付宝;2银行卡
* "ali_account": "ssssss", // 阿里账号
* "bank_no": "", // 银行卡号
* "bank_area": "",
* "bank_master": "",
* "bank_branch": "",
* "state": 1, // 1待处理2成功3失败
* "seller_id": 0,
* "seller_remark": "", // 处理备注
* "remark_at": null,
* "created_at": "2023-04-25T13:03:39.000000Z",
* "updated_at": "2023-04-25T13:03:39.000000Z",
* "deleted_at": null,
* "customer": {
* "id": 1,
* "name": "发达", // 真是姓名
* "nickname": "大象", // 昵称
* "avatar": "/avatar/TM1f852RwuK2Dh6b5xcfT8q3xPOdY2I3nKMl1G5P.png", //头像
* "level_name": "储备组长", //
* "level_score_group": "5000",
* "lottery_state_name": "",
* "client_type_name": "未知"
* }
*
* }
* }
*/
public function withdrawShow(Request $request) {
$id = $request->input('id');
ThrowException::isTrue($id < 1, '参数错误');
$withdraw = CustomerWithdraw::with('customer:id,name,nickname,avatar,phone')
->where('shop_id', $this->shopId())
->find($id);
ThrowException::isTrue(!$withdraw, '数据不存在');
$withdraw->customer->real_name = $withdraw->customer->name;
return $this->jsonSuccess($withdraw);
}
/**
* @api {POST} /api/seller/customer/balance_audit 用户提现-审核提现
* @apiVersion 0.1.0
* @apiGroup 店主
*
* @apiParam {Int} id 提现列表id
* @apiParam {Int} state 2成功3失败
* @apiParam {String} remark 审核备注
*
* @apiSuccessExample {json} 返回结果
* {
* "code": 200,
* "message": "",
* "data": []
* }
*/
public function withdrawAudit(Request $request, CustomerWalletService $walletService) {
$walletService->withdrawAudit($this->seller(), $request->all());
return $this->jsonSuccess();
}
/**
* @api {POST} /api/seller/customer/recharge 给用户充值
* @apiVersion 0.1.0
* @apiGroup 店主
*
* @apiParam {Int} customer_id 彩民id
* @apiParam {Float} money 充值金额
* @apiParam {Int} password_pay 支付密码
* @apiParam {Int} password_pay_confirmation 确认支付密码
* @apiParam {String} [remark] 备注
* @apiParam {String} [remark_img_path] 图片备注
*
* @apiSuccessExample {json} 返回结果
* {
* "code": 200,
* "message": "",
* "data": []
* }
*/
public function recharge(Request $request)
{
return $this->balanceIncr($request);
// $money = $request->input('money');
// $passwordPay = $request->input('password_pay');
// ThrowException::isTrue($money <= 0, '金额不能小于0');
//
// $seller = Seller::find($this->sellerId());
// ThrowException::isTrue(!$seller->password_pay, '请先设置支付密码');
// ThrowException::isTrue( !Seller::checkPassword($passwordPay,$seller->password_pay), '支付密码错误');
// CustomerWalletService::sellerRecharge($this->seller(),$request->all());
// return $this->jsonSuccess();
}
public function betsRecord(Request $request)
{
$lotteryState = $request->input('lottery_state');
$customerId = $request->input('customer_id');
$customer = Customer::where('shop_id', $this->shopId())->find($customerId);
ThrowException::isTrue(!$customer, '用户不存在');
$size = $request->input('size');
$query = Order::with('lottery:id,name')->select([
'id', 'type', 'play_type', 'lottery_state', 'lottery_prize', 'lottery_id', 'pay_at','order_sn','money'
])
->where('customer_id', $customer->id)
->where('pay_state', PayState::SUCCESS);
if ($lotteryState > 0 && LottState::hasValue($lotteryState, false)) {
if ($lotteryState == LottState::WIN) {
$query->whereIn('lottery_state', Order::zhongJiangStates());
} else {
$query->where('lottery_state', $lotteryState);
}
}
// 已开奖
if ($lotteryState == 10) {
$query->whereIn('lottery_state', [LottState::WIN, LottState::LOSS]);
}
$orders = $query->orderBy('id', 'desc')->paging($size);
return $this->jsonSuccess($orders);
}
}