diff --git a/app/admin/view/default/goods/save_info.html b/app/admin/view/default/goods/save_info.html index 5ab018a2ccf17bddebbcc4deaa36e2dde19c1803..549e4248c11fb3e2d21ade64781e64d4974bb3ef 100755 --- a/app/admin/view/default/goods/save_info.html +++ b/app/admin/view/default/goods/save_info.html @@ -125,7 +125,10 @@
-
+
+

订单完成自动将发放到用户锁定积分、再由脚本自动发放到有效积分

+
+
diff --git a/app/admin/view/default/site/extends/index.html b/app/admin/view/default/site/extends/index.html index bede312d21ddde16dfd0c94fdd1e31cf7b2ed579..ff55de02220bfac75cbc678fb3da41898243be83 100644 --- a/app/admin/view/default/site/extends/index.html +++ b/app/admin/view/default/site/extends/index.html @@ -74,6 +74,19 @@ {{:MyUrl('api/crontab/paylogorderclose')}}
+ +
+ +
+ + + + +
+ +
diff --git a/app/admin/view/default/user/save_info.html b/app/admin/view/default/user/save_info.html index 289a5e3d722422ac59ac1501600f43e10a3c2d35..c31c07443ae29af6d6e60c93d88cdc996416e31a 100755 --- a/app/admin/view/default/user/save_info.html +++ b/app/admin/view/default/user/save_info.html @@ -70,8 +70,12 @@ {{include file="lib/gender" /}}
- - + + +
+
+ +
{{include file="lib/user_status" /}} diff --git a/app/api/controller/Crontab.php b/app/api/controller/Crontab.php index e47a34cd2b4a7dcd9214c691db4e377c10a57d8b..66d5403cdec3bcc62dfa21d39ada637291bd7d2e 100644 --- a/app/api/controller/Crontab.php +++ b/app/api/controller/Crontab.php @@ -59,5 +59,18 @@ class Crontab extends Common $ret = CrontabService::PayLogOrderClose(); return 'count:'.$ret['data']; } + + /** + * 商品赠送积分 + * @author Devil + * @blog http://gong.gg/ + * @version 1.0.0 + * @datetime 2019-08-18T17:19:33+0800 + */ + public function GoodsGiveIntegral() + { + $ret = CrontabService::GoodsGiveIntegral(); + return 'sucs:'.$ret['data']['sucs'].', fail:'.$ret['data']['fail']; + } } ?> \ No newline at end of file diff --git a/app/service/CrontabService.php b/app/service/CrontabService.php index 9e5aefc6fcdc77828ab0cec2cf18204da51f35b6..aa215cafd6529bf9df6482667f90824b8f1b4f64 100644 --- a/app/service/CrontabService.php +++ b/app/service/CrontabService.php @@ -176,5 +176,69 @@ class CrontabService $res = Db::name('PayLog')->where($where)->update($data); return DataReturn('操作成功', 0, $res); } + + /** + * 商品积分赠送 + * @author Devil + * @blog http://gong.gg/ + * @version 1.0.0 + * @date 2020-07-28 + * @desc description + * @param [array] $params [输入参数] + */ + public static function GoodsGiveIntegral($params = []) + { + // 获取可赠送的日志数据 + $time = time()-(intval(MyC('common_goods_give_integral_limit_time', 21600, true))*60); + $where = [ + ['add_time', '<', $time], + ['status', '=', 0], + ]; + $data = Db::name('GoodsGiveIntegralLog')->where($where)->field('id,status,user_id,integral')->select()->toArray(); + + // 状态 + $sucs = 0; + $fail = 0; + if(!empty($data)) + { + // 更新状态 + $upd_data = [ + 'status' => 1, + 'upd_time' => time(), + ]; + foreach($data as $v) + { + // 开启事务 + Db::startTrans(); + if(Db::name('GoodsGiveIntegralLog')->where(['id'=>$v['id'], 'status'=>0])->update($upd_data)) + { + // 扣减用户锁定积分 + if(!Db::name('User')->where(['id'=>$v['user_id']])->dec('locking_integral', $v['integral'])->update()) + { + return DataReturn('用户锁定积分扣减失败['.$v['id'].'-'.$v['user_id'].']', -2); + } + + // 增加用户有效积分 + $user_integral = Db::name('User')->where(['id'=>$v['user_id']])->value('integral'); + if(!Db::name('User')->where(['id'=>$v['user_id']])->inc('integral', $v['integral'])->update()) + { + return DataReturn('用户有效积分增加失败['.$v['id'].'-'.$v['user_id'].']', -3); + } + + // 积分日志 + IntegralService::UserIntegralLogAdd($v['user_id'], $user_integral, $v['integral'], '订单商品赠送', 1); + + // 提交事务 + Db::commit(); + $sucs++; + continue; + } + // 事务回滚 + Db::rollback(); + $fail++; + } + } + return DataReturn('操作成功', 0, ['sucs'=>$sucs, 'fail'=>$fail]); + } } ?> \ No newline at end of file diff --git a/app/service/GoodsService.php b/app/service/GoodsService.php index fe8442bac43ccb4a295d6918e33b4c64ce67b95e..d37560baca6c805b4eee2b68932d00fc7071f571 100755 --- a/app/service/GoodsService.php +++ b/app/service/GoodsService.php @@ -1213,9 +1213,6 @@ class GoodsService $content_web = empty($params['content_web']) ? '' : ResourcesService::ContentStaticReplace(htmlspecialchars_decode($params['content_web']), 'add'); $fictitious_goods_value = empty($params['fictitious_goods_value']) ? '' : ResourcesService::ContentStaticReplace(htmlspecialchars_decode($params['fictitious_goods_value']), 'add'); - // 赠送积分 - $give_integral = max(0, (isset($params['give_integral']) && $params['give_integral'] <= 100) ? intval($params['give_integral']) : 0); - // 封面图片、默认相册第一张 $images = empty($attachment['data']['images']) ? (isset($photo['data'][0]) ? $photo['data'][0] : '') : $attachment['data']['images']; @@ -1227,7 +1224,6 @@ class GoodsService 'model' => $params['model'], 'place_origin' => isset($params['place_origin']) ? intval($params['place_origin']) : 0, 'inventory_unit' => $params['inventory_unit'], - 'give_integral' => $give_integral, 'buy_min_number' => max(1, isset($params['buy_min_number']) ? intval($params['buy_min_number']) : 1), 'buy_max_number' => isset($params['buy_max_number']) ? intval($params['buy_max_number']) : 0, 'is_deduction_inventory' => isset($params['is_deduction_inventory']) ? intval($params['is_deduction_inventory']) : 0, @@ -1246,6 +1242,12 @@ class GoodsService 'site_type' => isset($params['site_type']) ? $params['site_type'] : -1, ]; + // 是否存在赠送积分 + if(array_key_exists('give_integral', $params)) + { + $data['give_integral'] = max(0, ($params['give_integral'] <= 100) ? intval($params['give_integral']) : 0); + } + // 商品保存处理钩子 $hook_name = 'plugins_service_goods_save_handle'; $ret = EventReturnHandle(MyEventTrigger($hook_name, [ diff --git a/app/service/IntegralService.php b/app/service/IntegralService.php index 44d5275acce760533dec29a6c26a81e0cb09ea1b..1761892f30182d8434f5e0ffc1324a47e5454bfd 100755 --- a/app/service/IntegralService.php +++ b/app/service/IntegralService.php @@ -209,38 +209,66 @@ class IntegralService } // 获取订单商品 - $order_detail = Db::name('OrderDetail')->where(['order_id'=>$params['order_id']])->field('goods_id,total_price')->select()->toArray(); + $order_detail = Db::name('OrderDetail')->where(['order_id'=>$params['order_id']])->field('id,order_id,goods_id,user_id,total_price')->select()->toArray(); if(!empty($order_detail)) { // 获取赠送积分的商品 - $goods_give = Db::name('Goods')->where(['id'=>array_column($order_detail, 'goods_id')])->column('give_integral', 'id'); - - // 循环发放 - foreach($order_detail as $dv) + $where = [ + ['id', 'in', array_column($order_detail, 'goods_id')], + ['give_integral', '>', 0], + ]; + $goods_give = Db::name('Goods')->where($where)->column('give_integral', 'id'); + if(!empty($goods_give)) { - if(array_key_exists($dv['goods_id'], $goods_give)) + // 循环发放 + foreach($order_detail as $dv) { - $give_rate = $goods_give[$dv['goods_id']]; - if($give_rate > 0 && $give_rate <= 100) + if(array_key_exists($dv['goods_id'], $goods_give)) { - // 实际赠送积分 - $give_integral = intval(($give_rate/100)*$dv['total_price']); - if($give_integral >= 1) + $give_rate = $goods_give[$dv['goods_id']]; + if($give_rate > 0 && $give_rate <= 100) { - // 用户积分添加 - $user_integral = Db::name('User')->where(['id'=>$user['id']])->value('integral'); - if(!Db::name('User')->where(['id'=>$user['id']])->inc('integral', $give_integral)->update()) + // 实际赠送积分 + $give_integral = intval(($give_rate/100)*$dv['total_price']); + if($give_integral >= 1) { - return DataReturn('用户积分赠送失败['.$params['order_id'].'-'.$dv['goods_id'].']', -10); - } + // 是否已存在日志记录 + $where = [ + ['order_id', '=', $dv['order_id']], + ['order_detail_id', '=', $dv['id']], + ['goods_id', '=', $dv['goods_id']], + ]; + $temp = Db::name('GoodsGiveIntegralLog')->where($where)->count(); + if(empty($temp)) + { + // 增加用户锁定积分 + if(!Db::name('User')->where(['id'=>$user['id']])->inc('locking_integral', $give_integral)->update()) + { + return DataReturn('用户积分赠送失败['.$params['order_id'].'-'.$dv['goods_id'].']', -10); + } - // 积分日志 - self::UserIntegralLogAdd($user['id'], $user_integral, $give_integral, '订单商品完成赠送', 1); + // 积分赠送日志添加 + $log_data = [ + 'order_id' => $dv['order_id'], + 'order_detail_id' => $dv['id'], + 'goods_id' => $dv['goods_id'], + 'user_id' => $dv['user_id'], + 'status' => 0, + 'rate' => $give_rate, + 'integral' => $give_integral, + 'add_time' => time(), + ]; + if(Db::name('GoodsGiveIntegralLog')->insertGetId($log_data) <= 0) + { + return DataReturn('用户积分赠送日志添加失败['.$params['order_id'].'-'.$dv['goods_id'].']', -11); + } + } + } } } } + return DataReturn('操作成功', 0); } - return DataReturn('操作成功', 0); } return DataReturn('没有需要操作的数据', 0); } @@ -290,41 +318,63 @@ class IntegralService } // 获取用户信息 - $user = Db::name('User')->field('id,integral')->find($order_detail['user_id']); + $user = Db::name('User')->where(['id'=>$order_detail['user_id']])->field('id')->find(); if(empty($user)) { return DataReturn('用户不存在或已删除,终止操作', 0); } - // 获取商品相关信息 - $give_rate = Db::name('Goods')->where(['id'=>$order_detail['goods_id']])->value('give_integral'); - if($give_rate > 0 && $give_rate <= 100) + // 获取日志 + $where = [ + ['order_id', '=', $order_detail['order_id']], + ['order_detail_id', '=', $order_detail['id']], + ['goods_id', '=', $order_detail['goods_id']], + ['user_id', '=', $order_detail['user_id']], + ['status', '=', 0], + ]; + $info = Db::name('GoodsGiveIntegralLog')->where($where)->find(); + if(empty($info)) + { + return DataReturn('无待发放日志,终止操作', 0); + } + + // 存在退款金额则使用退款金额 + // 未存在退款金额则判断是否存在退款数量 + // 存在退款数量则使用退款数量*单价金额计算(防止订单退款金额为空仅存在退款数量) + $refund_integral = 0; + if($order_detail['refund_price'] > 0) { - // 存在退款金额则使用退款金额 - // 未存在退款金额则判断是否存在退款数量 - // 存在退款数量则使用退款数量*单价金额计算(防止订单退款金额为空仅存在退款数量) - $refund_integral = 0; - if($order_detail['refund_price'] > 0) + $refund_integral = intval(($info['rate']/100)*$order_detail['refund_price']); + } else { + if($order_detail['returned_quantity'] > 0) { - $refund_integral = intval(($give_rate/100)*$order_detail['refund_price']); - } else { - if($order_detail['returned_quantity'] > 0) - { - $refund_integral = intval(($give_rate/100)*($order_detail['price']*$order_detail['returned_quantity'])); - } + $refund_integral = intval(($info['rate']/100)*($order_detail['price']*$order_detail['returned_quantity'])); } - if($refund_integral >= 1) + } + if($refund_integral >= 1) + { + // 扣减用户锁定积分 + if(!Db::name('User')->where(['id'=>$user['id']])->dec('locking_integral', $refund_integral)->update()) { - // 用户积分添加 - if(!Db::name('User')->where(['id'=>$user['id']])->dec('integral', $refund_integral)->update()) + return DataReturn('用户锁定积分扣减失败['.$user['id'].'-'.$order_detail['order_id'].'-'.$order_detail['goods_id'].']', -10); + } + + // 扣减日志积分 + if(!Db::name('GoodsGiveIntegralLog')->where(['id'=>$info['id']])->dec('integral', $refund_integral)->update()) + { + return DataReturn('日志积分扣减失败['.$info['id'].'-'.$order_detail['order_id'].'-'.$order_detail['goods_id'].']', -11); + } + + // 剩余0积分则关闭 + if(Db::name('GoodsGiveIntegralLog')->where(['id'=>$info['id']])->value('integral') <= 0) + { + if(!Db::name('GoodsGiveIntegralLog')->where(['id'=>$info['id']])->update(['status'=>2, 'upd_time'=>time()])) { - return DataReturn('用户积分释放失败['.$order_detail['order_id'].'-'.$order_detail['goods_id'].']', -10); + return DataReturn('日志积分关闭失败['.$info['id'].'-'.$order_detail['order_id'].'-'.$order_detail['goods_id'].']', -12); } - - // 积分日志 - self::UserIntegralLogAdd($user['id'], $user['integral'], $refund_integral, '订单商品发生售后收回', 0); } } + return DataReturn('操作成功', 0); } diff --git a/app/service/UserService.php b/app/service/UserService.php index 1fb559f3dbb48679d9f10e75a89b4fc8131fe6be..b619cfbb6152e3b39f69eb74046c587eef0cf73f 100755 --- a/app/service/UserService.php +++ b/app/service/UserService.php @@ -348,6 +348,7 @@ class UserService 'address' => isset($params['address']) ? $params['address'] : '', 'gender' => intval($params['gender']), 'integral' => intval($params['integral']), + 'locking_integral' => intval($params['locking_integral']), 'status' => intval($params['status']), 'alipay_openid' => isset($params['alipay_openid']) ? $params['alipay_openid'] : '', 'baidu_openid' => isset($params['baidu_openid']) ? $params['baidu_openid'] : '',