Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

258 righe
10 KiB

  1. <?php
  2. namespace backend\modules\api\models;
  3. use backend\common\push\jpush;
  4. use yii\db\ActiveRecord;
  5. use Yii;
  6. /**
  7. * This is the model class for table "driver_message".
  8. *
  9. * @property integer $ID
  10. * @property integer $CANCEL_FLAG
  11. * @property integer $CREATE_USER_ID
  12. * @property string $CREATE_TIME
  13. * @property integer $UPDATE_USER_ID
  14. * @property string $UPDATE_TIME
  15. * @property integer $BUS_NUMBER
  16. * @property integer $DRIVER_ID
  17. * @property string $MSG_TYPE
  18. * @property string $SEND_MESSAGE
  19. * @property integer $IS_READ
  20. */
  21. class DriverMessage extends ActiveRecord
  22. {
  23. /**
  24. * @inheritdoc
  25. */
  26. public static function tableName()
  27. {
  28. return 'driver_message';
  29. }
  30. /**
  31. * @inheritdoc
  32. */
  33. public function rules()
  34. {
  35. return [
  36. [['CANCEL_FLAG', 'CREATE_USER_ID', 'UPDATE_USER_ID', 'BUS_NUMBER', 'DRIVER_ID', 'IS_READ'], 'integer'],
  37. [['BUS_NUMBER', 'DRIVER_ID', 'MSG_TYPE', 'SEND_MESSAGE'], 'required'],
  38. [['SEND_MESSAGE'], 'string'],
  39. [['CREATE_TIME', 'UPDATE_TIME'], 'string', 'max' => 20],
  40. [['MSG_TYPE'], 'string', 'max' => 11],
  41. ];
  42. }
  43. /**
  44. * @inheritdoc
  45. */
  46. public function attributeLabels()
  47. {
  48. return [
  49. 'ID' => 'ID',
  50. 'CANCEL_FLAG' => '记录有效性标记,CANCEL_FLAG=0记录有效;CANCEL_FLAG=1,记录已删除',
  51. 'CREATE_USER_ID' => '记录创建用户ID',
  52. 'CREATE_TIME' => '记录创建时间',
  53. 'UPDATE_USER_ID' => '记录最后更新用户ID',
  54. 'UPDATE_TIME' => '记录最后更新时间',
  55. 'BUS_NUMBER' => '出车单号',
  56. 'DRIVER_ID' => '司机id',
  57. 'MSG_TYPE' => '信息类型 对应dict_type.id 490报账 491派车',
  58. 'SEND_MESSAGE' => '内容',
  59. 'IS_READ' => '0未读,1已读',
  60. ];
  61. }
  62. /**
  63. * Function Description:获取司机信息
  64. * Function Name: getDriverMessage
  65. * @param int $driver_id 司机id
  66. * @param int $current_page 当前页
  67. * @param int $page_size 每页条数
  68. * @return array|\yii\db\ActiveRecord[]
  69. *
  70. * @author 张帅
  71. */
  72. public function getDriverMessage($driver_id,$current_page,$page_size)
  73. {
  74. $start_row = ($current_page-1)*$page_size;
  75. $result = self::find()
  76. ->select([
  77. 'message_id' => 'm.id',
  78. 'm.bus_number',//出车单号
  79. 'message_type_id' => 'm.msg_type',
  80. 'message_type' => DictType::find()->select('type_name')->where('id = m.msg_type')->limit(1),
  81. 'm.create_time',//发送时间
  82. 'm.send_message',//短信内容
  83. 'm.is_read',//是否已读
  84. 'o.run_date',
  85. 'finance_status_id' => 'o.finance_status',
  86. 'finance_status' => DictType::find()->select('type_name')->where('id = o.finance_status')->limit(1),
  87. 'run_status_id' => 'o.run_status',
  88. 'run_status' => DictType::find()->select('type_name')->where('id = o.run_status')->limit(1),
  89. ])
  90. ->from(self::tableName() . ' as m')
  91. ->innerJoin(BusOrder::tableName() . ' as o', 'm.bus_number = o.bus_number')
  92. ->where([
  93. 'and',
  94. ['=', 'm.driver_id', $driver_id],
  95. ['=', 'm.cancel_flag', 0],
  96. ['=', 'o.cancel_flag', 0],
  97. ['in', 'msg_type', [490, 491]],
  98. [
  99. 'or',
  100. ['!=','o.finance_status',446],
  101. ['!=','o.run_status',438],
  102. ],
  103. ['>=','run_date',date('Y-m-d',strtotime("-7 day"))],
  104. ])
  105. ->offset($start_row)
  106. ->limit($page_size)
  107. ->orderBy(['run_date' => SORT_DESC,'create_time' => SORT_DESC])
  108. ->asArray()->all();
  109. return $result;
  110. }
  111. /**
  112. * Function Description:添加司机消息
  113. * Function Name: addMessage
  114. * @param int $msg_type 司机类型
  115. * @param int $bus_number 出车单号
  116. *
  117. * @return array
  118. *
  119. * @author 张帅
  120. */
  121. public function addMessage($msg_type,$bus_number){
  122. #region 1.获取数据详情
  123. $finance_list = BusOrder::find()
  124. ->select([
  125. 'bus_number',
  126. 'driver_id' => 'send_bus_driver_res_id',
  127. 'run_date',
  128. 'itinerary_name',
  129. '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),//日志
  130. ])
  131. ->from(BusOrder::tableName() . ' as o')
  132. ->where([
  133. 'and',
  134. ['=', 'cancel_flag', 0],
  135. ['!=', 'send_bus_driver_res_id', ''],
  136. ['in', 'bus_number', explode(',',$bus_number)],
  137. ])
  138. ->asArray()->all();
  139. #endregion
  140. #region 2.插入信息表数据
  141. if (count($finance_list) > 0) {
  142. $msg_list = [];
  143. if($msg_type == 2){
  144. foreach ($finance_list as $key => $vel) {
  145. $msg_list[] = [
  146. 'CANCEL_FLAG' => 0,
  147. 'CREATE_USER_ID' => 1,
  148. 'CREATE_TIME' => date('Y-m-d H:i:s'),
  149. 'UPDATE_USER_ID' => 1,
  150. 'UPDATE_TIME' => date('Y-m-d H:i:s'),
  151. 'BUS_NUMBER' => $vel['bus_number'],
  152. 'DRIVER_ID' => $vel['driver_id'],
  153. 'MSG_TYPE' => 490,
  154. 'SEND_MESSAGE' => date('m',strtotime($vel['run_date'])) . '月' . date('d',strtotime($vel['run_date'])) . '日,(' . $vel['itinerary_name'] . ')的出车任务报账被驳回,驳回原因“' . $vel['finance_log'] . '”,请重新进行报账操作。点击进入报账页面,保留之前的报账信息',
  155. 'IS_READ' => 0,
  156. ];
  157. };
  158. }elseif($msg_type == 3){
  159. foreach ($finance_list as $key => $vel) {
  160. $msg_list[] = [
  161. 'CANCEL_FLAG' => 0,
  162. 'CREATE_USER_ID' => 1,
  163. 'CREATE_TIME' => date('Y-m-d H:i:s'),
  164. 'UPDATE_USER_ID' => 1,
  165. 'UPDATE_TIME' => date('Y-m-d H:i:s'),
  166. 'BUS_NUMBER' => $vel['bus_number'],
  167. 'DRIVER_ID' => $vel['driver_id'],
  168. 'MSG_TYPE' => 491,
  169. 'SEND_MESSAGE' => date('m',strtotime($vel['run_date'])) . '月' . date('d',strtotime($vel['run_date'])) . '日,(' . $vel['itinerary_name'] . ')的出车任务已下发,请在任务列表中进行查看,点击进入该任务的任务单页面',
  170. 'IS_READ' => 0,
  171. ];
  172. };
  173. }elseif($msg_type == 4){
  174. foreach ($finance_list as $key => $vel) {
  175. $msg_list[] = [
  176. 'CANCEL_FLAG' => 0,
  177. 'CREATE_USER_ID' => 1,
  178. 'CREATE_TIME' => date('Y-m-d H:i:s'),
  179. 'UPDATE_USER_ID' => 1,
  180. 'UPDATE_TIME' => date('Y-m-d H:i:s'),
  181. 'BUS_NUMBER' => $vel['bus_number'],
  182. 'DRIVER_ID' => $vel['driver_id'],
  183. 'MSG_TYPE' => 491,
  184. 'SEND_MESSAGE' => date('m',strtotime($vel['run_date'])) . '月' . date('d',strtotime($vel['run_date'])) . '日,(' . $vel['itinerary_name'] . ')的出车任务有信息被变更,请在任务列表中进行查看确认最新信息,点击进入该任务的任务单页面',
  185. 'IS_READ' => 0,
  186. ];
  187. };
  188. }else{
  189. return [];
  190. }
  191. $res = Yii::$app->db->createCommand()->batchInsert(DriverMessage::tableName(),
  192. ['CANCEL_FLAG', 'CREATE_USER_ID', 'CREATE_TIME', 'UPDATE_USER_ID', 'UPDATE_TIME', 'BUS_NUMBER', 'DRIVER_ID', 'MSG_TYPE', 'SEND_MESSAGE', 'IS_READ'],
  193. $msg_list)->execute();
  194. if($res){
  195. return $msg_list;
  196. }else{
  197. return [];
  198. }
  199. } else {
  200. return [];
  201. }
  202. }
  203. /**
  204. * Function Description:推送
  205. * Function Name: actionPush
  206. * @param int $push_type 推送类型 1.报账通知(未报账) 2.报账通知(已驳回) 3.派车通知发送时间
  207. * @param array $push_arr 推送数组
  208. *
  209. * @return bool|string
  210. *
  211. * @author 张帅
  212. */
  213. public function push($push_type, $push_arr)
  214. {
  215. foreach ($push_arr as $key => $vel) {
  216. if ($push_type == 1) {
  217. $n_title = '报账提醒';
  218. $n_content = $vel['date'] . '的出车任务未报账';
  219. } elseif ($push_type == 2) {
  220. $n_title = '报账提醒';
  221. $n_content = $vel['date'] . '的出车任务报账被驳回';
  222. } elseif ($push_type == 3) {
  223. $n_title = '派车提醒';
  224. $n_content = $vel['date'] . '的出车任务已下发';
  225. } elseif ($push_type == 4) {
  226. $n_title = '任务更新';
  227. $n_content = $vel['date'] . '的出车任务有更新';
  228. } else {
  229. $n_title = '标题';
  230. $n_content = '推送内容';
  231. }
  232. $arr = array('fromer' => '发送者', 'fromer_name' => '发送者名字', 'fromer_icon' => '发送者头像', 'image' => '发送图片链接', 'sound' => '发送音乐链接');
  233. //自定义参数
  234. $app_keys = '0fdf462e3c9d6ddaa525a4fd';
  235. $masterSecret = '9f0dfe4da5893fb6065b1d08';
  236. $send_no = 4;
  237. $receiver_value = $vel['driver_id'];//发送的司机资源ID,万一有多个以逗号分隔
  238. $platform = 'android';
  239. $msg_content = json_encode(array('n_builder_id' => 0, 'n_title' => $n_title, 'n_content' => $n_content, 'n_extras' => $arr));
  240. $jpush = new jpush($masterSecret, $app_keys);
  241. $jpush->send($send_no, 3, $receiver_value, 1, $msg_content, $platform);
  242. }
  243. return true;
  244. }
  245. }