From 68ab764c0f184947ba6e8d4377f734aec31da733 Mon Sep 17 00:00:00 2001 From: lihan <3398341942@qq.com> Date: Tue, 23 Oct 2018 14:50:24 +0800 Subject: [PATCH] 赠送积分模块 --- app/integral/model/IntegralMode.php | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ app/pay/controller/PayController.php | 25 +++++++++++++++++++++++-- 2 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 app/integral/model/IntegralMode.php diff --git a/app/integral/model/IntegralMode.php b/app/integral/model/IntegralMode.php new file mode 100644 index 0000000..ac6d594 --- /dev/null +++ b/app/integral/model/IntegralMode.php @@ -0,0 +1,80 @@ +<?php +namespace app\integral\model; +use think\Model; +use think\Db; + +class IntegralModel extends Model +{ + + /** + * 新增积分记录日志表 + * @param null $integral_give_id + * @param $uid + * @param $create_time + * @param $balance + * @param $type 1购物收入 2购物使用 3好友赠送 + * @param null $remark + * @return bool + */ + public function insertIntegralLog($integral_give_id = null, $uid, $create_time, $balance, $type, $remark = null) + { + $data = [ + 'integral_give_id' => $integral_give_id, + 'uid' => $uid, + 'create_time' => $create_time, + 'balance' => $balance, + 'type' => $type, + 'remark' => $remark + ]; + if (Db::name('zj_integral_log')->insert($data)) { + return true; + } else { + return false; + } + } + + /** + * 新增积分配送计划表 + * @param $uid + * @param $ratio + * @param $balance + * @param $create_time + * @return bool + */ + public function insertIntegralGive($uid, $ratio, $balance, $create_time) + { + $data = [ + 'uid' => $uid, + 'create_time' => $create_time, + 'balance' => $balance, + 'ratio' => $ratio + ]; + if (Db::name('zj_integral_give')->insert($data)) { + $integral_give_id = Db::name('zj_integral_give')->getLastInsID(); + return $integral_give_id; + } else { + return false; + } + } + + /** + * 每当自动执行给积分后,都要更新当前剩余给的积分 + * @param $integral_give_id + * @param $balance + * @return bool + * @throws \think\Exception + * @throws \think\exception\PDOException + */ + public function updateIntegralGive($integral_give_id, $balance) { + $data = [ + 'id' => $integral_give_id, + 'balance' => $balance + ]; + if(Db::name('zj_integral_give')->update($data)) { + return true; + }else { + return false; + } + } + +} \ No newline at end of file diff --git a/app/pay/controller/PayController.php b/app/pay/controller/PayController.php index c3a4359..57a7bd0 100644 --- a/app/pay/controller/PayController.php +++ b/app/pay/controller/PayController.php @@ -1,6 +1,7 @@ <?php namespace app\pay\controller; use cmf\controller\HomeBaseController; +use app\integral\model\IntegralModel; use think\Db; class PayController extends HomeBaseController @@ -219,6 +220,10 @@ class PayController extends HomeBaseController 'pay_time' => time() ]; if (Db::name('zj_order')->update($update)) { + //赠送先赠送一半积分逻辑 + $this->integral(); + + echo "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>"; exit(); } @@ -227,7 +232,7 @@ class PayController extends HomeBaseController } //赠送积分模块 - public function integral() + private function integral() { $oid = 156; //判断该订单是否属于赠送积分订单 @@ -244,7 +249,23 @@ class PayController extends HomeBaseController foreach ($data as $item) { $integral += $item['price'] * $item['num']; } - echo $integral; + + $ratio = Db::name('zj_system')->where(['id' => 1])->value('integral'); + $model = new IntegralModel; + + //插入配送计划表,先将此积分的一半立刻给用户,剩余一半每日按固定百分比给用户,直到发完为止 + $integral_half = $integral / 2; + Db::startTrans(); + $integral_give_id = $model->insertIntegralGive(session('user.id'), $ratio, $integral_half, time()); + if ($integral_give_id) { + if ($model->insertIntegralLog($integral_half, session('user.id'), time(), $integral_half, 1)) { + Db::commit(); + } else { + Db::rollback(); + } + } else { + Db::rollback(); + } } } } -- libgit2 0.24.0