model.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * The model file of aiapp module of ZenTaoPMS.
  4. *
  5. * @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.zentao.net)
  6. * @license ZPL(https://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
  7. * @author Wenrui LI <liwenrui@easycorp.ltd>
  8. * @package ai
  9. * @link https://www.zentao.net
  10. */
  11. class aiappModel extends model
  12. {
  13. /**
  14. * ai model.
  15. *
  16. * @var aiModel
  17. * @access public
  18. */
  19. public $ai;
  20. /**
  21. * Constructor.
  22. *
  23. * @access public
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. $this->loadModel('ai');
  30. }
  31. /**
  32. * Get latest mini programs.
  33. *
  34. * @param pager $pager
  35. * @access public
  36. * @return array
  37. */
  38. public function getLatestMiniPrograms($pager = null, $order = 'publishedDate_desc')
  39. {
  40. return $this->dao->select('*')
  41. ->from(TABLE_AI_MINIPROGRAM)
  42. ->where('deleted')->eq('0')
  43. ->andWhere('published')->eq('1')
  44. ->andWhere('publishedDate')->ge(date('Y-m-d H:i:s', strtotime('-1 months')))
  45. ->orderBy($order)
  46. ->page($pager)
  47. ->fetchAll('id', false);
  48. }
  49. /**
  50. * Count latest mini programs.
  51. *
  52. * @access private
  53. * @return int
  54. */
  55. private function countLatestMiniPrograms()
  56. {
  57. return (int)$this->dao->select('COUNT(1) AS count')
  58. ->from(TABLE_AI_MINIPROGRAM)
  59. ->where('deleted')->eq('0')
  60. ->andWhere('published')->eq('1')
  61. ->andWhere('createdDate')->ge(date('Y-m-d H:i:s', strtotime('-1 months')))
  62. ->fetch('count');
  63. }
  64. /**
  65. * Save mini program message.
  66. *
  67. * @param string $appID
  68. * @param string $type
  69. * @param string $content
  70. * @access public
  71. * @return bool
  72. */
  73. public function saveMiniProgramMessage($appID, $type, $content)
  74. {
  75. $message = new stdClass();
  76. $message->appID = $appID;
  77. $message->user = $this->app->user->id;
  78. $message->type = $type;
  79. $message->content = $content;
  80. $message->createdDate = helper::now();
  81. $this->dao->insert(TABLE_AI_MESSAGE)
  82. ->data($message)
  83. ->exec();
  84. return !dao::isError();
  85. }
  86. /**
  87. * Delete history messages by id.
  88. *
  89. * @param string $appID
  90. * @param string $userID
  91. * @param array $messageIDs
  92. * @access private
  93. * @return void
  94. */
  95. private function deleteHistoryMessagesByID($appID, $userID, $messageIDs)
  96. {
  97. $this->dao->delete()
  98. ->from(TABLE_AI_MESSAGE)
  99. ->where('appID')->eq($appID)
  100. ->andWhere('user')->eq($userID)
  101. ->andWhere('id')->notin($messageIDs)
  102. ->exec();
  103. }
  104. /**
  105. * Get history messages.
  106. *
  107. * @param string $appID
  108. * @param int $limit
  109. * @access public
  110. * @return array
  111. */
  112. public function getHistoryMessages($appID, $limit = 20)
  113. {
  114. $messages = $this->dao->select('*')
  115. ->from(TABLE_AI_MESSAGE)
  116. ->where('appID')->eq($appID)
  117. ->andWhere('user')->eq($this->app->user->id)
  118. ->orderBy('createdDate_desc')
  119. ->limit($limit)
  120. ->fetchAll('id', false);
  121. $messageIDs = array();
  122. foreach($messages as $message)
  123. {
  124. $message->createdDate = date('Y/n/j G:i', strtotime($message->createdDate));
  125. $messageIDs[] = $message->id;
  126. }
  127. $this->deleteHistoryMessagesByID($appID, $this->app->user->id, $messageIDs);
  128. return $messages;
  129. }
  130. /**
  131. * Get collected mini programIDs
  132. *
  133. * @param string $userID
  134. * @param pager $pager
  135. * @access public
  136. * @return array
  137. */
  138. public function getCollectedMiniProgramIDs($userID, $pager = null)
  139. {
  140. $programs = $this->dao->select('*')
  141. ->from(TABLE_AI_MINIPROGRAMSTAR)
  142. ->where('userID')->eq($userID)
  143. ->orderBy('createdDate_desc')
  144. ->page($pager)
  145. ->fetchAll('appID', false);
  146. return array_keys($programs);
  147. }
  148. /**
  149. * Get square category array.
  150. *
  151. * @param array $collectedIDs
  152. * @param int $latestSum
  153. * @access public
  154. * @return array
  155. */
  156. public function getSquareCategoryArray($collectedIDs = null, $latestSum = null)
  157. {
  158. $squareCategoryArray = $this->lang->aiapp->squareCategories;
  159. if($collectedIDs === null) $collectedIDs = $this->getCollectedMiniProgramIDs($this->app->user->id);
  160. if($latestSum === null) $latestSum = $this->countLatestMiniPrograms();
  161. if($latestSum == 0) unset($squareCategoryArray['latest']);
  162. return $squareCategoryArray;
  163. }
  164. /**
  165. * Get used category array.
  166. *
  167. * @access public
  168. * @return array
  169. */
  170. public function getUsedCategoryArray()
  171. {
  172. $publishedCustomCategories = $this->ai->getPublishedCustomCategories();
  173. $categoryArray = array_merge($this->lang->aiapp->categoryList, $this->ai->getCustomCategories());
  174. $usedCategoryArray = array();
  175. foreach($categoryArray as $key => $value)
  176. {
  177. if(in_array($key, $publishedCustomCategories)) $usedCategoryArray[$key] = $value;
  178. }
  179. return $usedCategoryArray;
  180. }
  181. }