package com.sqx.modules.reconciliation.mapper;

import com.sqx.modules.reconciliation.model.PlatformBill;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * <p>
 * 对账管理表 Mapper 接口
 * </p>
 *
 * @author ceshi
 * @since 2025-02-27
 */
@Mapper
public interface PlatformBillMapper extends BaseMapper<PlatformBill> {

    @Select("INSERT INTO `tcwm2.5`.platform_bill \n" +
            "(day_id, user_id, shop_name, `type`, start_money, revenue, revenue_count, \n" +
            "shop_payouts, shop_payouts_rates, shop_payouts_count, rider_payouts, rider_payouts_rates,\n" +
            "rider_payouts_count, refund_money, refund_count, end_money,sys_gift_amount, platform_rates,total_income,pay_money,create_time,update_time)\n" +
            "select #{dayId}, tu.user_id, null,2,null,sum(ti.rider_money),count(ti.rider_money),null,null,null,\n" +
            "sum(co.money),sum(co.rate), count(co.money),null,null,max(tu.balance),\n" +
            "(select sum(money) b from  user_money_details ud1 where ud1.user_id =tu.user_id and ud1.title like '系统赠送%' \n" +
            "and ud1.create_time>#{startTime} and ud1.create_time &lt;=#{endTime})\n" +
            "-(select sum(money) a from  user_money_details ud2 where ud2.user_id =tu.user_id and ud2.title like '系统扣除%' \n" +
            "and ud2.create_time>#{startTime} and ud2.create_time &lt;= #{endTime}) as gift_amount,null,\n" +
            "sum(ti.rider_money),null,#{now},#{now}\n" +
            "from tb_user tu USE INDEX(tb_user_rider_open_id_IDX)\n" +
            "left join tb_indent ti USE INDEX(tb_indent_finish_time_IDX) on  \n" +
            " ti.finish_time >#{startTime} \n" +
            "and ti.finish_time &lt;= #{endTime}\n" +
            "and ti.rider_user_id =tu.user_id\n" +
            "and ti.indent_state ='6'\n" +
            "left join cash_out co on co.user_id =tu.user_id and co.type =1 and co.state=1\n" +
            "and co.out_at >#{startTime} \n" +
            "and co.out_at &lt;=#{endTime}\n" +
            "where tu.rider_open_id is not null \n" +
            "group by tu.user_id \n" +
            "union all \n" +
            "select #{dayId}, tu.user_id, null,1,null,sum(tor.shop_income_money),count(tor.shop_income_money),\n" +
            "sum(co.money),sum(co.rate),count(co.money),null,null,null,sum(to1.pay_money),count(to1.pay_money),max(um.money),\n" +
            "(select sum(money) b from  user_money_details ud1 where ud1.user_id =tu.user_id and ud1.title like '系统赠送%' \n" +
            "and ud1.create_time>#{startTime} and ud1.create_time &lt;=#{endTime})\n" +
            "-(select sum(money) a from  user_money_details ud2 where ud2.user_id =tu.user_id and ud2.title like '系统扣除%' \n" +
            "and ud2.create_time>#{startTime} and ud2.create_time &lt;=#{endTime}) as gift_amount\n" +
            ",sum(tor.shop_income_money*(1-gs.shop_rate)/gs.shop_rate) platform_rates,sum(tor.shop_income_money),\n" +
            "sum(tor.pay_money),#{now},#{now} \n" +
            "from tb_user tu \n" +
            "left join goods_shop gs on tu.user_id =gs.user_id\n" +
            "left join tb_order  tor USE INDEX(tb_order_update_time_IDX) on gs.shop_id =tor.shop_id \n" +
            "and tor.update_time >#{startTime}\n" +
            "and tor.update_time &lt;=#{endTime}\n" +
            "and tor.status=4\n" +
            "left join tb_order  to1 USE INDEX(tb_order_update_time_IDX) on gs.shop_id =to1.shop_id \n" +
            "and to1.update_time >#{startTime}\n" +
            "and to1.update_time &lt;=#{endTime}\n" +
            "and to1.status in(5,8)\n" +
            "left join cash_out co on co.user_id =tu.user_id and co.type =2 and co.state=1\n" +
            "and co.out_at >#{startTime} \n" +
            "and co.out_at &lt;=#{endTime}\n" +
            "left join user_money um on um.user_id =tu.user_id\n" +
            "where  tu.admin_user_id is not null\n" +
            "group by tu.user_id ")
    int insertPlatformBill(@Param("dayId") String dayId,@Param("startTime") String startTime, @Param("endTime")String endTime, @Param("now") String now);

    @Select("update platform_bill a join platform_bill b on a.user_id =b.user_id and a.type =b.type and  TO_DAYS(a.day_id)-TO_DAYS(b.day_id)= 1 set a.start_money=b.end_money\n" +
            "where a.day_id =#{dayId} ")
    int updateStartMoney(@Param("dayId") String dayId);

    @Select("INSERT INTO `tcwm2.5`.platform_bill \n" +
            "(day_id, user_id, shop_name, `type`, start_money, revenue, revenue_count, \n" +
            "shop_payouts, shop_payouts_rates, shop_payouts_count, rider_payouts, rider_payouts_rates,\n" +
            "rider_payouts_count, refund_money, refund_count, end_money,sys_gift_amount, platform_rates,total_income,pay_money,create_time,update_time)\n" +
            "select\n" +
            "day_id,null,null,0,sum(if(type=1, start_money ,0)),sum(if(type=1, revenue,0)),sum(if(type=1, revenue_count,0)),\n" +
            "sum(if(type=1, shop_payouts ,0)),sum(if(type=1, shop_payouts_rates ,0)),sum(if(type=1, shop_payouts_count ,0)),\n" +
            "sum(if(type=2, rider_payouts ,0)),sum(if(type=2, rider_payouts_rates ,0)),sum(if(type=2, rider_payouts_count ,0)),\n" +
            "sum(if(type=1, refund_money ,0)),sum(if(type=1, refund_count ,0)),sum(if(type=1, end_money ,0)),null,\n" +
            "sum(if(type=1, platform_rates ,0)),null,null,max(create_time) ,max(update_time) \n" +
            "from platform_bill pb where day_id =#{dayId}\n")
    int insertTotalPlatformBill(@Param("dayId") String dayId);
}
