MessageService.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. *
  5. * .--, .--,
  6. * ( ( \.---./ ) )
  7. * '.__/o o\__.'
  8. * {= ^ =}
  9. * > - <
  10. * / \
  11. * // \\
  12. * //| . |\\
  13. * "'\ /'"_.-~^`'-.
  14. * \ _ /--' `
  15. * ___)( )(___
  16. * (((__) (__))) 高山仰止,景行行止.虽不能至,心向往之.生活步步是坎坷,笑到最后是大哥QQ:751818588.
  17. *
  18. * ---------------------*/
  19. namespace app\common\services\finance;
  20. use app\common\events\MessageEvent;
  21. use app\common\facades\Setting;
  22. use \app\common\models\Withdraw;
  23. use app\common\models\notice\MessageTemp;
  24. class MessageService
  25. {
  26. /**
  27. * @var Withdraw
  28. */
  29. private $withdraw;
  30. /**
  31. * @var array
  32. */
  33. private $withdrawSet;
  34. /**
  35. * @var string
  36. */
  37. private $templateKey;
  38. private $apply=2;
  39. /**
  40. * @var array
  41. */
  42. private $statusComment = [
  43. Withdraw::STATUS_INVALID => '审核无效',
  44. Withdraw::STATUS_INITIAL => '提现申请',
  45. Withdraw::STATUS_AUDIT => '审核通过',
  46. Withdraw::STATUS_PAY => '已打款',
  47. Withdraw::STATUS_REBUT => '审核驳回',
  48. Withdraw::STATUS_PAYING => '打款中',
  49. ];
  50. public function __construct(Withdraw $withdraw)
  51. {
  52. $this->withdraw = $withdraw;
  53. $this->withdrawSet = Setting::get('withdraw.notice');
  54. }
  55. /**
  56. * @return int
  57. */
  58. private function getTemplateId()
  59. {
  60. return isset($this->withdrawSet[$this->templateKey]) ? $this->withdrawSet[$this->templateKey] : 0;
  61. }
  62. /**
  63. * 收入提现申请消息通知
  64. */
  65. public function applyNotice()
  66. {
  67. $this->templateKey = 'income_withdraw';
  68. $this->apply = 1;
  69. $this->sendNotice();
  70. }
  71. /**
  72. * 收入提现审核消息通知
  73. */
  74. public function auditNotice()
  75. {
  76. $this->templateKey = 'income_withdraw_check';
  77. $this->apply = 2;
  78. $this->sendNotice();
  79. }
  80. /**
  81. * 收入提现打款消息通知
  82. */
  83. public function payedNotice()
  84. {
  85. $this->templateKey = 'income_withdraw_pay';
  86. $this->apply = 2;
  87. $this->sendNotice();
  88. }
  89. /**
  90. * 收入提现申请消息通知
  91. */
  92. public function arrivalNotice()
  93. {
  94. $this->templateKey = 'income_withdraw_arrival';
  95. $this->apply = 2;
  96. $this->sendNotice();
  97. }
  98. /**
  99. * 收入提现错误消息通知
  100. *
  101. * @param $memberId
  102. */
  103. public function failureNotice($memberId)
  104. {
  105. $this->templateKey = 'income_withdraw_fail';
  106. $this->apply = 2;
  107. $this->sendNotice($memberId);
  108. }
  109. /**
  110. * 收入提现申请消息通知
  111. * @param int $memberId
  112. */
  113. private function sendNotice($memberId = 0)
  114. {
  115. $memberId = $memberId ? $memberId : $this->withdraw->member_id;
  116. if ($templateId = $this->getTemplateId()) {
  117. $news_link = MessageTemp::find($templateId)->news_link;
  118. $news_link = $news_link ?:'';
  119. event(new MessageEvent(
  120. $memberId,
  121. $templateId,
  122. $this->noticeParams(),
  123. $news_link
  124. ));
  125. }
  126. }
  127. /**
  128. * 收入提现申请消息通知
  129. */
  130. private function noticeParams()
  131. {
  132. return [
  133. [
  134. 'name' => '昵称',
  135. 'value' => $this->nickname()
  136. ], [
  137. 'name' => '时间',
  138. 'value' => $this->timeString()
  139. ], [
  140. 'name' => '收入类型',
  141. 'value' => $this->typeName()
  142. ], [
  143. 'name' => '金额',
  144. 'value' => $this->amount()
  145. ], [
  146. 'name' => '手续费',
  147. 'value' => $this->poundage()
  148. ], [
  149. 'name' => '提现方式',
  150. 'value' => $this->payWayName()
  151. ], [
  152. 'name' => '状态',
  153. 'value' => $this->statusName()
  154. ], [
  155. 'name' => '提现单号',
  156. 'value' => $this->withdrawSn()
  157. ], [
  158. 'name' => '审核通过金额',
  159. 'value' => $this->actualAmount()
  160. ], [
  161. 'name' => '审核时间',
  162. 'value' => $this->examineTime()
  163. ], [
  164. 'name' => '劳务税金额',
  165. 'value' => $this->actualServiceTax()
  166. ], [
  167. 'name' => '提现到账金额',
  168. 'value' => $this->actualAmount()
  169. ],
  170. ];
  171. }
  172. /**
  173. * @return string
  174. */
  175. private function nickname()
  176. {
  177. return $this->withdraw->hasOneMember ? $this->withdraw->hasOneMember->nickname : '';
  178. }
  179. /**
  180. * @return string
  181. */
  182. private function timeString()
  183. {
  184. return $this->withdraw->created_at;
  185. }
  186. /**
  187. * @return bool|string
  188. */
  189. private function examineTime()
  190. {
  191. return date("Y-m-d H:i:s",time());
  192. }
  193. /**
  194. * @return string
  195. */
  196. private function typeName()
  197. {
  198. return $this->withdraw->type_name;
  199. }
  200. /**
  201. * @return string
  202. */
  203. private function payWayName()
  204. {
  205. return $this->withdraw->getPayWayNameAttribute();
  206. }
  207. /**
  208. * @return string
  209. */
  210. private function statusName()
  211. {
  212. return $this->getStatusNameAttribute();
  213. }
  214. /**
  215. * @return double
  216. */
  217. private function amount()
  218. {
  219. return $this->withdraw->amounts;
  220. }
  221. /**
  222. * @return double
  223. */
  224. private function poundage()
  225. {
  226. if ($this->apply == 1) {
  227. return $this->withdraw->poundage;
  228. }
  229. return $this->withdraw->actual_poundage;
  230. }
  231. /**
  232. * @return mixed
  233. */
  234. private function actualServiceTax()
  235. {
  236. if ($this->apply == 1) {
  237. return $this->withdraw->servicetax;
  238. }
  239. return $this->withdraw->actual_servicetax;
  240. }
  241. /**
  242. * @return string
  243. */
  244. private function withdrawSn()
  245. {
  246. return $this->withdraw->withdraw_sn;
  247. }
  248. /**
  249. * @return double
  250. */
  251. private function actualAmount()
  252. {
  253. return $this->withdraw->actual_amounts;
  254. }
  255. /**
  256. * @return string
  257. */
  258. private function getStatusNameAttribute()
  259. {
  260. return $this->getStatusComment($this->withdraw->status);
  261. }
  262. /**
  263. * @param int $status
  264. * @return string
  265. */
  266. private function getStatusComment($status)
  267. {
  268. return isset($this->statusComment[$status]) ? $this->statusComment[$status] : '';
  269. }
  270. }