Files
2026-03-11 18:24:59 +08:00

682 lines
26 KiB
PHP

<?php
namespace app\storeapi\logic;
use app\common\enum\notice\NoticeEnum;
use app\common\logic\BaseLogic;
use app\common\model\order\OrderStore;
use app\common\model\store\StoreMember;
use app\common\model\store\StoreUser;
use app\common\model\store\StoreUserAccountLog;
use app\common\model\store\StoreUserBank;
use app\common\model\store\StoreUserReflect;
use app\common\model\teastore\TeaStore;
use app\common\model\teastore\TeaStoreGroup;
use app\common\model\teastore\TeaStoreRoom;
use app\common\model\user\User;
use app\common\model\user\UserAccountLog;
use app\common\model\user\UserGroup;
use app\common\model\user\UserMember;
use app\common\model\user\UserStoreMoney;
use app\common\service\FileService;
use app\common\service\sms\SmsDriver;
use app\common\model\order\OrderStoreRecharge;
use app\common\model\order\OrderGroup;
use think\facade\Config;
use think\facade\Db;
class UserLogic extends BaseLogic
{
public static function info(int $userId)
{
$user = StoreUser::where(['id' => $userId])
->field('id,sn,sex,account,password,nickname,real_name,avatar,mobile,create_time,user_money,total_amount,total_reflect_amount')
->find();
// $user['avatar'] = FileService::getFileUrl($user['avatar']);
return $user->toArray();
}
public static function updateUser($params, $userId)
{
Db::startTrans();
try {
if(isset($params['band_mobile'])){
$user = User::where("mobile",$params['band_mobile'])
->where("is_disable",0)
->find();
if(!$user){
throw new \Exception('客户端用户不存在');
}
$params['bind_user_id'] = $user->id;
}
StoreUser::where(['id' => $userId])->update($params);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
public static function balanceLogList($post,$user_id){
$t = "";
if(isset($post['end_time'])){
if($post['end_time']!=""&&$post['end_time']!=null){
$end_time = strtotime($post['end_time']);
$t = "create_time <= '".$end_time."'";
}
}
$count = StoreUserAccountLog::where($t)->where("user_id",$user_id)->count();
$lists = StoreUserAccountLog::where($t)
->where("user_id",$user_id)
->page($post['page'], $post['size'])
->order("id","desc")
->select()
->toarray();
foreach($lists as $key=>$value){
$lists[$key]['mobile'] = "";
$lists[$key]['reflect_status'] = "";
if($value['change_type'] != 3){
$order = OrderStore::where("order_sn",$value['source_sn'])->find();
if($order!=null){
$user = User::where("id",$order['user_id'])->find();
$lists[$key]['mobile'] = $user['mobile'];
}
}else{
$reflect_status = StoreUserReflect::where("order_sn",$value['source_sn'])->find();
if($reflect_status!= null){
$lists[$key]['reflect_status'] = $reflect_status['status'];
}
}
}
$data = [
'list' => $lists,
'page' => $post['page'],
'size' => $post['size'],
'count' => $count,
'more' => is_more($count, $post['page'], $post['size'])
];
return $data;
}
public static function balanceLogDetails($post){
$details = StoreUserAccountLog::where("id",$post['id'])->find();
if($details['change_type'] == 3){
$order = StoreUserReflect::where("order_sn",$details['source_sn'])->find();
if($order!=null){
$bank = StoreUserBank::where("id",$order['bank_id'])->find();
$order['bank_name'] = $bank['bank_name'];
$order['bank_card'] = substr($bank['bank_card'], -4);
$order['dtime'] = date("Y-m-d H:i:s",$order['dtime']);
$order['update_dtime'] = date("Y-m-d H:i:s",$order['update_dtime']);
}else{
$order = [];
$order['bank_name'] = "";
$order['bank_card'] = "";
}
$details['order'] = $order;
}else{
$details['store'] = "";
$store = TeaStore::where("id",$details['store_id'])->find();
if($store!=null){
$details['store'] = $store;
}
$details['room'] = "";
$room = TeaStoreRoom::where("id",$details['room_id'])->find();
if($room!=null){
$details['room'] = $room;
}
if($details['change_type'] == 9){
$order = OrderStoreRecharge::where("order_sn",$details['source_sn'])->find();
$order['pay_time'] = date('Y-m-d H:i');
$order['dtime'] = $order['create_time'];
}elseif($details['change_type'] == 4){
$order = OrderGroup::where("order_sn",$details['source_sn'])->find();
$details['room'] = ["title"=>'到店核销'];
}else{
$order = OrderStore::where("order_sn",$details['source_sn'])->find();
}
if($order!=null){
$user = User::where("id",$order['user_id'])->find();
$order['nickname'] = "";
$order['mobile'] = "";
if($user!=null){
$order['nickname'] = $user['nickname'];
$order['mobile'] = $user['mobile'];
}
$order['pay_way_title'] = "";
if( $order['pay_way'] == 1){
$order['pay_way_title'] = "余额支付";
}
if( $order['pay_way'] == 2){
$order['pay_way_title'] = "微信支付";
}
if( $order['pay_way'] == 3){
$order['pay_way_title'] = "门店余额支付";
}
if( $order['pay_way'] == 4){
$order['pay_way_title'] = "管理员添加";
}
$order['group'] = [];
$user_group = UserGroup::where("id",$order['group_coupon_id'])->find();
if($details['change_type'] == 4){
$user_group = UserGroup::where("order_id",$order['id'])->find();
}
if($user_group!=null){
$order['group'] = TeaStoreGroup::where("id",$user_group['group_id'])->find();
$order['group']['yanquan_dtime'] = $user_group['update_dtime'];
$order['group']['qr_sn'] = $user_group['qr_sn'];
$order['group']['yanquan_status'] = $user_group['status'];
}
}else{
$order = [];
$order['group'] = [];
$order['pay_way_title'] = "";
$order['nickname'] = "";
$order['mobile'] = "";
}
$details['order'] = $order;
}
$data = [
'details' => $details
];
return $data;
}
public static function checkMoney($data){
$result = TeaStore::where("id",$data['store_id'])->find();
$d['store_msg'] = $result;
return $d;
}
public static function addBank($params,$user_id){
Db::startTrans();
try {
// 校验验证码
$smsDriver = new SmsDriver();
if (!$smsDriver->verify($params['mobile'], $params['code'], NoticeEnum::FIND_MOBILE_CAPTCHA)) {
throw new \Exception('验证码错误');
}
// 新增
StoreUserBank::create([
"user_id"=>$user_id,
"name"=>$params['name'],
"bank_name"=>$params['bank_name'],
"bank_card"=>$params['bank_card'],
"bank_open_name"=>$params['bank_open_name'],
"mobile"=>$params['mobile'],
"dtime"=>time()
]);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
public static function checkBank($user_id){
$lists = StoreUserBank::where("user_id",$user_id)
->where("del",0)
->where("status",1)
->order("id","desc")
->select()
->toarray();
foreach($lists as $key=>$value){
$lists[$key]['bank_card'] = substr($value['bank_card'], -4);
}
$data = [
'list' => $lists
];
return $data;
}
public static function delBank($params){
Db::startTrans();
try {
$result = StoreUserBank::where("id",$params['id'])->find();
if ($result!=null) {
throw new \Exception('暂无数据');
}
// 删除
$r = StoreUserBank::where("id",$params['id'])->update(['del'=>1]);
Db::commit();
if($r){
return true;
}
throw new \Exception('数据错误');
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
public static function submitReflect($params,$user_id){
Db::startTrans();
try {
// 查询是否余额充足
$store = TeaStore::where("id",$params['store_id'])->where("del",0)->find();
if($store == null){
throw new \Exception('暂无数据');
}
if($store['balance'] < $params['amount']){
throw new \Exception('余额不足');
}
// 新增提现记录
$order_sn = createSn("store_user_reflect","order_sn");
StoreUserReflect::create([
"user_id"=>$user_id,
"store_id"=>$params['store_id'],
"order_sn"=>$order_sn,
"bank_id"=>$params['bank_id'],
"amount"=>$params['amount'],
"dtime"=>time()
]);
// 计算扣除余额
$user_money = round($store['balance']-$params['amount'],2);
// 计算总提现金额
$reflect_money = round($store['total_reflect_amount']+$params['amount'],2);
$dt['balance'] = $user_money;
$dt['total_reflect_amount'] = $reflect_money;
$rs = TeaStore::where("id",$params['store_id'])->update($dt);
if(!$rs){
throw new \Exception('计算错误');
}
// 新增流水
$data = [
'sn' => createSn("store_user_account_log","sn"),
'user_id' => $user_id,
'change_type' => 3,
'action' => 2,
"amount"=>$params['amount'],
'before_amount' => $dt['balance'],
'after_amount' => $user_money,
'source_sn' => $order_sn,
'remark' => "用户提现"
];
$r = StoreUserAccountLog::create($data);
Db::commit();
if($r){
return true;
}
throw new \Exception('提现失败');
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
public static function checkStoreUserList($data){
$s = "";
if(isset($data['search'])){
if($data['search'] != ""){
$b = $data['search'];
$s = "b.mobile like '%".$b."%'";
}
}
$count = StoreMember::alias("a")
->join("user b", "b.id = a.user_id", "left")
->where("a.store_id",$data['store_id'])
->where($s)
->count();
$lists = StoreMember::alias("a")
->join("user b", "b.id = a.user_id", "left")
->where("a.store_id",$data['store_id'])
->where($s)
->field("a.id,a.remark,b.mobile,b.member,a.user_id,b.nickname,b.avatar")
->page($data['page'], $data['size'])
->order("a.id","desc")
->select()
->toarray();
$totalMoney = UserStoreMoney::where([
'store_id' => $data['store_id'],
'status' => 1 // 根据业务需要,可能只统计有效状态的
])->sum('money');
$money = 0;
foreach($lists as $key=>$value){
$lists[$key]['avatar'] =!empty($value['avatar']) ? FileService::getFileUrl($value['avatar']) : FileService::getFileUrl('uploads/images/20260106/20260106104232ef02b9224.png');
$StoreMoney = UserStoreMoney::where([
'store_id'=>$data['store_id'],
'user_id'=>$value['user_id']
])->find();
if($value['member'] ==1){
$UserMember = UserMember::where([
'user_id'=>$value['user_id']
])->find();
if($UserMember){
$lists[$key]['expiration_time'] = date('Y-m-d',$UserMember->expiration_time);
}else{
$lists[$key]['expiration_time'] = '--';
}
}else{
$lists[$key]['expiration_time'] = '--';
}
if($StoreMoney){
$lists[$key]['store_money'] = $StoreMoney->money;
}else{
$lists[$key]['store_money'] = $money;
}
}
$data = [
'list' => $lists,
'page' => $data['page'],
'size' => $data['size'],
'count' => $count,
'more' => is_more($count, $data['page'], $data['size']),
'totalMoney'=>$totalMoney
];
return $data;
}
public static function storeRechargeLists($data){
$yearMonth = $data['month'] ?? date('Y-m'); // 默认当前月
// 生成月份的起始和结束时间
$startTimestamp = strtotime(date('Y-m-01 00:00:00', strtotime($yearMonth)));
$endTimestamp = strtotime(date('Y-m-t 23:59:59', strtotime($yearMonth)));
$count = OrderStoreRecharge::alias("a")
->join("user b", "b.id = a.user_id", "left")
->join("tea_store_recharge c", "c.id = a.recharge_id", "left")
->where("a.store_id",$data['store_id'])
->where("a.pay_status",1)
->whereBetween("a.pay_time", [$startTimestamp, $endTimestamp])
->count();
$lists = OrderStoreRecharge::alias("a")
->join("user b", "b.id = a.user_id", "left")
->join("tea_store_recharge c", "c.id = a.recharge_id", "left")
->where("a.store_id",$data['store_id'])
->where("a.pay_status",1)
->whereBetween("a.pay_time", [$startTimestamp, $endTimestamp])
->field("a.order_amount,a.recharge_price,a.gift_price,b.mobile,b.nickname,a.pay_time,c.title")
->page($data['page'], $data['size'])
->order("a.id","desc")
->select()
->toarray();
$total_recharge_price= OrderStoreRecharge::where([
'store_id' => $data['store_id'],
'pay_status' => 1 // 根据业务需要,可能只统计有效状态的
])->whereBetween("pay_time", [$startTimestamp, $endTimestamp])->sum('recharge_price');
$total_gift_price= OrderStoreRecharge::where([
'store_id' => $data['store_id'],
'pay_status' => 1 // 根据业务需要,可能只统计有效状态的
])->whereBetween("pay_time", [$startTimestamp, $endTimestamp])->sum('gift_price');
foreach ($lists as &$item){
$item['pay_time'] = date('Y-m-d H:i');
}
$data = [
'total_recharge_price'=>$total_recharge_price,
'total_gift_price'=>$total_gift_price,
'list' => $lists,
'page' => $data['page'],
'size' => $data['size'],
'count' => $count,
'more' => is_more($count, $data['page'], $data['size']),
];
return $data;
}
public static function checkStoreUserDetails($params){
// 门店用户基本信息
$store_user_member = StoreMember::where("user_id",$params['user_id'])
->where("store_id",$params['store_id'])
->find();
// 用户基本信息
$user = User::where("id",$params['user_id'])->find();
$user['remark'] = $store_user_member['remark'];
$user['store_user_id'] = $store_user_member['id'];
$user['create_time'] = date("Y-m-d",strtotime($user['create_time']));
$user_member = UserMember::where('user_id',$params['user_id'])->find();
// 会员到期时间
$user['expiration_time'] = "";
if($user_member){
$user['expiration_time'] = date('Y-m-d H:i',$user_member['expiration_time']);
}
$user_store_money = UserStoreMoney::where("user_id",$params['user_id'])->where("store_id",$params['store_id'])->find();
if($user_store_money != null){
$user['user_store_money'] = $user_store_money['money'];
}
// 门店用户总消费
$user['order_amount'] = OrderStore::where("store_id",$params['store_id'])->where("user_id",$params['user_id'])->sum("order_amount");
$data = [
'user' => $user
];
return $data;
}
public static function editStoreUserRemark($data){
Db::startTrans();
try {
// 查询是否余额充足
$StoreMemberUser = StoreMember::where("id",$data['id'])->where("status",1)->find();
if($StoreMemberUser == null){
throw new \Exception('暂无用户');
}
$StoreMemberUser::where("id",$data['id'])->update(['remark'=>$data['remark']]);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
public static function checkStoreUserBuyList($data){
$count = UserAccountLog::where("store_id",$data['store_id'])
->where("user_id",$data['user_id'])
->where("action",2)
->count();
$lists = UserAccountLog::where("store_id",$data['store_id'])
->where("user_id",$data['user_id'])
->where("action",2)
->page($data['page'], $data['size'])
->order("id","desc")
->select()
->toarray();
foreach($lists as $key=>$value){
$lists[$key]['pay_way_title'] = "";
if( $value['change_object'] == 1){
$lists[$key]['pay_way_title'] = "余额支付";
}
if( $value['change_object'] == 2){
$lists[$key]['pay_way_title'] = "微信支付";
}
if( $value['change_object'] == 3){
$lists[$key]['pay_way_title'] = "支付宝支付";
}
$lists[$key]['store_name'] = TeaStore::where("id",$value['store_id'])->value("name");
}
$data = [
'list' => $lists,
'page' => $data['page'],
'size' => $data['size'],
'count' => $count,
'more' => is_more($count, $data['page'], $data['size'])
];
return $data;
}
public static function checkStoreAccountList($data){
$times = date("Y-m");
list($year, $month) = explode('-', $times);
$startTimestamp = strtotime($year . '-' . $month . '-01 00:00:00');
$endTimestamp = strtotime(date('Y-m-t 23:59:59', $startTimestamp));
if(isset($data['times'])){
if($data['times']!=""&&$data['times']!=null){
list($year, $month) = explode('-', $data['times']);
// 转换为日期范围
$startTimestamp = strtotime($year . '-' . $month . '-01 00:00:00');
$endTimestamp = strtotime(date('Y-m-t 23:59:59', $startTimestamp));
}
}
$count = StoreUserAccountLog::where("store_id",$data['store_id'])
->where("store_id",$data['store_id'])
->whereBetween('create_time', [$startTimestamp, $endTimestamp])
->count();
$all_price = StoreUserAccountLog::where("store_id",$data['store_id'])
->where("store_id",$data['store_id'])
->whereBetween('create_time', [$startTimestamp, $endTimestamp])
->where("action",1)
->sum("amount");
$all_action_price = StoreUserAccountLog::where("store_id",$data['store_id'])
->where("store_id",$data['store_id'])
->whereBetween('create_time', [$startTimestamp, $endTimestamp])
->where("action",2)
->sum("amount");
$lists = StoreUserAccountLog::where("store_id",$data['store_id'])
->where("store_id",$data['store_id'])
->whereBetween('create_time', [$startTimestamp, $endTimestamp])
->page($data['page'], $data['size'])
->order("id","desc")
->select()
->toarray();
foreach($lists as $key=>$value){
$lists[$key]['store'] = TeaStore::where("id",$value['store_id'])->find();
if($value['change_type'] == 4){
$lists[$key]['room'] = ["title"=>'到店核销'];
}else{
$lists[$key]['room'] = TeaStoreRoom::where("id",$value['room_id'])->find();
}
$lists[$key]['order'] = "";
$lists[$key]['group'] = "";
if($value['action'] == 1){
$order = OrderStore::where("order_sn",$value['source_sn'])->find();
$lists[$key]['order'] = $order;
if($order!=null){
$order_group = UserGroup::where("id",$order['group_coupon_id'])->find();
if($order_group!=null){
$lists[$key]['group'] = TeaStoreGroup::where("id",$order_group['group_id'])->find();
}
}
}
}
$data = [
'list' => $lists,
'page' => $data['page'],
'size' => $data['size'],
"all_price"=>round($all_price-$all_action_price,2),
'count' => $count,
'more' => is_more($count, $data['page'], $data['size'])
];
return $data;
}
public static function checkMoth($data,$user_id){
// 本月收入
$times = date("Y-m");
list($year, $month) = explode('-', $times);
$startTimestamp = strtotime($year . '-' . $month . '-01 00:00:00');
$endTimestamp = strtotime(date('Y-m-t 23:59:59', $startTimestamp));
$result['month'] = StoreUserAccountLog::where("store_id",$data['store_id'])
->whereBetween('create_time', [$startTimestamp, $endTimestamp])
->where("action",1)
->sum("amount");
$result['refund_month'] = StoreUserAccountLog::where("store_id",$data['store_id'])
->whereBetween('create_time', [$startTimestamp, $endTimestamp])
->where("action",2)
->sum("amount");
$result['month'] = $result['month']-$result['refund_month'];
// 今日收入
$today = date('Y-m-d');
$startTime = strtotime($today . ' 00:00:00');
$endTime = strtotime($today . ' 23:59:59');
$result['today_price'] = StoreUserAccountLog::where("store_id",$data['store_id'])
->whereBetween('create_time', [$startTime, $endTime])
// ->where("change_type","in","1,2")
->where("action",1)
->sum("amount");
$result['refund_today_price'] = StoreUserAccountLog::where("store_id",$data['store_id'])
->whereBetween('create_time', [$startTime, $endTime])
// ->where("change_type","in","1,2")
->where("action",2)
->sum("amount");
$result['today_price'] = $result['today_price'] -$result['refund_today_price'];
// 今日验券
$result['yan_price'] = StoreUserAccountLog::where("store_id",$data['store_id'])
->whereBetween('create_time', [$startTime, $endTime])
->where("change_object",4)
->where("action",1)
->sum("amount");
$result['refund_yan_price'] = StoreUserAccountLog::where("store_id",$data['store_id'])
->whereBetween('create_time', [$startTime, $endTime])
->whereIn("change_object",[0,4])
->where("action",2)
->sum("amount");
$result['yan_price'] = $result['yan_price'] -$result['refund_yan_price'];
// 昨日收入
$yesterday = date('Y-m-d', strtotime('-1 day'));
// 计算时间戳范围
$startTime = strtotime($yesterday . ' 00:00:00');
$endTime = strtotime($yesterday . ' 23:59:59');
$result['yesterday_price'] = StoreUserAccountLog::where("store_id",$data['store_id'])
->whereBetween('create_time', [$startTime, $endTime])
->where("change_type","in","1,2")
->where("action",1)
->sum("amount");
$result['refund_yesterday_price'] = StoreUserAccountLog::where("store_id",$data['store_id'])
->whereBetween('create_time', [$startTime, $endTime])
->where("change_type","in","1,2")
->where("action",2)
->sum("amount");
$result['yesterday_price'] = $result['yesterday_price'] -$result['refund_yesterday_price'];
// 昨日验券
$result['yesterday_yan_price'] = StoreUserAccountLog::where("store_id",$data['store_id'])
->whereBetween('create_time', [$startTime, $endTime])
->where("change_type",4)
->where("action",1)
->sum("amount");
$result['refund_yesterday_yan_price'] = StoreUserAccountLog::where("store_id",$data['store_id'])
->whereBetween('create_time', [$startTime, $endTime])
->where("change_type",4)
->where("action",1)
->sum("amount");
$result['yesterday_yan_price'] = $result['yesterday_yan_price']-$result['refund_yesterday_yan_price'];
$d = [
"result"=>$result
];
return $d;
}
}