SystemNotificationServices.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 crmeb\exceptions\AdminException;
  16. /**
  17. * 消息管理类
  18. * Class SystemNotificationServices
  19. * @package app\services\system
  20. * @method value($where, $value) 条件获取某个字段的值
  21. */
  22. class SystemNotificationServices extends BaseServices
  23. {
  24. /**
  25. * SystemNotificationServices constructor.
  26. * @param SystemNotificationDao $dao
  27. */
  28. public function __construct(SystemNotificationDao $dao)
  29. {
  30. $this->dao = $dao;
  31. }
  32. /**
  33. * 单个配置
  34. * @param int $where
  35. * @return array
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\DbException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. */
  40. public function getOneNotce(array $where)
  41. {
  42. return $this->dao->getOne($where);
  43. }
  44. /**
  45. * 后台获取列表
  46. * @param array $where
  47. * @return array
  48. * @throws \think\db\exception\DataNotFoundException
  49. * @throws \think\db\exception\DbException
  50. * @throws \think\db\exception\ModelNotFoundException
  51. */
  52. public function getNotList(array $where)
  53. {
  54. return $this->dao->getList($where);
  55. }
  56. /**
  57. * 获取单条数据
  58. * @param array $where
  59. * @return array
  60. * @throws \think\db\exception\DataNotFoundException
  61. * @throws \think\db\exception\DbException
  62. * @throws \think\db\exception\ModelNotFoundException
  63. */
  64. public function getNotInfo(array $where)
  65. {
  66. /** @var ServeServices $ServeServices */
  67. $ServeServices = app()->make(ServeServices::class);
  68. /** @var TemplateMessageServices $TemplateMessageServices */
  69. $TemplateMessageServices = app()->make(TemplateMessageServices::class);
  70. $type = $where['type'];
  71. unset($where['type']);
  72. $info = $this->dao->getOne($where);
  73. if (!$info) return [];
  74. $info = $info->toArray();
  75. switch ($type) {
  76. case 'is_system':
  77. $info['content'] = $info['system_text'] ?? '';
  78. break;
  79. case 'is_sms':
  80. $info['content'] = $info['sms_text'];
  81. break;
  82. case 'is_wechat':
  83. $wechat = $TemplateMessageServices->getOne(['id' => $info['wechat_id'], 'type' => 1]);
  84. $info['templage_message_id'] = $wechat['id'] ?? '';
  85. $info['tempkey'] = $wechat['tempkey'] ?? '';
  86. $info['tempid'] = $wechat['tempid'] ?? '';
  87. $info['content'] = $wechat['content'] ?? '';
  88. break;
  89. case 'is_routine':
  90. $wechat = $TemplateMessageServices->getOne(['id' => $info['routine_id'], 'type' => 0]);
  91. $info['templage_message_id'] = $wechat['id'] ?? '';
  92. $info['tempkey'] = $wechat['tempkey'] ?? '';
  93. $info['tempid'] = $wechat['tempid'] ?? '';
  94. $info['content'] = $wechat['content'] ?? '';
  95. break;
  96. }
  97. return $info;
  98. }
  99. /**
  100. * 保存数据
  101. * @param array $data
  102. * @return bool|\crmeb\basic\BaseModel|null
  103. * @throws \think\db\exception\DataNotFoundException
  104. * @throws \think\db\exception\DbException
  105. * @throws \think\db\exception\ModelNotFoundException
  106. */
  107. public function saveData(array $data)
  108. {
  109. $type = $data['type'];
  110. $id = $data['id'];
  111. $info = $this->dao->get($id);
  112. if (!$info) {
  113. throw new AdminException(100026);
  114. }
  115. /** @var TemplateMessageServices $TemplateMessageServices */
  116. $TemplateMessageServices = app()->make(TemplateMessageServices::class);
  117. $res = null;
  118. switch ($type) {
  119. case 'is_system':
  120. $update = [];
  121. $update['name'] = $data['name'];
  122. $update['title'] = $data['title'];
  123. $update['is_system'] = $data['is_system'];
  124. $update['is_app'] = $data['is_app'];
  125. $update['system_title'] = $data['system_title'];
  126. $update['system_text'] = $data['system_text'];
  127. $res = $this->dao->update((int)$id, $update);
  128. break;
  129. case 'is_sms':
  130. $update = [];
  131. $update['name'] = $data['name'];
  132. $update['title'] = $data['title'];
  133. $update['is_sms'] = $data['is_sms'];
  134. $update['sms_id'] = $data['sms_id'];
  135. $res = $this->dao->update((int)$id, $update);
  136. break;
  137. case 'is_wechat':
  138. $update['name'] = $data['name'];
  139. $update['title'] = $data['title'];
  140. $update['is_wechat'] = $data['is_wechat'];
  141. $res1 = $this->dao->update((int)$id, $update);
  142. $res2 = $TemplateMessageServices->update(['notification_id' => $id, 'type' => 1], ['tempid' => $data['tempid']]);
  143. $res = $res1 && $res2;
  144. break;
  145. case 'is_routine':
  146. $update['name'] = $data['name'];
  147. $update['title'] = $data['title'];
  148. $update['is_routine'] = $data['is_routine'];
  149. $res1 = $this->dao->update((int)$id, $update);
  150. $res2 = $TemplateMessageServices->update(['notification_id' => $id, 'type' => 0], ['tempid' => $data['tempid']]);
  151. $res = $res1 && $res2;
  152. break;
  153. case 'is_ent_wechat':
  154. $update['name'] = $data['name'];
  155. $update['title'] = $data['title'];
  156. $update['is_ent_wechat'] = $data['is_ent_wechat'];
  157. $update['ent_wechat_text'] = $data['ent_wechat_text'];
  158. $update['url'] = $data['url'];
  159. $res = $this->dao->update((int)$id, $update);
  160. break;
  161. }
  162. return $res;
  163. }
  164. }