|
@@ -11,7 +11,11 @@
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
+use App\Models\ActivityBooksModel;
|
|
|
use App\Models\ActivityModel;
|
|
|
+use App\Models\MemberModel;
|
|
|
+use App\Models\TradeModel;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
/**
|
|
|
* 寺院活动管理-服务类
|
|
@@ -22,6 +26,8 @@ use App\Models\ActivityModel;
|
|
|
*/
|
|
|
class ActivityService extends BaseService
|
|
|
{
|
|
|
+ protected static $instance = null;
|
|
|
+
|
|
|
/**
|
|
|
* 构造函数
|
|
|
* @author wesmiler
|
|
@@ -31,6 +37,21 @@ class ActivityService extends BaseService
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -71,4 +92,96 @@ class ActivityService extends BaseService
|
|
|
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]);
|
|
|
+ }
|
|
|
}
|