<?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.ProductOrderMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.diagbot.entity.ProductOrder">
        <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="num" property="num"/>
        <result column="user_id" property="userId"/>
        <result column="time" property="time"/>
        <result column="total_prices" property="totalPrices"/>
    </resultMap>

    <resultMap id="BaseResultWrapper" type="com.diagbot.entity.wrapper.ProductOrderWrapper">
        <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="num" property="num"/>
        <result column="user_id" property="userId"/>
        <result column="time" property="time"/>
        <result column="total_prices" property="totalPrices"/>
        <result column="audit_status" property="auditStatus"/>
    </resultMap>

    <select id="getUserProductOrder" parameterType="Long" resultMap="BaseResultMap">
    	select *
    	from diag_product_order
    	where is_deleted = 'N' and user_id=#{userId}
    </select>

    <select id="getOrderDetails" parameterType="string" resultType="com.diagbot.dto.AuthDetailDTO">
		select
			b.audit_status as auditStatus,
			c.name as name,
			b.reject_reason as rejectReason
		from diag_product_order a join diag_order_details b on a.num=b.order_num
		join diag_lantone_product c on b.product_id=c.id
		where a.is_deleted = 'N' and  b.is_deleted = 'N' and  c.is_deleted = 'N'
		and a.num=#{orderNum}
	</select>

    <select id="getOrderDetailsByNums" parameterType="list"
            resultType="com.diagbot.dto.AuthDetailDTO">
        select
        a.num as num,
        b.audit_status as auditStatus,
        c.name as name,
        b.reject_reason as rejectReason
        from diag_product_order a join diag_order_details b on a.num=b.order_num
        join diag_lantone_product c on b.product_id=c.id
        where a.is_deleted = 'N' and b.is_deleted = 'N' and c.is_deleted = 'N'
        and a.num in
        <foreach collection="list" separator="," open="(" close=")" item="num">
            #{num}
        </foreach>
    </select>

    <select id="selctOrderTimeByOrderNum" resultMap="BaseResultMap"
            parameterType="com.diagbot.entity.ProductOrder">
        SELECT * FROM diag_product_order d WHERE d.is_deleted='N'
        <if test="num != null and num != ''">
            AND num=#{num}
        </if>
    </select>

    <select id="waitExamOrderCou" resultType="int">
    	SELECT
			count(DISTINCT a.num)
		from diag_product_order a join diag_order_details b on a.num=b.order_num
		where b.audit_status=2
    </select>

    <select id="selectAllProductOrder" resultMap="BaseResultWrapper"
            parameterType="com.diagbot.entity.ProductOrderIndex">
        SELECT * FROM diag_product_order WHERE is_deleted='N'
        <if test="num != null and num != ''">
            AND num=#{num}
        </if>
        <if test="auditStatus != null and auditStatus != ''">
            AND audit_status=#{auditStatus}
        </if>
        <if test="userId != null and userId.size > 0">
            AND user_id IN
            <foreach item="userId" collection="userId" open="(" separator="," close=")">
                #{userId}
            </foreach>
        </if>
        LIMIT #{index},#{size}
    </select>

</mapper>
