2024-09-19 11:32:39 +08:00
< ? php
namespace service\modules\v1\controllers ;
use common\core\BaseServiceController ;
2024-12-19 19:20:27 +08:00
use common\models\oldDb\SystemNotice ;
2024-09-19 11:32:39 +08:00
use yii\base\Exception ;
/**
* @ doc - module 系统通知
*/
class SystemNoticeController extends BaseServiceController
{
public function actionIndex ()
{
$systemNotice = SystemNotice :: find () -> where ([
'or' ,
[
'and' ,
[ 'store_id' => \Yii :: $app -> request -> get ()[ 'store_id' ]],
[ 'scene_type' => 2 ],
[ 'user_id' => \Yii :: $app -> user -> identity -> id ],
[ 'base_type' => 5 ],
[ 'read_status' => 0 ]
],
[
'and' ,
[ 'scene_type' => 2 ],
[ 'base_type' => 99 ],
// ['user_id' => \Yii::$app->user->identity->id],
[ 'read_status' => 0 ]
]
]) -> select ( 'notice_at' ) -> orderBy ( 'notice_at DESC' ) -> asArray () -> all ();
$commentNotice = SystemNotice :: find () -> where ([
'store_id' => \Yii :: $app -> request -> get ()[ 'store_id' ],
'scene_type' => 2 ,
'user_id' => \Yii :: $app -> user -> identity -> id ,
'base_type' => 6 ,
'read_status' => 0
]) -> select ( 'notice_at' ) -> orderBy ( 'notice_at DESC' ) -> asArray () -> all ();
$patientNotice = SystemNotice :: find () -> where ([
'store_id' => \Yii :: $app -> request -> get ()[ 'store_id' ],
'scene_type' => 2 ,
'user_id' => \Yii :: $app -> user -> identity -> id ,
'base_type' => 2 ,
'read_status' => 0
]) -> select ( 'notice_at' ) -> orderBy ( 'notice_at DESC' ) -> asArray () -> all ();
$onlinePrescriptionNotice = SystemNotice :: find () -> where ([
'store_id' => \Yii :: $app -> request -> get ()[ 'store_id' ],
'scene_type' => 2 ,
'user_id' => \Yii :: $app -> user -> identity -> id ,
'base_type' => 8 ,
'read_status' => 0
]) -> select ( 'notice_at' ) -> orderBy ( 'notice_at DESC' ) -> asArray () -> all ();
$data = [];
if ( $systemNotice ) {
$data [ 'system' ] = [
'num' => count ( $systemNotice ),
'time' => $systemNotice [ 0 ][ 'notice_at' ]
];
}
if ( $commentNotice ) {
$data [ 'comment' ] = [
'num' => count ( $commentNotice ),
'time' => $commentNotice [ 0 ][ 'notice_at' ]
];
}
if ( $patientNotice ) {
$data [ 'patient' ] = [
'num' => count ( $patientNotice ),
'time' => $patientNotice [ 0 ][ 'notice_at' ]
];
}
if ( $onlinePrescriptionNotice ) {
$data [ 'online_prescription' ] = [
'num' => count ( $onlinePrescriptionNotice ),
'time' => $onlinePrescriptionNotice [ 0 ][ 'notice_at' ]
];
}
return $data ;
}
public function actionSystem ()
{
$query = SystemNotice :: find () -> where ([
'or' ,
[
'and' ,
[ 'store_id' => \Yii :: $app -> request -> get ()[ 'store_id' ]],
[ 'scene_type' => 2 ],
[ 'user_id' => \Yii :: $app -> user -> identity -> id ],
[ 'base_type' => 5 ],
],
[
'and' ,
[ 'scene_type' => 2 ],
// ['user_id' => \Yii::$app->user->identity->id],
[ 'base_type' => 99 ]
]
]) -> select ( 'id,content,data,url,url_type,read_status,base_type,notice_at' ) -> orderBy ( 'notice_at DESC' );
return $this -> create ( $query , \Yii :: $app -> request -> get ());
}
public function actionOnlinePrescription ()
{
$query = SystemNotice :: find () -> where ([
'store_id' => \Yii :: $app -> request -> get ()[ 'store_id' ],
'scene_type' => 2 ,
'user_id' => \Yii :: $app -> user -> identity -> id ,
'base_type' => 8 ,
'is_delete' => 0
]) -> select ( 'id,content,data,read_status,base_type,notice_at' ) -> orderBy ( 'notice_at DESC' );
return $this -> create ( $query , \Yii :: $app -> request -> get ());
}
public function actionRotation ()
{
$noticeId = \Yii :: $app -> request -> post ( 'notice_id' );
if ( ! $noticeId ) {
throw new Exception ( '消息id不能为空' );
}
SystemNotice :: updateAll ([ 'is_delete' => 1 ], [ 'id' => $noticeId ]);
return [ 'success' ];
}
public function actionComment ()
{
$query = SystemNotice :: find () -> where ([
'store_id' => \Yii :: $app -> request -> get ()[ 'store_id' ],
'scene_type' => 2 ,
'user_id' => \Yii :: $app -> user -> identity -> id ,
'base_type' => 6
]) -> select ( 'id,content,data,url,url_type,read_status,base_type,notice_at' ) -> orderBy ( 'notice_at DESC' );
return $this -> create ( $query , \Yii :: $app -> request -> get ());
}
public function actionPatient ()
{
$query = SystemNotice :: find () -> where ([
'store_id' => \Yii :: $app -> request -> get ()[ 'store_id' ],
'scene_type' => 2 ,
'user_id' => \Yii :: $app -> user -> identity -> id ,
'base_type' => 2 ,
]) -> select ( 'id,content,data,url,url_type,read_status,base_type,notice_at' ) -> orderBy ( 'notice_at DESC' );
return $this -> create ( $query , \Yii :: $app -> request -> get ());
}
public function actionRead ()
{
$post = \Yii :: $app -> request -> post ();
$this -> requestValidate ( $post , [
[ 'type' , 'required' ],
[ 'store_id' , 'required' ]
]);
$type = $post [ 'type' ];
switch ( $type ) {
case 'system' :
\Yii :: $app -> db -> createCommand () -> update ( SystemNotice :: tableName (), [ 'read_status' => '1' ], 'read_status=0 AND scene_type=2 AND (base_type=1 or base_type=5) AND user_id=' . \Yii :: $app -> user -> identity -> id . ' AND store_id=' . $post [ 'store_id' ]) -> execute ();
\Yii :: $app -> db -> createCommand () -> update ( SystemNotice :: tableName (), [ 'read_status' => '1' ], 'read_status=0 AND scene_type=2 AND base_type=99 AND user_id=' . \Yii :: $app -> user -> identity -> id ) -> execute ();
break ;
case 'comment' :
\Yii :: $app -> db -> createCommand () -> update ( SystemNotice :: tableName (), [ 'read_status' => '1' ], 'read_status=0 AND scene_type=2 AND base_type=6 AND user_id=' . \Yii :: $app -> user -> identity -> id . ' AND store_id=' . $post [ 'store_id' ]) -> execute ();
break ;
case 'patient' :
\Yii :: $app -> db -> createCommand () -> update ( SystemNotice :: tableName (), [ 'read_status' => '1' ], 'read_status=0 AND scene_type=2 AND base_type=2 AND user_id=' . \Yii :: $app -> user -> identity -> id . ' AND store_id=' . $post [ 'store_id' ]) -> execute ();
break ;
default :
throw new Exception ( '错误的type' );
break ;
}
return [ 'success' ];
}
/**
* 医生历史消息(当日已读)
*/
public function actionHistory ()
{
$start = strtotime ( date ( 'Y-m-d' ));
$end = strtotime ( date ( 'Y-m-d' , strtotime ( '+1 day' )));
$query = SystemNotice :: find () -> where ([
'store_id' => \Yii :: $app -> request -> get ()[ 'store_id' ],
'scene_type' => 2 ,
'user_id' => \Yii :: $app -> user -> identity -> id ,
'read_status' => 1
]) -> andWhere ([
'between' , 'created_at' , [ $start , $end ]
]) -> select ( 'id,content,data,url,url_type,read_status,base_type,notice_at' ) -> orderBy ( 'notice_at DESC' );
return $this -> create ( $query , \Yii :: $app -> request -> get ());
}
}