Message.php 18 KB

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