ActivityService.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 getDataList()
  57. {
  58. $params = request()->all();
  59. $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE;
  60. $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE;
  61. $dataList = $this->model::from('activity as a')
  62. ->where(function ($query) use ($params) {
  63. $query->where('a.mark', 1);
  64. $query->where('a.status',1);
  65. })
  66. ->select(['a.id', 'a.address', 'a.price', 'a.thumb', 'a.type', 'a.title', 'a.create_time', 'a.update_time', 'a.description', 'a.status', 'a.publish_start','a.publish_end'])
  67. ->orderBy('a.sort', 'desc')
  68. ->paginate($pageSize);
  69. $dataList = $dataList ? $dataList->toArray() : [];
  70. if ($dataList) {
  71. foreach ($dataList['data'] as &$item) {
  72. $item['thumb'] = $item['thumb'] ? get_image_url($item['thumb']) : '';
  73. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
  74. $publishStart = isset($item['publish_start']) && $item['publish_start']? date('m月d日', strtotime($item['publish_start'])) : '';
  75. $publishEnd = isset($item['publish_end']) && $item['publish_end']? date('m月d日', strtotime($item['publish_end'])) : '';
  76. $item['publish_at'] = $publishStart==$publishEnd? $publishStart : ($publishStart && $publishEnd>$publishStart? $publishStart.'-'.$publishEnd : '');
  77. $item['publish_at_text'] = $item['publish_start'] && $item['publish_end']? $item['publish_start'].'至'.$item['publish_end'] : '';
  78. $bookNum = ActivityBooksModel::where(['aid'=> $item['id'],'status'=> 1])->count('id');
  79. $item['count'] = intval($bookNum);
  80. }
  81. unset($item);
  82. }
  83. return [
  84. 'code' => 0,
  85. 'success'=> true,
  86. 'msg' => '操作成功',
  87. 'count' => isset($dataList['total']) ? $dataList['total'] : 0,
  88. 'data' => isset($dataList['data']) ? $dataList['data'] : 0,
  89. ];
  90. }
  91. /**
  92. * 添加或编辑
  93. * @return array
  94. * @since 2020/11/11
  95. * @author wesmiler
  96. */
  97. public function edit()
  98. {
  99. $data = request()->all();
  100. // 图片处理
  101. $image = trim($data['thumb']);
  102. $id = isset($data['id']) ? $data['id'] : 0;
  103. if (!$id && !$image) {
  104. return message('请上传活动图片', false);
  105. }
  106. if (strpos($image, "temp")) {
  107. $data['thumb'] = save_image($image, 'item');
  108. } else {
  109. $data['thumb'] = str_replace(IMG_URL, "", $data['thumb']);
  110. }
  111. if(isset($data['publish_at_text']) && $data['publish_at_text']){
  112. $times = $data['publish_at_text'];
  113. $data['publish_start'] = isset($times[0])? $times[0] : '';
  114. $data['publish_end'] = isset($times[1])? $times[1] : '';
  115. }
  116. $data['update_time'] = time();
  117. return parent::edit($data); // TODO: Change the autogenerated stub
  118. }
  119. /**
  120. * 报名处理
  121. * @param $userId 用户ID
  122. * @return array
  123. */
  124. public function books($userId){
  125. $params = request()->all();
  126. $gdName = isset($params['gd_name'])? trim($params['gd_name']) : '';
  127. $wsName = isset($params['ws_name'])? trim($params['ws_name']) : '';
  128. $couponGive = isset($params['coupon_give'])? intval($params['coupon_give']) : 0;
  129. $xyContent = isset($params['xy_content'])? trim($params['xy_content']) : '';
  130. // 验证活动
  131. $aid = isset($params['id'])? intval($params['id']) : 0;
  132. $activityInfo = $this->model::where(['id'=> $aid, 'status'=> 1, 'mark'=> 1])
  133. ->select(['id','type','price','status','publish_start','publish_end'])
  134. ->first();
  135. $activityInfo = $activityInfo? $activityInfo->toArray() : [];
  136. if(empty($activityInfo)){
  137. return message('活动不存在或已取消', false);
  138. }
  139. $coupon = isset($activityInfo['price'])? $activityInfo['price'] : 0;
  140. $publishStart = isset($activityInfo['publish_start'])? $activityInfo['publish_start'] : '';
  141. $publishEnd = isset($activityInfo['publish_end'])? $activityInfo['publish_end'] : '';
  142. $type = isset($activityInfo['type'])? $activityInfo['type'] : 1;
  143. $curDate = date('Y-m-d');
  144. if($publishStart && $publishEnd && ($curDate< $publishStart || $curDate > $publishEnd)){
  145. return message('活动已结束', false);
  146. }
  147. // 验证账户
  148. $couponTotal = intval($couponGive+$coupon);
  149. $memberInfo = $this->memberModel::where(['id'=> $userId, 'status'=>1])->select(['id','coupon','nickname'])->first();
  150. if(!$memberInfo){
  151. return message('当前账户已冻结或用户不存在无法操作', false);
  152. }
  153. $memberCoupon = $memberInfo->coupon;
  154. if($memberCoupon < $couponTotal){
  155. return message('您的账户不足,请先充值', false,[],'10003');
  156. }
  157. $data = [
  158. 'aid'=> $aid,
  159. 'user_id'=> $userId,
  160. 'order_sn'=> get_order_num('B'),
  161. 'coupon'=> $coupon,
  162. 'coupon_give'=> $couponGive,
  163. 'gd_name'=> $gdName,
  164. 'ws_name'=> $wsName,
  165. 'xy_content'=> $xyContent,
  166. 'create_time'=> time(),
  167. 'status'=> 1,
  168. ];
  169. // 扣款和处理
  170. DB::beginTransaction();
  171. if(!$this->memberModel::where(['id'=> $userId, 'status'=>1])->decrement('coupon', $couponTotal)){
  172. DB::rollBack();
  173. return message('账户扣除失败', false);
  174. }
  175. // 交易明细
  176. $types = [1=>'代订', 2=>'助印',3=>'供奉'];
  177. $typeName = isset($types[$type])? $types[$type] : '参加寺院活动';
  178. $tradeData = [
  179. 'user_id'=> $userId,
  180. 'type'=> 1,
  181. 'money'=> $couponTotal,
  182. 'balance'=> $memberCoupon,
  183. 'create_time'=> time(),
  184. 'remark'=> "{$typeName}消费{$couponTotal}券",
  185. ];
  186. if(!$this->tradeModel::insert($tradeData)){
  187. DB::rollBack();
  188. return message('交易处理失败', false);
  189. }
  190. // 报名记录
  191. if(!$bid = $this->bookModel::insertGetId($data)){
  192. DB::rollBack();
  193. return message('报名处理失败', false);
  194. }
  195. DB::commit();
  196. return message('活动报名成功', true, ['id'=> $bid]);
  197. }
  198. }