shop-php/app/common/dao/store/StoreActivityDao.php

192 lines
7.4 KiB
PHP
Raw Normal View History

2023-05-10 13:38:51 +08:00
<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
namespace app\common\dao\store;
use app\common\dao\BaseDao;
2024-01-23 10:24:39 +08:00
use app\common\dao\store\product\CloudProductDao;
2024-01-23 11:16:10 +08:00
use app\common\model\store\product\CloudProduct;
2024-01-25 10:27:00 +08:00
use app\common\model\store\product\Product;
2023-05-10 13:38:51 +08:00
use app\common\model\store\StoreActivity;
2024-01-23 11:16:10 +08:00
use app\common\model\store\StoreActivityOrderProduct;
2024-01-23 10:24:39 +08:00
use app\common\repositories\store\product\SpuRepository;
2024-01-24 10:14:09 +08:00
use think\db\Query;
2024-01-23 11:16:10 +08:00
use think\exception\ValidateException;
2024-01-23 10:24:39 +08:00
use think\facade\Db;
2024-01-28 15:57:49 +08:00
use think\facade\Log;
2023-05-10 13:38:51 +08:00
/**
*
* Class StoreActivityDao
* @package app\common\dao\system\merchant
*/
class StoreActivityDao extends BaseDao
{
protected function getModel(): string
{
return StoreActivity::class;
}
public function search(array $where = [])
{
$where['is_del'] = 0;
return $this->getSearch($where);
}
2024-01-23 10:24:39 +08:00
/**
* 活动商品专区
2024-01-23 18:26:57 +08:00
* @param $userId
2024-01-23 10:24:39 +08:00
* @param $location
* @param $streetCode
* @param $activityId
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
2024-01-23 18:26:57 +08:00
public function product($userId, $location, $streetCode, $activityId)
2024-01-23 10:24:39 +08:00
{
2024-01-27 16:55:55 +08:00
$limit = 5;
2024-01-23 14:05:55 +08:00
[$lat, $lng] = (new CityAreaDao())->getLngAndLat($location, $streetCode);
2024-01-27 16:55:55 +08:00
$cloud_product_arr = (new CloudProductDao())->getByDistance($lat, $lng, ['activity_id' => $activityId], $limit);
2024-01-23 10:24:39 +08:00
$cloud_product = [];
foreach ($cloud_product_arr as $key => $value) {
$cloud_product[] = $value['product_id'];
}
$where = [
'is_show' => 1,
'is_used' => 1,
'status' => 1,
'is_del' => 0,
'mer_status' => 1,
'product_id' => $cloud_product
];
if (!$cloud_product) {
return app('json')->success(['count' => 0, 'list' => []]);
}
/** @var SpuRepository $spuRep */
$spuRep = app()->make(SpuRepository::class);
2024-01-27 16:55:55 +08:00
$products = $spuRep->getApiSearch($where, 1, $limit, false, true);
2024-01-23 18:26:57 +08:00
$canBuy = 1;
2024-01-23 10:24:39 +08:00
if ($products['list']) {
$list = $products['list'];
2024-01-25 10:27:00 +08:00
$productList = array_column($list->toArray(), 'product');
$oldProductIds = array_column($productList, 'old_product_id');
$oldProductList = Product::whereIn('product_id', $oldProductIds)->field('product_id,stock')->select()->toArray();
$oldProductList = reset_index($oldProductList, 'product_id');
2024-01-23 10:24:39 +08:00
foreach ($cloud_product_arr as $key => $value) {
2024-01-23 18:26:57 +08:00
if (!empty($userId)) {
$buyRecord = $this->canBuy($userId, $value['product_id']);
if (!$buyRecord['is_true']) {
$canBuy = 0;
}
}
2024-01-23 10:24:39 +08:00
foreach ($list as $k => $v) {
if ($activityId == 2) {
$list[$k]['limited_price'] = '2.00';
2024-01-25 10:27:00 +08:00
if (isset($oldProductList[$v['product']['old_product_id']])) {
$list[$k]['product']['stock'] = $oldProductList[$v['product']['old_product_id']]['stock'];
2024-01-25 11:09:00 +08:00
$list[$k]['stock'] = $oldProductList[$v['product']['old_product_id']]['stock'];
2024-01-26 11:37:25 +08:00
if ($list[$k]['stock'] < 1) {
$list[$k]['product']['image'] = $list[$k]['product']->sellOutImage;
}
2024-01-25 10:27:00 +08:00
}
}
2024-01-23 10:24:39 +08:00
if ($value['product_id'] == $v['product_id']) {
if ($value['mer_labels'] == ',5,') {
$list[$k]['mer_labels_name'] = '五日达';
} else {
$list[$k]['mer_labels_name'] = '同城';
}
}
}
}
}
2024-01-23 18:26:57 +08:00
return ['can_buy' => $canBuy, 'count' => $products['count'], 'list' => $list ?? []];
2024-01-23 10:24:39 +08:00
}
2024-01-23 11:16:10 +08:00
/**
* 是否可购买活动商品
* @param $userId
* @param $productId
2024-01-23 11:41:00 +08:00
* @return bool|int
2024-01-23 11:16:10 +08:00
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function canBuy($userId, $productId)
{
$activityId = Db::name('cloud_product')->where('product_id', $productId)->value('activity_id');
2024-01-23 13:22:10 +08:00
$find = Db::name('store_activity_order_product')->where('user_id', $userId)->where('status', 1)->find();
2024-01-23 13:13:19 +08:00
$data=['activityId'=>$activityId,'is_true'=>true];
2024-01-23 11:16:10 +08:00
if ($find && $activityId == 2) {
2024-01-23 13:13:19 +08:00
$data['is_true']=false;
2024-01-23 11:16:10 +08:00
}
2024-01-23 13:13:19 +08:00
return $data;
2024-01-23 11:16:10 +08:00
}
/**
* 保存活动订单商品
* @param $activityId
* @param $order
* @return void
*/
public function saveOrderProduct($activityId, $order)
{
$orderProductIds = array_column($order->orderProduct->toArray(), 'product_id');
$productIds = CloudProduct::whereIn('product_id', $orderProductIds)->where('activity_id', $activityId)->column('product_id');
2024-01-26 15:05:40 +08:00
if (empty($productIds)) {
return;
2024-01-26 13:52:15 +08:00
}
2024-01-28 15:57:49 +08:00
// $find = StoreActivityOrderProduct::where('user_id', $order['uid'])->where('status', 1)->find();
2024-01-23 11:16:10 +08:00
foreach ($productIds as $productId) {
2024-01-28 15:57:49 +08:00
// if (!empty($find['product_id']) && $find['product_id'] == $productId && $activityId == 2) {
// throw new ValidateException('活动商品限购1个');
// }
2024-01-23 11:16:10 +08:00
$model = new StoreActivityOrderProduct();
$model->user_id = $order['uid'];
$model->activity_id = $activityId;
$model->product_id = $productId;
$model->number = 1;
if (!$model->save()) {
2024-01-28 15:57:49 +08:00
Log::error('活动商品数据保存失败productId'.$productId);
// throw new ValidateException('活动商品数据保存失败');
2024-01-23 11:16:10 +08:00
}
}
}
2024-01-24 10:14:09 +08:00
/**
* 根据活动获取对应的镇街
* @param $activityId
* @return array
*/
public function district($activityId)
{
$merchants = CloudProduct::with(['merchant' => function (Query $query) {
$query->with(['street' => function (Query $query) {
$query->field('street_code,street_name');
}])->field('mer_id,street_id');
}])->where('activity_id', $activityId)->field('mer_id')->group('mer_id')->select()->toArray();
$result = [];
foreach ($merchants as $merchant) {
$streetCode = $merchant['merchant']['street']['street_code'] ?? '';
$streetName = $merchant['merchant']['street']['street_name'] ?? '';
if (!isset($result[$streetCode]) && !empty($streetCode)) {
$result[$streetCode] = ['street_code' => $streetCode, 'street_name' => $streetName];
}
}
return array_values($result);
}
2023-05-10 13:38:51 +08:00
}