zhangdehua 1 rok pred
rodič
commit
503b172e46

+ 54 - 0
app/api/controller/Checkout.php

@@ -26,6 +26,7 @@ use app\common\enum\order\PayType as OrderPayTypeEnum;
 use app\common\exception\BaseException;
 use app\common\library\helper;
 use app\common\service\card\UserRiceCard as UserRiceCardService;
+use app\api\service\order\CheckoutExchange as CheckoutExchangeService;
 use app\api\service\order\CheckoutGroupbuy as CheckoutGroupBuyService;
 use app\api\service\order\CheckoutGroupbuylb as CheckoutGroupBuylbService;
 use app\api\model\groupbuy\GroupBuyJoin;
@@ -91,11 +92,64 @@ class Checkout extends Controller
             return $this->za();
         }else if ($mode=='zareceive'){
             return $this->zareceive();
+        }elseif($mode == 'pointsExchange'){
+            return $this->pointsExchange();
         }
 
         return $this->renderError('结算模式不合法');
     }
 
+    private function pointsExchange()
+    {
+        // 实例化结算台服务
+        $Checkout = new CheckoutExchangeService;
+        // 订单结算api参数
+        $params = $Checkout->setParam($this->getParam([
+            'goodsId' => 0,
+            'goodsSkuId' => '',
+            'goodsNum' => 0,
+            'staffUserId' => 0,
+            'deliveryType' => 10 // 配送方式 默认10 10-快递配送 20-门店自提
+        ]));
+        // 表单验证
+        if (!$this->validate->scene('pointsExchange')->check($params)) {
+            return $this->renderError($this->validate->getError(), ['is_created' => false]);
+        }
+        // 立即购买:获取订单商品列表
+        $model = new OrderModel;
+        $params['staffUserId'] = $params['staffUserId']??0;
+        $goodsList = $model->getOrderGoodsListByNow(
+            (int)$params['goodsId'],
+            (string)$params['goodsSkuId'],
+            (int)$params['goodsNum'],
+            (int)$params['staffUserId']
+        );
+        // 获取订单确认信息
+        $orderInfo = $Checkout->onCheckoutExchange($goodsList);
+        if ($this->request->isGet()) {
+            return $this->renderSuccess(['order' => $orderInfo]);
+        }
+        // 验证订单是否存在错误
+        if ($Checkout->hasError()) {
+            return $this->renderError($Checkout->getError(), ['is_created' => false]);
+        }
+
+        //金钱和积分的兑换比例 todo
+        if ($this->user['accumulate_points'] < bcmul($orderInfo['orderTotalPrice'] , "100",0)) {
+            return $this->renderError('积分不足哦', ['is_created' => false]);
+        }
+            // 创建订单
+        if (!$Checkout->createOrder($orderInfo)) {
+            return $this->renderError($Checkout->getError() ?: '订单创建失败', ['is_created' => false]);
+        }
+
+        return $this->renderSuccess([
+            'order_id' => $Checkout->model['order_id'],// 订单id
+            'pay_type' => OrderPayTypeEnum::EXCHANGE,// 支付方式
+        ]);
+
+    }
+
     //买一赠一 领取
     public function zareceive(){
         $Checkout = new CheckoutZaService;

+ 7 - 0
app/api/controller/Goods.php

@@ -15,6 +15,7 @@ namespace app\api\controller;
 use app\api\model\Cart as CartModel;
 use app\api\model\Goods as GoodsModel;
 use app\api\service\User as UserService;
+use app\common\enum\goods\GoodsType;
 use app\common\library\helper;
 use app\common\model\BrowseRecords;
 use app\store\model\qc\QcMjSendActivity;
@@ -46,7 +47,13 @@ class Goods extends Controller
         // 获取列表数据
         $model = new GoodsModel;
         $list = $model->getList($this->request->param());
+        $goodsType = $this->request->param('goods_type', null);
         foreach ($list as $item){
+            if ($goodsType == GoodsType::EXCHANGE){
+                $item['za'] = null;
+                $item['miaosha'] = null;
+                continue;
+            }
               //买一赠一
             $za = (new ZaActivityModel())->getZaGood($item['goods_id']);
             if(isset($za)){

+ 4 - 0
app/api/controller/Test.php

@@ -4,6 +4,7 @@ namespace app\api\controller;
 
 
 use app\api\model\Order as OrderModel;
+use app\api\model\User as UserModel;
 use app\common\enum\order\PayType as OrderPayTypeEnum;
 use app\common\enum\order\ReceiptStatus as ReceiptStatusEnum;
 use app\common\service\Message;
@@ -57,6 +58,9 @@ class Test extends Controller
 
 
     public function tj(){
+        $res = UserModel::setIncAccumulatePoints(107633,99.11,10,'老板赠送');
+        dd($res);
+
         $otj = new OrderTj;
         $day = $otj->day(0);
        

+ 17 - 0
app/api/model/Order.php

@@ -688,6 +688,23 @@ class Order extends OrderModel
     }
 
     /**
+     * 积分兑换支付标记订单已支付
+     * @param string $orderNo 订单号
+     * @return bool
+     */
+    public function onPaymentByAccumulatePoints(string $orderNo)
+    {
+        // 获取订单详情
+        $service = new OrderPaySuccesService($orderNo);
+        // 发起积分支付
+        $status = $service->onPaySuccess(OrderPayTypeEnum::EXCHANGE);
+        if (!$status) {
+            $this->error = $service->getError();
+        }
+        return $status;
+    }
+
+    /**
      * 获取用户订单列表
      * @param string $type 订单类型 (all全部 payment待付款 delivery待发货 received待收货 completed已完成 comment待评价)
      * @return \think\Paginator

+ 41 - 0
app/api/model/user/AccumulatePointsLog.php

@@ -0,0 +1,41 @@
+<?php
+// +----------------------------------------------------------------------
+// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
+// +----------------------------------------------------------------------
+// | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
+// +----------------------------------------------------------------------
+// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
+// +----------------------------------------------------------------------
+// | Author: 萤火科技 <admin@yiovo.com>
+// +----------------------------------------------------------------------
+declare (strict_types = 1);
+
+namespace app\api\model\user;
+
+use app\api\service\User as UserService;
+use app\common\model\user\AccumulatePointsLog as AccPointsLog;
+
+/**
+ * 用户余额变动明细模型
+ * Class PointsLog
+ * @package app\api\model\user
+ */
+class AccumulatePointsLog extends AccPointsLog
+{
+    /**
+     * 获取日志明细列表
+     * @return \think\Paginator
+     * @throws \app\common\exception\BaseException
+     * @throws \think\db\exception\DbException
+     */
+    public function getList()
+    {
+        // 当前用户ID
+        $userId = UserService::getCurrentLoginUserId();
+        // 获取列表数据
+        return $this->where('user_id', '=', $userId)
+            ->order(['create_time' => 'desc'])
+            ->paginate(15);
+    }
+
+}

+ 2 - 0
app/api/service/order/Checkout.php

@@ -71,6 +71,8 @@ class Checkout extends BaseService
 
     // 订单结算商品列表
     private $goodsList = [];
+    private $invalidGoodsList = [];
+    private $shopInfo = [];
 
     // 错误信息
     protected $error;

Rozdielové dáta súboru neboli zobrazené, pretože súbor je príliš veľký
+ 1887 - 0
app/api/service/order/CheckoutExchange.php


+ 25 - 2
app/api/service/order/PaySuccess.php

@@ -15,6 +15,7 @@ namespace app\api\service\order;
 use app\api\model\card\UserRiceCard as UserRiceCardModel;
 use app\api\model\card\UserRiceCardConsume as UserRiceCardConsumeModel;
 use app\api\model\member\GoldRice as GoldRiceModel;
+use app\common\enum\user\AccumulatePointsScene;
 use app\common\service\BaseService;
 use app\api\model\User as UserModel;
 use app\api\model\Order as OrderModel;
@@ -99,6 +100,26 @@ class PaySuccess extends BaseService
                 return false;
             }
         }
+
+        //积分不足 todo 补充积分兑换商品比例
+        if ($payType == OrderPayTypeEnum::EXCHANGE) {
+            if ($this->user['accumulate_points'] < $this->model['total_price']) {
+                $this->error = '用户积分不足,无法兑换成功';
+                return false;
+            }
+            $this->model->transaction(function () use ($payType, $payData) {
+                // 更新订单状态
+                $this->updateOrderInfo($payType, $payData);
+                //扣除用户积分 todo 确定积分兑换商品金额比例
+                $points = bcmul($this->model['total_price'],"100",0);
+                UserModel::setDecAccumulatePoints($this->user['user_id'], $points,AccumulatePointsScene::CONSUME,'兑换商品');
+                // 记录订单支付信息
+                $this->updatePayInfo($payType);
+            });
+            return true;
+
+        }
+
         // 事务处理
         $this->model->transaction(function () use ($payType, $payData) {
             // 更新订单状态
@@ -107,10 +128,12 @@ class PaySuccess extends BaseService
             UserModel::setIncPayMoney($this->user['user_id'], (float)$this->model['pay_price']);
             //累积VIP用户积分
             UserModel::setIncGrowthValue($this->user,$this->model['rice_card_money']+$this->model['pay_price']);
+            //累计用户积分
+            UserModel::setIncAccumulatePoints($this->user['user_id'], $this->model['pay_price']);
             // 记录订单支付信息
             $this->updatePayInfo($payType);
-            // 如果是普通订单,写入分销记录
-            if($this->model['order_source']!=OrderSourceEnum::BARGAIN){
+            // 如果是普通订单,写入分销记录(积分兑换商品订单无分佣)
+            if($this->model['order_source']!=OrderSourceEnum::BARGAIN || $this->model['or'] != OrderSourceEnum::EXCHANGE){
                 $this->writeCommission($payType);
             }
             //写入金米粒记录

+ 1 - 0
app/api/validate/order/Checkout.php

@@ -100,6 +100,7 @@ class Checkout extends Validate
      */
     protected $scene = [
         'buyNow' => ['goodsId', 'goodsNum', 'goodsSkuId', 'staffUserId'],
+        'pointsExchange' => ['goodsId', 'goodsNum', 'goodsSkuId', 'staffUserId'],
         'cart' => ['cartIds'],
     ];
 

+ 5 - 0
app/common/enum/OrderType.php

@@ -42,6 +42,7 @@ class OrderType extends EnumBasics
 
     const ZA = 70;
 
+    const EXCHANGE = 500;
     /**
      * 获取订单类型值
      * @return array
@@ -77,6 +78,10 @@ class OrderType extends EnumBasics
             self::ZA =>[
                 'name' => '买一赠一订单',
                 'value' => self::ZA
+            ],
+            self::EXCHANGE =>[
+                'name' => '积分兑换订单',
+                'value' => self::EXCHANGE
             ]
         ];
     }

+ 7 - 0
app/common/enum/goods/GoodsType.php

@@ -29,6 +29,9 @@ class GoodsType extends EnumBasics
 
     const MULTI = 30;
 
+    //积分兑换商品
+    const EXCHANGE = 40;
+
     /**
      * 获取枚举类型值
      * @return array
@@ -48,6 +51,10 @@ class GoodsType extends EnumBasics
                 'name' => '套餐商品',
                 'value' => self::MULTI,
             ],
+            self::EXCHANGE => [
+                'name' => '积分兑换商品',
+                'value' => self::EXCHANGE,
+            ],
         ];
     }
 }

+ 7 - 0
app/common/enum/order/OrderSource.php

@@ -42,6 +42,9 @@ class OrderSource extends EnumBasics
     //买一赠一订单
     const ZA = 70;
 
+    //积分兑换商品订单
+    const EXCHANGE = 80;
+
     /**
      * 获取枚举数据
      * @return array
@@ -76,6 +79,10 @@ class OrderSource extends EnumBasics
             self::ZA => [
                 'name'=>'买一赠一订单',
                 'value'=> self::ZA
+            ],
+            self::EXCHANGE => [
+                'name'=>'积分兑换商品订单',
+                'value'=> self::EXCHANGE
             ]
         ];
     }

+ 7 - 1
app/common/enum/order/PayType.php

@@ -31,8 +31,10 @@ class PayType extends EnumBasics
     const MICAH_EXCHANGE = 30;
 
     //活动赠送
-    const ACTIVITY = 40; 
+    const ACTIVITY = 40;
 
+    //积分兑换
+    const EXCHANGE = 50;
 
 
     /**
@@ -57,6 +59,10 @@ class PayType extends EnumBasics
             self::ACTIVITY => [
                 'name'=>'活动赠送',
                 'value' => self::ACTIVITY
+            ],
+            self::EXCHANGE => [
+                'name'=>'积分兑换',
+                'value' => self::EXCHANGE
             ]
         ];
     }

+ 58 - 0
app/common/enum/user/AccumulatePointsScene.php

@@ -0,0 +1,58 @@
+<?php
+// +----------------------------------------------------------------------
+// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
+// +----------------------------------------------------------------------
+// | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
+// +----------------------------------------------------------------------
+// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
+// +----------------------------------------------------------------------
+// | Author: 萤火科技 <admin@yiovo.com>
+// +----------------------------------------------------------------------
+declare (strict_types = 1);
+
+namespace app\common\enum\user;
+
+use app\common\enum\EnumBasics;
+
+/**
+ * 用户积分枚举类
+ * Class AccumulatePointsScene
+ * @package app\common\enum\user
+ */
+class AccumulatePointsScene extends EnumBasics
+{
+    // 用户消费获得
+    const RECHARGE = 10;
+
+    // 兑换商品
+    const CONSUME = 20;
+
+    // 管理员操作
+    const ADMIN = 30;
+
+    /**
+     * 获取订单类型值
+     * @return array
+     */
+    public static function data()
+    {
+        return [
+            self::RECHARGE => [
+                'name' => '用户消费获得',
+                'value' => self::RECHARGE,
+                'describe' => '用户消费获得:%s',
+            ],
+            self::CONSUME => [
+                'name' => '兑换商品',
+                'value' => self::CONSUME,
+                'describe' => '兑换商品:%s',
+            ],
+            self::ADMIN => [
+                'name' => '管理员操作',
+                'value' => self::ADMIN,
+                'describe' => '后台管理员 [%s] 操作',
+            ]
+        ];
+    }
+
+}

+ 4 - 0
app/common/model/Goods.php

@@ -443,6 +443,10 @@ class Goods extends BaseModel
             //需要单规格的商品或赠品
             $filter[] = ['goods_type','<',GoodsType::MULTI];
             $filter[] = ['stock_total','>',0];
+        }elseif(isset($params['goods_type']) &&  $params['goods_type'] == GoodsType::EXCHANGE){
+            //积分兑换商品
+            $filter[] = ['goods_type','=',GoodsType::EXCHANGE];
+            $filter[] = ['stock_total','>',0];
         }else{
             if (isset($params['is_backend']) && $params['is_backend'] == 1){
 

+ 55 - 0
app/common/model/User.php

@@ -12,6 +12,8 @@ declare (strict_types=1);
 
 namespace app\common\model;
 
+use app\common\enum\user\AccumulatePointsScene;
+use app\common\model\user\AccumulatePointsLog;
 use app\common\library\helper;
 use app\common\model\store\Setting as SettingModel;
 use app\common\model\user\CommissionsDetail;
@@ -279,6 +281,59 @@ class User extends BaseModel
     }
 
     /**
+     * @param int $userId
+     * @param int $payMoney 支付金额
+     * @param int $scene 积分场景
+     * @param string $describe
+     * @return mixed
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public static function setIncAccumulatePoints(int $userId, string $payMoney, int $scene = AccumulatePointsScene::RECHARGE, string $describe = '')
+    {
+        $rate = SettingModel::getItem('points_rate')['points_rate'];
+        $points = bcmul(strval($payMoney),$rate,0);
+        // 新增积分变动明细
+        AccumulatePointsLog::add([
+            'user_id' => $userId,
+            'value' => $points,
+            'scene' => $scene,
+            'describe' => $describe,
+        ]);
+
+        // 更新用户可用积分
+        return (new static)->setInc($userId, 'accumulate_points', intval($points));
+    }
+
+    /**
+     * 积分扣减
+     * @param int $userId
+     * @param float $payMoney
+     * @param int $scene
+     * @param string $describe
+     * @return mixed
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public static function setDecAccumulatePoints(int $userId, string $payMoney, int $scene = AccumulatePointsScene::RECHARGE, string $describe = '')
+    {
+        $rate = SettingModel::getItem('points_rate')['points_rate'];
+        $points = bcmul(strval($payMoney),$rate,0);
+        // 新增积分变动明细
+        AccumulatePointsLog::add([
+            'user_id' => $userId,
+            'value' => $points,
+            'scene' => $scene,
+            'describe' => $describe,
+        ]);
+
+        // 更新用户可用积分
+        return (new static)->setDec($userId, 'accumulate_points', intval($points));
+    }
+
+    /**
      * 多字段增减
      * @param array|int|bool $where 支持自增ID  也支持where语句
      * @param array $incData //自增字段

+ 62 - 0
app/common/model/user/AccumulatePointsLog.php

@@ -0,0 +1,62 @@
+<?php
+// +----------------------------------------------------------------------
+// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
+// +----------------------------------------------------------------------
+// | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
+// +----------------------------------------------------------------------
+// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
+// +----------------------------------------------------------------------
+// | Author: 萤火科技 <admin@yiovo.com>
+// +----------------------------------------------------------------------
+declare (strict_types = 1);
+
+namespace app\common\model\user;
+
+use app\common\model\BaseModel;
+
+/**
+ * 用户积分变动明细模型
+ * Class PointsLog
+ * @package app\common\model\user
+ */
+class AccumulatePointsLog extends BaseModel
+{
+    // 定义表名
+    protected $name = 'user_accumulate_points_log';
+
+    // 定义主键
+    protected $pk = 'log_id';
+
+    protected $updateTime = false;
+
+    /**
+     * 关联会员记录表
+     * @return \think\model\relation\BelongsTo
+     */
+    public function user()
+    {
+        $module = self::getCalledModule();
+        return $this->belongsTo("app\\{$module}\\model\\User");
+    }
+
+    /**
+     * 新增记录
+     * @param $data
+     */
+    public static function add($data)
+    {
+        $static = new static;
+        $static->save(array_merge(['store_id' => $static::$storeId], $data));
+    }
+
+    /**
+     * 新增记录 (批量)
+     * @param $saveData
+     * @return bool
+     */
+    public function onBatchAdd($saveData)
+    {
+        return $this->addAll($saveData) !== false;
+    }
+
+}