<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.diagbot.mapper.OrderDetailsMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.diagbot.entity.OrderDetails">
        <id column="id" property="id"/>
        <result column="is_deleted" property="isDeleted"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="gmt_modified" property="gmtModified"/>
        <result column="creator" property="creator"/>
        <result column="modifier" property="modifier"/>
        <result column="order_num" property="orderNum"/>
        <result column="product_id" property="productId"/>
        <result column="user_id" property="userId"/>
        <result column="audit_status" property="auditStatus"/>
        <result column="unit_price" property="unitPrice"/>
        <result column="status" property="status"/>
        <result column="reject_type" property="rejectType"/>
        <result column="reject_reason" property="rejectReason"/>
    </resultMap>

    <resultMap id="BaseResultWrapper" type="com.diagbot.entity.wrapper.OrderDetailsWapper">
        <id column="id" property="id"/>
        <result column="is_deleted" property="isDeleted"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="gmt_modified" property="gmtModified"/>
        <result column="creator" property="creator"/>
        <result column="modifier" property="modifier"/>
        <result column="order_num" property="orderNum"/>
        <result column="product_id" property="productId"/>
        <result column="user_id" property="userId"/>
        <result column="audit_status" property="auditStatus"/>
        <result column="unit_price" property="unitPrice"/>
        <result column="status" property="status"/>
        <result column="reject_type" property="rejectType"/>
        <result column="reject_reason" property="rejectReason"/>
        <result column="order_time" property="orderTime"/>
    </resultMap>
    <select id="seleAllOrderDetials" resultMap="BaseResultWrapper"
            parameterType="com.diagbot.entity.wrapper.OrderDetailsWapper">
        SELECT
        a.*, b.time order_time,c.`name` product_name
        FROM
        diag_order_details a
        LEFT JOIN diag_product_order b ON a.order_num = b.num
        LEFT JOIN diag_lantone_product c ON a.product_id =c.id
        WHERE 1=1
        <if test="status !=null and status !=''">
            AND status = #{status}
        </if>
        <if test="productId !=null and productId !=''">
            AND product_id = #{productId}
        </if>
        LIMIT #{index},#{size}
    </select>

    <select id="selectAllOrderDetialsByOrderNum" resultType="com.diagbot.entity.OrderDetailStatus">
        select
        (SELECT count(*) FROM `diag_order_details` where order_num = #{orderNum} and is_deleted = 'N') ct,
        (SELECT count(*) FROM `diag_order_details` where order_num = #{orderNum} and audit_status = 0 and is_deleted = 'N') not_pass,
        (SELECT count(*) FROM `diag_order_details` where order_num = #{orderNum} and audit_status = 1 and is_deleted = 'N') pass,
        (SELECT count(*) FROM `diag_order_details` where order_num = #{orderNum} and audit_status is null and is_deleted = 'N') not_audit
        from dual
    </select>
</mapper>
