This commit is contained in:
luofei 2024-03-15 10:50:11 +08:00
commit b72714fbd0
5 changed files with 298 additions and 1 deletions

View File

@ -16,6 +16,7 @@ use app\common\dao\system\financial\FinancialRecordDao;
use app\common\model\store\order\StoreGroupOrder;
use app\common\model\store\order\StoreOrder;
use app\common\model\store\order\StoreRefundOrder;
use app\common\model\store\product\ProductAttrValue;
use app\common\model\system\merchant\FinancialRecord;
use app\common\model\system\merchant\Merchant;
use app\common\model\user\User;
@ -2559,6 +2560,183 @@ class StoreOrderRepository extends BaseRepository
}
public function dealGoodsList($where,$money)
{
$data = ProductAttrValue::getDB()->alias('a')
->leftJoin('store_product p', 'a.product_id = p.product_id')
->where($where)
->where('a.stock', '>', 0)
->field('a.value_id,a.product_id,a.mer_id,a.sku,a.stock,a.image,a.price,
a.svip_price,
a.unique,p.store_name,p.store_info');
$list = $data->select();
$count = $data->count();
$minMoney = bcsub($money, 600, 2);
if ($count) {
$range = $this->getRangeNumber($minMoney); //减少一般的区间
//不存在--虚假的区间
if ($range == -1) {
//拿实际的区间去处理
$range = $this->getRangeNumber($money);//区间实际
$minMoney = bcdiv($money, 2, 2);
if ($range == -1) {
return app('json')->fail('不在区间情况中');
}
}
$deal = $this->dealArr($list->toArray());
//平均2个左右 0 1 2 阶梯往上查寻找
$end = array($this->findNearestPriceProduct($deal[$range]['items'], $minMoney));//17jiji
if ($range == 0 && $deal[$range]['quantity'] = 1) { //第一个并且只有一条数据
if ($money < $end[0]['price']) {
$list = $end;
} else {
$list = array_merge($end, $end);//满足2条数据
}
} else {
//在1 或者 2区间
$newArray = [];
$list = $this->findNearestPriceProductDg($deal, $minMoney, $range, $newArray, $money, $money);
}
$count = count($list);
return compact('count', 'list');
} else {
return compact('count', 'list');
}
}
public function getRangeNumber($price, $ranges = [[0, 100], [100, 500], [500, PHP_INT_MAX]])
{
foreach ($ranges as $index => $range) {
if ($price >= $range[0] && $price <= $range[1]) {
return $index;
}
}
return -1;
}
public function dealArr($originalArray): array
{
$range1 = []; // 0-100元的商品
$range2 = []; // 100-500元的商品
$range3 = []; // 500元以上商品
$countRange1 = 0;
$countRange2 = 0;
$countRange3 = 0;
foreach ($originalArray as $item) {
$price = floatval($item['price']);
if ($price >= 0 && $price <= 100) {
$range1[] = $item;
$countRange1++;
} elseif ($price > 100 && $price <= 500) {
$range2[] = $item;
$countRange2++;
} else {
$range3[] = $item;
$countRange3++;
}
}
return [
['range' => '0-100元', 'items' => $range1, 'quantity' => $countRange1],
['range' => '100-500元', 'items' => $range2, 'quantity' => $countRange2],
['range' => '500元以上', 'items' => $range3, 'quantity' => $countRange3],
];
}
public function findNearestPriceProduct($array, $targetPrice)
{
$minDiff = PHP_INT_MAX;
$nearestProduct = null;
foreach ($array as $product) {
$price = floatval($product['price']);
$diff = abs($targetPrice - $price);
if ($diff < $minDiff) {
$minDiff = $diff;
$nearestProduct = $product;
}
}
return $nearestProduct;
}
//全部数组 假总金额 区间 新数组 总 拿去减少的
public function findNearestPriceProductDg($array, $targetPrice, $range, &$newArray, $trueMoney, $remarkMoney)
{
$minDiff = PHP_INT_MAX;
$nearestProduct = null;
foreach ($array[$range]['items'] as $product) {
$price = floatval($product['price']);
$diff = abs($targetPrice - $price);
if ($diff < $minDiff) {
$minDiff = $diff;
$nearestProduct = $product;
}
}
$newArray[] = $nearestProduct;
$remarkId = $nearestProduct['value_id'];
$totalPrice = array_sum(array_column($newArray, 'price')); //本次全部的价格之和
if ($totalPrice <= $remarkMoney) {
$next = $remarkMoney - $totalPrice;//剩下的
$tt = $remarkMoney - $totalPrice;
$range = $this->getRangeNumber($next);
$array = $this->dealReduce($array,$remarkId);
$this->findNearestPriceProductDg($array['data'], $targetPrice, $range, $newArray, $tt, $remarkMoney);
}
return $newArray;
}
public function dealReduce($data, $targetValueId): array
{
$found = false;
foreach ($data as &$category) {
foreach ($category['items'] as $itemKey => $item) {
if ($item['value_id'] === $targetValueId) {
unset($category['items'][$itemKey]);
if($category['quantity'] == 0){
break 2;
}
$category['quantity'] -= 1;
$found = true;
break 2;
}
}
}
return [
'status' => $found,
'data' => $data
];
}
}

View File

@ -13,14 +13,22 @@
namespace app\controller\api\store\merchant;
use app\common\model\system\merchant\MerchantType;
use app\common\repositories\store\MerchantTakeRepository;
use app\common\repositories\store\product\ProductRepository;
use app\common\repositories\system\config\ConfigValueRepository;
use app\common\repositories\system\financial\FinancialRepository;
use app\common\repositories\system\merchant\MerchantRepository;
use app\common\repositories\user\UserBillRepository;
use app\common\repositories\user\UserMerchantRepository;
use app\validate\merchant\MerchantFinancialAccountValidate;
use app\validate\merchant\MerchantTakeValidate;
use app\validate\merchant\MerchantUpdateValidate;
use crmeb\jobs\ChangeMerchantStatusJob;
use think\App;
use crmeb\basic\BaseController;
use app\common\repositories\system\merchant\MerchantRepository as repository;
use think\facade\Db;
use think\facade\Queue;
class Merchant extends BaseController
{
@ -237,4 +245,90 @@ class Merchant extends BaseController
return true;
}
public function update(MerchantUpdateValidate $validate, MerchantTakeValidate $takeValidate, MerchantTakeRepository $repository)
{
$type = $this->request->param('type', 1);
$id = $this->request->param('id');
if (empty($id)) {
return app('json')->fail('参数错误');
}
$merchant = Db::name('merchant')->where('mer_id', $id)->find();
if ($type == 2) {
$data = $this->request->params([
'mer_info',
'mer_certificate',
'service_phone',
'mer_avatar',
'mer_banner',
'mer_state',
'mini_banner',
'mer_keyword',
'mer_address',
'long',
'lat',
['delivery_way', [2]],
'credit_buy',
'settle_cycle',
'interest_rate',
]);
if (empty($data['service_phone'])) {
return app('json')->fail('客户电话不能为空');
}
// 如果手机号不存在,则使用入驻时的手机号
$data['service_phone'] = empty($data['service_phone']) ? $merchant['mer_phone'] : $data['service_phone'];
$validate->check($data);
$sys_bases_status = systemConfig('sys_bases_status') === '0' ? 0 : 1;
if ($sys_bases_status && empty($data['mer_certificate']))
return app('json')->fail('店铺资质不可为空');
$merCertificate = merchantConfig($id, 'mer_certificate');
if (!is_array($merCertificate)) {
$merCertificate = explode(',', $merCertificate);
}
$merCertificate[0] = $data['mer_certificate'];
app()->make(ConfigValueRepository::class)->setFormData([
'mer_certificate' => $merCertificate
], $id);
unset($data['mer_certificate']);
foreach ($data['delivery_way'] as $datum) {
if ($datum == 1) {
$takeData = $this->request->params(['mer_take_status', 'mer_take_location', 'mer_take_day', 'mer_take_time']);
$takeData['mer_take_name'] = $merchant['mer_name'];
$takeData['mer_take_address'] = $data['mer_address'];
$takeData['mer_take_phone'] = $merchant['mer_phone'];
$takeValidate->check($takeData);
$repository->set($id, $takeData);
break;
}
}
$delivery_way = implode(',', $data['delivery_way']);
if (count($data['delivery_way']) == 1 && $data['delivery_way'] != $merchant['delivery_way']) {
app()->make(ProductRepository::class)->getSearch([])
->where('mer_id', $merchant['mer_id'])
->update(['delivery_way' => $delivery_way]);
}
$data['delivery_way'] = $delivery_way;
} else {
$data = $this->request->params(['mer_state']);
if ($merchant['is_margin'] == 1 && $data['mer_state'] == 1)
return app('json')->fail('开启店铺前请先支付押金');
if ($data['mer_state'] && !$merchant['sub_mchid'] && systemConfig('open_wx_combine'))
return app('json')->fail('开启店铺前请先完成微信子商户入驻');
}
Db::name('merchant')->where('mer_id', $id)->update($data);
Queue::push(ChangeMerchantStatusJob::class, $id);
return app('json')->success('修改成功');
}
}

View File

@ -303,4 +303,26 @@ class StoreOrder extends BaseController
$res = $orderRepository->show($id, $this->request->uid());
return app('json')->success($res);
}
public function getOrder()
{
//商户id 金额
$merId = $this->request->param('mer_id');
$money = $this->request->param('money');
if(empty($money) || empty($merId)){
return app('json')->fail('参数缺失');
}
$where = [
"p.is_show" => 1,
"p.status" => 1,
"p.product_type" => 0,
"p.is_gift_bag" => 0
];
$where['a.mer_id'] = $merId;
return app('json')->success($this->repository->dealGoodsList($where,$money));
}
}

View File

@ -20,7 +20,7 @@ class MerchantTypeValidate extends Validate
protected $failException = true;
protected $rule = [
'type_name|店铺类型名称' => 'require|max:5',
'type_name|店铺类型名称' => 'require|max:8',
'type_info|店铺类型要求' => 'max:256',
'is_margin|是否有保证金' => 'require|in:0,1',
'auth|权限' => 'require|array|min:1',

View File

@ -65,6 +65,7 @@ Route::group('api/', function () {
});
//订单
Route::any('order_mix', 'api.store.order.StoreOrder/getOrder');//商户获取商品
Route::group('order', function () {
Route::post('check', '/checkOrder');
Route::post('create', '/createOrder');
@ -537,6 +538,8 @@ Route::group('api/', function () {
Route::get('/detail/:id', 'Merchant/detail');
Route::get('/qrcode/:id', 'Merchant/qrcode');
Route::get('/local', 'Merchant/localLst');
//编辑商户信息
Route::post('update', 'Merchant/update');
})->prefix('api.store.merchant.');
Route::post('store/certificate/:merId', 'api.Auth/getMerCertificate');