197 lines
6.1 KiB
PHP
197 lines
6.1 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @author Any
|
|
* @description KISS
|
|
* @date 2021年9月28日
|
|
* @version 1.0.0
|
|
*
|
|
* _____LOG_____
|
|
*
|
|
*/
|
|
namespace app\models\common\msg;
|
|
|
|
use app\models\msg\SysMsg;
|
|
use app\models\msg\UserSysMsg;
|
|
use app\models\Model;
|
|
use app\components\SiteHelper;
|
|
|
|
|
|
class CommonSysMsgActionForm extends Model
|
|
{
|
|
public $sys_msg_id;
|
|
public $to_user_id;
|
|
public $cx_mch_id;
|
|
|
|
|
|
|
|
public function rules()
|
|
{
|
|
return [
|
|
[['cx_mch_id', ], 'integer'],
|
|
[['sys_msg_id', 'to_user_id'], 'safe'],
|
|
[['sys_msg_id', 'cx_mch_id', 'to_user_id'], 'required'],
|
|
];
|
|
}
|
|
|
|
|
|
public function delete()
|
|
{
|
|
if(!$this->validate()){
|
|
return $this->getModelError();
|
|
}
|
|
if(is_array($this->sys_msg_id)){
|
|
foreach ($this->sys_msg_id as $sys_msg_id){
|
|
$t = \Yii::$app->db->beginTransaction();
|
|
$model = SysMsg::findOne([
|
|
'id' => $sys_msg_id,
|
|
'is_delete' => 0,
|
|
'to_user_id' => $this->to_user_id,
|
|
'status' => SysMsg::STATUS_DRAFT,
|
|
'cx_mch_id' => $this->cx_mch_id
|
|
]);
|
|
if($model == null)
|
|
continue;
|
|
$model->is_delete = 1;
|
|
if(!$model->save()){
|
|
$t->rollBack();
|
|
return $this->getModelError($model);
|
|
}
|
|
$t->commit();
|
|
}
|
|
} else {
|
|
$t = \Yii::$app->db->beginTransaction();
|
|
$model = SysMsg::findOne([
|
|
'id' => $this->sys_msg_id,
|
|
'is_delete' => 0,
|
|
'to_user_id' => $this->to_user_id,
|
|
'status' => SysMsg::STATUS_DRAFT,
|
|
'cx_mch_id' => $this->cx_mch_id
|
|
]);
|
|
if($model == null){
|
|
return $this->apiReturnError('消息不存在或已经删除');
|
|
}
|
|
$model->is_delete = 1;
|
|
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->sys_msg_id)){
|
|
foreach ($this->sys_msg_id as $sys_msg_id){
|
|
$model = SysMsg::findOne([
|
|
'id' => $sys_msg_id,
|
|
'is_delete' => 0,
|
|
'to_user_id' => $this->to_user_id,
|
|
'status' => SysMsg::STATUS_DRAFT,
|
|
'cx_mch_id' => $this->cx_mch_id
|
|
]);
|
|
if($model == null)
|
|
continue;
|
|
$model->status = SysMsg::STATUS_PUBLISHED;
|
|
if(empty($model->published_at)){
|
|
$model->published_at = time();
|
|
}
|
|
if(!$model->save()){
|
|
return $this->getModelError($model);
|
|
}
|
|
}
|
|
} else {
|
|
$model = SysMsg::findOne([
|
|
'id' => $this->sys_msg_id,
|
|
'is_delete' => 0,
|
|
'to_user_id' => $this->to_user_id,
|
|
'status' => SysMsg::STATUS_DRAFT,
|
|
'cx_mch_id' => $this->cx_mch_id
|
|
]);
|
|
if($model == null){
|
|
return $this->apiReturnError('消息不存在或已经删除');
|
|
}
|
|
$model->status = SysMsg::STATUS_PUBLISHED;
|
|
if(empty($model->published_at)){
|
|
$model->published_at = time();
|
|
}
|
|
if(!$model->save()){
|
|
return $this->getModelError($model);
|
|
}
|
|
}
|
|
return $this->apiReturnSuccess("发布成功");
|
|
}
|
|
|
|
public function withdraw()
|
|
{
|
|
if(!$this->validate()){
|
|
return $this->getModelError();
|
|
}
|
|
if(is_array($this->sys_msg_id)){
|
|
foreach ($this->sys_msg_id as $sys_msg_id){
|
|
$model = SysMsg::findOne([
|
|
'id' => $sys_msg_id,
|
|
'is_delete' => 0,
|
|
'to_user_id' => $this->to_user_id,
|
|
'status' => SysMsg::STATUS_PUBLISHED,
|
|
'cx_mch_id' => $this->cx_mch_id
|
|
]);
|
|
if($model == null)
|
|
continue;
|
|
$model->status = SysMsg::STATUS_DRAFT;
|
|
if(empty($model->published_at)){
|
|
$model->published_at = time();
|
|
}
|
|
if(!$model->save()){
|
|
return $this->getModelError($model);
|
|
}
|
|
}
|
|
} else {
|
|
$model = SysMsg::findOne([
|
|
'id' => $this->sys_msg_id,
|
|
'is_delete' => 0,
|
|
'to_user_id' => $this->to_user_id,
|
|
'status' => SysMsg::STATUS_PUBLISHED,
|
|
'cx_mch_id' => $this->cx_mch_id
|
|
]);
|
|
if($model == null){
|
|
return $this->apiReturnError('消息不存在或已经撤回');
|
|
}
|
|
$model->status = SysMsg::STATUS_DRAFT;
|
|
if(empty($model->published_at)){
|
|
$model->published_at = time();
|
|
}
|
|
if(!$model->save()){
|
|
return $this->getModelError($model);
|
|
}
|
|
}
|
|
return $this->apiReturnSuccess("撤回成功");
|
|
}
|
|
|
|
public function detail()
|
|
{
|
|
if(!$this->validate()){
|
|
return $this->getModelError();
|
|
}
|
|
$form = new CommonSysMsgListForm();
|
|
$form->scenario = 'detail';
|
|
$form->sys_msg_id = $this->sys_msg_id;
|
|
$form->cx_mch_id = $this->cx_mch_id;
|
|
$form->status = SysMsg::STATUS_PUBLISHED;
|
|
$form->to_user_id = $this->to_user_id;
|
|
$res = $form->search();
|
|
if($res['code'] != 0)
|
|
return $res;
|
|
if(count($res['data']) == 0)
|
|
return $this->apiReturnError('消息通知详情不存在');
|
|
$data = $res['data'][0];
|
|
return $this->apiReturnSuccess('ok',$data);
|
|
}
|
|
}
|
|
|