package com.fs.store.mapper;

import com.fs.store.domain.FsStoreDelivers;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 发货信息Mapper接口
 *
 * @date 2022-07-28
 * @author xdd
 */
@Mapper
public interface FsStoreDeliversMapper {

    /**
     * 根据ID查询发货信息
     * @param id 主键ID
     * @return 发货信息
     */
    @Select("SELECT * FROM fs_store_delivers WHERE id = #{id}")
    FsStoreDelivers findById(@Param("id") Integer id);

    /**
     * 插入发货信息
     * @param fsStoreDelivers 发货信息对象
     * @return 插入影响的行数
     */
    @Insert("INSERT INTO fs_store_delivers (order_id, order_code, deliver_sn, deliver_name, deliver_id, deliver_send_time, create_time, update_time, create_by, update_by) " +
            "VALUES (#{orderId}, #{orderCode}, #{deliverSn}, #{deliverName}, #{deliverId}, #{deliverSendTime}, #{createTime}, #{updateTime}, #{createBy}, #{updateBy})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(FsStoreDelivers fsStoreDelivers);

    /**
     * 更新发货信息
     * @param fsStoreDelivers 发货信息对象
     * @return 更新影响的行数
     */
    @Update("<script>" +
            "UPDATE fs_store_delivers " +
            "<set>" +
            "   <if test='orderId != null'>order_id = #{orderId},</if>" +
            "   <if test='orderCode != null'>order_code = #{orderCode},</if>" +
            "   <if test='deliverSn != null'>deliver_sn = #{deliverSn},</if>" +
            "   <if test='deliverName != null'>deliver_name = #{deliverName},</if>" +
            "   <if test='deliverId != null'>deliver_id = #{deliverId},</if>" +
            "   <if test='deliverSendTime != null'>deliver_send_time = #{deliverSendTime},</if>" +
            "   <if test='updateTime != null'>update_time = #{updateTime},</if>" +
            "   <if test='updateBy != null'>update_by = #{updateBy},</if>" +
            "</set>" +
            "WHERE id = #{id}" +
            "</script>")
    int update(FsStoreDelivers fsStoreDelivers);
      /**
     * 根据orderId查询发货信息
     * @param orderId 订单ID
     * @return 发货信息
     */
    @Select("SELECT * FROM fs_store_delivers WHERE order_id = #{orderId}")
    List<FsStoreDelivers> findByOrderId(@Param("orderId") Long orderId);

    /**
     * 通过订单号和快递号查询物流信息
     * @param orderId 订单号
     * @param deliverId 物流号
     * @return FsStoreDelivers
     */
    @Select("SELECT * FROM fs_store_delivers WHERE order_id = #{orderId} and deliver_id=#{deliverId}")
    FsStoreDelivers findByOrderIdAndDeliverId(Long orderId, String deliverId);
    /**
     * 批量更新发货信息
     * @param byOrderId 发货信息对象列表
     * @return 更新影响的行数
     */
    @Update("<script>" +
            "<foreach collection='list' item='item' separator=';'>" +
            "UPDATE fs_store_delivers " +
            "<set>" +
            "   <if test='item.orderId != null'>order_id = #{item.orderId},</if>" +
            "   <if test='item.orderCode != null'>order_code = #{item.orderCode},</if>" +
            "   <if test='item.deliverSn != null'>deliver_sn = #{item.deliverSn},</if>" +
            "   <if test='item.deliverName != null'>deliver_name = #{item.deliverName},</if>" +
            "   <if test='item.deliverId != null'>deliver_id = #{item.deliverId},</if>" +
            "   <if test='item.status != null'>status = #{item.status},</if>" +
            "   <if test='item.stateEx != null'>state_ex = #{item.stateEx},</if>" +
            "   <if test='item.deliverSendTime != null'>deliver_send_time = #{item.deliverSendTime},</if>" +
            "   <if test='item.updateTime != null'>update_time = #{item.updateTime},</if>" +
            "   <if test='item.updateBy != null'>update_by = #{item.updateBy},</if>" +
            "</set>" +
            "WHERE id = #{item.id}" +
            "</foreach>" +
            "</script>")
    void updateBatch(List<FsStoreDelivers> byOrderId);


    /**
     * 查询订单号和快递号查询物流信息
     * @param orderCode 订单号
     * @param deliverId 物流Id
     * @return FsStoreDelivers
     */
    @Select("SELECT * FROM fs_store_delivers WHERE order_code = #{orderCode} and deliver_id=#{deliverId}")
    FsStoreDelivers findByOrderCodeAndDeliverId(String orderCode, String deliverId);
}
