cxfoot/models/common/msg/CommonSysMsgListForm.php
2023-10-27 14:25:12 +08:00

294 lines
12 KiB
PHP

<?php
/**
* @author Any
* @description KISS
* @date 2021年9月28日
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\models\common\msg;
use app\models\BallCart;
use app\models\BallMark;
use app\models\msg\SysMsg;
use app\models\msg\UserSysMsg;
use app\models\Model;
use app\models\MsgCentre;
use app\models\Order;
use app\models\StoreUser;
use app\modules\api\components\ApiHelper;
use app\modules\api\components\GetDistance;
use yii\data\Pagination;
use app\components\SiteHelper;
class CommonSysMsgListForm extends Model
{
public $keywords;
public $limit;
public $page;
public $cx_mch_id;
public $type;
public $to_user_id;
public $status;
public $sys_msg_id;
public $user_id;
public function rules()
{
return [
[['limit', 'page', 'cx_mch_id', 'status', 'type', 'sys_msg_id'], 'integer'],
[['page'], 'default', 'value' => 1],
[['limit'], 'default', 'value' => 20],
[['to_user_id',], 'safe'],
[['cx_mch_id', 'to_user_id'], 'required'],
[['user_id'], 'required', 'on' => 'user'],
[['sys_msg_id',], 'required', 'on' => 'detail'],
];
}
public function search()
{
if (!$this->validate()) {
return $this->getModelError();
}
$read_count_query = UserSysMsg::find()->where(['cx_mch_id' => $this->cx_mch_id, 'is_delete' => 0])->select('count(*) as read_count,sys_msg_id');
$query = SysMsg::find()->alias('sm')
->leftJoin(['rc' => $read_count_query], 'rc.sys_msg_id=sm.id')
->where([
'sm.cx_mch_id' => $this->cx_mch_id,
'sm.is_delete' => 0,
])
->andFilterWhere([
'OR',
['LIKE', 'sm.title', $this->keywords],
['LIKE', 'sm.content', $this->keywords],
])
->andFilterWhere([
'sm.type' => $this->type,
'sm.to_user_id' => $this->to_user_id,
'sm.status' => $this->status,
'sm.id' => $this->sys_msg_id
])
->select('rc.read_count,sm.id,sm.title,sm.content,sm.type,sm.to_user_id,sm.created_at,sm.status,sm.published_at,sm.is_richtext,sm.order_id');
$count_query = clone $query;
$count = $count_query->count();
$pagination = new Pagination(['pageSize' => $this->limit, 'totalCount' => $count, 'page' => $this->page - 1]);
$list = $query->offset($pagination->offset)->limit($pagination->limit)->orderBy(['sm.created_at' => SORT_DESC])->asArray()->all();
foreach ($list as $index => $item) {
$item['created_at_cn'] = date('Y-m-d H:i:s', $item['created_at']);
$item['published_at_cn'] = date('Y-m-d H:i:s', $item['published_at']);
$item['status_cn'] = SysMsg::getStatus($item['status']);
$item['read_count'] = $item['read_count'] ? $item['read_count'] : 0;
if ($this->scenario == 'detail')
$item['content'] = SiteHelper::repairContent($item['content']);
if ($this->scenario == 'user') {
//是否已读
$item['is_read'] = UserSysMsg::getUserSysStatus($item['id'], $this->user_id, 0, $this->cx_mch_id);
unset($item['to_user_id']);
unset($item['status']);
unset($item['status_cn']);
}
$list[$index] = $item;
}
//是否已经全部加载
$end_flag = $this->page > $pagination->pageCount ? true : false;
return [
'code' => 0,
'msg' => 'ok',
'data' => $list,
'count' => $count,
'page_size' => $this->limit,
'page_count' => $pagination->pageCount,
'page_no' => $this->page,
'end_flag' => $end_flag
];
}
public function msg_centre_search()
{
if (!$this->validate()) {
return $this->getModelError();
}
$type = empty($this->type) ? 1 : $this->type;
$query = MsgCentre::find()->where([
'type' => $type,
'user_id' => $this->user_id,
]);
$is_type = false;
if ($type == '1' || $type == '2') {
$is_type = true;
}
$count = $query->count();
$pagination = new Pagination(['pageSize' => $this->limit, 'totalCount' => $count, 'page' => $this->page - 1]);
$list = $query->offset($pagination->offset)->limit($pagination->limit)->orderBy(['id' => SORT_DESC])->asArray()->all();
foreach ($list as $index => &$item) {
$item['created_at_cn'] = date('m月d日 H:i', $item['created_at']);
if ($is_type) {
$item['content'] = json_decode($item['content'], true);
if ($type == '2') {
$time_often = $item['content']['end_date'] - $item['content']['start_date'];
$item['content']['time_content'] = GetDistance::mdate($time_often);
$time = $item['content']['end_date'] - $item['content']['start_date'];
$item['content']['number'] = sprintf('%.2f', ($time / 60) / 60);
$item['content']['start_date'] = date('m月d日 H:i', $item['content']['start_date']);
$item['content']['end_date'] = date('m月d日 H:i', $item['content']['end_date']);
} else {
$item['content']['start_date'] = date('m月d日 H:i', $item['content']['start_date']);
$item['content']['end_date'] = '~';
$item['content']['number'] = '~';
}
}
$item['order_status'] = 0;
if ($item['object_id']) {
$order = Order::find()->where(['id' => $item['object_id'], 'status' => 2])->exists();
if ($order) {
$item['order_status'] = 1;
}
}
}
//是否已经全部加载
$end_flag = $this->page >= $pagination->pageCount ? true : false;
return [
'code' => 0,
'msg' => 'ok',
'data' => $list,
'count' => $count,
'page_size' => $this->limit,
'page_count' => $pagination->pageCount,
'page_no' => $this->page,
'end_flag' => $end_flag
];
}
//报警消息
public function ball_warning($user_id)
{
$user = ApiHelper::findOneUser($user_id);
if ($user == null) {
return ['code' => 0, 'msg' => '无数据'];
}
$is_true = false;
$is_admin = false;
if ($user->type == '2') {
$is_exists = StoreUser::find()->where(['is_delete' => 0, 'status' => 0, 'user_id' => $user_id, 'user_type' => [1, 2]])->exists();
if ($is_exists) {
$is_true = true;
}
} elseif ($user->type == '8') {
$is_true = true;
$is_admin = true;
} else {
return ['code' => 0, 'msg' => '无数据'];
}
$ball_numbers = [];
if ($is_admin && $is_true) {
//总部账号
$ball_numbers = BallCart::find()->alias('bc')
->leftJoin(['bm' => BallMark::tableName()], 'bc.mark_id=bm.id')
->where(['bc.is_delete' => 0])
->select('bc.ball_number,bm.name')
->asArray()
->all();
} else if ($is_true) {
//门店管理员或门店服务员
$store_ids = StoreUser::find()->where(['is_delete' => 0, 'status' => 0, 'user_id' => $user_id, 'user_type' => [1, 2]])->select('store_id')->column();
if (!empty($store_ids)) {
$ball_numbers = BallCart::find()->alias('bc')
->leftJoin(['bm' => BallMark::tableName()], 'bc.mark_id=bm.id')
->where(['bc.is_delete' => 0, 'bc.store_id' => $store_ids])
->select('bc.ball_number,bm.name')
->asArray()
->all();
}
}
$list = [];
$redis = \Yii::$app->redis;
$ball_numbers = array_values($ball_numbers);
foreach ($ball_numbers as $index => $item) {
$key = "api:cxaibc:mqtt:warn_status:" . $item['ball_number'];
$ELEC_PERCENT = '--';
$ball_mark = empty($item['name']) ? '~~' : $item['name'];
$ball_cart = $item['ball_number'];
if ($data = $redis->get($key)) {
$data = json_decode($data, true);
$key2 = "api:cxaibc:mqtt:data:{$item['ball_number']}_Detail";
if ($data2 = $redis->get($key2)) {
$data2 = json_decode($data2, true);
$ELEC_PERCENT = json_decode($data2['data'], true)['ELEC_PERCENT'];
// $ELEC_PERCENT = $data2;
}
if (!empty($data['WARN_HARDWARE'])) {
$title = '硬件链路异常';
$content = '硬件链路异常,请管理员及时处理,避免影响客户用车体验';
$date_cn = date('m月d日 H:i', $data['WARN_HARDWARE']);
array_push($list, ['title' => $title, 'content' => $content, 'created_at_cn' => $date_cn, 'elec_percent' => $ELEC_PERCENT, 'ball_mark' => $ball_mark, 'ball_cart' => $ball_cart, 'sort' => $data['WARN_HARDWARE']]);
}
if (!empty($data['WARN_LOW_POWER'])) {
$title = '电量过低';
$content = '请管理员尽快将此车进行充电,避免影响客户用车体验';
$date_cn = date('m月d日 H:i', $data['WARN_LOW_POWER']);
array_push($list, ['title' => $title, 'content' => $content, 'created_at_cn' => $date_cn, 'elec_percent' => $ELEC_PERCENT, 'ball_mark' => $ball_mark, 'ball_cart' => $ball_cart, 'sort' => $data['WARN_LOW_POWER']]);
}
if (!empty($data['WARN_FALLDOWN'])) {
$title = '车辆倾倒';
$content = '车辆倾倒,请管理员及时处理,避免影响客户用车体验';
$date_cn = date('m月d日 H:i', $data['WARN_FALLDOWN']);
array_push($list, ['title' => $title, 'content' => $content, 'created_at_cn' => $date_cn, 'elec_percent' => $ELEC_PERCENT, 'ball_mark' => $ball_mark, 'ball_cart' => $ball_cart, 'sort' => $data['WARN_FALLDOWN']]);
}
if (!empty($data['GPS_STORE'])) {
$title = '车辆超出门店区域';
$content = '车辆超出门店范围,请知悉';
$date_cn = date('m月d日 H:i', $data['GPS_STORE']);
array_push($list, ['title' => $title, 'content' => $content, 'created_at_cn' => $date_cn, 'elec_percent' => $ELEC_PERCENT, 'ball_mark' => $ball_mark, 'ball_cart' => $ball_cart, 'sort' => $data['GPS_STORE']]);
}
if (!empty($data['GPS_CALL'])) {
$title = '车辆超出安全区域';
$content = '车辆超出安全范围,请知悉';
$date_cn = date('m月d日 H:i', $data['GPS_CALL']);
array_push($list, ['title' => $title, 'content' => $content, 'created_at_cn' => $date_cn, 'elec_percent' => $ELEC_PERCENT, 'ball_mark' => $ball_mark, 'ball_cart' => $ball_cart, 'sort' => $data['GPS_CALL']]);
}
}
}
$sort = array_column($list, 'sort');
array_multisort($sort, SORT_DESC, $list);
return [
'code' => 0,
'msg' => 'ok',
'data' => $list,
'count' => count($list),
];
}
}