package com.clx.performance.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.clx.performance.model.OrderChild;
import com.clx.performance.param.app.PageOrderChildOfDriverParam;
import com.clx.performance.param.app.PageOrderChildOfDriverSearchParam;
import com.clx.performance.param.pc.OrderChildCancelRecordParam;
import com.clx.performance.param.pc.OrderChildReportParam;
import com.clx.performance.param.pc.PageCarrierOrderChildParam;
import com.clx.performance.param.pc.PagePoundAuditParam;
import com.clx.performance.sqlProvider.OrderChildSqlProvider;
import com.clx.performance.vo.app.OrderChildVO;
import com.clx.performance.vo.pc.PageCarrierOrderChildVO;
import com.clx.performance.vo.pc.PageOrderChildPoundAuditVO;
import org.apache.ibatis.annotations.*;

import java.math.BigDecimal;
import java.util.List;


@Mapper
public interface OrderChildMapper extends BaseMapper<OrderChild> {
    String TABLE = "order_child";

    /**
     * 司机运单列表
     * @see OrderChildSqlProvider#pageOrderChildOfDriver(Page, PageOrderChildOfDriverParam)
     */
    @SelectProvider(type = OrderChildSqlProvider.class, method = "pageOrderChildOfDriver")
    IPage<OrderChildVO> pageOrderChildOfDriver(@Param("page") Page<OrderChildVO> page, @Param("param") PageOrderChildOfDriverParam param);

    /**
     * 搜索司机运单列表
     * @see OrderChildSqlProvider#pageSearchOrderChildOfDriver(Page, PageOrderChildOfDriverSearchParam)
     */
    @SelectProvider(type = OrderChildSqlProvider.class, method = "pageSearchOrderChildOfDriver")
    IPage<OrderChildVO> pageSearchOrderChildOfDriver(@Param("page") Page<OrderChildVO> page, @Param("param") PageOrderChildOfDriverSearchParam param);


    @SelectProvider(type = OrderChildSqlProvider.class, method = "pagePoundAuditList")
    IPage<PageOrderChildPoundAuditVO> pagePoundAuditList(@Param("page") Page<PageOrderChildPoundAuditVO> page, @Param("param") PagePoundAuditParam param);

    @SelectProvider(type = OrderChildSqlProvider.class, method = "pageCarrierOrderChildList")
    IPage<PageCarrierOrderChildVO> pageCarrierOrderChildList(@Param("page") Page<PageCarrierOrderChildVO> page, @Param("param") PageCarrierOrderChildParam param);

    @Select("select count(1) from order_child where order_goods_no = #{orderGoodsNo} and status < 110")
    int countValidByOrderGoodsNo(String orderGoodsNo);

    @UpdateProvider(type = OrderChildSqlProvider.class, method = "batchUpdateOrderChildStatus")
    Integer updateOrderGoodsSetResidueWeight(@Param(value = "status") Integer status, @Param(value = "remark") String remark, @Param(value = "ids") List<Integer> ids);

    @SelectProvider(type = OrderChildSqlProvider.class, method = "listAfterArrayReceiveChild")
    BigDecimal listAfterArrayReceiveChild(@Param(value = "orderNo")String orderNo);
    Integer updateOrderGoodsSetResidueWeight(@Param(value = "status") Integer status, @Param(value = "ids") List<Integer> ids);

    @Select("select AVG((load_net-unload_net)/load_net) from order_child " +
            " where send_system_address_id = #{sendSystemAddressId} " +
            " and receive_system_address_id = #{receiveSystemAddressId} " +
            " and status >= 90 and status <= 100 " +
            " and pay_time >= #{beginTime}")
    BigDecimal getLinePoundDifferenceRatioAvg(@Param("sendSystemAddressId") Integer sendSystemAddressId,
                                              @Param("receiveSystemAddressId") Integer receiveSystemAddressId,
                                              @Param("beginTime") String beginTime);

    @Select("select AVG(TIMESTAMPDIFF(SECOND, arrive_send_time, unload_time)) from order_child " +
            " where send_system_address_id = #{sendSystemAddressId} " +
            " and receive_system_address_id = #{receiveSystemAddressId} " +
            " and status >= 90 and status <= 100 " +
            " and pay_time >= #{beginTime}")
    Integer getLineArriveSendAddressToUnloadTimeAvg(@Param("sendSystemAddressId") Integer sendSystemAddressId,
                                         @Param("receiveSystemAddressId") Integer receiveSystemAddressId,
                                         @Param("beginTime") String beginTime);

    Integer updateOrderGoodsSetResidueWeight(@Param(value = "status") Integer status, @Param(value = "remark") String remark,
                                             @Param(value = "cancelTime") String cancelTime, @Param(value = "finishTime") String finishTime,
                                             @Param(value = "childNoList") List<String> childNoList);


    @SelectProvider(type = OrderChildSqlProvider.class, method = "findArtificialCancelOrder")
    OrderChildVO findArtificialCancelOrder(@Param("orderChildNo") String orderChildNo);

    @SelectProvider(type = OrderChildSqlProvider.class, method = "orderChildCancelRecord")
    IPage<OrderChildVO> orderChildCancelRecord(@Param("page")Page<OrderChildVO> page, @Param(value = "param") OrderChildCancelRecordParam param);

    @SelectProvider(type = OrderChildSqlProvider.class, method = "getLossNet")
    List<Double> getLossNet(@Param("param") OrderChildReportParam param);

    @Select("<script>" +
            "select sum(TIMESTAMPDIFF(SECOND,load_time,arrive_send_time)) from " + TABLE +
            " where load_time is not null and and pay_time &gt;= #{beginTime} " +
            " <if test = 'sendSystemAddressId != null'>and send_system_address_id = #{sendSystemAddressId} </if> " +
            " <if test = 'receiveSystemAddressId != null'>and receive_system_address_id = #{receiveSystemAddressId} </if> " +

            " <if test = 'sendSystemAddressId == null'>and send_address_id = #{sendAddressId} </if> " +
            " <if test = 'receiveSystemAddressId == null'>and receive_address_id = #{receiveAddressId} </if> " +
            " </script>")

    Integer loadTimeAvg(Integer sendAddressId, Integer receiveAddressId,
                        Integer sendSystemAddressId, Integer receiveSystemAddressId,
                        String beginTime);

    @Select("<script>" +
            "select sum(TIMESTAMPDIFF(SECOND,unload_time,arrive_receive_time)) from " + TABLE +
            " where unload_time is not null and and pay_time &gt;= #{beginTime} " +
            " <if test = 'sendSystemAddressId != null'>and send_system_address_id = #{sendSystemAddressId} </if> " +
            " <if test = 'receiveSystemAddressId != null'>and receive_system_address_id = #{receiveSystemAddressId} </if> " +

            " <if test = 'sendSystemAddressId == null'>and send_address_id = #{sendAddressId} </if> " +
            " <if test = 'receiveSystemAddressId == null'>and receive_address_id = #{receiveAddressId} </if> " +
            " </script>")

    Integer unloadTimeAvg(Integer sendAddressId, Integer receiveAddressId,
                        Integer sendSystemAddressId, Integer receiveSystemAddressId,
                        String beginTime);
}