|
|
@@ -0,0 +1,285 @@
|
|
|
+<?php
|
|
|
+// +----------------------------------------------------------------------
|
|
|
+// | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
|
|
|
+// +----------------------------------------------------------------------
|
|
|
+// | 版权所有 2017~2021 LARAVEL研发中心
|
|
|
+// +----------------------------------------------------------------------
|
|
|
+// | 官方网站: http://www.laravel.cn
|
|
|
+// +----------------------------------------------------------------------
|
|
|
+// | Author: laravel开发员 <laravel.qq.com>
|
|
|
+// +----------------------------------------------------------------------
|
|
|
+
|
|
|
+namespace App\Services\Api;
|
|
|
+
|
|
|
+use App\Models\MemberAddressModel;
|
|
|
+use App\Models\MemberCouponModel;
|
|
|
+use App\Services\BaseService;
|
|
|
+use App\Services\RedisService;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 用户优惠券管理-服务类
|
|
|
+ * @author laravel开发员
|
|
|
+ * @since 2020/11/11
|
|
|
+ * @package App\Services\Api
|
|
|
+ */
|
|
|
+class MemberCouponService extends BaseService
|
|
|
+{
|
|
|
+ // 静态对象
|
|
|
+ protected static $instance = null;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构造函数
|
|
|
+ * @author laravel开发员
|
|
|
+ * @since 2020/11/11
|
|
|
+ */
|
|
|
+ public function __construct()
|
|
|
+ {
|
|
|
+ $this->model = new MemberCouponModel();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 静态入口
|
|
|
+ * @return static|null
|
|
|
+ */
|
|
|
+ public static function make()
|
|
|
+ {
|
|
|
+ if (!self::$instance) {
|
|
|
+ self::$instance = (new static());
|
|
|
+ }
|
|
|
+ return self::$instance;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 列表数据
|
|
|
+ * @param $params
|
|
|
+ * @param int $pageSize
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function getDataList($params, $pageSize = 15)
|
|
|
+ {
|
|
|
+ $where = ['a.mark' => 1,'a.status'=>1];
|
|
|
+ $status = isset($params['status']) ? $params['status'] : 0;
|
|
|
+ if ($status > 0) {
|
|
|
+ $where['a.status'] = $status;
|
|
|
+ }
|
|
|
+ $userId = isset($params['user_id']) ? $params['user_id'] : 0;
|
|
|
+ if ($userId > 0) {
|
|
|
+ $where['a.user_id'] = $userId;
|
|
|
+ }
|
|
|
+ $list = $this->model->from('member_coupons as a')
|
|
|
+ ->where($where)
|
|
|
+ ->where(function($query) use($params){
|
|
|
+ $ids = isset($params['ids'])?$params['ids']:[];
|
|
|
+ if($ids){
|
|
|
+ $query->where(function($query) use($ids){
|
|
|
+ $query->where(DB::raw("FIND_IN_SET(?, goods_ids)",[implode(',', $ids)]))
|
|
|
+ ->orWhere('goods_ids','');
|
|
|
+ });
|
|
|
+ }else{
|
|
|
+ $query->where('goods_ids','');
|
|
|
+ }
|
|
|
+
|
|
|
+ $total = isset($params['total'])?$params['total']:0;
|
|
|
+ if($total){
|
|
|
+ $query->where(function($query) use($total){
|
|
|
+ $query->where(function($query) use($total){
|
|
|
+ $query->where('a.coupon_type',10)
|
|
|
+ ->where('a.min_price','<=', $total);
|
|
|
+ })
|
|
|
+ ->orWhereIn('a.coupon_type',[20,30]);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ })
|
|
|
+ ->select(['a.*'])
|
|
|
+ ->orderBy('a.reduce_price', 'desc')
|
|
|
+ ->orderBy('a.id', 'desc')
|
|
|
+ ->paginate($pageSize > 0 ? $pageSize : 9999999);
|
|
|
+ $list = $list ? $list->toArray() : [];
|
|
|
+ return [
|
|
|
+ 'pageSize' => $pageSize,
|
|
|
+ 'total' => isset($list['total']) ? $list['total'] : 0,
|
|
|
+ 'list' => isset($list['data']) ? $list['data'] : []
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存数据
|
|
|
+ * @param $userId
|
|
|
+ * @param $params
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function saveData($userId, $params)
|
|
|
+ {
|
|
|
+ $id = isset($params['id']) ? $params['id'] : 0;
|
|
|
+ $isDefault = isset($params['is_default']) ? $params['is_default'] : 2;
|
|
|
+ $data = [
|
|
|
+ 'user_id'=> $userId,
|
|
|
+ 'mobile'=> isset($params['mobile'])? $params['mobile'] : '',
|
|
|
+ 'realname'=> isset($params['realname'])? $params['realname'] : '',
|
|
|
+ 'province'=> isset($params['province'])? $params['province'] : '',
|
|
|
+ 'city'=> isset($params['city'])? $params['city'] : '',
|
|
|
+ 'district'=> isset($params['district'])? $params['district'] : '',
|
|
|
+ 'codes'=> isset($params['codes'])? $params['codes'] : '',
|
|
|
+ 'address'=> isset($params['address'])? $params['address'] : '',
|
|
|
+ 'is_default'=> $isDefault==1? 1: 2,
|
|
|
+ 'status'=> isset($params['status'])? $params['status'] : 1,
|
|
|
+ 'update_time'=> time(),
|
|
|
+ 'mark'=> 1,
|
|
|
+ ];
|
|
|
+
|
|
|
+ DB::beginTransaction();
|
|
|
+ if($isDefault==1){
|
|
|
+ $this->model->where(['user_id'=> $userId])->update(['is_default'=> 2,'update_time'=> time()]);
|
|
|
+ }
|
|
|
+
|
|
|
+ RedisService::keyDel("caches:members:address:{$userId}*");
|
|
|
+ if($id && $id = $this->model->where(['id'=> $id])->value('id')){
|
|
|
+ $this->model->where(['id'=> $id])->update($data);
|
|
|
+ $this->error = '保存成功';
|
|
|
+ DB::commit();
|
|
|
+ return ['id'=>$id];
|
|
|
+ }else{
|
|
|
+ $data['create_time'] = time();
|
|
|
+ if(!$id = $this->model->insertGetId($data)){
|
|
|
+ DB::rollBack();
|
|
|
+ $this->error = '添加失败';
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ $this->error = '添加成功';
|
|
|
+ return ['id'=>$id];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 默认
|
|
|
+ * @param $userId
|
|
|
+ * @param $params
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function setDefault($userId, $params)
|
|
|
+ {
|
|
|
+ $id = isset($params['id']) ? $params['id'] : 0;
|
|
|
+ $isDefault = isset($params['is_default']) ? $params['is_default'] : 2;
|
|
|
+ $data = [
|
|
|
+ 'user_id'=> $userId,
|
|
|
+ 'is_default'=> $isDefault==1? 1: 2,
|
|
|
+ 'update_time'=> time(),
|
|
|
+ ];
|
|
|
+
|
|
|
+
|
|
|
+ if($isDefault==1){
|
|
|
+ $this->model->where(['user_id'=> $userId])->update(['is_default'=> 2,'update_time'=> time()]);
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->model->where(['id'=> $id])->update($data);
|
|
|
+ $this->error = '保存成功';
|
|
|
+ RedisService::keyDel("caches:members:address:{$userId}*");
|
|
|
+ return ['id'=>$id];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param $id
|
|
|
+ * @param $userId
|
|
|
+ * @return array|mixed
|
|
|
+ */
|
|
|
+ public function getInfo($id, $userId)
|
|
|
+ {
|
|
|
+ $cacheKey = "caches:members:address:{$userId}_info_".($id?'_'.$id:'');
|
|
|
+ $info = RedisService::get($cacheKey);
|
|
|
+ if($info){
|
|
|
+ return $info;
|
|
|
+ }
|
|
|
+ $where = ['id'=> $id,'user_id'=> $userId,'mark'=>1,'status'=>1];
|
|
|
+ if($id<=0){
|
|
|
+ unset($where['id']);
|
|
|
+ }
|
|
|
+ $info = $this->model->where($where)->first();
|
|
|
+ $info = $info? $info->toArray() : [];
|
|
|
+ if($info){
|
|
|
+ $address = [];
|
|
|
+ $info['mobile_text'] = format_mobile($info['mobile']);
|
|
|
+ if(isset($info['province']) && $info['province']){
|
|
|
+ $address[] = $info['province'];
|
|
|
+ }
|
|
|
+ if(isset($info['city']) && $info['city']){
|
|
|
+ $address[] = $info['city'];
|
|
|
+ }
|
|
|
+ if(isset($info['district']) && $info['district']){
|
|
|
+ $address[] = $info['district'];
|
|
|
+ }
|
|
|
+
|
|
|
+ $info['area'] = $address? implode('/', $address) : '';
|
|
|
+ if($info['address']){
|
|
|
+ $address[] = $info['address'];
|
|
|
+ }
|
|
|
+ $info['address_text'] = $address? implode(' ', $address) : '';
|
|
|
+ RedisService::set($cacheKey, $info, rand(5, 10));
|
|
|
+ }
|
|
|
+
|
|
|
+ return $info;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param $userId
|
|
|
+ * @return array|mixed
|
|
|
+ */
|
|
|
+ public function getBindInfo($userId, $addressId=0)
|
|
|
+ {
|
|
|
+ $cacheKey = "caches:members:address:{$userId}".($addressId?'_'.$addressId:'');
|
|
|
+ $info = RedisService::get($cacheKey);
|
|
|
+ if($info){
|
|
|
+ return $info;
|
|
|
+ }
|
|
|
+
|
|
|
+ $where = ['id'=> $addressId,'user_id'=> $userId,'mark'=>1,'status'=>1];
|
|
|
+ if($addressId<=0){
|
|
|
+ unset($where['id']);
|
|
|
+ }
|
|
|
+ $info = $this->model->where($where)
|
|
|
+ ->orderBy('is_default','asc')
|
|
|
+ ->orderBy('id','desc')
|
|
|
+ ->first();
|
|
|
+ $info = $info? $info->toArray() : [];
|
|
|
+ if($info){
|
|
|
+ $address = [];
|
|
|
+ $info['mobile_text'] = format_mobile($info['mobile']);
|
|
|
+ if(isset($info['province']) && $info['province']){
|
|
|
+ $address[] = $info['province'];
|
|
|
+ }
|
|
|
+ if(isset($info['city']) && $info['city']){
|
|
|
+ $address[] = $info['city'];
|
|
|
+ }
|
|
|
+ if(isset($info['district']) && $info['district']){
|
|
|
+ $address[] = $info['district'];
|
|
|
+ }
|
|
|
+ $info['area'] = $address? implode('/', $address) : '';
|
|
|
+ if($info['address']){
|
|
|
+ $address[] = $info['address'];
|
|
|
+ }
|
|
|
+ $info['address_text'] = $address? implode(' ', $address) : '';
|
|
|
+ RedisService::set($cacheKey, $info, rand(5, 10));
|
|
|
+ }
|
|
|
+
|
|
|
+ return $info;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return array|false
|
|
|
+ */
|
|
|
+ public function delete()
|
|
|
+ {
|
|
|
+ // 参数
|
|
|
+ $id = request()->post('id');
|
|
|
+ if (empty($id)) {
|
|
|
+ $this->error = 2014;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->error = 1002;
|
|
|
+ $this->model->where(['id'=> $id,'mark'=>0])->where('update_time','<=', time() - 3*86400)->delete();
|
|
|
+ return $this->model->where(['id'=> $id])->update(['mark'=> 0, 'update_time'=> time()]);
|
|
|
+ }
|
|
|
+}
|