shop-php/app/common/dao/store/StoreActivityDao.php
mkm e2eb6d27c5 Merge branch 'master' of https://gitea.lihaink.cn/mkm/shop-php
Conflicts:
	app/common/dao/store/StoreActivityDao.php
2024-01-26 09:39:11 +08:00

180 lines
6.9 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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;
use app\common\dao\store\product\CloudProductDao;
use app\common\model\store\product\CloudProduct;
use app\common\model\store\product\Product;
use app\common\model\store\StoreActivity;
use app\common\model\store\StoreActivityOrderProduct;
use app\common\repositories\store\product\SpuRepository;
use think\db\Query;
use think\exception\ValidateException;
use think\facade\Db;
/**
*
* 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);
}
/**
* 活动商品专区
* @param $userId
* @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($userId, $location, $streetCode, $activityId)
{
[$lat, $lng] = (new CityAreaDao())->getLngAndLat($location, $streetCode);
$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' => []]);
}
/** @var SpuRepository $spuRep */
$spuRep = app()->make(SpuRepository::class);
$products = $spuRep->getApiSearch($where, 1, 4, false, true);
$canBuy = 1;
if ($products['list']) {
$list = $products['list'];
$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');
foreach ($cloud_product_arr as $key => $value) {
if (!empty($userId)) {
$buyRecord = $this->canBuy($userId, $value['product_id']);
if (!$buyRecord['is_true']) {
$canBuy = 0;
}
}
foreach ($list as $k => $v) {
if ($activityId == 2) {
$list[$k]['limited_price'] = '2.00';
if (isset($oldProductList[$v['product']['old_product_id']])) {
$list[$k]['product']['stock'] = $oldProductList[$v['product']['old_product_id']]['stock'];
$list[$k]['stock'] = $oldProductList[$v['product']['old_product_id']]['stock'];
$list[$k]['product']['image'] = $list[$k]['product']->sellOutImage;
}
}
if ($value['product_id'] == $v['product_id']) {
if ($value['mer_labels'] == ',5,') {
$list[$k]['mer_labels_name'] = '五日达';
} else {
$list[$k]['mer_labels_name'] = '同城';
}
}
}
}
}
return ['can_buy' => $canBuy, 'count' => $products['count'], 'list' => $list ?? []];
}
/**
* 是否可购买活动商品
* @param $userId
* @param $productId
* @return bool|int
* @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');
$find = Db::name('store_activity_order_product')->where('user_id', $userId)->where('status', 1)->find();
$data=['activityId'=>$activityId,'is_true'=>true];
if ($find && $activityId == 2) {
$data['is_true']=false;
}
return $data;
}
/**
* 保存活动订单商品
* @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('活动商品数据保存失败');
}
}
}
/**
* 根据活动获取对应的镇街
* @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);
}
}