| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- <?php
- // +----------------------------------------------------------------------
- // | Laravel框架 [ Laravel ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 南京Laravel研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: wesmiler <12345678@qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services;
- use App\Models\ActivityBooksModel;
- use App\Models\ActivityModel;
- use App\Models\MemberModel;
- use App\Models\TradeModel;
- use Illuminate\Support\Facades\DB;
- /**
- * 寺院活动管理-服务类
- * @author wesmiler
- * @since 2020/11/11
- * Class ActivityService
- * @package App\Services
- */
- class ActivityService extends BaseService
- {
- protected static $instance = null;
- /**
- * 构造函数
- * @author wesmiler
- * @since 2020/11/11
- * ActivityService constructor.
- */
- public function __construct()
- {
- $this->model = new ActivityModel();
- $this->bookModel = new ActivityBooksModel();
- $this->memberModel = new MemberModel();
- $this->tradeModel = new TradeModel();
- }
- /**
- * 静态入口
- * @return ActivityService()|null
- */
- public static function make(){
- if(!self::$instance){
- self::$instance = new ActivityService();
- }
- return self::$instance;
- }
- /**
- * 获取友链列表
- * @return array
- * @since 2020/11/11
- * @author wesmiler
- */
- public function getList()
- {
- $params = request()->all();
- return parent::getList();
- }
- /**
- * 添加或编辑
- * @return array
- * @since 2020/11/11
- * @author wesmiler
- */
- public function edit()
- {
- $data = request()->all();
- // 图片处理
- $image = trim($data['thumb']);
- $id = isset($data['id']) ? $data['id'] : 0;
- if (!$id && !$image) {
- return message('请上传活动图片', false);
- }
- if (strpos($image, "temp")) {
- $data['thumb'] = save_image($image, 'item');
- } else {
- $data['thumb'] = str_replace(IMG_URL, "", $data['thumb']);
- }
- $data['update_time'] = time();
- return parent::edit($data); // TODO: Change the autogenerated stub
- }
- /**
- * 报名处理
- * @param $userId 用户ID
- * @return array
- */
- public function books($userId){
- $params = request()->all();
- $gdName = isset($params['gd_name'])? trim($params['gd_name']) : '';
- $wsName = isset($params['ws_name'])? trim($params['ws_name']) : '';
- $couponGive = isset($params['coupon_give'])? intval($params['coupon_give']) : 0;
- $xyContent = isset($params['xy_content'])? trim($params['xy_content']) : '';
- // 验证活动
- $aid = isset($params['id'])? intval($params['id']) : 0;
- $activityInfo = $this->model::where(['id'=> $aid, 'status'=> 1, 'mark'=> 1])
- ->select(['id','type','price','status','publish_at'])
- ->first();
- $activityInfo = $activityInfo? $activityInfo->toArray() : [];
- if(empty($activityInfo)){
- return message('活动不存在或已取消', false);
- }
- $coupon = isset($activityInfo['price'])? $activityInfo['price'] : 0;
- $publishAt = isset($activityInfo['publish_at'])? $activityInfo['publish_at'] : '';
- $type = isset($activityInfo['type'])? $activityInfo['type'] : 1;
- $times = $publishAt? explode('-', $publishAt) : [];
- $timeStart = isset($times[0])? $times[0] : '';
- $timeEnd = isset($times[1])? $times[1] : '';
- $curDate = date('m-d');
- if($timeStart && $timeEnd && ($curDate< $timeStart || $curDate > $timeEnd)){
- return message('活动已结束', false);
- }
- // 验证账户
- $couponTotal = intval($couponGive+$coupon);
- $memberInfo = $this->memberModel::where(['id'=> $userId, 'status'=>1])->select(['id','coupon','nickname'])->first();
- if(!$memberInfo){
- return message('当前账户已冻结或用户不存在无法操作', false);
- }
- $memberCoupon = $memberInfo->coupon;
- if($memberCoupon < $couponTotal){
- return message('您的账户不足,请先充值', false,[],'1003');
- }
- $data = [
- 'aid'=> $aid,
- 'user_id'=> $userId,
- 'order_sn'=> get_order_num('B'),
- 'coupon'=> $coupon,
- 'coupon_give'=> $couponGive,
- 'gd_name'=> $gdName,
- 'ws_name'=> $wsName,
- 'xy_content'=> $xyContent,
- 'create_time'=> time(),
- 'status'=> 1,
- ];
- // 扣款和处理
- DB::beginTransaction();
- if(!$this->memberModel::where(['id'=> $userId, 'status'=>1])->decrement('coupon', $couponTotal)){
- DB::rollBack();
- return message('账户扣除失败', false);
- }
- // 交易明细
- $types = [1=>'代订', 2=>'助印',3=>'供奉'];
- $typeName = isset($types[$type])? $types[$type] : '参加寺院活动';
- $tradeData = [
- 'user_id'=> $userId,
- 'type'=> 1,
- 'money'=> $couponTotal,
- 'balance'=> $memberCoupon,
- 'create_time'=> time(),
- 'remark'=> "用户{$memberInfo->nickname}{$typeName}消费{$couponTotal}券",
- ];
- if(!$this->tradeModel::insert($tradeData)){
- DB::rollBack();
- return message('交易处理失败', false);
- }
- // 报名记录
- if(!$bid = $this->bookModel::insert($data)){
- DB::rollBack();
- return message('报名处理失败', false);
- }
- DB::commit();
- return message('请上传活动图片', true, ['id'=> $bid]);
- }
- }
|