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, coupon.name couponName, 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);
}
