<?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">
        SELECT
        a.id,a.user_id,a.audit_status,a.`status`,a.reject_type,a.reject_reason , b.time order_time,c.`name` product_name,a.order_num
        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 a.is_deleted = 'N'
        <if test="orderNum != null and orderNum.size > 0">
            <foreach item="orderNum" collection="orderNum" open="and(" separator="or" close=")" >
                a.order_num = #{orderNum}
            </foreach>
        </if>
        <if test="productId != null and productId !=''">
            AND product_id = #{productId}
        </if>
    </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>

    <insert id="saveOrderDetails" parameterType="java.util.List">
        <if test="list.size !=0 ">
            INSERT INTO `diag_order_details` (`gmt_create`, `creator`, `order_num`, `product_id`, `user_id`) VALUES
            <foreach collection="list" item="demo" index="index" separator=",">
                 (#{demo.gmtCreate}, #{demo.creator}, #{demo.orderNum}, #{demo.productId}, #{demo.userId})
            </foreach>
        </if>
    </insert>

    <select id="getOrderDetailsByUser" resultType="java.lang.Long">
        SELECT a.product_id FROM diag_order_details a WHERE a.is_deleted ='N' and a.user_id = #{userId}
    </select>
</mapper>
