一个分佣系统中,根据各种订单进行下单后分佣,涉及到钱,如果重复分佣后果可想而知。
产景:前端多次请求同一个接口,主从数据有延迟,造成数据重复插入
处理方案:
//方案①
$come_in_times = \Yii::$app->redis->incr($data['order_id'].$data['type']);
if($come_in_times > 1){
\Yii::$app->redis->del($data['order_id'].$data['type']);
return false;
}
//处理业务再删redis中的key
\Yii::$app->redis->del($data['order_id'].$data['type']);
//方案②
if(\Yii::$app->redis->setnx($data['order_id'].$data['type'],1)){
if(!$come_in_times){
\Yii::$app->redis->del($data['order_id'].$data['type']);
return false;
}
//处理业务再删redis中的key
\Yii::$app->redis->del($data['order_id'].$data['type']);
//方案③强制读主库https://segmentfault.com/a/1190000038304022?sort=newest
$data = [
'order_id'=>303130,
'type'=>0,
];
$users = ShareOrder::getDb()->useMaster(function($db) use ($data) {
/** @var $db \yii\db\Connection */
return $db->createCommand("SELECT * FROM " . ShareOrder::tableName() . " where `order_id` =:order_id AND `type`=:type", [
':order_id' => $data['order_id'],
':type' => $data['type'],
])->queryOne(\PDO::FETCH_OBJ);
});
print_r(gettype($users));
//重复数据分佣数据
select order_no,count(*) as count from xcxmall_share_order group by order_no having count(*) > 1;
//除了已取消数据分佣数据
select id,order_id,order_no from xcxmall_share_order where order_no in (select order_no from xcxmall_share_order group by order_no having count(*) > 1) and order_status < 4;
//①先删除分佣详情表数据。!!!
select * from xcxmall_share_order_detail where share_order_id in (select id from xcxmall_share_order where order_no in (select order_no from xcxmall_share_order group by order_no having count(*) > 1) and order_status < 4 and id>=470589 GROUP BY order_id) GROUP BY order_id;
//②再删今天下午上线数据。
select * from xcxmall_share_order where order_no in (select order_no from xcxmall_share_order group by order_no having count(*) > 1) and order_status < 4 and id>=470589 GROUP BY order_id;
470589 516726 20220615145749114012
470679 516800 20220615162900569265
471077 517201 20220615205521880355
20220614号的数据要删掉一条,20220615145749114012 开始的,要删掉一条的同时删掉 xcxmall_share_order_detail中对应的一条。
select * from xcxmall_order where order_no='20200416175752802575'