Message.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. <?php
  2. namespace app\common\model;
  3. use think\facade\Db;
  4. /**
  5. * ============================================================================
  6. *
  7. * ============================================================================
  8. *
  9. * ----------------------------------------------------------------------------
  10. *
  11. * ============================================================================
  12. * 数据层模型
  13. */
  14. class Message extends BaseModel
  15. {
  16. public $page_info;
  17. /**
  18. * 站内信列表
  19. * @access public
  20. * @author csdeshang
  21. * @param array $condition 条件数组
  22. * @param int $pagesize 分页页数
  23. * @return array
  24. */
  25. public function getMessageList($condition, $pagesize = '')
  26. {
  27. //得到条件语句
  28. $where = $this->getCondition($condition, false);
  29. $order = 'message_id DESC';
  30. if ($pagesize) {
  31. $message_list = Db::name('message')->where($where)->order($order)->paginate(['list_rows' => $pagesize, 'query' => request()->param()], false);
  32. $this->page_info = $message_list;
  33. $message = $message_list->items();
  34. } else {
  35. $message = Db::name('message')->where($where)->order($order)->select()->toArray();
  36. }
  37. return $message;
  38. }
  39. /**
  40. * 卖家站内信列表
  41. * @access public
  42. * @author csdeshang
  43. * @param array $condition 条件数组
  44. * @param int $pagesize 分页页数
  45. * @return array
  46. */
  47. public function getStoreMessageList($condition, $pagesize = '')
  48. {
  49. //得到条件语句
  50. $where = $this->getCondition($condition);
  51. $field = 'message.*,store.store_name,store.store_id';
  52. $order = 'message.message_id DESC';
  53. if ($pagesize) {
  54. $message_list = Db::name('message')->join('store', 'message.from_member_id = store.member_id', 'LEFT')->field($field)->where($where)->order($order)->paginate(['list_rows' => $pagesize, 'query' => request()->param()], false);
  55. $this->page_info = $message_list;
  56. $message = $message_list->items();
  57. } else {
  58. $message = Db::name('message')->join('store', 'message.from_member_id = store.member_id', 'LEFT')->field($field)->where($where)->order($order)->select()->toArray();
  59. }
  60. return $message;
  61. }
  62. /**
  63. * 站内信总数
  64. * @access public
  65. * @author csdeshang
  66. * @param array $condition 条件
  67. * @return int
  68. */
  69. public function getMessageCount($condition)
  70. {
  71. $where = $this->getCondition($condition, false);
  72. return Db::name('message')->where($where)->count('message_id');
  73. }
  74. /**
  75. * 获取未读信息数量
  76. * @access public
  77. * @author csdeshang
  78. * @param type $member_id 会员id
  79. * @return int
  80. */
  81. public function getNewMessageCount($member_id)
  82. {
  83. $special_condition = array();
  84. $special_condition['to_member_id'] = "$member_id";
  85. $special_condition['no_message_state'] = '2';
  86. $special_condition['message_open_common'] = '0';
  87. $special_condition['no_del_member_id'] = "$member_id";
  88. $special_condition['no_read_member_id'] = "$member_id";
  89. $countnum = $this->getMessageCount($special_condition);
  90. return $countnum;
  91. }
  92. /**
  93. * 站内信单条信息
  94. * @access public
  95. * @author csdeshang
  96. * @param array $condition 条件数组
  97. * @param int $pagesize 分页页数
  98. */
  99. public function getOneMessage($condition)
  100. {
  101. //得到条件语句
  102. $where = $this->getCondition($condition);
  103. $message_list = Db::name('message')->alias('message')->where($where)->select()->toArray();
  104. if (!empty($message_list)) {
  105. return $message_list[0];
  106. } else {
  107. return null;
  108. }
  109. }
  110. /**
  111. * 站内信保存
  112. * @access public
  113. * @author csdeshang
  114. * @param type $data 参数内容
  115. * @return boolean
  116. */
  117. public function addMessage($data)
  118. {
  119. if ($data['member_id'] == '') {
  120. return false;
  121. }
  122. $array = array();
  123. $array['message_parent_id'] = isset($data['message_parent_id']) ? $data['message_parent_id'] : '0';
  124. $array['from_member_id'] = isset($data['from_member_id']) ? $data['from_member_id'] : '0';
  125. $array['from_member_name'] = isset($data['from_member_name']) ? $data['from_member_name'] : '';
  126. $array['to_member_id'] = $data['member_id'];
  127. $array['to_member_name'] = isset($data['to_member_name']) ? $data['to_member_name'] : '';
  128. $array['message_body'] = trim($data['msg_content']);
  129. $array['message_time'] = TIMESTAMP;
  130. $array['message_update_time'] = TIMESTAMP;
  131. $array['message_type'] = isset($data['message_type']) ? $data['message_type'] : '0';
  132. $array['message_ismore'] = isset($data['message_ismore']) ? $data['message_ismore'] : '0';
  133. $array['read_member_id'] = isset($data['read_member_id']) ? $data['read_member_id'] : '';
  134. $array['del_member_id'] = isset($data['del_member_id']) ? $data['del_member_id'] : '';
  135. return Db::name('message')->insertGetId($array);
  136. }
  137. /**
  138. * 更新站内信
  139. * @access public
  140. * @author csdeshang
  141. * @param type $data 更新数据
  142. * @param type $condition 条件
  143. * @return boolean
  144. */
  145. public function editCommonMessage($data, $condition)
  146. {
  147. if (empty($data)) {
  148. return false;
  149. }
  150. //得到条件语句
  151. $where = $this->getCondition($condition);
  152. return Db::name('message')->alias('message')->where($where)->update($data);
  153. }
  154. /**
  155. * 删除发送信息
  156. * @access public
  157. * @author csdeshang
  158. * @param type $condition 条件
  159. * @param type $drop_type 删除类型
  160. * @return boolean
  161. */
  162. public function delCommonMessage($condition, $drop_type)
  163. {
  164. //得到条件语句
  165. $where = $this->getCondition($condition);
  166. //查询站内信列表
  167. $field = 'message_id,from_member_id,to_member_id,message_state,message_open';
  168. $message_list = Db::name('message')->alias('message')->where($where)->field($field)->select()->toArray();
  169. unset($where);
  170. if (empty($message_list)) {
  171. return true;
  172. }
  173. $delmessage_id = array();
  174. $updatemessage_id = array();
  175. foreach ($message_list as $k => $v) {
  176. if ($drop_type == 'msg_private') {
  177. if ($v['message_state'] == 2) {
  178. $delmessage_id[] = $v['message_id'];
  179. } elseif ($v['message_state'] == 0) {
  180. $updatemessage_id[] = $v['message_id'];
  181. }
  182. } elseif ($drop_type == 'msg_list') {
  183. if ($v['message_state'] == 1) {
  184. $delmessage_id[] = $v['message_id'];
  185. } elseif ($v['message_state'] == 0) {
  186. $updatemessage_id[] = $v['message_id'];
  187. }
  188. } elseif ($drop_type == 'sns_msg') {
  189. $delmessage_id[] = $v['message_id'];
  190. }
  191. }
  192. if (!empty($delmessage_id)) {
  193. $delmessage_id_str = "'" . implode("','", $delmessage_id) . "'";
  194. $where = $this->getCondition(array('message_id_in' => $delmessage_id_str));
  195. Db::name('message')->where($where)->delete();
  196. unset($where);
  197. }
  198. if (!empty($updatemessage_id)) {
  199. $updatemessage_id_str = "'" . implode("','", $updatemessage_id) . "'";
  200. $where = $this->getCondition(array('message_id_in' => $updatemessage_id_str));
  201. if ($drop_type == 'msg_private') {
  202. Db::name('message')->where($where)->update(array('message_state' => 1));
  203. } elseif ($drop_type == 'msg_list') {
  204. Db::name('message')->where($where)->update(array('message_state' => 2));
  205. }
  206. }
  207. return true;
  208. }
  209. /**
  210. * 删除批量信息
  211. * @access public
  212. * @author csdeshang
  213. * @param type $condition 条件
  214. * @param type $to_member_id 会员ID
  215. * @return boolean
  216. */
  217. public function delBatchMessage($condition, $to_member_id)
  218. {
  219. //得到条件语句
  220. $where = $this->getCondition($condition);
  221. //查询站内信列表
  222. $message_list = Db::name('message')->alias('message')->where($where)->select()->toArray();
  223. unset($where);
  224. if (empty($message_list)) {
  225. return true;
  226. }
  227. foreach ($message_list as $k => $v) {
  228. $tmp_delid_str = '';
  229. if (!empty($v['del_member_id'])) {
  230. $tmp_delid_arr = explode(',', $v['del_member_id']);
  231. if (!in_array($to_member_id, $tmp_delid_arr)) {
  232. $tmp_delid_arr[] = $to_member_id;
  233. }
  234. foreach ($tmp_delid_arr as $delid_k => $delid_v) {
  235. if ($delid_v == '') {
  236. unset($tmp_delid_arr[$delid_k]);
  237. }
  238. }
  239. $tmp_delid_arr = array_unique($tmp_delid_arr); //去除相同
  240. sort($tmp_delid_arr); //排序
  241. $tmp_delid_str = "," . implode(',', $tmp_delid_arr) . ",";
  242. } else {
  243. $tmp_delid_str = ",{$to_member_id},";
  244. }
  245. if ($tmp_delid_str == $v['to_member_id']) { //所有用户已经全部阅读过了可以删除
  246. Db::name('message')->where('message_id', $v['message_id'])->delete();
  247. } else {
  248. Db::name('message')->where('message_id', $v['message_id'])->update(array('del_member_id' => $tmp_delid_str));
  249. }
  250. }
  251. return true;
  252. }
  253. /**
  254. * 获取条件
  255. * @access public
  256. * @author csdeshang
  257. * @param type $condition_array 条件数组
  258. * @param bool $join 连接
  259. * @return type
  260. */
  261. private function getCondition($condition_array, $join = true)
  262. {
  263. $condition_sql = '1=1';
  264. //站内信编号
  265. if ($join) {
  266. if (isset($condition_array['message_id']) && $condition_array['message_id'] != '') {
  267. $condition_sql .= " and message.message_id = '{$condition_array['message_id']}'";
  268. }
  269. //父站内信
  270. if (isset($condition_array['message_parent_id']) && $condition_array['message_parent_id'] != '') {
  271. $condition_sql .= " and message.message_parent_id = '{$condition_array['message_parent_id']}'";
  272. }
  273. //站内信类型
  274. if (isset($condition_array['message_type']) && $condition_array['message_type'] != '') {
  275. $condition_sql .= " and message.message_type = '{$condition_array['message_type']}'";
  276. }
  277. //站内信类型
  278. if (isset($condition_array['message_type_in']) && $condition_array['message_type_in'] != '') {
  279. $condition_sql .= " and message.message_type in (" . $condition_array['message_type_in'] . ")";
  280. }
  281. //站内信不显示的状态
  282. if (isset($condition_array['no_message_state']) && $condition_array['no_message_state'] != '') {
  283. $condition_sql .= " and message.message_state != '{$condition_array['no_message_state']}'";
  284. }
  285. //是否已读
  286. if (isset($condition_array['message_open_common']) && $condition_array['message_open_common'] != '') {
  287. $condition_sql .= " and message.message_open = '{$condition_array['message_open_common']}'";
  288. }
  289. //普通信件接收到的会员查询条件为
  290. if (isset($condition_array['to_member_id_common']) && $condition_array['to_member_id_common'] != '') {
  291. $condition_sql .= " and message.to_member_id='{$condition_array['to_member_id_common']}' ";
  292. }
  293. //接收到的会员查询条件为如果message_ismore为1时则to_member_id like'%memberid%',如果message_ismore为0时则to_member_id = memberid
  294. if (isset($condition_array['to_member_id']) && $condition_array['to_member_id'] != '') {
  295. $condition_sql .= " and (message.to_member_id ='all' or (message.message_ismore=0 and message.to_member_id='{$condition_array['to_member_id']}') or (message.message_ismore=1 and message.to_member_id like '%,{$condition_array['to_member_id']},%'))";
  296. }
  297. //发信人
  298. if (isset($condition_array['from_member_id']) && $condition_array['from_member_id'] != '') {
  299. $condition_sql .= " and message.from_member_id='{$condition_array['from_member_id']}' ";
  300. }
  301. if (isset($condition_array['from_to_member_id']) && $condition_array['from_to_member_id'] != '') {
  302. $condition_sql .= " and (message.from_member_id='{$condition_array['from_to_member_id']}' or message.to_member_id ='all' or (message.message_ismore=0 and message.to_member_id='{$condition_array['from_to_member_id']}') or (message.message_ismore=1 and message.to_member_id like '%,{$condition_array['from_to_member_id']},%'))";
  303. }
  304. //未删除
  305. if (isset($condition_array['no_del_member_id']) && $condition_array['no_del_member_id'] != '') {
  306. $condition_sql .= " and message.del_member_id not like '%,{$condition_array['no_del_member_id']},%' ";
  307. }
  308. //未读
  309. if (isset($condition_array['no_read_member_id']) && $condition_array['no_read_member_id'] != '') {
  310. $condition_sql .= " and message.read_member_id not like '%,{$condition_array['no_read_member_id']},%' ";
  311. }
  312. //站内信编号in
  313. if (isset($condition_array['message_id_in'])) {
  314. if ($condition_array['message_id_in'] == '') {
  315. $condition_sql .= " and message_id in('')";
  316. } else {
  317. $condition_sql .= " and message_id in({$condition_array['message_id_in']})";
  318. }
  319. }
  320. } else {
  321. if (isset($condition_array['message_id']) && $condition_array['message_id'] != '') {
  322. $condition_sql .= " and message_id = '{$condition_array['message_id']}'";
  323. }
  324. //父站内信
  325. if (isset($condition_array['message_parent_id']) && $condition_array['message_parent_id'] != '') {
  326. $condition_sql .= " and message_parent_id = '{$condition_array['message_parent_id']}'";
  327. }
  328. //站内信类型
  329. if (isset($condition_array['message_type']) && $condition_array['message_type'] != '') {
  330. $condition_sql .= " and message_type = '{$condition_array['message_type']}'";
  331. }
  332. //站内信类型
  333. if (isset($condition_array['message_type_in']) && $condition_array['message_type_in'] != '') {
  334. $condition_sql .= " and message_type in (" . $condition_array['message_type_in'] . ")";
  335. }
  336. //站内信不显示的状态
  337. if (isset($condition_array['no_message_state']) && $condition_array['no_message_state'] != '') {
  338. $condition_sql .= " and message_state != '{$condition_array['no_message_state']}'";
  339. }
  340. //是否已读
  341. if (isset($condition_array['message_open_common']) && $condition_array['message_open_common'] != '') {
  342. $condition_sql .= " and message_open = '{$condition_array['message_open_common']}'";
  343. }
  344. //普通信件接收到的会员查询条件为
  345. if (isset($condition_array['to_member_id_common']) && $condition_array['to_member_id_common'] != '') {
  346. $condition_sql .= " and to_member_id='{$condition_array['to_member_id_common']}' ";
  347. }
  348. //接收到的会员查询条件为如果message_ismore为1时则to_member_id like'%memberid%',如果message_ismore为0时则to_member_id = memberid
  349. if (isset($condition_array['to_member_id']) && $condition_array['to_member_id'] != '') {
  350. $condition_sql .= " and (to_member_id ='all' or (message_ismore=0 and to_member_id='{$condition_array['to_member_id']}') or (message_ismore=1 and to_member_id like '%,{$condition_array['to_member_id']},%'))";
  351. }
  352. //发信人
  353. if (isset($condition_array['from_member_id']) && $condition_array['from_member_id'] != '') {
  354. $condition_sql .= " and from_member_id='{$condition_array['from_member_id']}' ";
  355. }
  356. if (isset($condition_array['from_to_member_id']) && $condition_array['from_to_member_id'] != '') {
  357. $condition_sql .= " and (from_member_id='{$condition_array['from_to_member_id']}' or (message_ismore=0 and to_member_id='{$condition_array['from_to_member_id']}') or (message_ismore=1 and to_member_id like '%,{$condition_array['from_to_member_id']},%'))";
  358. }
  359. //未删除
  360. if (isset($condition_array['no_del_member_id']) && $condition_array['no_del_member_id'] != '') {
  361. $condition_sql .= " and del_member_id not like '%,{$condition_array['no_del_member_id']},%' ";
  362. }
  363. //未读
  364. if (isset($condition_array['no_read_member_id']) && $condition_array['no_read_member_id'] != '') {
  365. $condition_sql .= " and read_member_id not like '%,{$condition_array['no_read_member_id']},%' ";
  366. }
  367. //站内信编号in
  368. if (isset($condition_array['message_id_in'])) {
  369. if ($condition_array['message_id_in'] == '') {
  370. $condition_sql .= " and message_id in('')";
  371. } else {
  372. $condition_sql .= " and message_id in({$condition_array['message_id_in']})";
  373. }
  374. }
  375. }
  376. return $condition_sql;
  377. }
  378. }