UserInteractModel.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace loyalsoft;
  3. /**
  4. * 用户交互信息数据模型
  5. * 注:mem中是独立于userInfo的存在, 以减少交互操作的时候出现写入冲突产生覆盖的情况。
  6. * attention: 读写数据的时候,利用redis的watch机制实现事务写入,加上自动重试机制. (Ps.这是目标,gwang.20170426153414)
  7. *
  8. * @version
  9. * 1.0.0 Created at 2017-3-25. by --gwang
  10. * @author gwang (mail@wanggangzero.cn)
  11. * @copyright ? 2017-3-25, SJZ LoyalSoft Corporation & gwang. All rights reserved.
  12. */
  13. class UserInteractModel extends Object_ext
  14. {
  15. public $uid;
  16. public $zoneid;
  17. // ------------字段声明-------------
  18. /**
  19. * 邮件列表
  20. * @var object
  21. */
  22. public $mailQueue;
  23. /**
  24. * 邮件序列id
  25. * @var int
  26. */
  27. public $mailMaxId = 0;
  28. /**
  29. * 系统邮件记录[存在于系统公共缓冲区的邮件仅能接收一次]
  30. * @var array
  31. */
  32. public $sysMailRecord;
  33. // ------------ 初始化 ------------------
  34. /**
  35. * 创建账号的时候执行初始化.
  36. */
  37. public function initialize()
  38. {
  39. $this->mailQueue = ObjectInit();
  40. $this->mailMaxId = 10000;
  41. $this->sysMailRecord = ArrayInit();
  42. }
  43. // ----------- 方法 ---------------------
  44. // <editor-fold defaultstate="collapsed" desc=" 公开方法 ">
  45. /**
  46. * 新增系统邮件
  47. * @param int $sysId 邮件的id
  48. * @param string $title 邮件标题
  49. * @param type $content 邮件正文
  50. * @param type $itemid 道具id (附加于tag字段)
  51. * @param type $startts 生效时间
  52. * @param type $endts 过期时间
  53. * @param type $interact 目标交互体
  54. */
  55. static function insertSysMail($sysId, $title, $content, $itemid, $startts, $endts, &$interact)
  56. {
  57. self::insertMail("0", "系统邮件", "", "", 1, $title, $content, $itemid, $startts, $endts, $interact);
  58. if ($sysId != 0) {
  59. $sysMailRecord = $interact->sysMailRecord;
  60. $sysMailRecord[] = $sysId;
  61. $interact->sysMailRecord = $sysMailRecord;
  62. }
  63. }
  64. /**
  65. * 新增邮件
  66. * @param string $fromoid 发送人的oid
  67. * @param type $name 发送人的呢称
  68. * @param type $pf pf
  69. * @param type $img 图像
  70. * @param type $type 邮件类型
  71. * @param string $title 邮件标题
  72. * @param string $content 邮件正文
  73. * @param type $tag 额外标签可以传递数据
  74. * @param ts $startts 生效时间
  75. * @param ts $endts 过期时间
  76. * @param UserInteractModel $interact 目标交互体
  77. * @return void
  78. */
  79. static function insertMail($fromoid, $name, $pf, $img, $type, $title, $content, $tag, $startts, $endts, &$interact)
  80. {
  81. if (CommUtil::tsCurrent() > $endts) {
  82. return;
  83. }
  84. $interact->mailMaxId++;
  85. $mailMaxId = $interact->mailMaxId;
  86. $mail = new EmailModel;
  87. $mail->fromoid = $fromoid;
  88. $mail->name = $name;
  89. $mail->pf = $pf;
  90. $mail->img = $img;
  91. $mail->type = $type;
  92. $mail->title = $title;
  93. $mail->content = $content;
  94. $mail->tag = $tag;
  95. $mail->startts = $startts;
  96. $mail->endts = $endts;
  97. $mail->ts = CommUtil::tsCurrent();
  98. $interact->mailQueue->$mailMaxId = $mail;
  99. # 记录日志
  100. self::_insertMailRecord($interact->oid, $interact->zoneid, $mail, $mailMaxId);
  101. }
  102. /**
  103. * 插入发送邮件的日志
  104. * @param string uid
  105. * @param int $zoneid
  106. * @param EmailModel $mail
  107. * @param int $mailId
  108. */
  109. static function _insertMailRecord($uid, $zoneid, $mail, $mailId)
  110. {
  111. $sql = <<<ins
  112. INSERT INTO `tab_mailrecord` (oid,`zoneid`,mailId, fromoid,`name`, pf,img,type,title,content,tag,startts,endts,insertts)
  113. VALUES ('%s',%d,%d,'%s','%s','%s','%s',%d,'%s','%s','%s',%d,%d,now());
  114. ins;
  115. $insertstr = sprintf($sql, $uid, $zoneid, $mailId, $mail->fromoid, $mail->name, #
  116. $mail->pf, $mail->img, $mail->type, $mail->title, $mail->content, #
  117. $mail->tag, $mail->startts, $mail->endts);
  118. daoInst()->query($insertstr);
  119. }
  120. /**
  121. * 获取邮件序列[自动过滤尚未到(生效)期的邮件,自动删除已过期的邮件]
  122. * @param UserInteractModel $interact Description
  123. */
  124. static function getMailQueue($interact)
  125. {
  126. $mailList = ObjectInit();
  127. $mailQueue = $interact->mailQueue;
  128. foreach ($mailQueue as $mailId => $mail) {
  129. $ts = CommUtil::tsCurrent();
  130. if ($ts >= $mail->startts) {
  131. if ($ts > $mail->endts) {
  132. self::deleteMail($mailId, $interact);
  133. } else {
  134. $mailList->$mailId = $mail;
  135. }
  136. }
  137. }
  138. return $mailList;
  139. }
  140. /**
  141. * 删除邮件[附件接收完毕之后]
  142. * @param type $mailId
  143. * @param UserInteractModel $interact Description
  144. */
  145. static function deleteMail($mailId, &$interact)
  146. {
  147. self::_drawMailRecord($mailId, $interact); # 领取记录
  148. StlUtil::dictRemove($interact->mailQueue, $mailId);
  149. }
  150. /**
  151. * 更新领取记录
  152. * @param type $mailId
  153. * @param UserInteractModel $interact
  154. */
  155. static function _drawMailRecord($mailId, $interact)
  156. {
  157. $sql = "update tab_mailrecord set drawedts=now() where mailId=%d and zoneid=%s and uid=%s;";
  158. $updatestr = sprintf($sql, $mailId, $interact->zoneid, $interact->uid);
  159. daoInst()->query($updatestr);
  160. }
  161. // </editor-fold>
  162. }