MemberShipServices.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\user\member;
  12. use app\dao\user\member\MemberShipDao;
  13. use app\services\BaseServices;
  14. use app\services\user\UserServices;
  15. use crmeb\exceptions\AdminException;
  16. use think\exception\ValidateException;
  17. /**
  18. * Class MemberShipServices
  19. * @package app\services\user\member
  20. * @mixin MemberShipDao
  21. */
  22. class MemberShipServices extends BaseServices
  23. {
  24. public function __construct(MemberShipDao $memberShipDao)
  25. {
  26. $this->dao = $memberShipDao;
  27. }
  28. /**
  29. * 获取单个付费会员信息
  30. * @param int $id
  31. * @param string $field
  32. * @return array|\think\Model
  33. * @throws \think\db\exception\DataNotFoundException
  34. * @throws \think\db\exception\DbException
  35. * @throws \think\db\exception\ModelNotFoundException
  36. */
  37. public function getMemberInfo(int $id, string $field = '*')
  38. {
  39. $member = $this->dao->getOne(['id' => $id, 'is_del' => 0], $field);
  40. if (!$member) {
  41. throw new ValidateException('该会员类型不存在或已删除');
  42. }
  43. return $member;
  44. }
  45. /**
  46. * 后台获取会员类型
  47. * @param array $where
  48. * @return array
  49. */
  50. public function getSearchList(array $where = [])
  51. {
  52. [$page, $limit] = $this->getPageValue();
  53. $list = $this->dao->getSearchList($where, $page, $limit);
  54. $count = $this->dao->count($where);
  55. return compact('list', 'count');
  56. }
  57. /**
  58. * 获取会员卡api接口
  59. * @return mixed
  60. */
  61. public function getApiList(array $where)
  62. {
  63. return $this->dao->getApiList($where);
  64. }
  65. /**
  66. * 卡类型编辑保存
  67. * @param int $id
  68. * @param array $data
  69. */
  70. public function save(int $id, array $data)
  71. {
  72. if (!$data['title']) throw new AdminException("请填写会员名称");
  73. if (!$data['type']) throw new AdminException("会员类型缺失");
  74. if ($data['type'] == "ever") {
  75. $data['vip_day'] = -1;
  76. } else {
  77. if (!$data['vip_day']) throw new AdminException("请填写体验天数");
  78. if ($data['vip_day'] < 0) throw new AdminException("体验天数不能为负数");
  79. }
  80. if ($data['type'] == "free") {
  81. // $data['price'] = 0.00;
  82. $data['pre_price'] = 0.00;
  83. } else {
  84. if ($data['pre_price'] == 0 || $data['price'] == 0) throw new AdminException("请填写价格");
  85. }
  86. if ($data['pre_price'] < 0 || $data['price'] < 0) throw new AdminException("价格不能为负数");
  87. if ($data['pre_price'] > $data['price']) throw new AdminException("优惠价不能大于原价");
  88. if ($id) {
  89. $data['id'] = $id;
  90. $this->dao->cacheUpdate($data);
  91. return $this->dao->update($id, $data);
  92. } else {
  93. $res = $this->dao->save($data);
  94. $data['id'] = $res->id;
  95. $this->dao->cacheUpdate($data);
  96. return $res;
  97. }
  98. }
  99. /**
  100. * 获取卡会员天数
  101. * @param array $where
  102. * @return mixed
  103. */
  104. public function getVipDay(array $where)
  105. {
  106. return $this->dao->value($where, 'vip_day');
  107. }
  108. /**
  109. * 修改会员类型状态
  110. * @param $id
  111. * @param $is_del
  112. * @return bool
  113. */
  114. public function setStatus($id, $is_del)
  115. {
  116. $res = $this->dao->update($id, ['is_del' => $is_del]);
  117. if ($is_del) {
  118. $this->cacheDelById($id);
  119. } else {
  120. $this->cacheSaveValue($id, 'is_del', 0);
  121. }
  122. if ($res) return true;
  123. return false;
  124. }
  125. /**
  126. * 查询会员类型select
  127. * @return array
  128. * @throws \think\db\exception\DataNotFoundException
  129. * @throws \think\db\exception\DbException
  130. * @throws \think\db\exception\ModelNotFoundException
  131. */
  132. public function getShipSelect()
  133. {
  134. $menus = [];
  135. foreach ($this->dao->getSearchList(['is_del' => 0], 0, 0, ['id', 'title']) as $menu) {
  136. $menus[] = ['value' => $menu['id'], 'label' => $menu['title']];
  137. }
  138. $menus[] = ['value' => 'card' , 'label' => '卡密激活'];
  139. return $menus;
  140. }
  141. /**
  142. * 获取过期时间
  143. * @param int $uid
  144. * @param int $id
  145. * @param $userInfo
  146. * @param $memberInfo
  147. * @return false|string
  148. * @throws \think\db\exception\DataNotFoundException
  149. * @throws \think\db\exception\DbException
  150. * @throws \think\db\exception\ModelNotFoundException
  151. */
  152. public function getOverdueTime(int $uid, int $id, $userInfo = [], $memberInfo = [])
  153. {
  154. if (!$id) {
  155. throw new ValidateException('缺少会员ID');
  156. }
  157. if (!$memberInfo) {
  158. /** @var MemberShipServices $memberShipService */
  159. $memberShipService = app()->make(MemberShipServices::class);
  160. $memberInfo = $memberShipService->get($id);
  161. }
  162. if (!$memberInfo) {
  163. throw new ValidateException('会员卡类型不存在');
  164. }
  165. if ($uid && !$userInfo) {
  166. /** @var UserServices $userServices */
  167. $userServices = app()->make(UserServices::class);
  168. $userInfo = $userServices->getUserInfo($uid);
  169. }
  170. $member_type = $memberInfo['type'];
  171. $vip_day = $memberInfo['vip_day'];
  172. if ($member_type == 'ever') {
  173. $overdue_time = 0;
  174. $is_ever_level = 1;
  175. } else {
  176. if (!$userInfo || $userInfo['is_money_level'] == 0) {
  177. $overdue_time = bcadd(bcmul($vip_day, 86400, 0), time(), 0);
  178. } else {
  179. $overdue_time = bcadd(bcmul($vip_day, 86400, 0), $userInfo['overdue_time'], 0);
  180. }
  181. $is_ever_level = 0;
  182. }
  183. if ($is_ever_level == 1) {
  184. $res = "永久会员";
  185. } else {
  186. $res = date('Y-m-d', $overdue_time);
  187. }
  188. return $res;
  189. }
  190. }