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