|
@@ -438,4 +438,148 @@ if(!function_exists('formatCurrency')) {
|
|
|
}
|
|
|
|
|
|
|
|
|
+// // 发送个人消息
|
|
|
+// $message_id = sendMessage(
|
|
|
+// '个人消息标题',
|
|
|
+// '消息内容',
|
|
|
+// 2, // 客户相关
|
|
|
+// 0, // 个人消息
|
|
|
+// 123, // 员工ID
|
|
|
+// 1 // 重要
|
|
|
+// );
|
|
|
+
|
|
|
+// // 发送部分群发消息
|
|
|
+// $message_id = sendMessage(
|
|
|
+// '部门通知',
|
|
|
+// '通知内容',
|
|
|
+// 1, // 系统消息
|
|
|
+// 1, // 部分群发
|
|
|
+// [101, 102, 103], // 员工ID数组
|
|
|
+// 0 // 普通优先级
|
|
|
+// );
|
|
|
+
|
|
|
+// // 发送全体公告
|
|
|
+// $message_id = sendMessage(
|
|
|
+// '系统升级通知',
|
|
|
+// '系统将于今晚10点维护',
|
|
|
+// 1, // 系统消息
|
|
|
+// 2, // 全体公告
|
|
|
+// [], // 无需指定接收者
|
|
|
+// 2 // 紧急
|
|
|
+// );
|
|
|
+
|
|
|
+// // 获取未读消息数
|
|
|
+// $unread_count = getUnreadMessageCount();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * 发送消息函数
|
|
|
+ *
|
|
|
+ * @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;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取新插入消息的ID
|
|
|
+ $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) {
|
|
|
+ // 全体公告,不需要添加接收人记录
|
|
|
+ // 在message_list.php中通过target_type=2来判断全员可见
|
|
|
+ }
|
|
|
+
|
|
|
+ // 记录操作日志
|
|
|
+ logAction("发送消息: $title");
|
|
|
+
|
|
|
+ return $message_id;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 获取未读消息数量
|
|
|
+ *
|
|
|
+ * @param int $employee_id 员工ID,默认为当前登录员工
|
|
|
+ * @return int 未读消息数量
|
|
|
+ */
|
|
|
+function getUnreadMessageCount($employee_id = null) {
|
|
|
+ global $conn;
|
|
|
+
|
|
|
+ // 如果没有指定员工ID,使用当前登录员工ID
|
|
|
+ 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']);
|
|
|
+}
|
|
|
|