multi-store/app/admin/logic/ActivityZoneLogic.php

140 lines
3.8 KiB
PHP

<?php
namespace app\admin\logic;
use app\common\model\ActivityZone;
use app\common\logic\BaseLogic;
use app\common\model\ActivityZoneForm;
use support\exception\BusinessException;
use think\facade\Db;
/**
* ActivityZone逻辑
* Class ActivityZoneLogic
* @package app\admin\logic
*/
class ActivityZoneLogic extends BaseLogic
{
/**
* @notes 添加
* @param array $params
* @return bool
* @author admin
* @date 2024/12/20 10:52
*/
public static function add(array $params): bool
{
Db::startTrans();
try {
$insert = [];
$time = time();
foreach ($params['product_ids'] as $product_id) {
$insert[] = [
'form_id' => $params['form_id'],
'product_id' => $product_id,
'create_time' => $time,
'update_time' => $time,
];
}
ActivityZone::insertAll($insert);
Db::commit();
return true;
} catch (\Throwable $e) {
Db::rollback();
throw new BusinessException($e->getMessage());
}
}
/**
* @notes 编辑
* @param array $params
* @return bool
* @author admin
* @date 2024/12/20 10:52
*/
public static function edit(array $params): bool
{
Db::startTrans();
try {
ActivityZone::where('id', $params['id'])->update([
'form_id' => $params['form_id'],
'product_id' => $params['product_id'],
]);
Db::commit();
return true;
} catch (\Throwable $e) {
Db::rollback();
throw new BusinessException($e->getMessage());
}
}
/**
* @notes 删除
* @param array $params
* @return bool
* @author admin
* @date 2024/12/20 10:52
*/
public static function delete(array $params): bool
{
return ActivityZone::destroy($params['id']);
}
/**
* @notes 获取详情
* @param $params
* @return array
* @author admin
* @date 2024/12/20 10:52
*/
public static function detail($params): array
{
return ActivityZone::findOrEmpty($params['id'])->toArray();
}
public function addProduct($product)
{
$activityFormId1 = ActivityZoneForm::whereRaw('FIND_IN_SET(:cate_id,cate_ids)', ['cate_id' => $product['two_cate_id']])->column('id');
$activityFormId2 = ActivityZoneForm::whereRaw('FIND_IN_SET(:cate_id,cate_ids)', ['cate_id' => $product['cate_id']])->column('id');
$activityFormIds = array_unique(array_merge($activityFormId1, $activityFormId2));
foreach ($activityFormIds as $activityFormId) {
$activityZone = new ActivityZone();
$activityZone->form_id = $activityFormId;
$activityZone->product_id = $product['id'];
$activityZone->save();
}
}
public function updateProduct($productId, $product)
{
$product['id'] = $productId;
$formIds = ActivityZone::where('product_id', $productId)->column('form_id');
if (empty($formIds)) {
$this->addProduct($product);
return;
}
$forms = ActivityZoneForm::whereIn('id', $formIds)->select()->toArray();
foreach ($forms as $form) {
$cateIds = explode(',', $form['cate_ids']);
if (!in_array($product['two_cate_id'], $cateIds) && !in_array($product['cate_id'], $cateIds)) {
ActivityZone::where('product_id', $productId)->where('form_id', $form['id'])->update(['delete_time' => time()]);
}
$this->addProduct($product);
}
}
public function deleteProduct($productId)
{
ActivityZone::where('product_id', $productId)->update(['delete_time' => time()]);
}
}