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

153 lines
5.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;
2023-05-10 13:38:51 +08:00
use app\common\model\BaseModel;
2024-01-23 10:24:39 +08:00
use app\common\model\store\GeoStreet;
2024-01-23 11:16:10 +08:00
use app\common\model\store\product\CloudProduct;
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;
2023-05-10 13:38:51 +08:00
use app\common\repositories\system\RelevanceRepository;
2024-01-23 11:16:10 +08:00
use think\exception\ValidateException;
2024-01-23 10:24:39 +08:00
use think\facade\Db;
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
/**
* 活动商品专区
* @param $location
* @param $streetCode
* @param $activityId
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function product($location, $streetCode, $activityId)
{
if (!empty($location) && $location != ',') {
[$lat, $lng] = explode(',', $location);
} elseif (!empty($streetCode)) {
$location = GeoStreet::where('street_code', $streetCode)->field('lng,lat')->find();
if (!empty($location)) {
[$lat, $lng] = [$location['lat'], $location['lng']];
}
}
if (empty($lat) || empty($lng)) {
[$lat, $lng] = ['28.889137', '105.443352'];
}
$cloud_product_arr = (new CloudProductDao())->getByDistance($lat, $lng, ['activity_id' => $activityId], 4);
$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' => []]);
}
$query = Db::name('cloud_product')->where('status', 1);
$count = $query->where(function($query){
$query->whereOr('mer_labels', '')
->whereOr('mer_labels',',5,');
})->count();
/** @var SpuRepository $spuRep */
$spuRep = app()->make(SpuRepository::class);
$products = $spuRep->getApiSearch($where, 1, 4, false, true);
if ($products['list']) {
$list = $products['list'];
foreach ($cloud_product_arr as $key => $value) {
foreach ($list as $k => $v) {
if ($value['product_id'] == $v['product_id']) {
if ($value['mer_labels'] == ',5,') {
$list[$k]['mer_labels_name'] = '五日达';
} else {
$list[$k]['mer_labels_name'] = '同城';
}
}
}
}
}
return ['count' => $count, 'list' => $list ?? []];
}
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:13:19 +08:00
$find = Db::name('store_activity_order_product')->where('user_id', $userId)->where('product_id',$productId)->where('status', 1)->find();
$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');
foreach ($productIds as $productId) {
$model = new StoreActivityOrderProduct();
$model->user_id = $order['uid'];
$model->activity_id = $activityId;
$model->product_id = $productId;
$model->number = 1;
if (!$model->save()) {
throw new ValidateException('活动商品数据保存失败');
}
}
}
2023-05-10 13:38:51 +08:00
}