|
@@ -438,4 +438,148 @@ if(!function_exists('formatCurrency')) {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ * 发送消息函数
|
|
|
|
+ *
|
|
|
|
+ * @param string $title 消息标题
|
|
|
|
+ * @param string $content 消息内容
|
|
|
|
+ * @param int $message_type 消息类型: 1=系统消息, 2=客户相关, 3=订单相关, 4=任务提醒, 5=其他
|
|
|
|
+ * @param int $target_type 接收目标类型: 0=个人, 1=部分群发, 2=全体公告
|
|
|
|
+ * @param array|int $recipients 当target_type=0或1时,接收者ID数组或单个接收者ID
|
|
|
|
+ * @param int $priority 优先级: 0=普通, 1=重要, 2=紧急
|
|
|
|
+ * @param int|null $related_customer_id 相关客户ID (可选)
|
|
|
|
+ * @param int|null $related_order_id 相关订单ID (可选)
|
|
|
|
+ * @return int|false 成功返回消息ID,失败返回false
|
|
|
|
+ */
|
|
|
|
+function sendMessage($title, $content, $message_type = 1, $target_type = 0, $recipients = [], $priority = 0, $related_customer_id = null, $related_order_id = null) {
|
|
|
|
+ global $conn;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ $title = mysqli_real_escape_string($conn, trim($title));
|
|
|
|
+ $content = mysqli_real_escape_string($conn, trim($content));
|
|
|
|
+ $message_type = intval($message_type);
|
|
|
|
+ $target_type = intval($target_type);
|
|
|
|
+ $priority = intval($priority);
|
|
|
|
+ $related_customer_id = $related_customer_id ? intval($related_customer_id) : "NULL";
|
|
|
|
+ $related_order_id = $related_order_id ? intval($related_order_id) : "NULL";
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if (empty($title) || empty($content)) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ $sql = "INSERT INTO messages (title, content, message_type, target_type, priority, related_customer_id, related_order_id)
|
|
|
|
+ VALUES ('$title', '$content', $message_type, $target_type, $priority, $related_customer_id, $related_order_id)";
|
|
|
|
+
|
|
|
|
+ if (!$conn->query($sql)) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ $message_id = $conn->insert_id;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if ($target_type == 0 || $target_type == 1) {
|
|
|
|
+
|
|
|
|
+ if (!is_array($recipients)) {
|
|
|
|
+ $recipients = [$recipients];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (empty($recipients)) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ $values = [];
|
|
|
|
+ foreach ($recipients as $employee_id) {
|
|
|
|
+ $employee_id = intval($employee_id);
|
|
|
|
+ if ($employee_id > 0) {
|
|
|
|
+ $values[] = "($message_id, $employee_id, 0, NULL, 0, NOW())";
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (!empty($values)) {
|
|
|
|
+ $recipientSql = "INSERT INTO message_recipients (message_id, employee_id, is_read, read_time, is_deleted, created_at)
|
|
|
|
+ VALUES " . implode(",", $values);
|
|
|
|
+ $conn->query($recipientSql);
|
|
|
|
+ }
|
|
|
|
+ } else if ($target_type == 2) {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ logAction("发送消息: $title");
|
|
|
|
+
|
|
|
|
+ return $message_id;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ * 获取未读消息数量
|
|
|
|
+ *
|
|
|
|
+ * @param int $employee_id 员工ID,默认为当前登录员工
|
|
|
|
+ * @return int 未读消息数量
|
|
|
|
+ */
|
|
|
|
+function getUnreadMessageCount($employee_id = null) {
|
|
|
|
+ global $conn;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if ($employee_id === null) {
|
|
|
|
+ if (empty($_SESSION['employee_id'])) {
|
|
|
|
+ return 0;
|
|
|
|
+ }
|
|
|
|
+ $employee_id = $_SESSION['employee_id'];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $employee_id = intval($employee_id);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ $sql = "SELECT COUNT(*) AS count
|
|
|
|
+ FROM messages m
|
|
|
|
+ LEFT JOIN message_recipients mr ON m.id = mr.message_id AND mr.employee_id = $employee_id
|
|
|
|
+ WHERE (m.target_type = 2 OR (mr.employee_id = $employee_id))
|
|
|
|
+ AND (mr.is_deleted = 0 OR mr.is_deleted IS NULL)
|
|
|
|
+ AND (mr.is_read = 0 OR mr.is_read IS NULL)";
|
|
|
|
+
|
|
|
|
+ $result = mysqli_query($conn, $sql);
|
|
|
|
+ $row = mysqli_fetch_assoc($result);
|
|
|
|
+
|
|
|
|
+ return intval($row['count']);
|
|
|
|
+}
|
|
|
|
|