Message.php 18 KB

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