ActivityService.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. $data['update_time'] = time();
  82. return parent::edit($data); // TODO: Change the autogenerated stub
  83. }
  84. /**
  85. * 报名处理
  86. * @param $userId 用户ID
  87. * @return array
  88. */
  89. public function books($userId){
  90. $params = request()->all();
  91. $gdName = isset($params['gd_name'])? trim($params['gd_name']) : '';
  92. $wsName = isset($params['ws_name'])? trim($params['ws_name']) : '';
  93. $couponGive = isset($params['coupon_give'])? intval($params['coupon_give']) : 0;
  94. $xyContent = isset($params['xy_content'])? trim($params['xy_content']) : '';
  95. // 验证活动
  96. $aid = isset($params['id'])? intval($params['id']) : 0;
  97. $activityInfo = $this->model::where(['id'=> $aid, 'status'=> 1, 'mark'=> 1])
  98. ->select(['id','type','price','status','publish_at'])
  99. ->first();
  100. $activityInfo = $activityInfo? $activityInfo->toArray() : [];
  101. if(empty($activityInfo)){
  102. return message('活动不存在或已取消', false);
  103. }
  104. $coupon = isset($activityInfo['price'])? $activityInfo['price'] : 0;
  105. $publishAt = isset($activityInfo['publish_at'])? $activityInfo['publish_at'] : '';
  106. $type = isset($activityInfo['type'])? $activityInfo['type'] : 1;
  107. $times = $publishAt? explode('-', $publishAt) : [];
  108. $timeStart = isset($times[0])? $times[0] : '';
  109. $timeEnd = isset($times[1])? $times[1] : '';
  110. $curDate = date('m-d');
  111. if($timeStart && $timeEnd && ($curDate< $timeStart || $curDate > $timeEnd)){
  112. return message('活动已结束', false);
  113. }
  114. // 验证账户
  115. $couponTotal = intval($couponGive+$coupon);
  116. $memberInfo = $this->memberModel::where(['id'=> $userId, 'status'=>1])->select(['id','coupon','nickname'])->first();
  117. if(!$memberInfo){
  118. return message('当前账户已冻结或用户不存在无法操作', false);
  119. }
  120. $memberCoupon = $memberInfo->coupon;
  121. if($memberCoupon < $couponTotal){
  122. return message('您的账户不足,请先充值', false,[],'1003');
  123. }
  124. $data = [
  125. 'aid'=> $aid,
  126. 'user_id'=> $userId,
  127. 'order_sn'=> get_order_num('B'),
  128. 'coupon'=> $coupon,
  129. 'coupon_give'=> $couponGive,
  130. 'gd_name'=> $gdName,
  131. 'ws_name'=> $wsName,
  132. 'xy_content'=> $xyContent,
  133. 'create_time'=> time(),
  134. 'status'=> 1,
  135. ];
  136. // 扣款和处理
  137. DB::beginTransaction();
  138. if(!$this->memberModel::where(['id'=> $userId, 'status'=>1])->decrement('coupon', $couponTotal)){
  139. DB::rollBack();
  140. return message('账户扣除失败', false);
  141. }
  142. // 交易明细
  143. $types = [1=>'代订', 2=>'助印',3=>'供奉'];
  144. $typeName = isset($types[$type])? $types[$type] : '参加寺院活动';
  145. $tradeData = [
  146. 'user_id'=> $userId,
  147. 'type'=> 1,
  148. 'money'=> $couponTotal,
  149. 'balance'=> $memberCoupon,
  150. 'create_time'=> time(),
  151. 'remark'=> "用户{$memberInfo->nickname}{$typeName}消费{$couponTotal}券",
  152. ];
  153. if(!$this->tradeModel::insert($tradeData)){
  154. DB::rollBack();
  155. return message('交易处理失败', false);
  156. }
  157. // 报名记录
  158. if(!$bid = $this->bookModel::insert($data)){
  159. DB::rollBack();
  160. return message('报名处理失败', false);
  161. }
  162. DB::commit();
  163. return message('请上传活动图片', true, ['id'=> $bid]);
  164. }
  165. }