You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
200 lines
7.2 KiB
200 lines
7.2 KiB
2 months ago
|
<?php
|
||
|
|
||
|
namespace app\admin\service\document;
|
||
|
|
||
|
use app\admin\service\AdminBaseService;
|
||
|
use app\model\AnnouncementModel;
|
||
|
use app\model\LanguageSettingModel;
|
||
|
|
||
|
class AnnouncementService extends AdminBaseService
|
||
|
{
|
||
|
public function index()
|
||
|
{
|
||
|
try {
|
||
|
$rows = [];
|
||
|
$list = AnnouncementModel::where('is_delete', AnnouncementModel::IS_DELETE_NO)
|
||
|
->field('id,lang,name,title,status,weight')
|
||
|
->order('name', 'asc')
|
||
|
->order('weight', 'desc')->select();
|
||
|
if(!$list->isEmpty()){
|
||
|
$rows = $list->toArray();
|
||
|
$lang = LanguageSettingModel::where('id', '>', 0)->column('china_name', 'id');
|
||
|
foreach ($rows as $key=>$item){
|
||
|
$rows[$key]['lang'] = $lang[$item['lang']] ?? '-';
|
||
|
$rows[$key]['status'] = AnnouncementModel::STATUS_MAP[$item['status']] ?? '-';
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return $this->toData('0', 'SUCCESS', ['list' => $rows, 'total' => count($rows)]);
|
||
|
}catch (\Exception $exception){
|
||
|
return $this->toData('1', '系统繁忙', [$exception->getMessage()]);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public function add($param)
|
||
|
{
|
||
|
try {
|
||
|
$langIds = LanguageSettingModel::column('id');
|
||
|
if(empty($param['lang']) || !in_array($param['lang'], $langIds)){
|
||
|
return $this->toData('1', '语言类型无效');
|
||
|
}
|
||
|
|
||
|
if(empty($param['name']) || !is_string($param['name'])){
|
||
|
return $this->toData('1', '名称无效');
|
||
|
}
|
||
|
|
||
|
if(empty($param['title']) || !is_string($param['title'])){
|
||
|
return $this->toData('1', '标题无效');
|
||
|
}
|
||
|
|
||
|
if(empty($param['content']) || !is_string($param['content'])){
|
||
|
return $this->toData('1', '内容无效');
|
||
|
}
|
||
|
|
||
|
if(!isset($param['weight']) || !is_numeric($param['weight']) || ceil($param['weight']) != $param['weight'] || $param['weight'] < 0){
|
||
|
return $this->toData('1', '权重无效');
|
||
|
}
|
||
|
|
||
|
if(!isset($param['status']) || !in_array($param['status'], [1,2])){
|
||
|
return $this->toData('1', '状态无效');
|
||
|
}
|
||
|
//
|
||
|
// if(!isset($param['is_pop_ups']) || !in_array($param['is_pop_ups'], [1,2])){
|
||
|
// return $this->toData('1', 'h5弹窗状态无效');
|
||
|
// }
|
||
|
|
||
|
$docExists = AnnouncementModel::where('is_delete', AnnouncementModel::IS_DELETE_NO)
|
||
|
->where('name', $param['name'])
|
||
|
->where('lang', $param['lang'])->find();
|
||
|
if(!empty($docExists)){
|
||
|
return $this->toData('1', '已存在相同内容');
|
||
|
}
|
||
|
|
||
|
$doc = new AnnouncementModel;
|
||
|
$doc->lang = $param['lang'];
|
||
|
$doc->title = $param['title'];
|
||
|
$doc->content = $param['content'];
|
||
|
$doc->status = $param['status'];
|
||
|
$doc->weight = $param['weight'];
|
||
|
if (!empty($param['is_pop_ups'])) $doc->is_pop_ups = $param['is_pop_ups'];
|
||
|
$doc->create_time = date('Y-m-d H:i:s');
|
||
|
$doc->update_time = date('Y-m-d H:i:s');
|
||
|
$doc->name = $param['name'];
|
||
|
$doc->save();
|
||
|
|
||
|
return $this->toData('0', 'SUCCESS');
|
||
|
}catch (\Exception $exception){
|
||
|
return $this->toData('1', '系统繁忙', [$exception->getMessage()]);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public function detail($param)
|
||
|
{
|
||
|
try {
|
||
|
if(empty($param['id']) || !is_numeric($param['id'])){
|
||
|
return $this->toData('1', '目标不存在');
|
||
|
}
|
||
|
|
||
|
$doc = AnnouncementModel::where('id', $param['id'])
|
||
|
->where('is_delete', AnnouncementModel::IS_DELETE_NO)
|
||
|
->find();
|
||
|
if(empty($doc)){
|
||
|
return $this->toData('1', '目标不存在');
|
||
|
}
|
||
|
|
||
|
$docArr = $doc->toArray();
|
||
|
|
||
|
return $this->toData('0', 'SUCCESS', ['row' => $docArr,]);
|
||
|
|
||
|
}catch (\Exception $exception){
|
||
|
return $this->toData('1', '系统繁忙', [$exception->getMessage()]);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public function edit($param)
|
||
|
{
|
||
|
try {
|
||
|
if(empty($param['id']) || !is_numeric($param['id'])){
|
||
|
return $this->toData('1', '目标不存在');
|
||
|
}
|
||
|
|
||
|
$doc = AnnouncementModel::where('id', $param['id'])
|
||
|
->where('is_delete', AnnouncementModel::IS_DELETE_NO)
|
||
|
->find();
|
||
|
if(empty($doc)){
|
||
|
return $this->toData('1', '目标不存在');
|
||
|
}
|
||
|
|
||
|
|
||
|
$langIds = LanguageSettingModel::column('id');
|
||
|
if(empty($param['lang']) || !in_array($param['lang'], $langIds)){
|
||
|
return $this->toData('1', '语言类型无效');
|
||
|
}
|
||
|
|
||
|
if(empty($param['name']) || !is_string($param['name'])){
|
||
|
return $this->toData('1', '名称无效');
|
||
|
}
|
||
|
|
||
|
if(empty($param['title']) || !is_string($param['title'])){
|
||
|
return $this->toData('1', '标题无效');
|
||
|
}
|
||
|
|
||
|
if(empty($param['content']) || !is_string($param['content'])){
|
||
|
return $this->toData('1', '内容无效');
|
||
|
}
|
||
|
|
||
|
if(!isset($param['weight']) || !is_numeric($param['weight']) || ceil($param['weight']) != $param['weight'] || $param['weight'] < 0){
|
||
|
return $this->toData('1', '权重无效');
|
||
|
}
|
||
|
|
||
|
if(!isset($param['status']) || !in_array($param['status'], [1,2])){
|
||
|
return $this->toData('1', '状态无效');
|
||
|
}
|
||
|
|
||
|
// $docExists = AnnouncementModel::where('is_delete', AnnouncementModel::IS_DELETE_NO)
|
||
|
// ->where('id', '<>', $param['id'])
|
||
|
// ->where('name', $param['name'])
|
||
|
// ->where('lang', $param['lang'])->find();
|
||
|
// if(!empty($docExists)){
|
||
|
// return $this->toData('1', '已存在相同内容');
|
||
|
// }
|
||
|
|
||
|
$doc->lang = $param['lang'];
|
||
|
$doc->title = $param['title'];
|
||
|
$doc->content = $param['content'];
|
||
|
$doc->status = $param['status'];
|
||
|
$doc->weight = $param['weight'];
|
||
|
if (!empty($param['is_pop_ups'])) $doc->is_pop_ups = $param['is_pop_ups'];
|
||
|
$doc->update_time = date('Y-m-d H:i:s');
|
||
|
$doc->name = $param['name'];
|
||
|
$doc->save();
|
||
|
|
||
|
return $this->toData('0', 'SUCCESS');
|
||
|
}catch (\Exception $exception){
|
||
|
return $this->toData('1', '系统繁忙', [$exception->getMessage()]);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public function del($param)
|
||
|
{
|
||
|
try {
|
||
|
if(empty($param['id']) || !is_numeric($param['id'])){
|
||
|
return $this->toData('1', '目标不存在');
|
||
|
}
|
||
|
|
||
|
$doc = AnnouncementModel::where('id', $param['id'])
|
||
|
->where('is_delete', AnnouncementModel::IS_DELETE_NO)
|
||
|
->find();
|
||
|
if(empty($doc)){
|
||
|
return $this->toData('1', '目标不存在');
|
||
|
}
|
||
|
|
||
|
$doc->is_delete = AnnouncementModel::IS_DELETE_YES;
|
||
|
$doc->save();
|
||
|
|
||
|
return $this->toData('0', 'SUCCESS');
|
||
|
}catch (\Exception $exception){
|
||
|
return $this->toData('1', '系统繁忙', [$exception->getMessage()]);
|
||
|
}
|
||
|
}
|
||
|
}
|