|
@@ -987,11 +987,16 @@ class WebsiteService implements WebsiteServiceInterface
|
|
|
{
|
|
|
$where = [
|
|
|
'website_id' => $data['website_id'],
|
|
|
- 'pid' => 0,
|
|
|
];
|
|
|
- $result = WebsiteCategory::where($where)->get();
|
|
|
+ $result = WebsiteCategory::where($where)->orderBy('sort', 'asc')->get();
|
|
|
+ $list = [];
|
|
|
+ if($result){
|
|
|
+ foreach ($result->toArray() as $val) {
|
|
|
+ array_push($list, json_decode($val['category_arr_id']));
|
|
|
+ }
|
|
|
+ }
|
|
|
if ($result) {
|
|
|
- return Result::success($result);
|
|
|
+ return Result::success($list);
|
|
|
} else {
|
|
|
return Result::error("查询失败", 0);
|
|
|
}
|
|
@@ -1006,83 +1011,82 @@ class WebsiteService implements WebsiteServiceInterface
|
|
|
{
|
|
|
Db::beginTransaction();
|
|
|
try {
|
|
|
- //合并栏目id
|
|
|
- $reqIds = array_merge($data['old_category_arr_id'], $data['new_category_arr_id']);
|
|
|
- //对比old 数组差异化,把差异化的删除
|
|
|
- $result = WebsiteCategory::where(['website_id' => $data['website_id'], 'pid' => 0])->get();
|
|
|
- $result = $result->toArray();
|
|
|
- $categoryIds = [];
|
|
|
- if ($result) {
|
|
|
- foreach ($result as $val) {
|
|
|
- array_push($categoryIds, $val['category_id']);
|
|
|
- }
|
|
|
- }
|
|
|
- //和原始数据对比取交际
|
|
|
- $reqidsIntersect = array_intersect($reqIds, $categoryIds);
|
|
|
- //再取差集 进行对比
|
|
|
- $differenceIDS = array_merge(array_diff($reqidsIntersect, $categoryIds), array_diff($categoryIds, $reqidsIntersect));
|
|
|
- var_dump("差集:", $differenceIDS);
|
|
|
- $arr_ids = [];
|
|
|
- if (count($differenceIDS) > 0) {
|
|
|
- foreach ($differenceIDS as $vv) {
|
|
|
- $idV = $this->getUnderlingUIds(intval($vv));
|
|
|
- $ids_arrV = explode(",", $idV);
|
|
|
- array_push($arr_ids, $ids_arrV);
|
|
|
- }
|
|
|
- }
|
|
|
- $del_ids = array_reduce($arr_ids, 'array_merge', array());
|
|
|
- //有差异 删除
|
|
|
- if (count($del_ids) > 0) {
|
|
|
- WebsiteCategory::where(['website_id' => $data['website_id']])->whereIn("category_id", $del_ids)->delete();
|
|
|
- }
|
|
|
- //传过来的值 和 交际 对比,选出要添加的值 进行插入
|
|
|
- $insertIDS = array_merge(array_diff($reqIds, $reqidsIntersect), array_diff($reqidsIntersect, $reqIds));
|
|
|
- var_dump("要存储的:", $insertIDS);
|
|
|
- //新的数组重新创建
|
|
|
- if (count($insertIDS) > 0) {
|
|
|
- $arr = [];
|
|
|
- $categoryListIds = $insertIDS;
|
|
|
- if ($categoryListIds) {
|
|
|
- foreach ($categoryListIds as $v) {
|
|
|
- $ids = $this->getUnderlingUIds(intval($v));
|
|
|
- $ids_arr = explode(",", $ids);
|
|
|
- array_push($arr, $ids_arr);
|
|
|
- }
|
|
|
- }
|
|
|
- $mergedArray = [];
|
|
|
- foreach ($arr as $subarray) {
|
|
|
- $mergedArray = array_merge($mergedArray, $subarray);
|
|
|
- }
|
|
|
- var_dump("要插入的ID:", $mergedArray);
|
|
|
- //查询出所有的分类进行分割插入 组装数据
|
|
|
- $categoryListData = Category::whereIn('id', $mergedArray)->get();
|
|
|
- $categoryListData = $categoryListData->toArray();
|
|
|
- $insertData = [];
|
|
|
- if ($categoryListData) {
|
|
|
- foreach ($categoryListData as $key => $value) {
|
|
|
- $insertData[$key]['website_id'] = $data['website_id'];
|
|
|
- $insertData[$key]['name'] = $value['name'];
|
|
|
- $insertData[$key]['sort'] = $value['sort'];
|
|
|
- $insertData[$key]['pid'] = $value['pid'];
|
|
|
- $insertData[$key]['pid_arr'] = $value['pid_arr'];
|
|
|
- $insertData[$key]['seo_title'] = $value['seo_title'];
|
|
|
- $insertData[$key]['seo_keywords'] = $value['seo_keywords'];
|
|
|
- $insertData[$key]['seo_description'] = $value['seo_description'];
|
|
|
- $insertData[$key]['alias'] = $value['name'];
|
|
|
- $insertData[$key]['category_id'] = $value['id'];
|
|
|
- }
|
|
|
- }
|
|
|
- WebsiteCategory::insert($insertData);
|
|
|
-
|
|
|
- }
|
|
|
+ if($data['category_arr_id']){
|
|
|
+ $oldCategoryList = WebsiteCategory::where(['website_id' => $data['website_id']])->get();
|
|
|
+ if($oldCategoryList){
|
|
|
+ $oldCategoryList = $oldCategoryList->toArray();
|
|
|
+ $oldCategoryListIds = [];
|
|
|
+ foreach ($oldCategoryList as $val) {
|
|
|
+ array_push($oldCategoryListIds, $val['category_id']);
|
|
|
+ }
|
|
|
+ $newCategoryListIds = [];
|
|
|
+ foreach ($data['category_arr_id'] as $v){
|
|
|
+ array_push($newCategoryListIds, end($v));
|
|
|
+ }
|
|
|
+ $differenceIDS = array_merge(array_diff($oldCategoryListIds, $newCategoryListIds), array_diff($newCategoryListIds, $oldCategoryListIds));
|
|
|
+ if($differenceIDS){
|
|
|
+ foreach ($differenceIDS as $v){
|
|
|
+ WebsiteCategory::where(['website_id' => $data['website_id'], 'category_id' => $v])->delete();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ foreach ($data['category_arr_id'] as $key => $value){
|
|
|
+ $info = Category::where(['id' => end($value)])->first();
|
|
|
+ $info = $info->toArray();
|
|
|
+ // 假设 $data、$value 和 $info 是已经定义好的变量
|
|
|
+ $attributes = [
|
|
|
+ 'website_id' => $data['website_id'],
|
|
|
+ 'category_id' => end($value)
|
|
|
+ ];
|
|
|
+ // 先复制一份要更新或创建的数据
|
|
|
+ $values = [
|
|
|
+ 'website_id' => $data['website_id'],
|
|
|
+ 'name' => $info['name']?? '',
|
|
|
+ 'alias' => $info['name']?? '',
|
|
|
+ 'sort' => $info['sort']?? 0,
|
|
|
+ 'pid' => $info['pid']?? 0,
|
|
|
+ 'pid_arr' => $info['pid_arr']?? json_encode([]),
|
|
|
+ 'seo_title' => $info['seo_title']?? "",
|
|
|
+ 'seo_keywords' => $info['seo_keywords']?? "",
|
|
|
+ 'seo_description' => $info['seo_description']?? "",
|
|
|
+ 'is_url' => $info['is_url']?? 0,
|
|
|
+ 'web_url' => $info['web_url']?? '',
|
|
|
+ 'category_id' => end($value),
|
|
|
+ 'category_arr_id' => $value? json_encode($value) : json_encode([])
|
|
|
+ ];
|
|
|
+ // 检查数据库中是否已有该记录
|
|
|
+ $existingRecord = WebsiteCategory::where($attributes)->first();
|
|
|
+ if ($existingRecord && $existingRecord->name) {
|
|
|
+ unset($values['name']);
|
|
|
+ }
|
|
|
+ if ($existingRecord && $existingRecord->sort) {
|
|
|
+ unset($values['sort']);
|
|
|
+ }
|
|
|
+ if ($existingRecord && $existingRecord->seo_title) {
|
|
|
+ unset($values['seo_title']);
|
|
|
+ }
|
|
|
+ if ($existingRecord && $existingRecord->seo_keywords) {
|
|
|
+ unset($values['seo_keywords']);
|
|
|
+ }
|
|
|
+ if ($existingRecord && $existingRecord->seo_description) {
|
|
|
+ unset($values['seo_description']);
|
|
|
+ }
|
|
|
+ if ($existingRecord && $existingRecord->alias) {
|
|
|
+ unset($values['alias']);
|
|
|
+ }
|
|
|
+ // 执行 updateOrCreate 操作
|
|
|
+ WebsiteCategory::updateOrCreate($attributes, $values);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
Db::commit();
|
|
|
+ return Result::success();
|
|
|
} catch (\Throwable $ex) {
|
|
|
Db::rollBack();
|
|
|
- var_dump($ex->getMessage());
|
|
|
- return Result::error("修改失败", 0);
|
|
|
+// var_dump($ex->getMessage());
|
|
|
+ return Result::error("修改失败".$ex->getMessage(), 0);
|
|
|
}
|
|
|
|
|
|
- return Result::success();
|
|
|
+
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -1632,4 +1636,23 @@ class WebsiteService implements WebsiteServiceInterface
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @param array $data
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function getAllCategory(array $data): array
|
|
|
+ {
|
|
|
+ $result = Category::when($data, function ($query) use ($data) {
|
|
|
+ if (isset($data['name']) && !empty($data['name'])) {
|
|
|
+ $query->where('category.name', 'like', '%' . trim($data['name']) . '%');
|
|
|
+ }
|
|
|
+ })->get();
|
|
|
+ if (empty($result)) {
|
|
|
+ return Result::error("获取失败", 0);
|
|
|
+ } else {
|
|
|
+ return Result::success($result);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|