Fast.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 上海牛之云网络科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\servicer\model;
  11. use app\model\BaseModel;
  12. use think\facade\Cache;
  13. /**
  14. * 快捷回复
  15. * Class Fast
  16. * @package addon\servicer\model
  17. */
  18. class Fast extends BaseModel
  19. {
  20. /**
  21. * 缓冲标签名称
  22. */
  23. const CACHE_TAG_NAME = 'servicer_fast_reply';
  24. /**
  25. * 表名称
  26. */
  27. const TABLE_NAME = 'servicer_fast_reply';
  28. /**
  29. * 添加
  30. * @param array $params
  31. * @return array
  32. */
  33. public function add(array $params)
  34. {
  35. $site_id = $params['site_id'] ?? '';
  36. if ($site_id === '') {
  37. return $this->error('', 'REQUEST_SITE_ID');
  38. }
  39. $res = model(self::TABLE_NAME)->add(array_merge($params, ['create_time' => time()]));
  40. Cache::tag(self::CACHE_TAG_NAME)->clear();
  41. return $this->success($res);
  42. }
  43. /**
  44. * 编辑
  45. * @param array $params
  46. * @param array $condition
  47. * @return array
  48. */
  49. public function edit(array $params, array $condition)
  50. {
  51. $check_condition = array_column($condition, 2, 0);
  52. $site_id = $check_condition['site_id'] ?? 0;
  53. $id = $check_condition['id'] ?? 0;
  54. if ($site_id === '') {
  55. return $this->error('', 'REQUEST_SITE_ID');
  56. }
  57. if (empty($id)) {
  58. return $this->error('', 'PARAMETER_ERROR');
  59. }
  60. // 检测是否存在
  61. $info = $this->getInfo($condition, 'id')['data'];
  62. if (empty($info)) {
  63. return $this->error('', '数据不存在');
  64. }
  65. $res = model(self::TABLE_NAME)->update($params, $condition);
  66. Cache::tag(self::CACHE_TAG_NAME)->clear();
  67. return $this->success($res);
  68. }
  69. /**
  70. * 修改
  71. * @param $data
  72. * @param array $condition
  73. * @return array
  74. */
  75. public function update($data, array $condition)
  76. {
  77. $res = model(self::TABLE_NAME)->update($data, $condition);
  78. Cache::tag(self::CACHE_TAG_NAME)->clear();
  79. return $this->success($res);
  80. }
  81. /**
  82. * 删除
  83. * @param array $condition
  84. * @return array
  85. */
  86. public function delete(array $condition)
  87. {
  88. $check_condition = array_column($condition, 2, 0);
  89. $site_id = $check_condition['site_id'] ?? 0;
  90. if ($site_id === '') {
  91. return $this->error('', 'REQUEST_SITE_ID');
  92. }
  93. $res = model(self::TABLE_NAME)->delete($condition);
  94. Cache::tag(self::CACHE_TAG_NAME)->clear();
  95. return $this->success($res);
  96. }
  97. /**
  98. * 获取信息
  99. * @param $condition
  100. * @param bool $field
  101. * @return array
  102. */
  103. public function getInfo($condition, $field = true)
  104. {
  105. $params = json_encode(func_get_args());
  106. $cache_name = self::CACHE_TAG_NAME . '_' . __FUNCTION__ . '_' . $params;
  107. $res = Cache::get($cache_name);
  108. if (empty($res)) {
  109. $res = model(self::TABLE_NAME)->getInfo($condition, $field);
  110. Cache::tag(self::CACHE_TAG_NAME)->set($cache_name, $res);
  111. }
  112. return $this->success($res);
  113. }
  114. /**
  115. * 获取分页列表
  116. * @param array $condition
  117. * @param int $page
  118. * @param int $page_size
  119. * @param string $order
  120. * @param bool $field
  121. * @param string $alias
  122. * @param array $join
  123. * @return array
  124. */
  125. public function getPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'id desc', $field = true, $alias = '', $join = [])
  126. {
  127. $params = json_encode(func_get_args());
  128. $cache_name = self::CACHE_TAG_NAME . '_' . __FUNCTION__ . '_' . $params;
  129. $res = Cache::get($cache_name);
  130. if (empty($res)) {
  131. $res = model(self::TABLE_NAME)->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
  132. Cache::tag(self::CACHE_TAG_NAME)->set($cache_name, $res);
  133. }
  134. return $this->success($res);
  135. }
  136. /**
  137. * 获取列表
  138. * @param array $condition
  139. * @param mixed $field
  140. * @param string $order
  141. * @param null $limit
  142. * @return array
  143. */
  144. public function getList($condition = [], $field = true, $order = 'id desc', $limit = null)
  145. {
  146. $params = json_encode(func_get_args());
  147. $cache_name = self::CACHE_TAG_NAME . '_' . __FUNCTION__ . '_' . $params;
  148. $res = Cache::get($cache_name);
  149. if (empty($res)) {
  150. $res = model(self::TABLE_NAME)->getList($condition, $field, $order, '', '', '', $limit);
  151. Cache::tag(self::CACHE_TAG_NAME)->set($cache_name, $res);
  152. }
  153. return $this->success($res);
  154. }
  155. /**
  156. * 获取数量
  157. * @param $condition
  158. * @param string $field
  159. * @return array
  160. */
  161. public function getCount($condition, $field = 'id')
  162. {
  163. $params = json_encode(func_get_args());
  164. $cache_name = self::CACHE_TAG_NAME . '_' . __FUNCTION__ . '_' . $params;
  165. $res = Cache::get($cache_name);
  166. if (empty($res)) {
  167. $res = model(self::TABLE_NAME)->getCount($condition, $field);
  168. Cache::tag(self::CACHE_TAG_NAME)->set($cache_name, $res);
  169. }
  170. return $this->success($res);
  171. }
  172. }