package shop.alien.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import shop.alien.entity.store.StoreIncomeDetailsRecord;
import shop.alien.entity.store.vo.StoreIncomeDetailsRecordVo;

import java.util.List;

/**
 * 商户收入明细表 Mapper 接口
 *
 * @author ssk
 * @since 2025-02-25
 */
public interface StoreIncomeDetailsRecordMapper extends BaseMapper<StoreIncomeDetailsRecord> {

    @Select("select income.id, income.money, income.commission, income.created_time checkTime, uorder.created_time orderTime, date_add(income.created_time, interval 3 day) incomeTime, " +
            "       income_type, uorder.quan_code,  uorder.price as orderPrice, coupon.name couponName, coupon.coupon_code, store.store_name, if(lfo.store_id is not null ,'true','false') refundType " +
            "from store_income_details_record income " +
            "join store_info store on store.id = income.store_id " +
            "left join life_user_order uorder on uorder.id = income.user_order_id " +
            "left join life_coupon coupon on coupon.id = uorder.quan_id " +
            "left join life_refund_order lfo on uorder.id = lfo.order_id and lfo.status = 0 " +
            "${ew.customSqlSegment}")
    List<StoreIncomeDetailsRecordVo> getIncomeList(@Param(Constants.WRAPPER) QueryWrapper<StoreIncomeDetailsRecordVo> wrapper);

    @Update("UPDATE store_income_details_record SET cash_out_id = NULL WHERE delete_flag=0 AND store_id = #{storeId} AND cash_out_id = #{cashOutId}")
    int updateByCashOutId(@Param("storeId") Integer storeId,
                                 @Param("cashOutId") Integer cashOutId);

    @Select("with totalCoupon as (\n" +
            "select lc.name,1 type,lc.id\n" +
            "from life_coupon lc  where lc.delete_flag = 0\n" +
            "union all\n" +
            "select lgbm.group_name,2 type,lgbm.id\n" +
            "from life_group_buy_main lgbm where lgbm.delete_flag = 0 \n" +
            ")            \n" +
            "SELECT\n" +
            "\tsidr.id,sidr.updated_time,sidr.created_user_id,sidr.created_time,sidr.income_type,sidr.business_id,sidr.cash_out_id,sidr.store_id,sidr.user_order_id,sidr.delete_flag,sidr.money,sidr.commission,sidr.updated_user_id,\n" +
            "\tluo.buy_time orderTime,\n" +
            "\tluo.price orderPrice,\n" +
            "\tsidr.created_time checkTime,\n" +
            "\tADDDATE(sidr.created_time, 4) incomeTime,\n" +
            "\ttc.name couponName\n" +
            "FROM\n" +
            "\tstore_income_details_record sidr\n" +
            "left join order_coupon_middle ocm on ocm.id = sidr.user_order_id and ocm.delete_flag = 0\n" +
            "left join life_user_order luo on luo.id  = ocm.order_id and luo.delete_flag = 0\n" +
            "left join totalCoupon tc on tc.id = sidr.business_id and tc.type = luo.coupon_type\n"+
            "${ew.customSqlSegment}" )
    List<StoreIncomeDetailsRecordVo> selectRecordList(@Param(Constants.WRAPPER) QueryWrapper<StoreIncomeDetailsRecord> wrapper);
}
