package com.fzm.mall.server.front.order.mapper;

import com.fzm.mall.server.front.asset.model.po.qo.OrderQO;
import com.fzm.mall.server.front.asset.sql.DeliverySqlFactory;
import com.fzm.mall.server.front.order.model.Delivery;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fzm.mall.server.front.order.model.vo.DeliveryVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 * 用户提货表 Mapper 接口
 * </p>
 *
 * @author tdz
 * @since 2021-04-13
 */
public interface DeliveryMapper extends BaseMapper<Delivery> {

    /**
     * 查询订单
     */
    @Select("select d.order_delivery_id deliveryId, d.uid, d.merchant_id, m.enterprise_name, " +
            "d.goods_id, d.name, d.coin, d.number,p.thumb, " +
            "d.uname, d.phone, d.address, d.note,d.sku_id, " +
            "d.create_time, d.delivery_time, d.finish_time, " +
            "d.express_type, d.express_no, d.delivery_state, " +
            "d.buyer_state, d.seller_state, d.post_fee " +
            "from order_delivery d left join merchant m on d.merchant_id = m.merchant_id " +
            "left join goods_spu p on d.goods_id = p.goods_id " +
            "left join goods_sku k on d.goods_id = k.goods_id and d.coin = k.coin_name and k.status = 1 " +
            "where d.order_delivery_id = #{oid} and d.is_delete = 0")
    DeliveryVO deliveryVO(@Param("oid") String oid);

    /**
     * 分页查询订单
     */
    @SelectProvider(type = DeliverySqlFactory.class, method = "listDelivery")
    List<DeliveryVO> listDelivery(OrderQO orderQO);

    /**
     * 分页查询订单
     */
    @SelectProvider(type = DeliverySqlFactory.class, method = "countDelivery")
    Integer countDelivery(OrderQO orderQO);

    /**
     * 分页查询售后订单
     */
    @SelectProvider(type = DeliverySqlFactory.class, method = "afterSaleDelivery")
    List<DeliveryVO> afterSaleDelivery(OrderQO orderQO);

    /**
     * 分页查询订单
     */
    @SelectProvider(type = DeliverySqlFactory.class, method = "countAfterSaleDelivery")
    Integer countAfterSaleDelivery(OrderQO orderQO);

    @Update("update order_delivery set delivery_state = 2, finish_time = #{time} where uid = #{uid} and  order_delivery_id = #{orderDeliveryId} and delivery_state = 1")
    void finish(@Param("uid") String uid, @Param("orderDeliveryId") String orderDeliveryId,@Param("time")Long time);

    @Select("select express_detail from order_delivery where order_delivery_id = #{orderDeliveryId}")
    String queryLogistics(@Param("orderDeliveryId") String orderDeliveryId);

    @Update("update order_delivery set delivery_state = 4 where uid = #{uid} and  order_delivery_id = #{orderDeliveryId} and delivery_state in (0,1,2)")
    int returnGoods(@Param("uid") String uid,@Param("orderDeliveryId") String orderDeliveryId);

    @Select("select delivery_state from order_delivery where order_delivery_id = #{orderDeliveryId}")
    Integer getStatus(@Param("orderDeliveryId") String orderDeliveryId);

    @Update("update order_delivery set delivery_state = 3 where delivery_state = 4 and uid = #{uid} and order_delivery_id = #{orderDeliveryId}")
    int revokeReturn(@Param("uid") String uid, @Param("orderDeliveryId") String orderDeliveryId);

    @Select("select address from order_delivery where order_delivery_id = #{orderDeliveryId}")
    String getAddress(@Param("orderDeliveryId") String orderDeliveryId);
}
