feat(user): 新增设置采购款和余额功能

- 在 UserController 中添加 setPurchaseFunds 和 setnowMoney 方法
- 在 UserLogic 中实现 PurchaseFunds 和 nowMoney 方法
- 更新 CapitalFlowLogic 以支持新的资金变动类型
This commit is contained in:
mkm 2024-10-29 12:00:25 +08:00
parent 36c1c45415
commit e6e2cc78c3
3 changed files with 161 additions and 86 deletions

View File

@ -23,7 +23,7 @@ class UserController extends BaseAdminController
return $this->dataLists(new UserLists());
}
/**
/**
* @notes 添加用户列表
* @return \think\response\Json
* @author likeadmin
@ -61,17 +61,17 @@ class UserController extends BaseAdminController
{
// $params = (new UserValidate())->post()->goCheck('edit');
$params = $this->request->post();
$result = UserLogic::edit($params);
if (true === $result) {
return $this->success('编辑成功', [], 1, 1);
}
return $this->fail(UserLogic::getError());
$result = UserLogic::edit($params);
if (true === $result) {
return $this->success('编辑成功', [], 1, 1);
}
return $this->fail(UserLogic::getError());
}
//采购款明细、余额明细、礼品券明细、返还金明细
public function fundList()
{
(new UserValidate())->get()->goCheck('fund');
(new UserValidate())->get()->goCheck('fund');
$page_no = (int)$this->request->get('page_no', 1);
$page_size = (int)$this->request->get('page_size', 15);
$params = $this->request->get();
@ -81,7 +81,6 @@ class UserController extends BaseAdminController
$res['page_no'] = $params['page_no'];
$res['page_size'] = $params['page_size'];
return $this->success('ok', $res);
}
@ -91,7 +90,6 @@ class UserController extends BaseAdminController
public function userSing()
{
return $this->dataLists(new UserSignLogLists());
}
@ -107,10 +105,24 @@ class UserController extends BaseAdminController
$params['page_no'] = $page_no > 0 ? $page_no : 1;
$params['page_size'] = $page_size > 0 ? $page_size : 15;
$uid = $uid['id'];
$res = UserLogic::giftList($uid,$params);
$res = UserLogic::giftList($uid, $params);
$res['page_no'] = $params['page_no'];
$res['page_size'] = $params['page_size'];
return $this->success('ok', $res);
}
}
//设置采购款
public function setPurchaseFunds()
{
$params = $this->request->post();
$res = UserLogic::PurchaseFunds($params);
return $this->success( '设置成功',[],1,1);
}
//设置余额
public function setnowMoney()
{
$params = $this->request->post();
$res = UserLogic::nowMoney($params);
return $this->success( '设置成功',[],1,1);
}
}

View File

@ -17,6 +17,7 @@ use app\common\enum\OrderEnum;
use app\common\enum\user\UserTerminalEnum;
use app\common\enum\YesNoEnum;
use app\common\logic\BaseLogic;
use app\common\logic\CapitalFlowLogic;
use app\common\model\finance\CapitalFlow;
use app\common\model\store_finance_flow\StoreFinanceFlow;
use app\common\model\store_order\StoreOrder;
@ -57,7 +58,7 @@ class UserLogic extends BaseLogic
Db::startTrans();
try {
$res=User::create([
$res = User::create([
'avatar' => $avatar,
'real_name' => $params['real_name'],
'nickname' => $params['nickname'],
@ -78,21 +79,21 @@ class UserLogic extends BaseLogic
public static function checkAddress(array $params)
{
$user_ship=$params['user_ship']??0;
if($user_ship==2){
if(!isset($params['village'])){
$user_ship = $params['user_ship'] ?? 0;
if ($user_ship == 2) {
if (!isset($params['village'])) {
throw new BusinessException('请设置村参数');
}
$arr=User::where('user_ship',$user_ship)->alias('user')->join('user_address address','user.id=address.uid and village='.$params['village'])->find();
$arr = User::where('user_ship', $user_ship)->alias('user')->join('user_address address', 'user.id=address.uid and village=' . $params['village'])->find();
if ($arr) {
throw new BusinessException('该区域已有村长请重新选择');
}
}elseif($user_ship==3){
if(!isset($params['brigade'])){
} elseif ($user_ship == 3) {
if (!isset($params['brigade'])) {
throw new BusinessException('请设置队参数');
}
$arr=User::where('user_ship',$user_ship)->alias('user')->join('user_address address','user.id=address.uid and village='.$params['village'] .' and brigade='.$params['brigade'])->find();
if($arr){
$arr = User::where('user_ship', $user_ship)->alias('user')->join('user_address address', 'user.id=address.uid and village=' . $params['village'] . ' and brigade=' . $params['brigade'])->find();
if ($arr) {
throw new BusinessException('该区域已有队长请重新选择');
}
}
@ -107,37 +108,37 @@ class UserLogic extends BaseLogic
$avatar = !empty($params['avatar']) ? FileService::setFileUrl($params['avatar']) : $defaultAvatar;
Db::startTrans();
try {
$data=[
$data = [
'avatar' => $avatar,
'real_name' => $params['real_name']??"",
'nickname' => '用户'.time(),
'real_name' => $params['real_name'] ?? "",
'nickname' => '用户' . time(),
'account' => $params['mobile'],
'password' => $password,
'mobile' => $params['mobile'],
'label_id' => $params['label_id']??0,
'store_id' => $params['store_id']??0,
'label_id' => $params['label_id'] ?? 0,
'store_id' => $params['store_id'] ?? 0,
];
if(isset($params['user_ship']) &&$params['user_ship']==4){
$data['user_ship']=4;
if (isset($params['user_ship']) && $params['user_ship'] == 4) {
$data['user_ship'] = 4;
}
$res=User::create($data);
$res = User::create($data);
UserCreateLog::create([
'uid' => $res['id'],
'create_uid' => $params['create_uid']??0,
'store_id' => $params['store_id']??0,
'staff_id' => $params['staff_id']??0,
'user_ship' => $data['user_ship']??0,
'create_uid' => $params['create_uid'] ?? 0,
'store_id' => $params['store_id'] ?? 0,
'staff_id' => $params['staff_id'] ?? 0,
'user_ship' => $data['user_ship'] ?? 0,
]);
UserAddress::create([
'uid' => $res['id'],
'real_name' => $params['real_name']??"",
'mobile' => $params['mobile']??'',
'province' => $params['province']??'',
'city' => $params['city']??'',
'area' => $params['area']??'',
'street' => $params['street']??'',
'village' => $params['village']??'',
'brigade' => $params['brigade']??'',
'real_name' => $params['real_name'] ?? "",
'mobile' => $params['mobile'] ?? '',
'province' => $params['province'] ?? '',
'city' => $params['city'] ?? '',
'area' => $params['area'] ?? '',
'street' => $params['street'] ?? '',
'village' => $params['village'] ?? '',
'brigade' => $params['brigade'] ?? '',
'is_default' => 1,
]);
Db::commit();
@ -167,7 +168,7 @@ class UserLogic extends BaseLogic
// 'mobile' => $params['mobile'] ?? '',
// 'sex' => $params['sex'] ?? 0,
// 'is_disable' => $params['is_disable'] ?? 0,
'label_id'=>$params['label_id']
'label_id' => $params['label_id']
]);
Db::commit();
@ -188,8 +189,21 @@ class UserLogic extends BaseLogic
public static function detail(int $userId): array
{
$field = [
'id', 'account', 'nickname', 'avatar', 'real_name','integral','label_id','user_ship',
'sex', 'mobile', 'create_time', 'login_time', 'channel','now_money','purchase_funds'
'id',
'account',
'nickname',
'avatar',
'real_name',
'integral',
'label_id',
'user_ship',
'sex',
'mobile',
'create_time',
'login_time',
'channel',
'now_money',
'purchase_funds'
];
$user = User::where(['id' => $userId])->field($field)
@ -197,8 +211,8 @@ class UserLogic extends BaseLogic
$user['channel'] = UserTerminalEnum::getTermInalDesc($user['channel']);
$user->sex = $user->getData('sex');
$user['number']=StoreFinanceFlow::where('other_uid',$userId)->where(['status'=>0,'financial_pm'=>1,'type'=>1])->sum('number');
$user['user_ship_name']=$user->user_ship>0?UserShip::where('id',$user->user_ship)->value('title'):"一般用户";
$user['number'] = StoreFinanceFlow::where('other_uid', $userId)->where(['status' => 0, 'financial_pm' => 1, 'type' => 1])->sum('number');
$user['user_ship_name'] = $user->user_ship > 0 ? UserShip::where('id', $user->user_ship)->value('title') : "一般用户";
return $user->toArray();
}
@ -218,66 +232,116 @@ class UserLogic extends BaseLogic
]);
}
/**
* 更新采购款
*/
public static function PurchaseFunds(array $params)
{
$find = User::where(['id' => $params['id']])->find();
Db::startTrans();
try {
$capitalFlowDao = new CapitalFlowLogic($find, 'user');
if ($params['type'] == 1) {
$capitalFlowDao->userIncome('system_purchase_add', 'system', 0, $params['purchase_funds'],'',1);
$find->purchase_funds = bcadd($params['purchase_funds'], $find['purchase_funds'], 2);
$find->save();
} else {
$capitalFlowDao->userExpense('system_purchase_dec', 'system', 0, $params['purchase_funds']);
$find->purchase_funds = bcsub($params['purchase_funds'], $find['purchase_funds'], 2);
$find->save();
}
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
throw new BusinessException($e->getMessage());
}
}
/**
* 更新余额
*/
public static function nowMoney(array $params)
{
$find = User::where(['id' => $params['id']])->find();
Db::startTrans();
try {
$capitalFlowDao = new CapitalFlowLogic($find, 'user');
if ($params['type'] == 1) {
$capitalFlowDao->userIncome('system_balance_add', 'system', 0, $params['now_money'],);
$find->now_money = bcadd($params['now_money'], $find['now_money'], 2);
$find->save();
} else {
$capitalFlowDao->userExpense('system_balance_reduce', 'system', 0, $params['now_money']);
$find->now_money = bcsub($params['now_money'], $find['now_money'], 2);
$find->save();
}
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
throw new BusinessException($e->getMessage());
}
}
public static function dealDetails($params)
{
switch ($params['type']){
switch ($params['type']) {
case 1:
//采购款明细
$categories = ['user_balance_recharge', 'user_order_purchase_pay','system_purchase_add','user_balance_recharge_refund'];
$categories = ['user_balance_recharge', 'user_order_purchase_pay', 'system_purchase_add', 'user_balance_recharge_refund'];
$query = CapitalFlow::where('uid', $params['id'])
->whereIn('category', $categories);
$count = $query->count();
$data = $query
->page($params['page_no'],$params['page_size'])
->order('id','desc')
->page($params['page_no'], $params['page_size'])
->order('id', 'desc')
->select()->toArray();
foreach ($data as &$value){
if($value['category'] == 'user_order_purchase_pay'){
$value['order_sn'] = StoreOrder::where('id',$value['link_id'])->value('order_id');
}elseif($value['category'] == 'user_balance_recharge'){
$value['order_sn'] = UserRecharge::where('id',$value['link_id'])->value('order_id');
foreach ($data as &$value) {
if ($value['category'] == 'user_order_purchase_pay') {
$value['order_sn'] = StoreOrder::where('id', $value['link_id'])->value('order_id');
} elseif ($value['category'] == 'user_balance_recharge') {
$value['order_sn'] = UserRecharge::where('id', $value['link_id'])->value('order_id');
}
}
break;
case 2:
//余额明细
$category = ['system_balance_add','user_order_balance_pay'];
$category = ['system_balance_add', 'user_order_balance_pay'];
$query = CapitalFlow::where('uid', $params['id'])
->whereIn('category', $category);
$count = $query->count();
$data = $query
->page($params['page_no'],$params['page_size'])
->order('id','desc')
->page($params['page_no'], $params['page_size'])
->order('id', 'desc')
->select()->toArray();
foreach ($data as &$value){
$value['order_sn'] = StoreOrder::where('id',$value['link_id'])->value('order_id');
foreach ($data as &$value) {
$value['order_sn'] = StoreOrder::where('id', $value['link_id'])->value('order_id');
}
break;
case 3:
//礼品券明细
$query = UserSign::where(['uid'=>$params['id']]);
$query = UserSign::where(['uid' => $params['id']]);
$count = $query->count();
$data =$query
->page($params['page_no'],$params['page_size'])
->order('id','desc')
$data = $query
->page($params['page_no'], $params['page_size'])
->order('id', 'desc')
->select()->toArray();
break;
case 4:
//返还金明细 -todo back
$query = VipFlow::with('store')->where(['user_id'=>$params['id']]);
$query = VipFlow::with('store')->where(['user_id' => $params['id']]);
$count = $query->count();
$data = $query
->page($params['page_no'],$params['page_size'])
->order('id','desc')
->page($params['page_no'], $params['page_size'])
->order('id', 'desc')
->select()->toArray();
foreach ($data as &$value){
if($value['status'] == 0){
$value['title'] = "购买商品".$value['all']."元获得".$value['number']."元返还金";
}else{
foreach ($data as &$value) {
if ($value['status'] == 0) {
$value['title'] = "购买商品" . $value['all'] . "元获得" . $value['number'] . "元返还金";
} else {
//退回到余额、微信、采购款
$back = self::dealTitleCate($value['pay_type']);
$value['title'] = "返还金解冻".$value['number']."元退回到".$back;
$value['title'] = "返还金解冻" . $value['number'] . "元退回到" . $back;
}
}
break;
@ -289,27 +353,24 @@ class UserLogic extends BaseLogic
'lists' => $data,
'count' => $count
];
}
public static function giftList($uid,$params)
public static function giftList($uid, $params)
{
$query = StoreProductGift::with(['store','user','goodsName'])->where('uid',$uid);
$query = StoreProductGift::with(['store', 'user', 'goodsName'])->where('uid', $uid);
$count = $query->count();
$list = $query->page($params['page_no'],$params['page_size'])
->order('id','desc')
$list = $query->page($params['page_no'], $params['page_size'])
->order('id', 'desc')
->select()->toArray();
return [
'lists' => $list,
'count' => $count
];
}
public static function dealTitleCate($pay_type)
{
switch ($pay_type){
switch ($pay_type) {
case 18:
$title = "采购款";
break;
@ -317,17 +378,17 @@ class UserLogic extends BaseLogic
$title = "现金";
break;
case 3:
$title ="余额";
$title = "余额";
break;
case 7:
case 9:
$title ="微信";
$title = "微信";
break;
case 13:
$title ="支付宝";
$title = "支付宝";
break;
default:
$title ="默认";
$title = "默认";
}
return $title;
}

View File

@ -80,8 +80,8 @@ class CapitalFlowLogic extends BaseLogic
$model->before_balance = bcadd($this->user['now_money'], $amount, 2);
$model->balance = $this->user['now_money'];
} else {
$model->before_balance = $this->user['now_money'];
$model->balance = $this->user['now_money'];
$model->before_balance = $this->user['purchase_funds'];
$model->balance = bcsub($this->user['purchase_funds'], $amount, 2);
}
$model->create_time = date('Y-m-d H:i:s');
$model->type = 'out';
@ -176,6 +176,8 @@ class CapitalFlowLogic extends BaseLogic
return "系统增加余额{$amount}";
case 'system_purchase_add':
return "系统增加采购款{$amount}";
case 'system_purchase_dec':
return "系统减少采购款{$amount}";
case 'system_balance_reduce':
return "系统减少余额{$amount}";
case 'user_balance_recharge_refund':