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

148 lines
4.6 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_category\StoreCategory;
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);
$products = StoreProduct::field('id,two_cate_id,cate_id')->where('two_cate_id', 'in', $cateIds)->whereOr('cate_id', 'in', $cateIds)->select()->toArray();
$productInfo = [];
$time = time();
foreach ($products as $product) {
$productInfo[] = [
'product_id' => $product['id'],
'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();
$productIds = ActivityZone::where('form_id', $params['id'])->column('product_id');
$products = StoreProduct::field('id,unit,store_name,two_cate_id')->whereIn('id', $productIds)->order('two_cate_id asc,cate_id asc')->select()->toArray();
$unitIds = array_unique(array_column($products, 'unit'));
$cateIds = array_unique(array_column($products, 'two_cate_id'));
$unit = StoreProductUnit::whereIn('id', $unitIds)->field('id,name')->withTrashed()->select()->toArray();
$categories = StoreCategory::whereIn('id', $cateIds)->field('id,name')->withTrashed()->select()->toArray();
$unit = reset_index($unit, 'id');
$categories = reset_index($categories, 'id');
$data = [];
foreach ($products as $item) {
$currentCate = $categories[$item['two_cate_id']]['name'] ?? '';
if (!empty($currentCate)) {
$item['unit_name'] = $unit[$item['unit']]['name'] ?? '';
unset($item['unit'], $item['two_cate_id']);
$data[$currentCate][] = $item;
}
}
return $service->export($data, $activityZoneForm['title'], $activityZoneForm['remark']);
}
}