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

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.approval.OrderApprovalDO;
import cn.iocoder.yudao.framework.mybatis.core.vo.PageVO;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.order.vo.approval.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;

/**
 * 订单相关审核详情 Mapper
 *
 * @author 郑屹
 */
@Mapper
public interface OrderApprovalMapper extends AbstractMapper<OrderApprovalDO> {
    @Override
    default PageResult<OrderApprovalDO> selectPage(PageVO page, Object object) {
        if (object instanceof OrderApprovalQueryVO) {
            OrderApprovalQueryVO vo = (OrderApprovalQueryVO) object;
            return selectPage(page, new LambdaQuery<OrderApprovalDO>()
                    .eqIfPresent(OrderApprovalDO::getOrderId, vo.getOrderId())
                    .eqIfPresent(OrderApprovalDO::getFormId, vo.getFormId())
                    .eqIfPresent(OrderApprovalDO::getType, vo.getType())
                    .eqIfPresent(OrderApprovalDO::getFollowUpSalesmanId, vo.getFollowUpSalesmanId())
                    .eqIfPresent(OrderApprovalDO::getDetails, vo.getDetails())
                    .eqIfPresent(OrderApprovalDO::getStatus, vo.getStatus())
                    .eqIfPresent(OrderApprovalDO::getProcessingResults, vo.getProcessingResults())
                    .betweenIfPresent(OrderApprovalDO::getCreateTime, vo.getBeginCreateTime(), vo.getEndCreateTime())
                    .eqIfPresent(OrderApprovalDO::getFinishBy, vo.getFinishBy())
                    .betweenIfPresent(OrderApprovalDO::getFinishTime, vo.getBeginFinishTime(), vo.getEndFinishTime())
                    .orderByDesc(OrderApprovalDO::getOrderApprovalId));
        }

        return null;
    }

    @Override
    default List<OrderApprovalDO> selectList(Object object) {
        if (object instanceof OrderApprovalQueryVO) {
            OrderApprovalQueryVO vo = (OrderApprovalQueryVO) object;
            return selectList(new LambdaQuery<OrderApprovalDO>()
                    .eqIfPresent(OrderApprovalDO::getOrderId, vo.getOrderId())
                    .eqIfPresent(OrderApprovalDO::getFormId, vo.getFormId())
                    .eqIfPresent(OrderApprovalDO::getType, vo.getType())
                    .eqIfPresent(OrderApprovalDO::getFollowUpSalesmanId, vo.getFollowUpSalesmanId())
                    .eqIfPresent(OrderApprovalDO::getDetails, vo.getDetails())
                    .eqIfPresent(OrderApprovalDO::getStatus, vo.getStatus())
                    .eqIfPresent(OrderApprovalDO::getProcessingResults, vo.getProcessingResults())
                    .betweenIfPresent(OrderApprovalDO::getCreateTime, vo.getBeginCreateTime(), vo.getEndCreateTime())
                    .eqIfPresent(OrderApprovalDO::getFinishBy, vo.getFinishBy())
                    .betweenIfPresent(OrderApprovalDO::getFinishTime, vo.getBeginFinishTime(), vo.getEndFinishTime())
                    .orderByDesc(OrderApprovalDO::getOrderApprovalId));
        }

        return null;
    }

    @ResultType(OrderApprovalDO.class)
    @Select({
            "<script>",
            "select",
            " * ",
            "from ecw_order_approval ",
            "where ",
            "order_id = #{orderId} ",
            "and type = #{type} ",
            "<when test = 'orderItemId != null'>",
            "and details->>'$.orderItemId' = #{orderItemId} ",
            "</when>",
            "order by order_approval_id desc",
            "limit 1",
            "</script>"
    })
    OrderApprovalDO newItemInfo(@Param("orderItemId") Long orderItemId, @Param("orderId") Long orderId, @Param("type") Integer type);


    @ResultType(OrderApprovalDO.class)
    @Select({
            "<script>",
            "select",
            " * ",
            "from ecw_order_approval ",
            "where ",
            "order_id = #{orderId} ",
            "and type in #{type} ",
            "<when test = 'orderItemId != null'>",
            "and details->>'$.orderItemId' = #{orderItemId} ",
            "</when>",
            "order by order_approval_id desc",
            "limit 1",
            "</script>"
    })
    OrderApprovalDO itemApprovalCount(@Param("orderItemId") Long orderItemId, @Param("orderId") Long orderId, @Param("types") List<Integer> types);


    @ResultType(Integer.class)
    @Select({
            "<script>",
            "select",
            " count(1)",
            "from ecw_order_approval ",
            "where ",
            "status = 1 ",
            "and order_id = #{orderId} ",
            "<when test = 'types != null'>",
            "AND type in ",
            "<foreach item='type' index='index' collection='types' open='(' separator=',' close=')'>#{type}</foreach>",
            "</when>",
            "<when test = 'orderItemId != null'>",
            "and details->>'$.orderItemId' = #{orderItemId} ",
            "</when>",
            "</script>"
    })
    int processingItemApproval(@Param("orderItemId") Long orderItemId, @Param("orderId") Long orderId, @Param("types") List<Integer> types);

    /**
     * 根据订单id和类型查询未处理的审批
     *
     * @param orderId
     * @param types
     * @return
     */
    @ResultType(OrderApprovalDO.class)
    @Select({
            "<script>",
            "select",
            " * ",
            "from ecw_order_approval ",
            "where ",
            "status = 1 ",
            "<when test = 'orderId != null'>",
            "and order_id = #{orderId} ",
            "</when>",
            "<when test = 'types != null'>",
            "AND type in ",
            "<foreach item='type' index='index' collection='types' open='(' separator=',' close=')'>#{type}</foreach>",
            "</when>",
            "limit 1",
            "</script>"
    })
    OrderApprovalDO selectProcessingApproval(@Param("orderId") Long orderId, @Param("types") List<Integer> types);

    @ResultType(OrderApprovalDO.class)
    @Select({
            "<script>",
            "select",
            " * ",
            "from ecw_order_approval ",
            "where ",
//            "status = 1 ",
            "order_id = #{orderId} ",
            "<when test = 'id != null'>",
            "and details->>'$.cargoControlPickId' = #{id} ",
            "</when>",
            "and type = #{type} ",
            "order by order_approval_id desc",
            "limit 1",
            "</script>"
    })
    OrderApprovalDO newOrderCargoControlPickInfo(@Param("orderId") Long orderId, @Param("id") Long id, @Param("type") Integer type);


    @ResultType(OrderApprovalDO.class)
    @Select({
            "<script>",
            "select",
            " * ",
            "from ecw_order_approval ",
            "where ",
            "order_id = #{orderId} ",
            "and type = #{type} ",
            "order by order_approval_id desc",
            "limit 1",
            "</script>"
    })
    OrderApprovalDO orderApprovalInfoByOrderIdAndType(@Param("orderId") Long orderId, @Param("type") Integer type);
    @ResultType(OrderApprovalDO.class)
    @Select({
            "<script>",
            "select",
            " a.* ,",
            " u.nickname as creator_name",
            "from ecw_order_approval a",
            "left join system_user u ",
            "on u.id = a.creator",
            "where ",
            "a.order_id = #{orderId} ",
            "and a.type in <foreach item='type' index='index' collection='typeList' open='(' separator=',' close=')'>#{type}</foreach> ",
            "<when test = 'status != null'>",
            "and a.status = #{status} ",
            "</when>",
            "order by a.order_approval_id desc",
            "</script>"
    })
    List<OrderApprovalDO> approvalListBytTypeAndStatus(@Param("orderId") Long orderId, @Param("typeList") List<Integer> typeList, @Param("status") Integer status);


    @ResultType(Long.class)
    @Select({
            "<script>",
            "select",
            " count(1),",
            "from ecw_order_approval a",
            "where ",
            "a.order_id = #{orderId} ",
            "<when test = 'types != null and types.size() > 0'>",
            "and a.type in <foreach item='type' index='index' collection='typeList' open='(' separator=',' close=')'>#{type}</foreach> ",
            "</when>",
            "<when test = 'orderItemIdList != null and orderItemIdList.size() > 0'>",
            "and (a.details->>'$.orderItemId' is null or (a.details->>'$.orderItemId' is not null and a.details->>'$.orderItemId' in <foreach item='orderItemId' index='index' collection='orderItemIdList' open='(' separator=',' close=')'>#{orderItemId}</foreach>)) ",
            "</when>",
            "and a.status = 1 ",
            "</script>"
    })
    long countProcessingApproval(@Param("orderId") Long orderId, @Param("orderItemIdList") List<Long> orderItemIdList, @Param("types") List<Integer> types);
}
