Keyword.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 Keyword
  16. * @package addon\servicer\model
  17. */
  18. class Keyword extends BaseModel
  19. {
  20. /**
  21. * 缓冲标签名称
  22. */
  23. const CACHE_TAG_NAME = 'servicer_keyword_reply';
  24. /**
  25. * 表名称
  26. */
  27. const TABLE_NAME = 'servicer_keyword_reply';
  28. /**
  29. * 消息类型 -- 文本
  30. */
  31. const CONTENT_TYPE_TEXT = 0;
  32. /**
  33. * 消息类型 -- 商品
  34. */
  35. const CONTENT_TYPE_GOODS = 1;
  36. /**
  37. * 消息类型 -- 订单
  38. */
  39. const CONTENT_TYPE_ORDER = 2;
  40. /**
  41. * 消息类型 -- 图片
  42. */
  43. const CONTENT_TYPE_IMAGE = 3;
  44. /**
  45. * 状态
  46. * @param mixed $content_type
  47. * @param string $type
  48. * @return array
  49. */
  50. public static function contentType($content_type = null, $type = 'content_type')
  51. {
  52. $data = array(
  53. array(
  54. 'content_type' => self::CONTENT_TYPE_TEXT,
  55. 'name' => '文本',
  56. 'type' => 'text',
  57. 'const' => 'CONTENT_TYPE_STRING',
  58. ),
  59. array(
  60. 'content_type' => self::CONTENT_TYPE_GOODS,
  61. 'name' => '商品',
  62. 'type' => 'goods',
  63. 'const' => 'CONTENT_TYPE_GOODS',
  64. ),
  65. array(
  66. 'content_type' => self::CONTENT_TYPE_ORDER,
  67. 'name' => '订单',
  68. 'type' => 'order',
  69. 'const' => 'CONTENT_TYPE_ORDER',
  70. ),
  71. array(
  72. 'content_type' => self::CONTENT_TYPE_IMAGE,
  73. 'name' => '图片',
  74. 'type' => 'image',
  75. 'const' => 'CONTENT_TYPE_IMAGE',
  76. ),
  77. );
  78. $result = [];
  79. if (is_null($content_type)) {
  80. $result = $data;
  81. } else {
  82. if (is_array($content_type)) {
  83. foreach ($data as $val) {
  84. if (in_array($val[$type], $content_type)) {
  85. $result[] = $val;
  86. }
  87. }
  88. } else {
  89. foreach ($data as $val) {
  90. if ($content_type == $val[$type]) {
  91. $result = $val;
  92. break;
  93. }
  94. }
  95. }
  96. }
  97. return $result;
  98. }
  99. /**
  100. * 添加
  101. * @param array $params
  102. * @return array
  103. */
  104. public function add(array $params)
  105. {
  106. $site_id = $params['site_id'] ?? '';
  107. $keyword = $params['keyword'] ?? '';
  108. $content_type = $params['content_type'] ?? '';
  109. if ($site_id === '') {
  110. return $this->error('', 'REQUEST_SITE_ID');
  111. }
  112. if ($keyword === '') {
  113. return $this->error('', 'PARAMETER_ERROR');
  114. }
  115. if (is_null($content_type)) {
  116. return $this->error('', 'PARAMETER_ERROR');
  117. }
  118. if (empty(self::contentType($content_type))) {
  119. return $this->error('', 'PARAMETER_ERROR');
  120. }
  121. // 检测重复
  122. $info = $this->getInfo([['site_id', '=', $site_id], ['keyword', '=', $keyword]], 'id')['data'];
  123. if (!empty($info)) {
  124. return $this->error('', '关键词重复');
  125. }
  126. $res = model(self::TABLE_NAME)->add(array_merge($params, ['create_time' => time()]));
  127. Cache::tag(self::CACHE_TAG_NAME)->clear();
  128. return $this->success($res);
  129. }
  130. /**
  131. * 编辑
  132. * @param array $params
  133. * @param array $condition
  134. * @return array
  135. */
  136. public function edit(array $params, array $condition)
  137. {
  138. $check_condition = array_column($condition, 2, 0);
  139. $site_id = $check_condition['site_id'] ?? 0;
  140. $id = $check_condition['id'] ?? 0;
  141. $keyword = $params['keyword'] ?? '';
  142. $content_type = $params['content_type'] ?? '';
  143. if ($site_id === '') {
  144. return $this->error('', 'REQUEST_SITE_ID');
  145. }
  146. if (empty($id)) {
  147. return $this->error('', 'PARAMETER_ERROR');
  148. }
  149. if ($keyword === '') {
  150. return $this->error('', 'PARAMETER_ERROR');
  151. }
  152. if (is_null($content_type)) {
  153. return $this->error('', 'PARAMETER_ERROR');
  154. }
  155. if (empty(self::contentType($content_type))) {
  156. return $this->error('', 'PARAMETER_ERROR');
  157. }
  158. // 检测是否存在
  159. $info = $this->getInfo($condition, 'id')['data'];
  160. if (empty($info)) {
  161. return $this->error('', '数据不存在');
  162. }
  163. // 检测重复
  164. $info = $this->getInfo([
  165. ['site_id', '=', $site_id],
  166. ['keyword', '=', $keyword],
  167. ['id', '<>', $id],
  168. ], 'id')['data'];
  169. if (!empty($info)) {
  170. return $this->error('', '关键词重复');
  171. }
  172. $res = model(self::TABLE_NAME)->update($params, $condition);
  173. Cache::tag(self::CACHE_TAG_NAME)->clear();
  174. return $this->success($res);
  175. }
  176. /**
  177. * 修改
  178. * @param $data
  179. * @param array $condition
  180. * @return array
  181. */
  182. public function update($data, array $condition)
  183. {
  184. $res = model(self::TABLE_NAME)->update($data, $condition);
  185. Cache::tag(self::CACHE_TAG_NAME)->clear();
  186. return $this->success($res);
  187. }
  188. /**
  189. * 删除
  190. * @param array $condition
  191. * @return array
  192. */
  193. public function delete(array $condition)
  194. {
  195. $check_condition = array_column($condition, 2, 0);
  196. $site_id = $check_condition['site_id'] ?? 0;
  197. if ($site_id === '') {
  198. return $this->error('', 'REQUEST_SITE_ID');
  199. }
  200. $res = model(self::TABLE_NAME)->delete($condition);
  201. Cache::tag(self::CACHE_TAG_NAME)->clear();
  202. return $this->success($res);
  203. }
  204. /**
  205. * 获取信息
  206. * @param $condition
  207. * @param bool $field
  208. * @return array
  209. */
  210. public function getInfo($condition, $field = true)
  211. {
  212. $params = json_encode(func_get_args());
  213. $cache_name = self::CACHE_TAG_NAME . '_' . __FUNCTION__ . '_' . $params;
  214. $res = Cache::get($cache_name);
  215. if (empty($res)) {
  216. $res = model(self::TABLE_NAME)->getInfo($condition, $field);
  217. Cache::tag(self::CACHE_TAG_NAME)->set($cache_name, $res);
  218. }
  219. return $this->success($res);
  220. }
  221. /**
  222. * 根据关键词获取回复内容
  223. * @param $site_id
  224. * @param $keyword
  225. * @param int $content_type
  226. * @param int $is_use
  227. * @return mixed|string
  228. */
  229. public function getContentByKeyword($site_id, $keyword, $content_type = self::CONTENT_TYPE_TEXT, $is_use = 1)
  230. {
  231. $content = '';
  232. if ($site_id !== '' && !empty($keyword)) {
  233. $info = $this->getInfo([
  234. ['site_id', '=', $site_id],
  235. ['keyword', '=', $keyword],
  236. ['is_use', '=', $is_use],
  237. ['content_type', '=', $content_type],
  238. ], 'content')['data'];
  239. if (!empty($info)) {
  240. $content = $info['content'];
  241. }
  242. }
  243. return $content;
  244. }
  245. /**
  246. * 获取分页列表
  247. * @param array $condition
  248. * @param int $page
  249. * @param int $page_size
  250. * @param string $order
  251. * @param bool $field
  252. * @param string $alias
  253. * @param array $join
  254. * @return array
  255. */
  256. public function getPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'id desc', $field = true, $alias = '', $join = [])
  257. {
  258. $params = json_encode(func_get_args());
  259. $cache_name = self::CACHE_TAG_NAME . '_' . __FUNCTION__ . '_' . $params;
  260. $res = Cache::get($cache_name);
  261. if (empty($res)) {
  262. $res = model(self::TABLE_NAME)->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
  263. Cache::tag(self::CACHE_TAG_NAME)->set($cache_name, $res);
  264. }
  265. return $this->success($res);
  266. }
  267. /**
  268. * 获取列表
  269. * @param array $condition
  270. * @param mixed $field
  271. * @param string $order
  272. * @param null $limit
  273. * @return array
  274. */
  275. public function getList($condition = [], $field = true, $order = 'id desc', $limit = null)
  276. {
  277. $params = json_encode(func_get_args());
  278. $cache_name = self::CACHE_TAG_NAME . '_' . __FUNCTION__ . '_' . $params;
  279. $res = Cache::get($cache_name);
  280. if (empty($res)) {
  281. $res = model(self::TABLE_NAME)->getList($condition, $field, $order, '', '', '', $limit);
  282. Cache::tag(self::CACHE_TAG_NAME)->set($cache_name, $res);
  283. }
  284. return $this->success($res);
  285. }
  286. /**
  287. * 获取数量
  288. * @param $condition
  289. * @param string $field
  290. * @return array
  291. */
  292. public function getCount($condition, $field = 'id')
  293. {
  294. $params = json_encode(func_get_args());
  295. $cache_name = self::CACHE_TAG_NAME . '_' . __FUNCTION__ . '_' . $params;
  296. $res = Cache::get($cache_name);
  297. if (empty($res)) {
  298. $res = model(self::TABLE_NAME)->getCount($condition, $field);
  299. Cache::tag(self::CACHE_TAG_NAME)->set($cache_name, $res);
  300. }
  301. return $this->success($res);
  302. }
  303. }