175 lines
6.7 KiB
PHP
175 lines
6.7 KiB
PHP
<?php
|
|
|
|
namespace app\common\repositories\user;
|
|
|
|
use app\common\dao\user\ExchangePickupPointDao;
|
|
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\BaseRepository;
|
|
use think\facade\Db;
|
|
|
|
class ExchangePickupPointRepository extends BaseRepository{
|
|
|
|
protected $dao;
|
|
|
|
public function __construct(ExchangePickupPointDao $dao){
|
|
$this->dao = $dao;
|
|
}
|
|
|
|
|
|
/**
|
|
* Common: 编辑信息
|
|
* Author: wu-hui
|
|
* Time: 2024/01/14 10:52
|
|
* @param $params
|
|
*/
|
|
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']);
|
|
$info->lat = $params['lat'];
|
|
$info->lng = $params['lng'];
|
|
|
|
$info->save();
|
|
}
|
|
/**
|
|
* Common: 获取信息列表
|
|
* Author: wu-hui
|
|
* Time: 2024/01/14 10:47
|
|
* @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([])
|
|
->when((isset($params['lng']) && $params['lng'] > 0) && (isset($params['lat']) && $params['lat'] > 0),function($query) use ($params){
|
|
$distance = "ROUND((ST_DISTANCE(point(lng, lat), point({$params['lng']}, {$params['lat']})) * 111195),2)";
|
|
$query->field("*,{$distance} as distance")
|
|
->orderRaw("{$distance} ASC");
|
|
})
|
|
->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(isset($params['default_point_id']) && $params['default_point_id'] > 0,function($query) use ($params){
|
|
$query->where('id',$params['default_point_id']);
|
|
})
|
|
->when(!empty($params['address']),function($query) use ($params){
|
|
$query->where('address', "like", "%{$params['address']}%")
|
|
->whereOr('title','like',"%{$params['address']}%");
|
|
})
|
|
->order("create_time DESC,id DESC");
|
|
$count = $query->count();
|
|
$list = $query->page($page,$limit)->append(['user_list'])->select()->each(function($item){
|
|
if($item->distance > 0){
|
|
if ($item->distance < 100) $item->distance_text = '100m以内';
|
|
else $item->distance_text = sprintf("%.2f",$item->distance / 1000).'km';
|
|
}
|
|
|
|
return $item;
|
|
});
|
|
|
|
return compact('count','list');
|
|
}
|
|
/**
|
|
* Common: 获取单条信息
|
|
* Author: wu-hui
|
|
* Time: 2024/01/14 11:31
|
|
* @param $id
|
|
* @return array
|
|
*/
|
|
public function getInfo($id){
|
|
return $this->dao->getSearch([])
|
|
->append(['user_list'])
|
|
->where('id',$id)
|
|
->findOrEmpty()
|
|
->toArray();
|
|
}
|
|
/**
|
|
* Common: 用户是否存在有效的兑换站点
|
|
* Author: wu-hui
|
|
* Time: 2024/01/15 15:19
|
|
* @param $uid
|
|
* @return int
|
|
*/
|
|
public function userIsStaff($uid):int{
|
|
$count = $this->dao->getSearch([])
|
|
->where('is_show', 1)
|
|
->where('find_in_set(' . $uid . ',`uid_list`)')
|
|
->count();
|
|
|
|
return $count > 0 ? 1 : 0;
|
|
}
|
|
/**
|
|
* Common: 兑换成功处理
|
|
* Author: wu-hui
|
|
* Time: 2024/01/18 10:03
|
|
* @param $pickupRecordId
|
|
*/
|
|
public function exchangeSuccessHandle($pickupRecordId){
|
|
$pickupRecordInfo = app()->make(ExchangePickupRecordRepository::class)
|
|
->getSearch([])
|
|
->where('id',$pickupRecordId)
|
|
->findOrEmpty();
|
|
Db::transaction(function () use ($pickupRecordInfo) {
|
|
// 修改状态
|
|
ExchangePickupRecord::update(['status'=>1],['id'=>$pickupRecordInfo['id']]);
|
|
// 积分&额度变更数量
|
|
$changeNum = (float)sprintf("%.2f",$pickupRecordInfo['total_money'] - $pickupRecordInfo['diff_money_pay']);
|
|
// 变更额度
|
|
$userHoldInfo = app()->make(ExchangeQuotaRepository::class)
|
|
->searchModel(['uid'=>$pickupRecordInfo['uid'],'quota_type'=>$pickupRecordInfo['quota_type']])
|
|
->findOrEmpty();
|
|
$changeFront = (float)$userHoldInfo->surplus_quota;
|
|
$userHoldInfo->use_quota += (float)$changeNum;
|
|
$userHoldInfo->surplus_quota -= (float)$changeNum;
|
|
$userHoldInfo->save();
|
|
ExchangeQuotaRecord::insert([
|
|
'uid' => $pickupRecordInfo['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,
|
|
'quota_type' => $pickupRecordInfo['quota_type'],
|
|
]);
|
|
// 变更积分
|
|
$userInfo = User::where('uid',$pickupRecordInfo['uid'])->findOrEmpty();
|
|
$integralChangeFront = (float)$userInfo->exchange_integral;
|
|
$userInfo->exchange_integral -= (float)$changeNum;
|
|
$userInfo->exchange_integral = $userInfo->exchange_integral > 0 ? $userInfo->exchange_integral : 0;
|
|
$userInfo->save();
|
|
ExchangeIntegralRecord::insert([
|
|
'uid' => $pickupRecordInfo['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' => "兑换消费",
|
|
]);
|
|
});
|
|
}
|
|
|
|
|
|
|
|
}
|