fixex 修改产品类型和新闻导入导出
This commit is contained in:
parent
575dea7f7d
commit
c151191430
@ -25,25 +25,26 @@ class News extends BaseController
|
||||
$params = $request->param();
|
||||
$con = [];
|
||||
|
||||
$con = Tool::getOptionalQuery(['news_type','='],['news_title','LIKE'],);
|
||||
$con = Tool::getOptionalQuery(['news_type', '='], ['news_title', 'LIKE'],);
|
||||
|
||||
$query = ModelNews::where($con)
|
||||
->field([
|
||||
'news_id',
|
||||
'news_guid',
|
||||
'news_type',
|
||||
'news_title',
|
||||
'news_intro',
|
||||
'news_source',
|
||||
'news_link',
|
||||
'news_issue_date',
|
||||
'news_views_num',
|
||||
'news_sort',
|
||||
'news_content'
|
||||
])
|
||||
'news_id',
|
||||
'news_guid',
|
||||
'news_type',
|
||||
'news_cover',
|
||||
'news_title',
|
||||
'news_intro',
|
||||
'news_source',
|
||||
'news_link',
|
||||
'news_issue_date',
|
||||
'news_views_num',
|
||||
'news_sort',
|
||||
'news_content'
|
||||
])
|
||||
->order('news_sort', 'asc');
|
||||
|
||||
return $isExport ? $query->select()->toArray() : msg("获取新闻列表成功!",$query);
|
||||
return $isExport ? $query->select()->toArray() : msg("获取新闻列表成功!", $query);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -54,29 +55,31 @@ class News extends BaseController
|
||||
Db::startTrans();
|
||||
Tool::adminLockTableWrite('news');
|
||||
try {
|
||||
$params = $request->param();
|
||||
$this->validate($params, [
|
||||
'news_type|新闻类型'=>'require',
|
||||
'news_title|新闻标题'=>'require',
|
||||
'news_sort|新闻排序'=>'require'
|
||||
]);
|
||||
$model = ModelNews::create($params,[
|
||||
'news_type',
|
||||
'news_title',
|
||||
'news_intro',
|
||||
'news_source',
|
||||
'news_link',
|
||||
'news_issue_date',
|
||||
'news_views_num',
|
||||
'news_sort',
|
||||
'news_content',
|
||||
'news_guid',
|
||||
'news_create_user_guid',
|
||||
'news_update_user_guid'
|
||||
]);
|
||||
Db::commit();
|
||||
Tool::unlockTable();
|
||||
return msg('添加成功!');
|
||||
$params = $request->param();
|
||||
$this->validate($params, [
|
||||
'news_type|新闻类型' => 'require',
|
||||
'news_title|新闻标题' => 'require',
|
||||
'news_cover|新闻封面' => 'require',
|
||||
'news_sort|新闻排序' => 'require'
|
||||
]);
|
||||
$model = ModelNews::create($params, [
|
||||
'news_type',
|
||||
'news_title',
|
||||
'news_cover',
|
||||
'news_intro',
|
||||
'news_source',
|
||||
'news_link',
|
||||
'news_issue_date',
|
||||
'news_views_num',
|
||||
'news_sort',
|
||||
'news_content',
|
||||
'news_guid',
|
||||
'news_create_user_guid',
|
||||
'news_update_user_guid'
|
||||
]);
|
||||
Db::commit();
|
||||
Tool::unlockTable();
|
||||
return msg('添加成功!');
|
||||
} catch (\Throwable $th) {
|
||||
Db::rollback();
|
||||
Tool::unlockTable();
|
||||
@ -92,29 +95,31 @@ class News extends BaseController
|
||||
Db::startTrans();
|
||||
Tool::adminLockTableWrite('news');
|
||||
try {
|
||||
$params = $request->param();
|
||||
$this->validate($params, [
|
||||
'news_type|新闻类型'=>'require',
|
||||
'news_title|新闻标题'=>'require',
|
||||
'news_sort|新闻排序'=>'require'
|
||||
]);
|
||||
$model = ModelNews::where('news_guid',$params['news_guid'])->find();
|
||||
if (!$model) throwErrorMsg("该新闻不存在", 1);
|
||||
$model->allowField([
|
||||
'news_type',
|
||||
'news_title',
|
||||
'news_intro',
|
||||
'news_source',
|
||||
'news_link',
|
||||
'news_issue_date',
|
||||
'news_views_num',
|
||||
'news_sort',
|
||||
'news_content',
|
||||
'news_update_user_guid'
|
||||
])->save($params);
|
||||
Db::commit();
|
||||
Tool::unlockTable();
|
||||
return msg('编辑成功!');
|
||||
$params = $request->param();
|
||||
$this->validate($params, [
|
||||
'news_type|新闻类型' => 'require',
|
||||
'news_title|新闻标题' => 'require',
|
||||
'news_cover|新闻封面' => 'require',
|
||||
'news_sort|新闻排序' => 'require'
|
||||
]);
|
||||
$model = ModelNews::where('news_guid', $params['news_guid'])->find();
|
||||
if (!$model) throwErrorMsg("该新闻不存在", 1);
|
||||
$model->allowField([
|
||||
'news_type',
|
||||
'news_title',
|
||||
'news_cover',
|
||||
'news_intro',
|
||||
'news_source',
|
||||
'news_link',
|
||||
'news_issue_date',
|
||||
'news_views_num',
|
||||
'news_sort',
|
||||
'news_content',
|
||||
'news_update_user_guid'
|
||||
])->save($params);
|
||||
Db::commit();
|
||||
Tool::unlockTable();
|
||||
return msg('编辑成功!');
|
||||
} catch (\Throwable $th) {
|
||||
Db::rollback();
|
||||
Tool::unlockTable();
|
||||
@ -130,17 +135,17 @@ class News extends BaseController
|
||||
Db::startTrans();
|
||||
Tool::adminLockTableWrite('news');
|
||||
try {
|
||||
$params = $request->param();
|
||||
$this->validate($params, [
|
||||
'news_guid' => 'require',
|
||||
]);
|
||||
$news = ModelNews::where([
|
||||
'news_guid' => explode(',', $params['news_guid'])
|
||||
])->select();
|
||||
$news->delete();
|
||||
Db::commit();
|
||||
Tool::unlockTable();
|
||||
return msg('删除成功!');
|
||||
$params = $request->param();
|
||||
$this->validate($params, [
|
||||
'news_guid' => 'require',
|
||||
]);
|
||||
$news = ModelNews::where([
|
||||
'news_guid' => explode(',', $params['news_guid'])
|
||||
])->select();
|
||||
$news->delete();
|
||||
Db::commit();
|
||||
Tool::unlockTable();
|
||||
return msg('删除成功!');
|
||||
} catch (\Throwable $th) {
|
||||
Db::rollback();
|
||||
Tool::unlockTable();
|
||||
@ -149,39 +154,49 @@ class News extends BaseController
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出Excel
|
||||
*/
|
||||
public function exportExcel(Request $request):void
|
||||
{
|
||||
ModelNews::exportExcel(self::getNewsList($request, true));
|
||||
}
|
||||
* 导出Excel
|
||||
*/
|
||||
public function exportExcel(Request $request): void
|
||||
{
|
||||
ModelNews::exportExcel(self::getNewsList($request, true));
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载导入模板
|
||||
*/
|
||||
public function downloadTemplate(Request $request):void
|
||||
{
|
||||
$params = $request->param();
|
||||
$data = [
|
||||
array_values(ModelNews::EXCELFIELD),
|
||||
['默认值1','默认值2','默认值3','默认值4','默认值5','默认值6','默认值7','默认值8','默认值9',]
|
||||
];
|
||||
$excel = (new Excel())->exporTsheet($data);
|
||||
$excel->save('新闻导入模板.xlsx');
|
||||
}
|
||||
* 下载导入模板
|
||||
*/
|
||||
public function downloadTemplate(Request $request): void
|
||||
{
|
||||
$params = $request->param();
|
||||
$data = [
|
||||
array_values(ModelNews::EXCELFIELD),
|
||||
[
|
||||
'公司新闻',
|
||||
'想学嵌入式Linux?领免费的瑞萨RZ/G2L开发板!',
|
||||
'https://www.myir-tech.com/attached/image/20230609/1-1.jpg',
|
||||
'学习嵌入式系统开发是一个渐进的过程,一般我们从51单片机开始,逐步迁移到STM32微控制器,然后学习使用FreeRTOS操作系统,最终进入嵌入式Linux领域。以下是一个典型的学习路线:51单片机:作为嵌入式系统的入门级平台,学习51单片机可以帮助我们了解基本的嵌入式开发概念和编程技巧。可以学习使用C语言编写简单的驱动程序、控制IO口和外设等。 ',
|
||||
'米尔科技',
|
||||
'',
|
||||
'2023.6.9',
|
||||
'0',
|
||||
'1',
|
||||
'<p style="text-align: start;"><strong>需要报名RZ/G2L免费开发板请扫码</strong></p><p style="text-align: center;"><img src="https://www.myir-tech.com/attached/image/20230609/22.png" alt="" data-href="" style=""></p>',
|
||||
]
|
||||
];
|
||||
$excel = (new Excel())->exporTsheet($data);
|
||||
$excel->save('新闻导入模板.xlsx');
|
||||
}
|
||||
|
||||
/**
|
||||
* 导入excel
|
||||
*/
|
||||
public function importExcel(Request $request):array
|
||||
{
|
||||
$file = new UploadFile('uploads', 'fileExt:xlsx');
|
||||
$file->putFile('news');
|
||||
$msg = ModelNews::importExcel($file);
|
||||
return [
|
||||
'code' => 0,
|
||||
'msg' => $msg
|
||||
];
|
||||
}
|
||||
|
||||
* 导入excel
|
||||
*/
|
||||
public function importExcel(Request $request): array
|
||||
{
|
||||
$file = new UploadFile('uploads', 'fileExt:xlsx');
|
||||
$file->putFile('news');
|
||||
$msg = ModelNews::importExcel($file);
|
||||
return [
|
||||
'code' => 0,
|
||||
'msg' => $msg
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -27,7 +27,7 @@ class ProductType extends BaseController
|
||||
* @author xjh
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function getProductTypeTree(Request $request): array
|
||||
public function getProductTypeTree(Request $request, $isExport = false): array
|
||||
{
|
||||
$con = Tool::getOptionalQuery(
|
||||
['b.product_delete_time', 'NULL'],
|
||||
@ -64,9 +64,50 @@ class ProductType extends BaseController
|
||||
];
|
||||
});
|
||||
|
||||
return msg("获取产品类目列表成功!", $product_type_tree);
|
||||
return $isExport ? $product_type : msg("获取产品类目列表成功!", $product_type_tree);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加产品类目接口
|
||||
*
|
||||
* @param Request request
|
||||
* @date 2023-03-25
|
||||
* @example
|
||||
* @author xjh
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function addProductType(Request $request): array
|
||||
{
|
||||
$params = $request->param();
|
||||
$this->validate($params, [
|
||||
'product_type_name|产品类型名称' => 'require',
|
||||
'product_type_order|产品类目排序' => 'require'
|
||||
]);
|
||||
|
||||
ModelProductType::isDuplicateName($params['product_type_name'], $params['product_type_parent_guid']);
|
||||
|
||||
$product_type_parent_where = [];
|
||||
if (!empty($params['product_type_parent_guid'])) {
|
||||
$params['product_type_ancestors_guid'] = ModelProductType::buildAncestorsGuid($params['product_type_parent_guid']);
|
||||
$product_type_parent_where['product_type_parent_guid'] = $params['product_type_parent_guid'];
|
||||
}
|
||||
|
||||
ModelProductType::create($params, [
|
||||
'product_type_guid',
|
||||
'product_type_create_user_guid',
|
||||
'product_type_update_user_guid',
|
||||
'product_type_parent_guid',
|
||||
'product_type_ancestors_guid',
|
||||
'product_type_name',
|
||||
'product_type_title',
|
||||
'product_type_link',
|
||||
'product_type_icon',
|
||||
'product_type_order'
|
||||
]);
|
||||
return msg('添加成功!');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 编辑产品类目接口
|
||||
*
|
||||
@ -88,6 +129,9 @@ class ProductType extends BaseController
|
||||
|
||||
$model = ModelProductType::where('product_type_guid', $params['product_type_guid'])->find();
|
||||
if (!$model) throwErrorMsg("该产品类目不存在", 1);
|
||||
if ($model->product_type_parent_guid == $model->product_type_guid) {
|
||||
throwErrorMsg("上级菜单不能选择与当前菜单一样的", 1);
|
||||
}
|
||||
|
||||
ModelProductType::isDuplicateName($params['product_type_name'], $params['product_type_guid']);
|
||||
|
||||
@ -107,46 +151,6 @@ class ProductType extends BaseController
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 添加产品类目接口
|
||||
*
|
||||
* @param Request request
|
||||
* @date 2023-03-25
|
||||
* @example
|
||||
* @author xjh
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function addProductType(Request $request): array
|
||||
{
|
||||
$params = $request->param();
|
||||
$this->validate($params, [
|
||||
'product_type_name|产品类型名称' => 'require',
|
||||
'product_type_order|产品类目排序' => 'require'
|
||||
]);
|
||||
|
||||
ModelProductType::isDuplicateName($params['product_type_name']);
|
||||
|
||||
$product_type_parent_where = [];
|
||||
if (!empty($params['product_type_parent_guid'])) {
|
||||
$params['product_type_ancestors_guid'] = ModelProductType::buildAncestorsGuid($params['product_type_parent_guid']);
|
||||
$product_type_parent_where['product_type_parent_guid'] = $params['product_type_parent_guid'];
|
||||
}
|
||||
|
||||
ModelProductType::create($params, [
|
||||
'product_type_guid',
|
||||
'product_type_create_user_guid',
|
||||
'product_type_update_user_guid',
|
||||
'product_type_parent_guid',
|
||||
'product_type_ancestors_guid',
|
||||
'product_type_name',
|
||||
'product_type_title',
|
||||
'product_type_link',
|
||||
'product_type_icon',
|
||||
'product_type_order'
|
||||
]);
|
||||
return msg('添加成功!');
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除产品类目接口
|
||||
*
|
||||
@ -186,15 +190,7 @@ class ProductType extends BaseController
|
||||
*/
|
||||
public function exportExcel(Request $request)
|
||||
{
|
||||
$params = $request->param();
|
||||
$select = ModelProductType::field([
|
||||
'product_type_title',
|
||||
'product_type_icon',
|
||||
'product_type_cover'
|
||||
])
|
||||
->order('product_type_update_time', 'desc')
|
||||
->select();
|
||||
return ModelProductType::exportExcel($select);
|
||||
ModelProductType::exportExcel(self::getProductTypeTree($request, true));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -9,6 +9,7 @@ use app\common\arw\adjfut\src\Excel;
|
||||
use app\Request;
|
||||
use app\common\exception\Tool;
|
||||
use think\facade\Db;
|
||||
use app\common\model\Dictionary\Dictionary as ModelDictionary;
|
||||
|
||||
class News extends BaseModel
|
||||
{
|
||||
@ -21,25 +22,26 @@ class News extends BaseModel
|
||||
protected $disuse = [];
|
||||
// 设置字段信息
|
||||
protected $schema = [
|
||||
'news_id' => 'int' ,
|
||||
'news_guid' => 'string' ,
|
||||
'news_type' => 'int' ,
|
||||
'news_title' => 'string' ,
|
||||
'news_intro' => 'string' ,
|
||||
'news_source' => 'string' ,
|
||||
'news_link' => 'string' ,
|
||||
'news_issue_date' => '' ,
|
||||
'news_views_num' => 'int' ,
|
||||
'news_sort' => 'int' ,
|
||||
'news_content' => '' ,
|
||||
'news_create_time' => 'datetime' ,
|
||||
'news_create_user_guid' => 'string' ,
|
||||
'news_update_time' => 'datetime' ,
|
||||
'news_update_user_guid' => 'string' ,
|
||||
'news_delete_time' => 'datetime' ,
|
||||
'news_delete_user_guid' => 'string' ,
|
||||
'news_id' => 'int',
|
||||
'news_guid' => 'string',
|
||||
'news_type' => 'int',
|
||||
'news_title' => 'string',
|
||||
'news_cover' => 'string',
|
||||
'news_intro' => 'string',
|
||||
'news_source' => 'string',
|
||||
'news_link' => 'string',
|
||||
'news_issue_date' => '',
|
||||
'news_views_num' => 'int',
|
||||
'news_sort' => 'int',
|
||||
'news_content' => '',
|
||||
'news_create_time' => 'datetime',
|
||||
'news_create_user_guid' => 'string',
|
||||
'news_update_time' => 'datetime',
|
||||
'news_update_user_guid' => 'string',
|
||||
'news_delete_time' => 'datetime',
|
||||
'news_delete_user_guid' => 'string',
|
||||
|
||||
];
|
||||
];
|
||||
// 设置json类型字段
|
||||
protected $json = [''];
|
||||
// 开启自动写入时间戳字段
|
||||
@ -52,18 +54,19 @@ class News extends BaseModel
|
||||
//排序字段
|
||||
public $order_field = 'news_sort';
|
||||
|
||||
// excel导入/下载模板表头
|
||||
public const EXCELFIELD = [
|
||||
'news_type' => '新闻类型',
|
||||
'news_title' => '新闻标题',
|
||||
'news_intro' => '新闻简介',
|
||||
'news_source' => '文章来源',
|
||||
'news_link' => '跳转链接',
|
||||
'news_issue_date' => '发布日期',
|
||||
'news_views_num' => '浏览次数',
|
||||
'news_sort' => '新闻排序',
|
||||
'news_content' => '新闻内容',
|
||||
];
|
||||
// excel导入/下载模板表头
|
||||
public const EXCELFIELD = [
|
||||
'news_type' => '新闻类型',
|
||||
'news_title' => '新闻标题',
|
||||
'news_cover' => '新闻封面',
|
||||
'news_intro' => '新闻简介',
|
||||
'news_source' => '文章来源',
|
||||
'news_link' => '跳转链接',
|
||||
'news_issue_date' => '发布日期',
|
||||
'news_views_num' => '浏览次数',
|
||||
'news_sort' => '新闻排序',
|
||||
'news_content' => '新闻内容',
|
||||
];
|
||||
|
||||
/**
|
||||
* 新增前
|
||||
@ -92,114 +95,138 @@ class News extends BaseModel
|
||||
$model->completeDeleteField();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 导出Excel
|
||||
*
|
||||
* @param array $select 导出的数据
|
||||
*/
|
||||
public static function exportExcel(array $select): void
|
||||
{
|
||||
$data = [[
|
||||
'新闻类型',
|
||||
'新闻标题',
|
||||
'新闻简介',
|
||||
'文章来源',
|
||||
'跳转链接',
|
||||
'发布日期',
|
||||
'浏览次数',
|
||||
'新闻排序',
|
||||
'新闻内容'
|
||||
]];
|
||||
foreach ($select as $key => $val) {
|
||||
$data[] = [
|
||||
$val['news_type'],
|
||||
$val['news_title'],
|
||||
$val['news_intro'],
|
||||
$val['news_source'],
|
||||
$val['news_link'],
|
||||
$val['news_issue_date'],
|
||||
$val['news_views_num'],
|
||||
$val['news_sort'],
|
||||
$val['news_content'],
|
||||
];
|
||||
}
|
||||
$excel = (new Excel())->exporTsheet($data);
|
||||
$excel->save('新闻.xlsx');
|
||||
|
||||
/**
|
||||
* 导出Excel
|
||||
*
|
||||
* @param array $select 导出的数据
|
||||
*/
|
||||
public static function exportExcel(array $select): void
|
||||
{
|
||||
$data = [[
|
||||
'新闻类型',
|
||||
'新闻标题',
|
||||
'新闻封面',
|
||||
'新闻简介',
|
||||
'文章来源',
|
||||
'跳转链接',
|
||||
'发布日期',
|
||||
'浏览次数',
|
||||
'新闻排序',
|
||||
// '新闻内容'
|
||||
]];
|
||||
foreach ($select as $key => $val) {
|
||||
|
||||
// 获取新闻类型名称
|
||||
$news_type = ModelDictionary::getDictionaryData('news_type');
|
||||
$val['news_type'] = ModelDictionary::getDataDictionaryName($news_type, $val['news_type']);
|
||||
|
||||
$data[] = [
|
||||
$val['news_type'],
|
||||
$val['news_title'],
|
||||
Excel::ExportImgFiled($val['news_cover']),
|
||||
$val['news_intro'],
|
||||
$val['news_source'],
|
||||
$val['news_link'],
|
||||
$val['news_issue_date'],
|
||||
$val['news_views_num'],
|
||||
$val['news_sort'],
|
||||
// $val['news_content'],
|
||||
];
|
||||
}
|
||||
$excel = (new Excel())->exporTsheet($data);
|
||||
$excel->save('新闻.xlsx');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 导入excel
|
||||
*
|
||||
* @param \app\common\arw\adjfut\src\UploadFile $file excel
|
||||
*/
|
||||
public static function importExcel(\app\common\arw\adjfut\src\UploadFile $file): string
|
||||
{
|
||||
$msg = [];
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
$excel = new Excel($file);
|
||||
$data = $excel->parseExcel(
|
||||
Tool::getExcelRule(self::EXCELFIELD),
|
||||
['titleLine' => [1]]);
|
||||
if (!$data) throwErrorMsg('excel无数据', 1);
|
||||
$msg = [];
|
||||
foreach ($data as $line => $value) {
|
||||
try {
|
||||
$model = self::importExcelInit($value);
|
||||
$msg[] = "{$line} <span style='color:#27af49'>新增成功!</span><br>";
|
||||
} catch (\Throwable $th) {
|
||||
$msg[] = "{$line} <span style='color:red'>{$th->getMessage()}</span><br>";
|
||||
}
|
||||
}
|
||||
Db::commit();
|
||||
return implode(', ', $msg);
|
||||
} catch (\Throwable $th) {
|
||||
Db::rollback();
|
||||
throw $th;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 导入excel
|
||||
*
|
||||
* @param \app\common\arw\adjfut\src\UploadFile $file excel
|
||||
*/
|
||||
public static function importExcel(\app\common\arw\adjfut\src\UploadFile $file): string
|
||||
{
|
||||
$msg = [];
|
||||
|
||||
|
||||
/**
|
||||
* 导入excel初始化
|
||||
*
|
||||
* @param array $value excel每行数据
|
||||
*/
|
||||
public static function importExcelInit(array $value):void
|
||||
{
|
||||
$news_type = $value['news_type'];$news_title = $value['news_title'];$news_intro = $value['news_intro'];$news_source = $value['news_source'];$news_link = $value['news_link'];$news_issue_date = $value['news_issue_date'];$news_views_num = $value['news_views_num'];$news_sort = $value['news_sort'];$news_content = $value['news_content'];
|
||||
|
||||
self::create(
|
||||
['news_type' => $news_type,
|
||||
'news_title' => $news_title,
|
||||
'news_intro' => $news_intro,
|
||||
'news_source' => $news_source,
|
||||
'news_link' => $news_link,
|
||||
'news_issue_date' => $news_issue_date,
|
||||
'news_views_num' => $news_views_num,
|
||||
'news_sort' => $news_sort,
|
||||
'news_content' => $news_content,
|
||||
],
|
||||
[
|
||||
'news_type',
|
||||
'news_title',
|
||||
'news_intro',
|
||||
'news_source',
|
||||
'news_link',
|
||||
'news_issue_date',
|
||||
'news_views_num',
|
||||
'news_sort',
|
||||
'news_content',
|
||||
'news_guid',
|
||||
'news_create_user_guid',
|
||||
'news_update_user_guid'
|
||||
]
|
||||
Db::startTrans();
|
||||
try {
|
||||
$excel = new Excel($file);
|
||||
$data = $excel->parseExcel(
|
||||
Tool::getExcelRule(self::EXCELFIELD),
|
||||
['titleLine' => [1]]
|
||||
);
|
||||
if (!$data) throwErrorMsg('excel无数据', 1);
|
||||
$msg = [];
|
||||
foreach ($data as $line => $value) {
|
||||
try {
|
||||
$model = self::importExcelInit($value);
|
||||
$msg[] = "{$line} <span style='color:#27af49'>新闻:【{$value['news_title']}】 新增成功!</span><br>";
|
||||
} catch (\Throwable $th) {
|
||||
$msg[] = "{$line} <span style='color:red'>{$th->getMessage()}</span><br>";
|
||||
}
|
||||
}
|
||||
Db::commit();
|
||||
return implode(', ', $msg);
|
||||
} catch (\Throwable $th) {
|
||||
Db::rollback();
|
||||
throw $th;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 导入excel初始化
|
||||
*
|
||||
* @param array $value excel每行数据
|
||||
*/
|
||||
public static function importExcelInit(array $value): void
|
||||
{
|
||||
$news_type = $value['news_type'];
|
||||
$news_title = $value['news_title'];
|
||||
$news_cover = $value['news_cover'];
|
||||
$news_intro = $value['news_intro'];
|
||||
$news_source = $value['news_source'];
|
||||
$news_link = $value['news_link'];
|
||||
$news_issue_date = $value['news_issue_date'];
|
||||
$news_views_num = $value['news_views_num'];
|
||||
$news_sort = $value['news_sort'];
|
||||
$news_content = $value['news_content'];
|
||||
|
||||
|
||||
// 根据新闻类型名称获取新闻类型的字段值
|
||||
$news_type_dic = ModelDictionary::getDictionaryData('news_type');
|
||||
$news_type_value = ModelDictionary::getDataDictionaryValue($news_type_dic, $news_type);
|
||||
if (!$news_type_value) throwErrorMsg("新闻类型:【{$news_type}】 不存在,请重新确认!");
|
||||
|
||||
|
||||
self::create(
|
||||
[
|
||||
'news_type' => $news_type_value,
|
||||
'news_title' => $news_title,
|
||||
'news_cover' => $news_cover,
|
||||
'news_intro' => $news_intro,
|
||||
'news_source' => $news_source,
|
||||
'news_link' => $news_link,
|
||||
'news_issue_date' => $news_issue_date,
|
||||
'news_views_num' => $news_views_num,
|
||||
'news_sort' => $news_sort,
|
||||
'news_content' => $news_content,
|
||||
],
|
||||
[
|
||||
'news_type',
|
||||
'news_title',
|
||||
'news_cover',
|
||||
'news_intro',
|
||||
'news_source',
|
||||
'news_link',
|
||||
'news_issue_date',
|
||||
'news_views_num',
|
||||
'news_sort',
|
||||
'news_content',
|
||||
'news_guid',
|
||||
'news_create_user_guid',
|
||||
'news_update_user_guid'
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -96,17 +96,18 @@ class ProductType extends BaseModel
|
||||
* @param string $product_type_name 产品类目名称
|
||||
* @param string $product_type_guid 产品类目guid
|
||||
*/
|
||||
public static function isDuplicateName(string $product_type_name, string $product_type_guid = null): void
|
||||
public static function isDuplicateName(string $product_type_name, string $product_type_parent_guid = null): void
|
||||
{
|
||||
$con = [
|
||||
['product_type_name', '=', $product_type_name]
|
||||
];
|
||||
if ($product_type_guid) {
|
||||
$con[] = ['product_type_guid', '<>', $product_type_guid];
|
||||
if ($product_type_parent_guid) {
|
||||
$parent_children = self::where('product_type_parent_guid', $product_type_parent_guid)->select();
|
||||
if ($parent_children) {
|
||||
foreach ($parent_children as $key => $value) {
|
||||
if ($product_type_name == $value['product_type_name']) {
|
||||
throwErrorMsg('产品类目不可重名!');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (self::where($con)->find()) {
|
||||
throwErrorMsg('产品类目不可重名!');
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@ -115,15 +116,28 @@ class ProductType extends BaseModel
|
||||
public static function exportExcel($select)
|
||||
{
|
||||
$data = [[
|
||||
'上级类目',
|
||||
'产品类目名称',
|
||||
'产品类目标题',
|
||||
'产品类目图标',
|
||||
'产品类目封面'
|
||||
'产品类目链接',
|
||||
'排序',
|
||||
'图标',
|
||||
]];
|
||||
|
||||
foreach ($select as $key => $val) {
|
||||
// $product_type_name = "";
|
||||
// if($val['product_type_parent_guid'] != 0){
|
||||
// $product_type_parent = self::where('product_type_guid', $val['product_type_parent_guid'])->find();
|
||||
// $product_type_name = $product_type_parent['product_type_name'];
|
||||
// }
|
||||
$data[] = [
|
||||
// $product_type_parent->product_type_name,
|
||||
$val['product_type_parent_name'],
|
||||
$val['product_type_name'],
|
||||
$val['product_type_title'],
|
||||
$val['product_type_icon'],
|
||||
Excel::ExportImgFiled($val['product_type_cover']),
|
||||
$val['product_type_link'],
|
||||
$val['product_type_order'],
|
||||
Excel::ExportImgFiled($val['product_type_icon']),
|
||||
];
|
||||
}
|
||||
$excel = (new Excel())->exporTsheet($data);
|
||||
|
BIN
public/excel/news/新闻导入模板.xlsx
Normal file
BIN
public/excel/news/新闻导入模板.xlsx
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user