package com.fs.live.mapper;


import com.fs.live.domain.LiveAfterSales;
import com.fs.live.param.LiveAfterSalesListUParam;
import com.fs.live.param.LiveAfterSalesQueryParam;
import com.fs.live.vo.LiveAfterSalesListUVO;
import com.fs.live.vo.LiveAfterSalesQueryVO;
import com.fs.live.vo.LiveAfterSalesVo;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * 售后记录Mapper接口
 *
 * @author fs
 * @date 2025-07-08
 */
public interface LiveAfterSalesMapper {
    /**
     * 查询售后记录
     *
     * @param id 售后记录主键
     * @return 售后记录
     */
    LiveAfterSales selectLiveAfterSalesById(Long id);

    /**
     * 查询售后记录列表
     *
     * @param liveAfterSales 售后记录
     * @return 售后记录集合
     */
    List<LiveAfterSales> selectLiveAfterSalesList(LiveAfterSales liveAfterSales);

    /**
     * 新增售后记录
     *
     * @param liveAfterSales 售后记录
     * @return 结果
     */
    int insertLiveAfterSales(LiveAfterSales liveAfterSales);

    /**
     * 修改售后记录
     *
     * @param liveAfterSales 售后记录
     * @return 结果
     */
    int updateLiveAfterSales(LiveAfterSales liveAfterSales);

    /**
     * 删除售后记录
     *
     * @param id 售后记录主键
     * @return 结果
     */
    int deleteLiveAfterSalesById(Long id);

    /**
     * 批量删除售后记录
     *
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    int deleteLiveAfterSalesByIds(Long[] ids);

    @Select({"<script> " +
            "select s.*,o.order_code  from live_after_sales s left join live_order o on o.order_id=s.order_id  " +
            "where s.is_del=0 " +
            "<if test = 'maps.status != null and maps.status ==1   '> " +
            "and s.sales_status = 0 " +
            "</if>" +
            "<if test = 'maps.status != null and maps.status ==2   '> " +
            "and s.sales_status = 3 " +
            "</if>" +
            "<if test = 'maps.userId != null'> " +
            "and s.user_id = #{maps.userId} " +
            "</if>" +
            "order by s.create_time desc "+
            "</script>"})
    List<LiveAfterSalesListUVO> selectLiveAfterSalesListUVO(@Param("maps") LiveAfterSalesListUParam param);

    @Select("select * from  live_after_sales where order_id=#{orderId}")
    LiveAfterSales selectLiveAfterSalesByOrderId(@Param("orderId") Long orderId);

    @Select({"<script> " +
            "select s.*  from live_after_sales s   " +
            "where s.sales_status=0 and s.status=0 and date_format(date_add(s.create_time, interval 24 hour),'%Y-%m-%d %H') &gt;= date_format(now(),'%Y-%m-%d %H') order by create_time desc " +
            "</script>"})
    List<LiveAfterSales> selectLiveAfterSalesByDoAudit();

    List<LiveAfterSalesVo> selectLiveAfterSalesVoList(LiveAfterSalesVo liveAfterSales);


    @Select({
            "<script>",
            "select id,order_id from live_after_sales ",
            "where order_id in",
            "<foreach collection='orderIdList' item='orderId' open='(' separator=',' close=')'>",
            "#{orderId}",
            "</foreach>",
            "</script>"
    })
    @MapKey("orderId")
    Map<Long, LiveAfterSales> findByOrderIds(@Param("orderIdList") List<Long> orderIdList);

    @Select({"<script> " +
            "select *  from live_after_sales  " +
            "where 1=1 " +
            "<if test = 'maps.status != null and maps.status ==1   '> " +
            "and sales_status = 0 " +
            "</if>" +
            "<if test = 'maps.status != null and maps.status ==2   '> " +
            "and sales_status = 3 " +
            "</if>" +
            "<if test = 'maps.userId != null    '> " +
            "and user_id = #{maps.userId} " +
            "</if>" +
            "order by create_time desc "+
            "</script>"})
    List<LiveAfterSalesQueryVO> selectLiveAfterSalesListQuery(@Param("maps") LiveAfterSalesQueryParam param);

    @Select({"<script> " +
            "select ifnull(count(1),0) from live_after_sales   " +
            "where user_id=#{userId}  and sales_status =#{salesStatus} " +
            "</script>"})
    Integer selectLiveAfterSalesCount(@Param("userId")long userId,@Param("salesStatus") int salesStatus);
}
