new-admin-api/app/controller/api/store/user/UserMerchant.php

166 lines
6.4 KiB
PHP

<?php
namespace app\controller\api\store\user;
use app\common\model\user\ExchangeIntegralRecord;
use app\common\model\user\User;
use app\common\repositories\user\IntegralGiveRecordRepository;
use app\common\repositories\user\IntegralRepository;
use app\common\repositories\user\UserBillRepository;
use app\common\repositories\user\UserMerchantRepository;
use app\common\repositories\user\UserRepository;
use crmeb\basic\BaseController;
use think\App;
use think\facade\Db;
/**
* Common: 客户管理
* Author: wu-hui
* Time: 2023/11/09 16:58
* Class UserMerchant
* @package app\controller\merchant\user
*/
class UserMerchant extends BaseController{
/**
* @var UserMerchantRepository
*/
protected $repository;
/**
* UserMerchant constructor.
* @param App $app
* @param UserMerchantRepository $repository
*/
public function __construct(App $app, UserMerchantRepository $repository)
{
parent::__construct($app);
$this->repository = $repository;
}
/**
* Common: 列表获取
* Author: wu-hui
* Time: 2023/11/09 17:04
* @param $merId
* @return mixed
*/
public function getList($merId){
$where = $this->request->params(['nickname','default_consume_id']);
$where['mer_id'] = $merId;
if((int)$where['default_consume_id'] > 0) $where['uids'] = [$where['default_consume_id']] ?? [];
[$page, $limit] = $this->getPage();
return app('json')->success($this->repository->getList($where, $page, $limit));
}
/**
* Common: 商户赠送积分(客户积分变更)
* Author: wu-hui
* Time: 2023/11/10 11:28
* @param $merId
* @return mixed
*/
public function integralChange($merId){
$params = $this->request->params(['uid','pm','number','voucher_image']);
$params['number'] = abs($params['number']);
// 变更操作
try {
Db::transaction(function () use ($merId, $params) {
// 变更记录
$giveRecord = app()->make(IntegralGiveRecordRepository::class)->create([
'operate_uid' => $this->request->uid(),
'uid' => $params['uid'],
'mer_id' => $merId,
'number' => $params['number'],
'voucher_image' => $params['voucher_image'],
]);
// 总积分到账 & 记录总平台积分变更日志
app()->make(UserRepository::class)->incIntegral((int)$params['uid'],$params['number'],'商户赠送积分','mer_give',[
'link_id' => $giveRecord->record_id,
'number' => $params['number'],
'mark' => '商户赠送积分' . $params['number']
]);
// 商户积分变更
app()->make(IntegralRepository::class)->changeIntegral((int)$params['uid'],(int)$merId,$params['number']);
// 记录商户端积分变更日志
$user = app()->make(UserRepository::class)->get($params['uid']);
app()->make(UserBillRepository::class)->create([
'uid' => $params['uid'],
'link_id' => $giveRecord->record_id,
'pm' => $params['pm'],
'title' => '商户赠送积分',
'category' => 'mer_integral',
'type' => 'mer_give',
'number' => $params['number'],
'balance' => $user->integral,
'mark' => '商户赠送积分'.$params['number'],
'mer_id' => $merId,
'status' => 1
]);
});
return app('json')->success('操作成功');
} catch (\Throwable $e) {
return app('json')->fail($e->getMessage());
}
}
/**
* Common: 预支积分
* Author: wu-hui
* Time: 2024/02/05 15:01
* @return mixed
*/
public function advanceExchangeIntegral(){
$params = $this->request->params(['uid','integral','mer_id']);
// 变更操作
try {
Db::transaction(function () use ($params) {
$integral = $params['integral'] ?? 0;
// 获取用户当前持有
$userHoldInfo = User::where('uid',$params['uid'])->findOrEmpty();
// 赠送 预支酒水卡积分
$changeFront = (float)$userHoldInfo->exchange_integral;
$advanceChangeFront = (float)$userHoldInfo->advance_exchange_integral;
$userHoldInfo->exchange_integral += (float)$integral;// 总额度
$userHoldInfo->advance_exchange_integral += (float)$integral;// 总额度
// 记录
$insertData[] = [
'uid' => $params['uid'],
'product_id' => 0,
'order_id' => 0,
'order_product_id' => 0,
'change_type' => 1,
'change_quantity' => (float)$integral,
'change_front' => $advanceChangeFront,
'change_after' => (float)$userHoldInfo->advance_exchange_integral,
'remark' => "预支酒水卡积分",
'source' => 1
];
$insertData[] = [
'uid' => $params['uid'],
'product_id' => 0,
'order_id' => 0,
'order_product_id' => 0,
'change_type' => 1,
'change_quantity' => (float)$integral,
'change_front' => $changeFront,
'change_after' => (float)$userHoldInfo->exchange_integral,
'remark' => "预支酒水卡积分,增加积分",
'source' => 0
];
// 修改用户持有
$userHoldInfo->save();
// 记录酒卡额度信息
if(count($insertData) > 0) ExchangeIntegralRecord::insertAll($insertData);
});
return app('json')->success('操作成功');
} catch (\Throwable $e) {
return app('json')->fail($e->getMessage());
}
}
}