SystemNotificationServices.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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 TemplateMessageServices $TemplateMessageServices */
  67. $TemplateMessageServices = app()->make(TemplateMessageServices::class);
  68. $type = $where['type'];
  69. unset($where['type']);
  70. $info = $this->dao->getOne($where);
  71. if (!$info) return [];
  72. $info = $info->toArray();
  73. switch ($type) {
  74. case 'is_system':
  75. $info['content'] = $info['system_text'] ?? '';
  76. break;
  77. case 'is_sms':
  78. $info['content'] = $info['sms_text'];
  79. break;
  80. case 'is_wechat':
  81. case 'is_routine':
  82. if ('is_wechat' === $type) {
  83. $wechat = $TemplateMessageServices->getOne(['id' => $info['wechat_id'], 'type' => 1]);
  84. } else {
  85. $wechat = $TemplateMessageServices->getOne(['id' => $info['routine_id'], 'type' => 0]);
  86. }
  87. $info['templage_message_id'] = $wechat['id'] ?? '';
  88. $info['tempkey'] = $wechat['tempkey'] ?? '';
  89. $info['tempid'] = $wechat['tempid'] ?? '';
  90. $info['content'] = $wechat['content'] ?? '';
  91. break;
  92. }
  93. return $info;
  94. }
  95. /**
  96. * 保存数据
  97. * @param array $data
  98. * @return bool|\crmeb\basic\BaseModel|null
  99. * @throws \think\db\exception\DataNotFoundException
  100. * @throws \think\db\exception\DbException
  101. * @throws \think\db\exception\ModelNotFoundException
  102. */
  103. public function saveData(array $data)
  104. {
  105. $type = $data['type'];
  106. $id = $data['id'];
  107. $info = $this->dao->get($id, 'id');
  108. if (!$info) {
  109. throw new AdminException(100026);
  110. }
  111. /** @var TemplateMessageServices $TemplateMessageServices */
  112. $TemplateMessageServices = app()->make(TemplateMessageServices::class);
  113. $res = null;
  114. switch ($type) {
  115. case 'is_system':
  116. $update = [];
  117. $update['name'] = $data['name'];
  118. $update['title'] = $data['title'];
  119. $update['is_system'] = $data['is_system'];
  120. $update['is_app'] = $data['is_app'];
  121. $update['system_title'] = $data['system_title'];
  122. $update['system_text'] = $data['system_text'];
  123. $res = $this->dao->update((int)$id, $update);
  124. break;
  125. case 'is_sms':
  126. $update = [];
  127. $update['name'] = $data['name'];
  128. $update['title'] = $data['title'];
  129. $update['is_sms'] = $data['is_sms'];
  130. $update['sms_id'] = $data['sms_id'];
  131. $res = $this->dao->update((int)$id, $update);
  132. break;
  133. case 'is_wechat':
  134. $update['name'] = $data['name'];
  135. $update['title'] = $data['title'];
  136. $update['is_wechat'] = $data['is_wechat'];
  137. $res1 = $this->dao->update((int)$id, $update);
  138. $res2 = $TemplateMessageServices->update(['notification_id' => $id, 'type' => 1], ['tempid' => $data['tempid']]);
  139. $res = $res1 && $res2;
  140. break;
  141. case 'is_routine':
  142. $update['name'] = $data['name'];
  143. $update['title'] = $data['title'];
  144. $update['is_routine'] = $data['is_routine'];
  145. $res1 = $this->dao->update((int)$id, $update);
  146. $res2 = $TemplateMessageServices->update(['notification_id' => $id, 'type' => 0], ['tempid' => $data['tempid']]);
  147. $res = $res1 && $res2;
  148. break;
  149. case 'is_ent_wechat':
  150. $update['name'] = $data['name'];
  151. $update['title'] = $data['title'];
  152. $update['is_ent_wechat'] = $data['is_ent_wechat'];
  153. $update['ent_wechat_text'] = $data['ent_wechat_text'];
  154. $update['url'] = $data['url'];
  155. $res = $this->dao->update((int)$id, $update);
  156. break;
  157. }
  158. return $res;
  159. }
  160. }