<?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(1) 
		FROM diag_product_order a join
		(SELECT DISTINCT order_num
		from diag_order_details where is_deleted='N' and audit_status=2) b on a.num=b.order_num
		where a.is_deleted='N'
    </select>

    <select id="selectAllProductOrder" resultMap="BaseResultWrapper">
        SELECT id , `num` , `user_id` , `time` , `audit_status` FROM diag_product_order WHERE is_deleted='N'
        <if test="product.num != null and product.num != ''">
            AND num like concat ('%',#{product.num},'%')
        </if>
        <if test="product.auditStatus != null">
            AND audit_status=#{product.auditStatus}
        </if>
        <if test="product.userId != null and product.userId.size > 0">
            <foreach item="product.userId" collection="product.userId" open="and(" separator="or" close=")" >
                 user_id = #{product.userId}
            </foreach>
        </if>
        <if test="product.startTime != null and product.endTime != null">
            AND time BETWEEN #{product.startTime} and #{product.endTime}
        </if>
        ORDER BY time DESC
    </select>
</mapper>
