cxgj/models/common/cms/CommonNoticeActionForm.php
2023-11-27 09:45:13 +08:00

377 lines
12 KiB
PHP

<?php
/**
* @author Any
* @description KISS
* @date 2021年7月2日
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\models\common\cms;
use app\models\cms\Notice;
use app\models\cms\RichText;
use app\models\Model;
use yii\data\Pagination;
use app\components\SiteHelper;
class CommonNoticeActionForm extends Model
{
public $notice_id;
public $cx_mch_id;
public function rules()
{
return [
[['cx_mch_id'], 'integer'],
[['notice_id', ], 'safe'],
[['notice_id', 'cx_mch_id'], 'required'],
];
}
public function delete()
{
if(!$this->validate()){
return $this->getModelError();
}
if(is_array($this->notice_id)){
foreach ($this->notice_id as $notice_id){
$t = \Yii::$app->db->beginTransaction();
$model = Notice::findOne([
'id' => $notice_id,
'is_delete' => 0,
// 'status' => Notice::STATUS_TRASH,
// 'cx_mch_id' => $this->cx_mch_id
]);
if($model == null)
continue;
$model->is_delete = 1;
//删除RichText
RichText::deleteRichText($model->rich_text_id, $this->cx_mch_id);
if(!$model->save()){
$t->rollBack();
return $this->getModelError($model);
}
$t->commit();
}
} else {
$t = \Yii::$app->db->beginTransaction();
$model = Notice::findOne([
'id' => $this->notice_id,
'is_delete' => 0,
'status' => Notice::STATUS_TRASH,
'cx_mch_id' => $this->cx_mch_id
]);
if($model == null){
return $this->apiReturnError('公告不存在或已经删除');
}
$model->is_delete = 1;
//删除RichText
RichText::deleteRichText($model->rich_text_id, $this->cx_mch_id);
if(!$model->save()){
$t->rollBack();
return $this->getModelError($model);
}
$t->commit();
}
return $this->apiReturnSuccess("删除成功");
}
public function publish()
{
if(!$this->validate()){
return $this->getModelError();
}
if(is_array($this->notice_id)){
foreach ($this->notice_id as $notice_id){
$model = Notice::findOne([
'id' => $notice_id,
'is_delete' => 0,
'status' => Notice::STATUS_DRAFT,
'cx_mch_id' => $this->cx_mch_id
]);
if($model == null)
continue;
$model->status = Notice::STATUS_PUBLISHED;
if(empty($model->published_at)){
$model->published_at = time();
}
if(!$model->save()){
return $this->getModelError($model);
}
}
} else {
$model = Notice::findOne([
'id' => $this->notice_id,
'is_delete' => 0,
'status' => Notice::STATUS_DRAFT,
'cx_mch_id' => $this->cx_mch_id
]);
if($model == null){
return $this->apiReturnError('公告不存在或已经删除');
}
$model->status = Notice::STATUS_PUBLISHED;
if(empty($model->published_at)){
$model->published_at = time();
}
if(!$model->save()){
return $this->getModelError($model);
}
}
return $this->apiReturnSuccess("发布成功");
}
public function trash()
{
if(!$this->validate()){
return $this->getModelError();
}
if(is_array($this->notice_id)){
foreach ($this->notice_id as $notice_id){
$model = Notice::findOne([
'id' => $notice_id,
'is_delete' => 0,
'status' => [Notice::STATUS_DRAFT, Notice::STATUS_PUBLISHED],
'cx_mch_id' => $this->cx_mch_id
]);
if($model == null)
continue;
$model->status = Notice::STATUS_TRASH;
if(!$model->save()){
return $this->getModelError($model);
}
}
} else {
$model = Notice::findOne([
'id' => $this->notice_id,
'is_delete' => 0,
'status' => [Notice::STATUS_DRAFT, Notice::STATUS_PUBLISHED],
'cx_mch_id' => $this->cx_mch_id
]);
if($model == null){
return $this->apiReturnError('公告不存在或已经删除');
}
$model->status = Notice::STATUS_TRASH;
if(!$model->save()){
return $this->getModelError($model);
}
}
return $this->apiReturnSuccess("操作成功");
}
public function cancel_trash()
{
if(!$this->validate()){
return $this->getModelError();
}
if(is_array($this->notice_id)){
foreach ($this->notice_id as $notice_id){
$model = Notice::findOne([
'id' => $notice_id,
'is_delete' => 0,
'status' => Notice::STATUS_TRASH,
'cx_mch_id' => $this->cx_mch_id
]);
if($model == null)
continue;
$model->status = Notice::STATUS_DRAFT;
if(!$model->save()){
return $this->getModelError($model);
}
}
} else {
$model = Notice::findOne([
'id' => $this->notice_id,
'is_delete' => 0,
'status' => Notice::STATUS_TRASH,
'cx_mch_id' => $this->cx_mch_id
]);
if($model == null){
return $this->apiReturnError('公告不存在或已经删除');
}
$model->status = Notice::STATUS_DRAFT;
if(!$model->save()){
return $this->getModelError($model);
}
}
return $this->apiReturnSuccess("操作成功");
}
public function top()
{
if(!$this->validate()){
return $this->getModelError();
}
if(is_array($this->notice_id)){
foreach ($this->notice_id as $notice_id){
$model = Notice::findOne([
'id' => $notice_id,
'is_delete' => 0,
'is_top' => 0,
'cx_mch_id' => $this->cx_mch_id
]);
if($model == null)
continue;
$model->is_top = 1;
if(!$model->save()){
return $this->getModelError($model);
}
}
} else {
$model = Notice::findOne([
'id' => $this->notice_id,
'is_delete' => 0,
'is_top' => 0,
'cx_mch_id' => $this->cx_mch_id
]);
if($model == null){
return $this->apiReturnError('公告不存在或已经删除');
}
$model->is_top = 1;
if(!$model->save()){
return $this->getModelError($model);
}
}
return $this->apiReturnSuccess("设置成功");
}
public function cancel_top()
{
if(!$this->validate()){
return $this->getModelError();
}
if(is_array($this->notice_id)){
foreach ($this->notice_id as $notice_id){
$model = Notice::findOne([
'id' => $notice_id,
'is_delete' => 0,
'is_top' => 1,
'cx_mch_id' => $this->cx_mch_id
]);
if($model == null)
continue;
$model->is_top = 0;
if(!$model->save()){
return $this->getModelError($model);
}
}
} else {
$model = Notice::findOne([
'id' => $this->notice_id,
'is_delete' => 0,
'is_top' => 1,
'cx_mch_id' => $this->cx_mch_id
]);
if($model == null){
return $this->apiReturnError('公告不存在或已经删除');
}
$model->is_top = 0;
if(!$model->save()){
return $this->getModelError($model);
}
}
return $this->apiReturnSuccess("设置成功");
}
public function show_index()
{
if(!$this->validate()){
return $this->getModelError();
}
if(is_array($this->notice_id)){
foreach ($this->notice_id as $notice_id){
$model = Notice::findOne([
'id' => $notice_id,
'is_delete' => 0,
'is_index' => 0,
'cx_mch_id' => $this->cx_mch_id
]);
if($model == null)
continue;
$model->is_index = 1;
if(!$model->save()){
return $this->getModelError($model);
}
}
} else {
$model = Notice::findOne([
'id' => $this->notice_id,
'is_delete' => 0,
'is_index' => 0,
'cx_mch_id' => $this->cx_mch_id
]);
if($model == null){
return $this->apiReturnError('公告不存在或已经删除');
}
$model->is_index = 1;
if(!$model->save()){
return $this->getModelError($model);
}
}
return $this->apiReturnSuccess("设置成功");
}
public function hide_index()
{
if(!$this->validate()){
return $this->getModelError();
}
if(is_array($this->notice_id)){
foreach ($this->notice_id as $notice_id){
$model = Notice::findOne([
'id' => $notice_id,
'is_delete' => 0,
'is_index' => 1,
'cx_mch_id' => $this->cx_mch_id
]);
if($model == null)
continue;
$model->is_index = 0;
if(!$model->save()){
return $this->getModelError($model);
}
}
} else {
$model = Notice::findOne([
'id' => $this->notice_id,
'is_delete' => 0,
'is_index' => 1,
'cx_mch_id' => $this->cx_mch_id
]);
if($model == null){
return $this->apiReturnError('公告不存在或已经删除');
}
$model->is_index = 0;
if(!$model->save()){
return $this->getModelError($model);
}
}
return $this->apiReturnSuccess("设置成功");
}
public function detail()
{
if(!$this->validate()){
return $this->getModelError();
}
$form = new CommonNoticeListForm();
$form->scenario = 'detail';
$form->notice_id = $this->notice_id;
$form->cx_mch_id = $this->cx_mch_id;
$form->status = Notice::STATUS_PUBLISHED;
$res = $form->search(1);
if($res['code'] != 0)
return $res;
if(count($res['data']) == 0)
return $this->apiReturnError('公告详情不存在');
Notice::setViewedNum($this->notice_id, $this->cx_mch_id);
$data = $res['data'][0];
return $this->apiReturnSuccess('ok',$data);
}
}