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

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.orderCargoControlPick.OrderCargoControlPickDO;
import cn.iocoder.yudao.framework.mybatis.core.vo.PageVO;
import cn.iocoder.yudao.module.order.dto.OrderCargoControlReleaseInfoDto;
import org.apache.ibatis.annotations.*;
import cn.iocoder.yudao.module.order.vo.orderCargoControlPick.*;

/**
 * 订单控货人放货记录 Mapper
 *
 * @author 郑屹
 */
@Mapper
public interface OrderCargoControlPickMapper extends AbstractMapper<OrderCargoControlPickDO> {

    @Override
    default PageResult<OrderCargoControlPickDO> selectPage(PageVO page, Object object) {
        if (object instanceof OrderCargoControlPickQueryVO) {
            OrderCargoControlPickQueryVO vo = (OrderCargoControlPickQueryVO) object;
            return selectPage(page, new LambdaQuery<OrderCargoControlPickDO>()
                    .eqIfPresent(OrderCargoControlPickDO::getOrderId, vo.getOrderId())
                    .eqIfPresent(OrderCargoControlPickDO::getOrderNo, vo.getOrderNo())
                    .eqIfPresent(OrderCargoControlPickDO::getTidanNo, vo.getTidanNo())
                    .eqIfPresent(OrderCargoControlPickDO::getPickNum, vo.getPickNum())
                    .eqIfPresent(OrderCargoControlPickDO::getPickQuantity, vo.getPickQuantity())
                    .eqIfPresent(OrderCargoControlPickDO::getPickVolume, vo.getPickVolume())
                    .eqIfPresent(OrderCargoControlPickDO::getPickWeight, vo.getPickWeight())
                    .eqIfPresent(OrderCargoControlPickDO::getPickId, vo.getPickId())
                    .eqIfPresent(OrderCargoControlPickDO::getStatus, vo.getStatus())
                    .eqIfPresent(OrderCargoControlPickDO::getRemarks, vo.getRemarks())
                    .eqIfPresent(OrderCargoControlPickDO::getName, vo.getName())
                    .eqIfPresent(OrderCargoControlPickDO::getNameEn, vo.getNameEn())
                    .eqIfPresent(OrderCargoControlPickDO::getPhone, vo.getPhone())
                    .eqIfPresent(OrderCargoControlPickDO::getEmail, vo.getEmail())
                    .eqIfPresent(OrderCargoControlPickDO::getCompany, vo.getCompany())
                    .eqIfPresent(OrderCargoControlPickDO::getCompanyEn, vo.getCompanyEn())
                    .eqIfPresent(OrderCargoControlPickDO::getCountryCode, vo.getCountryCode())
                    .eqIfPresent(OrderCargoControlPickDO::getConsigneeName, vo.getConsigneeName())
                    .eqIfPresent(OrderCargoControlPickDO::getConsigneeNameEn, vo.getConsigneeNameEn())
                    .eqIfPresent(OrderCargoControlPickDO::getConsigneePhone, vo.getConsigneePhone())
                    .eqIfPresent(OrderCargoControlPickDO::getConsigneeEmail, vo.getConsigneeEmail())
                    .eqIfPresent(OrderCargoControlPickDO::getConsigneeCountryCode, vo.getConsigneeCountryCode())
                    .betweenIfPresent(OrderCargoControlPickDO::getCreateTime, vo.getBeginCreateTime(), vo.getEndCreateTime())
                    .orderByDesc(OrderCargoControlPickDO::getId));
        }

        return null;
    }

    @Override
    default List<OrderCargoControlPickDO> selectList(Object object) {
        if (object instanceof OrderCargoControlPickQueryVO) {
            OrderCargoControlPickQueryVO vo = (OrderCargoControlPickQueryVO) object;
            return selectList(new LambdaQuery<OrderCargoControlPickDO>()
                    .eqIfPresent(OrderCargoControlPickDO::getOrderId, vo.getOrderId())
                    .eqIfPresent(OrderCargoControlPickDO::getOrderNo, vo.getOrderNo())
                    .eqIfPresent(OrderCargoControlPickDO::getTidanNo, vo.getTidanNo())
                    .eqIfPresent(OrderCargoControlPickDO::getPickNum, vo.getPickNum())
                    .eqIfPresent(OrderCargoControlPickDO::getPickQuantity, vo.getPickQuantity())
                    .eqIfPresent(OrderCargoControlPickDO::getPickVolume, vo.getPickVolume())
                    .eqIfPresent(OrderCargoControlPickDO::getPickWeight, vo.getPickWeight())
                    .eqIfPresent(OrderCargoControlPickDO::getPickId, vo.getPickId())
                    .eqIfPresent(OrderCargoControlPickDO::getStatus, vo.getStatus())
                    .eqIfPresent(OrderCargoControlPickDO::getRemarks, vo.getRemarks())
                    .eqIfPresent(OrderCargoControlPickDO::getName, vo.getName())
                    .eqIfPresent(OrderCargoControlPickDO::getNameEn, vo.getNameEn())
                    .eqIfPresent(OrderCargoControlPickDO::getPhone, vo.getPhone())
                    .eqIfPresent(OrderCargoControlPickDO::getEmail, vo.getEmail())
                    .eqIfPresent(OrderCargoControlPickDO::getCompany, vo.getCompany())
                    .eqIfPresent(OrderCargoControlPickDO::getCompanyEn, vo.getCompanyEn())
                    .eqIfPresent(OrderCargoControlPickDO::getCountryCode, vo.getCountryCode())
                    .eqIfPresent(OrderCargoControlPickDO::getConsigneeName, vo.getConsigneeName())
                    .eqIfPresent(OrderCargoControlPickDO::getConsigneeNameEn, vo.getConsigneeNameEn())
                    .eqIfPresent(OrderCargoControlPickDO::getConsigneePhone, vo.getConsigneePhone())
                    .eqIfPresent(OrderCargoControlPickDO::getConsigneeEmail, vo.getConsigneeEmail())
                    .eqIfPresent(OrderCargoControlPickDO::getConsigneeCountryCode, vo.getConsigneeCountryCode())
                    .betweenIfPresent(OrderCargoControlPickDO::getCreateTime, vo.getBeginCreateTime(), vo.getEndCreateTime())
                    .orderByDesc(OrderCargoControlPickDO::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.marks, ",
            "o.`status`, ",
            "o.abnormal_state, ",
            "o.in_warehouse_state, ",
            "o.shipment_state, ",
            "o.pick_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_time`, ",
            "o.`lock_consignee_day`, ",
            "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>"
    })
    @Results({
            @Result(property = "orderId", column = "order_id"),
            @Result(property = "cargoControlPickBackVOList", column = "order_id",
                    many = @Many(select = "cn.iocoder.yudao.module.order.dal.mysql.orderCargoControlPick.OrderCargoControlPickMapper.getOrderCargoControlPickByOrderId"))
    })
    OrderCargoControlReleaseInfoDto getOrderCargoControlReleaseInfo(@Param("orderId") Long orderId);


    @ResultType(OrderCargoControlPickBackVO.class)
    @Select({
            "<script>",
            "select ccp.*, ",
            "(select form_id from ecw_order_approval oa where oa.order_id = #{orderId} and oa.type in(7, 8, 9, 10) order by oa.order_approval_id desc limit 1) as form_id",
            "from ecw_order_cargo_control_pick ccp ",
            "where ccp.order_id = #{orderId}",
            "and ccp.deleted = 0",
            "order by ccp.id DESC ",
            "</script>"
    })
    List<OrderCargoControlPickBackVO> getOrderCargoControlPickByOrderId(@Param("orderId") Long orderId);

    @ResultType(Integer.class)
    @Select("SELECT IFNULL(a.num,0) FROM (select SUM(pick_num) as num FROM ecw_order_cargo_control_pick where order_id = #{orderId} and status in(3,4)) as a ")
    int getPickNumByOrderId(@Param("orderId") Long orderId);

}
