diff --git a/app/common/dao/user/ExchangePickupRecordDao.php b/app/common/dao/user/ExchangePickupRecordDao.php new file mode 100644 index 0000000..eb89435 --- /dev/null +++ b/app/common/dao/user/ExchangePickupRecordDao.php @@ -0,0 +1,23 @@ +hasOne(ExchangePickupPoint::class,'id','point_id'); + } + + public function user(){ + return $this->hasOne(User::class, 'uid', 'uid'); + } + + public function staff(){ + return $this->hasOne(User::class, 'uid', 'staff_uid'); + } + + +} diff --git a/app/common/repositories/user/ExchangePickupPointRepository.php b/app/common/repositories/user/ExchangePickupPointRepository.php index a9f40a3..454817c 100644 --- a/app/common/repositories/user/ExchangePickupPointRepository.php +++ b/app/common/repositories/user/ExchangePickupPointRepository.php @@ -14,9 +14,6 @@ class ExchangePickupPointRepository extends BaseRepository{ } - - - /** * Common: 编辑信息 * Author: wu-hui @@ -25,6 +22,7 @@ class ExchangePickupPointRepository extends BaseRepository{ */ public function editInfo($params){ $info = $this->dao->getSearch([])->where('id',(int)$params['id'])->findOrEmpty(); + $info->title = $params['title']; $info->address = $params['address']; $info->is_show = $params['is_show']; $info->uid_list = implode(',',$params['uid_list']); @@ -48,8 +46,12 @@ class ExchangePickupPointRepository extends BaseRepository{ ->when((int)$params['uid'] > 0,function($query) use ($params){ $query->where('find_in_set(' . $params['uid'] . ',`uid_list`)'); }) + ->when(isset($params['is_show']) && $params['is_show'] !== '',function($query) use ($params){ + $query->where('is_show',$params['is_show']); + }) ->when(!empty($params['address']),function($query) use ($params){ - $query->where('address', "like", "%{$params['address']}%"); + $query->where('address', "like", "%{$params['address']}%") + ->whereOr('title','like',"%{$params['address']}%"); }) ->order('create_time DESC,id DESC'); $count = $query->count(); diff --git a/app/common/repositories/user/ExchangePickupRecordRepository.php b/app/common/repositories/user/ExchangePickupRecordRepository.php new file mode 100644 index 0000000..7e5a192 --- /dev/null +++ b/app/common/repositories/user/ExchangePickupRecordRepository.php @@ -0,0 +1,64 @@ +dao = $dao; + } + /** + * Common: 获取信息列表 + * Author: wu-hui + * Time: 2024/01/14 18:21 + * @param array $params + * @param int $page + * @param int $limit + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function getList(array $params,int $page,int $limit):array{ + $query = $this->dao->getSearch([]) + ->with([ + 'point' => function ($query) { + $query->field('id,address as point_address,title as point_title')->bind(['point_address','point_title']); + }, + 'user' => function ($query) { + $query->field('uid,nickname,avatar')->bind(['nickname','avatar']); + }, + 'staff' => function ($query) { + $query->field('uid,nickname as staff_nickname,avatar as staff_avatar')->bind(['staff_nickname','staff_avatar']); + }, + ]) + ->when((int)$params['uid'] > 0,function($query) use ($params){ + $query->where('uid',$params['uid']); + }) + ->when((int)$params['staff_uid'] > 0,function($query) use ($params){ + $query->where('staff_uid',$params['staff_uid']); + }) + ->when(!empty($params['address']),function($query) use ($params){ + $query->hasWhere('point', function ($hasQuery) use ($params) { + $hasQuery->where('address', "like", "%{$params['address']}%") + ->whereOr('title','like',"%{$params['address']}%"); + })->order('ExchangePickupRecord.create_time DESC,ExchangePickupRecord.id DESC'); + },function($query){ + $query->order('create_time DESC,id DESC'); + }); + $count = $query->count(); + $list = $query->page($page,$limit)->select(); + + return compact('count','list'); + } + + + + + +} diff --git a/app/controller/admin/user/ExchangeQuota.php b/app/controller/admin/user/ExchangeQuota.php index 2dac117..f72eaed 100644 --- a/app/controller/admin/user/ExchangeQuota.php +++ b/app/controller/admin/user/ExchangeQuota.php @@ -5,6 +5,7 @@ namespace app\controller\admin\user; use app\common\model\user\ExchangePickupPoint; use app\common\repositories\user\ExchangeIntegralRecordRepository; use app\common\repositories\user\ExchangePickupPointRepository; +use app\common\repositories\user\ExchangePickupRecordRepository; use app\common\repositories\user\ExchangeQuotaRecordRepository; use app\common\repositories\user\ExchangeQuotaRepository; use crmeb\basic\BaseController; @@ -126,7 +127,8 @@ class ExchangeQuota extends BaseController */ public function pickupPointEdit(){ // 参数获取 - $params = $this->request->params(['id','address','is_show','uid_list']); + $params = $this->request->params(['id','title','address','is_show','uid_list']); + if(empty($params['title'])) return app('json')->fail('请输入提货点名称!'); if(empty($params['address'])) return app('json')->fail('请输入提货点地址!'); if(!is_array($params['uid_list']) || count($params['uid_list']) <= 0) return app('json')->fail('请至少选择一个负责人!'); // 判断:地址是否已经存在 @@ -167,11 +169,19 @@ class ExchangeQuota extends BaseController return app('json')->success($data); } - // 提货记录 + /** + * Common: 提货记录 + * Author: wu-hui + * Time: 2024/01/14 18:21 + * @return mixed + */ public function pickupPointRecord(){ + [$page, $limit] = $this->getPage(); + $params = $this->request->params(['uid','address', 'staff_uid']); + $data = app()->make(ExchangePickupRecordRepository::class)->getList((array)$params,(int)$page,(int)$limit); - debug('开发中...'); + return app('json')->success($data); } diff --git a/app/controller/api/user/Exchange.php b/app/controller/api/user/Exchange.php new file mode 100644 index 0000000..901f3e8 --- /dev/null +++ b/app/controller/api/user/Exchange.php @@ -0,0 +1,135 @@ +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()); + } + } + + + +} diff --git a/route/api.php b/route/api.php index a0b92c9..192e0b8 100644 --- a/route/api.php +++ b/route/api.php @@ -344,19 +344,13 @@ Route::group('api/', function () { Route::group('onlinePayment', function () { Route::get('searchMerList', 'OnlinePay/searchMer'); Route::get('createOrder', 'OnlinePay/createOrder'); - - - - - - - - })->prefix('api.store.order.'); - - - - + // 商品兑换 + Route::group('exchange', function () { + Route::get('pointList', 'Exchange/getPointList'); + Route::get('userHold', 'Exchange/getUserHold'); + Route::get('exchangeHandle', 'Exchange/exchangeHandle'); + })->prefix('api.user.'); })->middleware(UserTokenMiddleware::class, true);