SystemNotificationServices.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\services\message;
  13. use app\dao\system\SystemNotificationDao;
  14. use app\services\BaseServices;
  15. use app\services\serve\ServeServices;
  16. use crmeb\services\CacheService;
  17. use crmeb\services\template\Template;
  18. use think\facade\Cache;
  19. use crmeb\exceptions\AdminException;
  20. /**
  21. * 消息管理类
  22. * Class SystemNotificationServices
  23. * @package app\services\system
  24. * @method value($where, $value) 条件获取某个字段的值
  25. */
  26. class SystemNotificationServices extends BaseServices
  27. {
  28. /**
  29. * SystemNotificationServices constructor.
  30. * @param SystemNotificationDao $dao
  31. */
  32. public function __construct(SystemNotificationDao $dao)
  33. {
  34. $this->dao = $dao;
  35. }
  36. /**
  37. * 单个配置
  38. * @param int $where
  39. * @return array
  40. * @throws \think\db\exception\DataNotFoundException
  41. * @throws \think\db\exception\DbException
  42. * @throws \think\db\exception\ModelNotFoundException
  43. */
  44. public function getOneNotce(array $where)
  45. {
  46. return $this->dao->getOne($where);
  47. }
  48. /**
  49. * 后台获取列表
  50. * @param array $where
  51. * @return array
  52. * @throws \think\db\exception\DataNotFoundException
  53. * @throws \think\db\exception\DbException
  54. * @throws \think\db\exception\ModelNotFoundException
  55. */
  56. public function getNotList(array $where)
  57. {
  58. return $this->dao->getList($where);
  59. }
  60. /**
  61. * 获取单条数据
  62. * @param array $where
  63. * @return array
  64. * @throws \think\db\exception\DataNotFoundException
  65. * @throws \think\db\exception\DbException
  66. * @throws \think\db\exception\ModelNotFoundException
  67. */
  68. public function getNotInfo(array $where)
  69. {
  70. /** @var ServeServices $ServeServices */
  71. $ServeServices = app()->make(ServeServices::class);
  72. /** @var TemplateMessageServices $TemplateMessageServices */
  73. $TemplateMessageServices = app()->make(TemplateMessageServices::class);
  74. $type = $where['type'];
  75. unset($where['type']);
  76. $info = $this->dao->getOne($where);
  77. if (!$info) return [];
  78. $info = $info->toArray();
  79. switch ($type) {
  80. case 'is_system':
  81. $info['content'] = $info['system_text'] ?? '';
  82. break;
  83. case 'is_sms':
  84. $info['content'] = $info['sms_text'];
  85. break;
  86. case 'is_wechat':
  87. $wechat = $TemplateMessageServices->getOne(['id' => $info['wechat_id'], 'type' => 1]);
  88. $info['templage_message_id'] = $wechat['id'] ?? '';
  89. $info['tempkey'] = $wechat['tempkey'] ?? '';
  90. $info['tempid'] = $wechat['tempid'] ?? '';
  91. $info['content'] = $wechat['content'] ?? '';
  92. break;
  93. case 'is_routine':
  94. $wechat = $TemplateMessageServices->getOne(['id' => $info['routine_id'], 'type' => 0]);
  95. $info['templage_message_id'] = $wechat['id'] ?? '';
  96. $info['tempkey'] = $wechat['tempkey'] ?? '';
  97. $info['tempid'] = $wechat['tempid'] ?? '';
  98. $info['content'] = $wechat['content'] ?? '';
  99. break;
  100. }
  101. return $info;
  102. }
  103. /**
  104. * 保存数据
  105. * @param array $data
  106. * @return bool|\crmeb\basic\BaseModel|null
  107. * @throws \think\db\exception\DataNotFoundException
  108. * @throws \think\db\exception\DbException
  109. * @throws \think\db\exception\ModelNotFoundException
  110. */
  111. public function saveData(array $data)
  112. {
  113. $type = $data['type'];
  114. $id = $data['id'];
  115. $info = $this->dao->get($id);
  116. if (!$info) {
  117. throw new AdminException(100026);
  118. }
  119. /** @var TemplateMessageServices $TemplateMessageServices */
  120. $TemplateMessageServices = app()->make(TemplateMessageServices::class);
  121. $res = null;
  122. switch ($type) {
  123. case 'is_system':
  124. $update = [];
  125. $update['name'] = $data['name'];
  126. $update['title'] = $data['title'];
  127. $update['is_system'] = $data['is_system'];
  128. $update['is_app'] = $data['is_app'];
  129. $update['system_title'] = $data['system_title'];
  130. $update['system_text'] = $data['system_text'];
  131. $res = $this->dao->update((int)$id, $update);
  132. break;
  133. case 'is_sms':
  134. $update = [];
  135. $update['name'] = $data['name'];
  136. $update['title'] = $data['title'];
  137. $update['is_sms'] = $data['is_sms'];
  138. $update['sms_id'] = $data['sms_id'];
  139. $res = $this->dao->update((int)$id, $update);
  140. break;
  141. case 'is_wechat':
  142. $update['name'] = $data['name'];
  143. $update['title'] = $data['title'];
  144. $update['is_wechat'] = $data['is_wechat'];
  145. $res1 = $this->dao->update((int)$id, $update);
  146. $res2 = $TemplateMessageServices->update(['notification_id' => $id, 'type' => 1], ['tempid' => $data['tempid']]);
  147. $res = $res1 && $res2;
  148. break;
  149. case 'is_routine':
  150. $update['name'] = $data['name'];
  151. $update['title'] = $data['title'];
  152. $update['is_routine'] = $data['is_routine'];
  153. $res1 = $this->dao->update((int)$id, $update);
  154. $res2 = $TemplateMessageServices->update(['notification_id' => $id, 'type' => 0], ['tempid' => $data['tempid']]);
  155. $res = $res1 && $res2;
  156. break;
  157. case 'is_ent_wechat':
  158. $update['name'] = $data['name'];
  159. $update['title'] = $data['title'];
  160. $update['is_ent_wechat'] = $data['is_ent_wechat'];
  161. $update['ent_wechat_text'] = $data['ent_wechat_text'];
  162. $update['url'] = $data['url'];
  163. $res = $this->dao->update((int)$id, $update);
  164. break;
  165. }
  166. return $res;
  167. }
  168. }