|
- <?php
-
- namespace backend\modules\api\models;
-
- use backend\common\push\jpush;
- use yii\db\ActiveRecord;
- use Yii;
-
- /**
- * This is the model class for table "driver_message".
- *
- * @property integer $ID
- * @property integer $CANCEL_FLAG
- * @property integer $CREATE_USER_ID
- * @property string $CREATE_TIME
- * @property integer $UPDATE_USER_ID
- * @property string $UPDATE_TIME
- * @property integer $BUS_NUMBER
- * @property integer $DRIVER_ID
- * @property string $MSG_TYPE
- * @property string $SEND_MESSAGE
- * @property integer $IS_READ
- */
- class DriverMessage extends ActiveRecord
- {
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return 'driver_message';
- }
-
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['CANCEL_FLAG', 'CREATE_USER_ID', 'UPDATE_USER_ID', 'BUS_NUMBER', 'DRIVER_ID', 'IS_READ'], 'integer'],
- [['BUS_NUMBER', 'DRIVER_ID', 'MSG_TYPE', 'SEND_MESSAGE'], 'required'],
- [['SEND_MESSAGE'], 'string'],
- [['CREATE_TIME', 'UPDATE_TIME'], 'string', 'max' => 20],
- [['MSG_TYPE'], 'string', 'max' => 11],
- ];
- }
-
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'ID' => 'ID',
- 'CANCEL_FLAG' => '记录有效性标记,CANCEL_FLAG=0记录有效;CANCEL_FLAG=1,记录已删除',
- 'CREATE_USER_ID' => '记录创建用户ID',
- 'CREATE_TIME' => '记录创建时间',
- 'UPDATE_USER_ID' => '记录最后更新用户ID',
- 'UPDATE_TIME' => '记录最后更新时间',
- 'BUS_NUMBER' => '出车单号',
- 'DRIVER_ID' => '司机id',
- 'MSG_TYPE' => '信息类型 对应dict_type.id 490报账 491派车',
- 'SEND_MESSAGE' => '内容',
- 'IS_READ' => '0未读,1已读',
- ];
- }
-
- /**
- * Function Description:获取司机信息
- * Function Name: getDriverMessage
- * @param int $driver_id 司机id
- * @param int $current_page 当前页
- * @param int $page_size 每页条数
- * @return array|\yii\db\ActiveRecord[]
- *
- * @author 张帅
- */
- public function getDriverMessage($driver_id,$current_page,$page_size)
- {
- $start_row = ($current_page-1)*$page_size;
- $result = self::find()
- ->select([
- 'message_id' => 'm.id',
- 'm.bus_number',//出车单号
- 'message_type_id' => 'm.msg_type',
- 'message_type' => DictType::find()->select('type_name')->where('id = m.msg_type')->limit(1),
- 'm.create_time',//发送时间
- 'm.send_message',//短信内容
- 'm.is_read',//是否已读
- 'o.run_date',
- 'finance_status_id' => 'o.finance_status',
- 'finance_status' => DictType::find()->select('type_name')->where('id = o.finance_status')->limit(1),
- 'run_status_id' => 'o.run_status',
- 'run_status' => DictType::find()->select('type_name')->where('id = o.run_status')->limit(1),
- ])
- ->from(self::tableName() . ' as m')
- ->innerJoin(BusOrder::tableName() . ' as o', 'm.bus_number = o.bus_number')
- ->where([
- 'and',
- ['=', 'm.driver_id', $driver_id],
- ['=', 'm.cancel_flag', 0],
- ['=', 'o.cancel_flag', 0],
- ['in', 'msg_type', [490, 491]],
- [
- 'or',
- ['!=','o.finance_status',446],
- ['!=','o.run_status',438],
- ],
- ['>=','run_date',date('Y-m-d',strtotime("-7 day"))],
- ])
- ->offset($start_row)
- ->limit($page_size)
- ->orderBy(['run_date' => SORT_DESC,'create_time' => SORT_DESC])
- ->asArray()->all();
-
- return $result;
- }
-
- /**
- * Function Description:添加司机消息
- * Function Name: addMessage
- * @param int $msg_type 司机类型
- * @param int $bus_number 出车单号
- *
- * @return array
- *
- * @author 张帅
- */
- public function addMessage($msg_type,$bus_number){
- #region 1.获取数据详情
- $finance_list = BusOrder::find()
- ->select([
- 'bus_number',
- 'driver_id' => 'send_bus_driver_res_id',
- 'run_date',
- 'itinerary_name',
- 'finance_log' => BusOrderStatusLog::find()->select('msg')->where('bus_number = o.bus_number and type = 432 and after_status = o.finance_status')->orderBy(['id' => SORT_DESC])->limit(1),//日志
- ])
- ->from(BusOrder::tableName() . ' as o')
- ->where([
- 'and',
- ['=', 'cancel_flag', 0],
- ['!=', 'send_bus_driver_res_id', ''],
- ['in', 'bus_number', explode(',',$bus_number)],
- ])
- ->asArray()->all();
- #endregion
-
- #region 2.插入信息表数据
- if (count($finance_list) > 0) {
- $msg_list = [];
- if($msg_type == 2){
- foreach ($finance_list as $key => $vel) {
- $msg_list[] = [
- 'CANCEL_FLAG' => 0,
- 'CREATE_USER_ID' => 1,
- 'CREATE_TIME' => date('Y-m-d H:i:s'),
- 'UPDATE_USER_ID' => 1,
- 'UPDATE_TIME' => date('Y-m-d H:i:s'),
- 'BUS_NUMBER' => $vel['bus_number'],
- 'DRIVER_ID' => $vel['driver_id'],
- 'MSG_TYPE' => 490,
- 'SEND_MESSAGE' => date('m',strtotime($vel['run_date'])) . '月' . date('d',strtotime($vel['run_date'])) . '日,(' . $vel['itinerary_name'] . ')的出车任务报账被驳回,驳回原因“' . $vel['finance_log'] . '”,请重新进行报账操作。点击进入报账页面,保留之前的报账信息',
- 'IS_READ' => 0,
- ];
- };
- }elseif($msg_type == 3){
- foreach ($finance_list as $key => $vel) {
- $msg_list[] = [
- 'CANCEL_FLAG' => 0,
- 'CREATE_USER_ID' => 1,
- 'CREATE_TIME' => date('Y-m-d H:i:s'),
- 'UPDATE_USER_ID' => 1,
- 'UPDATE_TIME' => date('Y-m-d H:i:s'),
- 'BUS_NUMBER' => $vel['bus_number'],
- 'DRIVER_ID' => $vel['driver_id'],
- 'MSG_TYPE' => 491,
- 'SEND_MESSAGE' => date('m',strtotime($vel['run_date'])) . '月' . date('d',strtotime($vel['run_date'])) . '日,(' . $vel['itinerary_name'] . ')的出车任务已下发,请在任务列表中进行查看,点击进入该任务的任务单页面',
- 'IS_READ' => 0,
- ];
- };
- }elseif($msg_type == 4){
- foreach ($finance_list as $key => $vel) {
- $msg_list[] = [
- 'CANCEL_FLAG' => 0,
- 'CREATE_USER_ID' => 1,
- 'CREATE_TIME' => date('Y-m-d H:i:s'),
- 'UPDATE_USER_ID' => 1,
- 'UPDATE_TIME' => date('Y-m-d H:i:s'),
- 'BUS_NUMBER' => $vel['bus_number'],
- 'DRIVER_ID' => $vel['driver_id'],
- 'MSG_TYPE' => 491,
- 'SEND_MESSAGE' => date('m',strtotime($vel['run_date'])) . '月' . date('d',strtotime($vel['run_date'])) . '日,(' . $vel['itinerary_name'] . ')的出车任务有信息被变更,请在任务列表中进行查看确认最新信息,点击进入该任务的任务单页面',
- 'IS_READ' => 0,
- ];
- };
- }else{
- return [];
- }
-
- $res = Yii::$app->db->createCommand()->batchInsert(DriverMessage::tableName(),
- ['CANCEL_FLAG', 'CREATE_USER_ID', 'CREATE_TIME', 'UPDATE_USER_ID', 'UPDATE_TIME', 'BUS_NUMBER', 'DRIVER_ID', 'MSG_TYPE', 'SEND_MESSAGE', 'IS_READ'],
- $msg_list)->execute();
- if($res){
- return $msg_list;
- }else{
- return [];
- }
- } else {
- return [];
- }
- }
-
- /**
- * Function Description:推送
- * Function Name: actionPush
- * @param int $push_type 推送类型 1.报账通知(未报账) 2.报账通知(已驳回) 3.派车通知发送时间
- * @param array $push_arr 推送数组
- *
- * @return bool|string
- *
- * @author 张帅
- */
- public function push($push_type, $push_arr)
- {
- foreach ($push_arr as $key => $vel) {
- if ($push_type == 1) {
- $n_title = '报账提醒';
- $n_content = $vel['date'] . '的出车任务未报账';
- } elseif ($push_type == 2) {
- $n_title = '报账提醒';
- $n_content = $vel['date'] . '的出车任务报账被驳回';
- } elseif ($push_type == 3) {
- $n_title = '派车提醒';
- $n_content = $vel['date'] . '的出车任务已下发';
- } elseif ($push_type == 4) {
- $n_title = '任务更新';
- $n_content = $vel['date'] . '的出车任务有更新';
- } else {
- $n_title = '标题';
- $n_content = '推送内容';
- }
-
- $arr = array('fromer' => '发送者', 'fromer_name' => '发送者名字', 'fromer_icon' => '发送者头像', 'image' => '发送图片链接', 'sound' => '发送音乐链接');
- //自定义参数
- $app_keys = '0fdf462e3c9d6ddaa525a4fd';
- $masterSecret = '9f0dfe4da5893fb6065b1d08';
- $send_no = 4;
- $receiver_value = $vel['driver_id'];//发送的司机资源ID,万一有多个以逗号分隔
- $platform = 'android';
- $msg_content = json_encode(array('n_builder_id' => 0, 'n_title' => $n_title, 'n_content' => $n_content, 'n_extras' => $arr));
- $jpush = new jpush($masterSecret, $app_keys);
- $jpush->send($send_no, 3, $receiver_value, 1, $msg_content, $platform);
- }
- return true;
- }
- }
|