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

145 lines
4.2 KiB
PHP

<?php
namespace app\admin\logic;
use app\common\model\ActivityZone;
use app\common\logic\BaseLogic;
use app\common\model\ActivityZoneForm;
use app\common\model\store_product\StoreProduct;
use app\common\model\store_product_unit\StoreProductUnit;
use app\common\service\xlsx\ActivityZoneService;
use support\exception\BusinessException;
use think\facade\Db;
/**
* ActivityZone逻辑
* Class ActivityZoneLogic
* @package app\admin\logic
*/
class ActivityZoneFormLogic 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 {
$secondCateIds = [];
$thirdCateIds = [];
foreach ($params['cate_ids'] as $item) {
if (count($item) == 3) {
$thirdCateIds[] = $item[2];
} else {
$secondCateIds[] = $item[1];
}
}
$cateIds = array_merge($secondCateIds, $thirdCateIds);
$params['cate_ids'] = !empty($cateIds) ? implode(',', $cateIds) : '';
$activityZoneForm = new ActivityZoneForm();
$activityZoneForm->save($params);
$productIds = StoreProduct::where('two_cate_id', 'in', $secondCateIds)->whereOr('cate_id', 'in', $thirdCateIds)->column('id');
$productInfo = [];
$time = time();
foreach ($productIds as $productId) {
$productInfo[] = [
'product_id' => $productId,
'form_id' => $activityZoneForm->id,
'create_time' => $time,
'update_time' => $time,
];
}
if (!empty($productInfo)) {
ActivityZone::insertAll($productInfo);
}
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 {
ActivityZoneForm::where('id', $params['id'])->update([
'type' => $params['type'],
'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 ActivityZoneForm::destroy($params['id']);
}
/**
* @notes 获取详情
* @param $params
* @return array
* @author admin
* @date 2024/12/20 10:52
*/
public static function detail($params): array
{
return ActivityZoneForm::findOrEmpty($params['id'])->toArray();
}
public static function export($params)
{
$service = new ActivityZoneService();
$activityZoneForm = ActivityZoneForm::findOrEmpty($params['id'])->toArray();
$products = ActivityZone::with('product')->field('id,product_id')->where('form_id', $params['id'])->select()->toArray();
$unitIds = array_unique(array_column($products, 'unit'));
$unit = StoreProductUnit::whereIn('id', $unitIds)->field('id,name')->withTrashed()->select()->toArray();
$unit = reset_index($unit, 'id');
foreach ($products as &$item) {
$item['unit_name'] = $unit[$item['unit']]['name'] ?? '';
unset($item['unit'], $item['product_id']);
}
$data = ConfigLogic::getDictByType('activity_zone');
foreach ($data['activity_zone'] as $value) {
if ($value['value'] == $activityZoneForm['type']) {
$typeName = $value['remark'];
break;
}
}
return $service->export($products, $activityZoneForm['title'], $typeName ?? '', $activityZoneForm['remark']);
}
}