insert into ct_user_commission ( agent_id, channel_id, game_id, player_id, user_id, parent_user_id, room_code, used_card, owner_id, room_type, room_mode, commission_type, grade, mode, type, commission_total, commission_system, commission_user, commission_parent_user, commission_user_rate, commission_parent_user_rate, create_time, out_id ) select agent_id, channel_id, game_id, player_id, user_id, parent_user_id, room_code, used_card, owner_id, room_type, room_mode, commission_type, grade, mode, type, ifnull(commission_total, 0), ifnull(commission_total, 0) - floor(ifnull(commission_total, 0) * ifnull(user_rate, 0)) - floor(ifnull(commission_total, 0) * ifnull(parent_user_rate, 0)), floor(ifnull(commission_total, 0) * ifnull(user_rate, 0)), floor(ifnull(commission_total, 0) * ifnull(parent_user_rate, 0)), ifnull(user_rate, 0), ifnull(parent_user_rate, 0), create_time, out_id from ( select a.agent_id, a.channel_id, a.game_id, a.player_id, a.user_id, a.parent_user_id, a.room_code, a.used_card, a.owner_id, a.room_type, a.room_mode, a.commission_type, a.grade, a.mode, a.type, a.commission_total, case when (0 = a.user_id) or (0 != a.user_is_admin) then 0 else case ifnull(c.is_custom, 0) when 0 then b.user_commission_rate else c.commission_rate end end + case when ((0 != a.user_id) and (0 = a.user_is_admin)) and ((0 = a.parent_user_id) or (0 != a.parent_user_is_admin)) then case ifnull(c.is_custom, 0) when 0 then b.parent_user_commission_rate else c.parent_commission_rate end else 0 end user_rate, case when (0 = a.parent_user_id) or (0 != a.parent_user_is_admin) then 0 else case ifnull(c.is_custom, 0) when 0 then b.parent_user_commission_rate else c.parent_commission_rate end end parent_user_rate, a.user_is_admin, a.parent_user_is_admin, a.create_time, a.out_id from ( select a.core_agentid agent_id, b.play_channelid channel_id, a.core_gameid game_id, a.core_playerid player_id, ifnull(b.play_invitecode, 0) user_id, ifnull(c.global_power, 0) user_is_admin, ifnull(c.saus_parentid, 0) parent_user_id, ifnull(d.global_power, 0) parent_user_is_admin, a.core_roomcode room_code, a.core_roomcard used_card, a.core_ownerid owner_id, a.core_roomtype room_type, a.core_roommode room_mode, case a.core_roommode when 0 then '001' when 1 then '002' else '001' end commission_type, a.core_grade grade, a.core_mode mode, a.core_datatype type, a.core_value commission_total, a.core_time create_time, a.idx out_id from coin_rebate a left join player b on a.core_agentid = b.play_agentid and a.core_playerid = b.play_playerid left join sales_user c on b.play_agentid = c.saus_agentid and b.play_channelid = c.saus_channelid and b.play_invitecode = c.saus_salesid left join sales_user d on c.saus_agentid = d.saus_agentid and c.saus_channelid = d.saus_channelid and c.saus_parentid = d.saus_salesid ) a left join ct_agent_commission b on a.agent_id = b.agent_id and a.channel_id = b.channel_id and a.commission_type = b.type_id left join ct_agent_commission_user c on a.agent_id = c.agent_id and a.channel_id = c.channel_id and b.type_id = c.type_id and a.user_id = c.user_id ) t where t.out_id not in (select out_id from ct_user_commission)