package cn.iocoder.yudao.module.order.dal.mysql.orderCargoControl;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQuery;
import cn.iocoder.yudao.framework.mybatis.core.mapper.AbstractMapper;
import cn.iocoder.yudao.module.order.dal.dataobject.orderCargoControl.OrderCargoControlDO;
import cn.iocoder.yudao.framework.mybatis.core.vo.PageVO;
import cn.iocoder.yudao.module.order.dto.OrderCargoControlReleaseInfoDto;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.order.vo.orderCargoControl.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;

/**
 * 订单控货人信息 Mapper
 *
 * @author 郑屹
 */
@Mapper
public interface OrderCargoControlMapper extends AbstractMapper<OrderCargoControlDO> {
    @Override
    default PageResult<OrderCargoControlDO> selectPage(PageVO page, Object object) {
        if (object instanceof OrderCargoControlQueryVO) {
            OrderCargoControlQueryVO vo = (OrderCargoControlQueryVO) object;
            return selectPage(page, new LambdaQuery<OrderCargoControlDO>()
                    .eqIfPresent(OrderCargoControlDO::getUserId, vo.getUserId())
                    .eqIfPresent(OrderCargoControlDO::getOrderId, vo.getOrderId())
                    .eqIfPresent(OrderCargoControlDO::getOrderNo, vo.getOrderNo())
                    .eqIfPresent(OrderCargoControlDO::getTidanNo, vo.getTidanNo())
                    .eqIfPresent(OrderCargoControlDO::getCargoControlId, vo.getCargoControlId())
                    .likeIfPresent(OrderCargoControlDO::getName, vo.getName())
                    .eqIfPresent(OrderCargoControlDO::getPhone, vo.getPhone())
                    .eqIfPresent(OrderCargoControlDO::getEmail, vo.getEmail())
                    .eqIfPresent(OrderCargoControlDO::getCompany, vo.getCompany())
                    .eqIfPresent(OrderCargoControlDO::getIso2, vo.getIso2())
                    .eqIfPresent(OrderCargoControlDO::getDialCode, vo.getDialCode())
                    .eqIfPresent(OrderCargoControlDO::getCustomerId, vo.getCustomerId())
                    .eqIfPresent(OrderCargoControlDO::getCustomerContactsId, vo.getCustomerContactsId())
                    .eqIfPresent(OrderCargoControlDO::getIsActual, vo.getIsActual())
                    .betweenIfPresent(OrderCargoControlDO::getCreateTime, vo.getBeginCreateTime(), vo.getEndCreateTime())
                    .orderByDesc(OrderCargoControlDO::getId));
        }

        return null;
    }

    @Override
    default List<OrderCargoControlDO> selectList(Object object) {
        if (object instanceof OrderCargoControlQueryVO) {
            OrderCargoControlQueryVO vo = (OrderCargoControlQueryVO) object;
            return selectList(new LambdaQuery<OrderCargoControlDO>()
                    .eqIfPresent(OrderCargoControlDO::getUserId, vo.getUserId())
                    .eqIfPresent(OrderCargoControlDO::getOrderId, vo.getOrderId())
                    .eqIfPresent(OrderCargoControlDO::getOrderNo, vo.getOrderNo())
                    .eqIfPresent(OrderCargoControlDO::getTidanNo, vo.getTidanNo())
                    .eqIfPresent(OrderCargoControlDO::getCargoControlId, vo.getCargoControlId())
                    .likeIfPresent(OrderCargoControlDO::getName, vo.getName())
                    .eqIfPresent(OrderCargoControlDO::getPhone, vo.getPhone())
                    .eqIfPresent(OrderCargoControlDO::getEmail, vo.getEmail())
                    .eqIfPresent(OrderCargoControlDO::getCompany, vo.getCompany())
                    .eqIfPresent(OrderCargoControlDO::getIso2, vo.getIso2())
                    .eqIfPresent(OrderCargoControlDO::getDialCode, vo.getDialCode())
                    .eqIfPresent(OrderCargoControlDO::getCustomerId, vo.getCustomerId())
                    .eqIfPresent(OrderCargoControlDO::getCustomerContactsId, vo.getCustomerContactsId())
                    .eqIfPresent(OrderCargoControlDO::getIsActual, vo.getIsActual())
                    .betweenIfPresent(OrderCargoControlDO::getCreateTime, vo.getBeginCreateTime(), vo.getEndCreateTime())
                    .orderByDesc(OrderCargoControlDO::getId));
        }

        return null;
    }

    @ResultType(OrderCargoControlReleaseInfoDto.class)
    @Select({
            "<script>",
            "select o.order_id, ",
            "(select  ifnull(sum(ccp.pick_num),0) from ecw_order_cargo_control_pick ccp where ccp.order_id = o.order_id and ccp.status in(1,2,3,4) ) as release_num, ",
            "o.number, ",
            "o.order_no, ",
            "o.tidan_no, ",
            "o.parent_number, ",
            "o.container_number, ",
            "o.marks, ",
            "o.type, ",
            "o.`status`, ",
            "o.abnormal_state, ",
            "o.in_warehouse_state, ",
            "o.shipment_state, ",
            "o.to_warehouse_state, ",
            "o.transport_id, ",
            "o.is_cargo_control, ",
            "o.cargo_control_status, ",
            "o.cost ->> '$.totalNum' as total_num, ",
            "o.sum_num, ",
            "o.sum_volume, ",
            "o.sum_weight, ",
            "o.create_time, ",
            "o.unload_time, ",
            "o.is_limit_update_consignee, ",
            "o.lock_consignee_day, ",
            "o.lock_consignee_time, ",
            "nee.name as consignee_name, ",
            "nee.name_en as consignee_name_en, ",
            "nee.phone as consignee_phone, ",
            "nee.country_code as consignee_country_code, ",
            "nee.email as consignee_email, ",
            "occ.name, ",
            "occ.name_en, ",
            "occ.phone, ",
            "occ.email, ",
            "occ.dial_code ",
            "from ecw_order o ",
            "left join ecw_order_consignee nee on nee.order_id = o.order_id ",
            "left join ecw_order_cargo_control occ ",
            "on occ.order_id = o.order_id and occ.is_actual = 1 and occ.deleted = 0",
            "where o.deleted = 0",
            "and o.is_cargo_control = 1",
            "and o.order_id = #{orderId}",
            "</script>"
    })
    OrderCargoControlReleaseInfoDto getOrderCargoControlReleaseInfo(@Param("orderId") Long orderId);
}
