new-admin-api/app/controller/api/user/Exchange.php

136 lines
5.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\controller\api\user;
use app\common\model\user\ExchangeIntegralRecord;
use app\common\model\user\ExchangePickupRecord;
use app\common\model\user\ExchangeQuota;
use app\common\model\user\ExchangeQuotaRecord;
use app\common\model\user\User;
use app\common\repositories\user\ExchangePickupPointRepository;
use app\common\repositories\user\ExchangeQuotaRepository;
use crmeb\basic\BaseController;
use think\App;
use think\facade\Db;
class Exchange extends BaseController{
protected $user;
public function __construct(App $app){
parent::__construct($app);
$this->user = $this->request->userInfo();
}
/**
* Common: 获取用户持有信息
* Author: wu-hui
* Time: 2024/01/14 16:53
* @return mixed
*/
public function getUserHold(){
$uid = $this->request->uid();
// 获取额度
$info = app()->make(ExchangeQuotaRepository::class)
->getSearch([])
->field([
'uid',
// 可用额度=剩余额度-冻结额度
'(surplus_quota - freeze_quota) as available'
])
->where('uid',$uid)
->findOrEmpty();
$info->available_integral = $this->user->exchange_integral;
$info->diff_rate = 30;// 差价 应补金额默认为30%
return app('json')->success($info);
}
/**
* Common: 获取提货点列表
* Author: wu-hui
* Time: 2024/01/14 16:06
* @return mixed
*/
public function getPointList(){
$search = $this->request->params(['uid','address']);
$search['is_show'] = 1;
$data = app()->make(ExchangePickupPointRepository::class)->getList($search, 1, 30);
$list = $data['list'];
return app('json')->success($list);
}
/**
* Common: 兑换处理
* Author: wu-hui
* Time: 2024/01/14 18:02
* @return mixed
*/
public function exchangeHandle(){
// 参数获取
$data = $this->request->params(['total_money','use_integral','diff_money','diff_money_pay','point_id','staff_uid']);
if ((float)$data['total_money'] <= 0) return app('json')->fail('价值必须大于0');
if ((float)$data['point_id'] <= 0) return app('json')->fail('请选择提货点!');
if ((float)$data['staff_uid'] <= 0) return app('json')->fail('请选择操作员!');
// 添加兑换记录
try{
$uid = $this->request->uid();
Db::transaction(function () use ($data, $uid) {
// 添加兑换记录
ExchangePickupRecord::insert([
'uid' => $uid,
'point_id' => $data['point_id'],
'staff_uid' => $data['staff_uid'],
'total_money' => $data['total_money'],
'use_integral' => $data['use_integral'],
'diff_money' => $data['diff_money'],
'diff_money_pay' => $data['diff_money_pay'],
]);
// 积分&额度变更数量
$changeNum = (float)sprintf("%.2f",$data['total_money'] - $data['diff_money_pay']);
// 变更额度
$userHoldInfo = ExchangeQuota::where('uid',$uid)->findOrEmpty();
$changeFront = (float)$userHoldInfo->surplus_quota;
$userHoldInfo->use_quota += (float)$changeNum;
$userHoldInfo->surplus_quota -= (float)$changeNum;
$userHoldInfo->save();
ExchangeQuotaRecord::insert([
'uid' => $uid,
'product_id' => 0,
'order_id' => 0,
'order_product_id' => 0,
'change_type' => 0,
'change_quantity' => (float)$changeNum,
'change_front' => $changeFront,
'change_after' => (float)$userHoldInfo->surplus_quota,
'remark' => "兑换消费",
'source' => 2,
]);
// 变更积分
$userInfo = User::where('uid',$uid)->findOrEmpty();
$integralChangeFront = (float)$userInfo->exchange_integral;
$userInfo->exchange_integral -= (float)$changeNum;
$userInfo->save();
ExchangeIntegralRecord::insert([
'uid' => $uid,
'product_id' => 0,
'order_id' => 0,
'order_product_id' => 0,
'change_type' => 0,
'change_quantity' => (float)$changeNum,
'change_front' => $integralChangeFront,
'change_after' => (float)$userInfo->exchange_integral,
'remark' => "兑换消费",
]);
});
return app('json')->success('success');
}catch(\Exception $e){
return app('json')->fail($e->getMessage());
}
}
}