提交 c2f30ce6 编写于 作者: G gongfuxiang

订单商品赠送积分逻辑优化

上级 dd10126a
......@@ -125,7 +125,10 @@
</div>
<div class="am-form-group">
<label>购买赠送积分比例<span class="am-form-group-label-tips">选填</span><span class="am-form-group-label-tips">按照商品金额比例乘以数量的比例进行发放</span></label>
<div class="am-input-group am-input-group-sm">
<div class="am-alert am-alert-warning am-radius am-margin-top-0" data-am-alert>
<p>订单完成自动将发放到用户锁定积分、再由脚本自动发放到有效积分</p>
</div>
<div class="am-input-group am-input-group-sm am-margin-top-xs">
<input type="number" name="give_integral" placeholder="购买赠送积分" min="0" max="100" data-validation-message="购买赠送积分比例 0~100 的数字" class="am-form-field am-radius" {{if !empty($data['give_integral'])}} value="{{$data.give_integral}}"{{/if}} />
<span class="am-input-group-btn">
<button type="button" class="am-btn am-btn-default am-radius">%</button>
......
......@@ -74,6 +74,19 @@
<a href="{{:MyUrl('api/crontab/paylogorderclose')}}" target="_blank">{{:MyUrl('api/crontab/paylogorderclose')}}</a>
</div>
</div>
<div class="am-form-group">
<label>{{$data.common_goods_give_integral_limit_time.name}}<span class="am-form-group-label-tips">{{$data.common_goods_give_integral_limit_time.describe}}</span></label>
<div class="am-input-group am-input-group-sm">
<input type="number" min="0" name="{{$data.common_goods_give_integral_limit_time.only_tag}}" placeholder="{{$data.common_goods_give_integral_limit_time.name}}" data-validation-message="{{$data.common_goods_give_integral_limit_time.error_tips}}" class="am-radius" {{if !empty($data)}}value="{{$data.common_goods_give_integral_limit_time.value}}"{{/if}} />
<span class="am-input-group-btn">
<button class="am-btn am-btn-default" type="button">分钟</button>
</span>
</div>
<div class="am-alert">
<a href="{{:MyUrl('api/crontab/goodsgiveintegral')}}" target="_blank">{{:MyUrl('api/crontab/goodsgiveintegral')}}</a>
</div>
</div>
</div>
</div>
......
......@@ -70,8 +70,12 @@
</div>
{{include file="lib/gender" /}}
<div class="am-form-group">
<label>积分</label>
<input type="number" placeholder="积分" name="integral" data-validation-message="详细地址2~60 个字符" class="am-radius" {{if !empty($data)}} value="{{$data.integral}}"{{/if}} />
<label>有效积分</label>
<input type="number" placeholder="有效积分" name="integral" min="0" data-validation-message="请输入有效积分" class="am-radius" {{if !empty($data)}} value="{{$data.integral}}"{{/if}} />
</div>
<div class="am-form-group">
<label>锁定积分</label>
<input type="number" placeholder="锁定积分" name="locking_integral" min="0" data-validation-message="请输入锁定积分" class="am-radius" {{if !empty($data)}} value="{{$data.locking_integral}}"{{/if}} />
</div>
{{include file="lib/user_status" /}}
......
......@@ -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
......@@ -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
......@@ -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, [
......
......@@ -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);
}
......
......@@ -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'] : '',
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册