ActivityService.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Laravel框架 [ Laravel ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 南京Laravel研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: wesmiler <12345678@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services;
  12. use App\Models\ActivityBooksModel;
  13. use App\Models\ActivityModel;
  14. use App\Models\MemberModel;
  15. use App\Models\TradeModel;
  16. use Illuminate\Support\Facades\DB;
  17. /**
  18. * 寺院活动管理-服务类
  19. * @author wesmiler
  20. * @since 2020/11/11
  21. * Class ActivityService
  22. * @package App\Services
  23. */
  24. class ActivityService extends BaseService
  25. {
  26. protected static $instance = null;
  27. /**
  28. * 构造函数
  29. * @author wesmiler
  30. * @since 2020/11/11
  31. * ActivityService constructor.
  32. */
  33. public function __construct()
  34. {
  35. $this->model = new ActivityModel();
  36. $this->bookModel = new ActivityBooksModel();
  37. $this->memberModel = new MemberModel();
  38. $this->tradeModel = new TradeModel();
  39. }
  40. /**
  41. * 静态入口
  42. * @return ActivityService()|null
  43. */
  44. public static function make(){
  45. if(!self::$instance){
  46. self::$instance = new ActivityService();
  47. }
  48. return self::$instance;
  49. }
  50. /**
  51. * 获取友链列表
  52. * @return array
  53. * @since 2020/11/11
  54. * @author wesmiler
  55. */
  56. public function getList()
  57. {
  58. $params = request()->all();
  59. return parent::getList();
  60. }
  61. /**
  62. * 添加或编辑
  63. * @return array
  64. * @since 2020/11/11
  65. * @author wesmiler
  66. */
  67. public function edit()
  68. {
  69. $data = request()->all();
  70. // 图片处理
  71. $image = trim($data['thumb']);
  72. $id = isset($data['id']) ? $data['id'] : 0;
  73. if (!$id && !$image) {
  74. return message('请上传活动图片', false);
  75. }
  76. if (strpos($image, "temp")) {
  77. $data['thumb'] = save_image($image, 'item');
  78. } else {
  79. $data['thumb'] = str_replace(IMG_URL, "", $data['thumb']);
  80. }
  81. if(isset($data['publish_at_text']) && $data['publish_at_text']){
  82. $times = $data['publish_at_text'];
  83. $data['publish_start'] = isset($times[0])? $times[0] : '';
  84. $data['publish_end'] = isset($times[1])? $times[1] : '';
  85. }
  86. $data['update_time'] = time();
  87. return parent::edit($data); // TODO: Change the autogenerated stub
  88. }
  89. /**
  90. * 报名处理
  91. * @param $userId 用户ID
  92. * @return array
  93. */
  94. public function books($userId){
  95. $params = request()->all();
  96. $gdName = isset($params['gd_name'])? trim($params['gd_name']) : '';
  97. $wsName = isset($params['ws_name'])? trim($params['ws_name']) : '';
  98. $couponGive = isset($params['coupon_give'])? intval($params['coupon_give']) : 0;
  99. $xyContent = isset($params['xy_content'])? trim($params['xy_content']) : '';
  100. // 验证活动
  101. $aid = isset($params['id'])? intval($params['id']) : 0;
  102. $activityInfo = $this->model::where(['id'=> $aid, 'status'=> 1, 'mark'=> 1])
  103. ->select(['id','type','price','status','publish_start','publish_end'])
  104. ->first();
  105. $activityInfo = $activityInfo? $activityInfo->toArray() : [];
  106. if(empty($activityInfo)){
  107. return message('活动不存在或已取消', false);
  108. }
  109. $coupon = isset($activityInfo['price'])? $activityInfo['price'] : 0;
  110. $publishStart = isset($activityInfo['publish_start'])? $activityInfo['publish_start'] : '';
  111. $publishEnd = isset($activityInfo['publish_end'])? $activityInfo['publish_end'] : '';
  112. $type = isset($activityInfo['type'])? $activityInfo['type'] : 1;
  113. $curDate = date('Y-m-d');
  114. if($publishStart && $publishEnd && ($curDate< $publishStart || $curDate > $publishEnd)){
  115. return message('活动已结束', false);
  116. }
  117. // 验证账户
  118. $couponTotal = intval($couponGive+$coupon);
  119. $memberInfo = $this->memberModel::where(['id'=> $userId, 'status'=>1])->select(['id','coupon','nickname'])->first();
  120. if(!$memberInfo){
  121. return message('当前账户已冻结或用户不存在无法操作', false);
  122. }
  123. $memberCoupon = $memberInfo->coupon;
  124. if($memberCoupon < $couponTotal){
  125. return message('您的账户不足,请先充值', false,[],'1003');
  126. }
  127. $data = [
  128. 'aid'=> $aid,
  129. 'user_id'=> $userId,
  130. 'order_sn'=> get_order_num('B'),
  131. 'coupon'=> $coupon,
  132. 'coupon_give'=> $couponGive,
  133. 'gd_name'=> $gdName,
  134. 'ws_name'=> $wsName,
  135. 'xy_content'=> $xyContent,
  136. 'create_time'=> time(),
  137. 'status'=> 1,
  138. ];
  139. // 扣款和处理
  140. DB::beginTransaction();
  141. if(!$this->memberModel::where(['id'=> $userId, 'status'=>1])->decrement('coupon', $couponTotal)){
  142. DB::rollBack();
  143. return message('账户扣除失败', false);
  144. }
  145. // 交易明细
  146. $types = [1=>'代订', 2=>'助印',3=>'供奉'];
  147. $typeName = isset($types[$type])? $types[$type] : '参加寺院活动';
  148. $tradeData = [
  149. 'user_id'=> $userId,
  150. 'type'=> 1,
  151. 'money'=> $couponTotal,
  152. 'balance'=> $memberCoupon,
  153. 'create_time'=> time(),
  154. 'remark'=> "用户{$memberInfo->nickname}{$typeName}消费{$couponTotal}券",
  155. ];
  156. if(!$this->tradeModel::insert($tradeData)){
  157. DB::rollBack();
  158. return message('交易处理失败', false);
  159. }
  160. // 报名记录
  161. if(!$bid = $this->bookModel::insertGetId($data)){
  162. DB::rollBack();
  163. return message('报名处理失败', false);
  164. }
  165. DB::commit();
  166. return message('活动报名成功', true, ['id'=> $bid]);
  167. }
  168. }