<?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.cftech.orderdetail.dao.OrderDetailsMapper">

    <resultMap id="resultMap" type="com.cftech.orderdetail.model.OrderDetails">
        <id column="id" property="id"/>
        <result column="order_id" property="orderId"/>
        <result column="order_code" property="orderCode"/>
        <result column="drugs_id" property="drugsId"/>
        <result column="openid" property="openid"/>
        <result column="price" property="price"/>
        <result column="amount" property="amount"/>
        <result column="drugs_code" property="drugsCode"/>
        <result column="drugs_sku" property="drugsSku"/>
        <result column="drugs_materiel" property="drugsMateriel"/>
        <result column="accounts_id" property="accountsId"/>
        <result column="del_flag" property="delFlag"/>
        <result column="status" property="status"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="description" property="description"/>
        <result column="create_by" property="createBy"/>
        <result column="update_by" property="updateBy"/>
        <result column="product_name" property="productName"/>
    </resultMap>

    <sql id="sqlWhere">
        <if test="conds!=null">
            <trim prefix="WHERE" prefixOverrides="AND|OR">
                <foreach collection="conds.conds" index="index" item="cond">
                    ${cond.linkType}
                    <if test="cond.condType == 'EQUAL'">${cond.param} = #{cond.value}</if>
                    <if test="cond.condType == 'NOTEQUAL'">${cond.param} &lt;&gt; #{cond.value}</if>
                    <if test="cond.condType == 'GREATEQUAL'">${cond.param} &gt;= #{cond.value}</if>
                    <if test="cond.condType == 'GREATTHAN'">${cond.param} &gt; #{cond.value}</if>
                    <if test="cond.condType == 'LESSEQUAL'">${cond.param} &lt;= #{cond.value}</if>
                    <if test="cond.condType == 'LESSTHAN'">${cond.param} &lt; #{cond.value}</if>
                    <if test="cond.condType == 'BETWEEN'">${cond.param} BETWEEN #{cond.startValue} AND
                        #{cond.endValue}
                    </if>
                    <if test="cond.condType == 'ISNULL'">${cond.param} IS NULL</if>
                    <if test="cond.condType == 'NOTNULL'">${cond.param} IS NOT NULL</if>
                    <if test="cond.condType == 'LIKE'">${cond.param} LIKE #{cond.value}</if>
                    <if test="cond.condType == 'IN'">${cond.param} IN
                        <foreach item="item" index="index" collection="cond.value" open="(" separator="," close=")">
                            #{item}
                        </foreach>
                    </if>
                </foreach>
            </trim>
        </if>
    </sql>

    <sql id="sqlColumns">
   	d.id,
	d.order_id,
	d.order_code,
	d.drugs_id,
	d.drugs_num,
	d.openid,
	d.price,
	d.amount,
	d.drugs_code,
	d.drugs_sku,
	d.drugs_materiel,
	d.accounts_id,
	d.del_flag,
	d.STATUS,
	d.create_time,
	d.update_time,
	d.description,
	d.create_by,
	d.update_by,
	t.product_name
                        </sql>


    <insert id="save" parameterType="com.cftech.orderdetail.model.OrderDetails" useGeneratedKeys="true"
            keyProperty="id">
        insert into t_order
        (
        <include refid="sqlColumns"/>
        )
        values
        (
        #{id, jdbcType=BIGINT},
        #{orderId, jdbcType=BIGINT},
        #{orderCode, jdbcType=VARCHAR},
        #{drugsId, jdbcType=BIGINT},
        #{drugsNum, jdbcType=BIGINT},
        #{openid, jdbcType=VARCHAR},
        #{price, jdbcType=CHAR},
        #{amount, jdbcType=CHAR},
        #{drugsCode, jdbcType=CHAR},
        #{drugsSku, jdbcType=VARCHAR},
        #{drugsMateriel, jdbcType=VARCHAR},
        #{accountsId, jdbcType=BIGINT},
        #{delFlag, jdbcType=TINYINT},
        #{status, jdbcType=VARCHAR},
        now(),
        now(),
        #{description, jdbcType=VARCHAR},
        #{createBy, jdbcType=BIGINT},
        #{updateBy, jdbcType=BIGINT},
        #{drugsName, jdbcType=VARCHAR}
        )
    </insert>

    <select id="fetchById" parameterType="java.lang.Long" resultMap="resultMap">
        SELECT
        <include refid="sqlColumns"/>
        FROM t_order_details d
        LEFT JOIN t_aidea_product t ON t.id = d.drugs_id
        WHERE d.id=#{id}
    </select>

    <select id="count" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT COUNT(1) FROM t_order_details d
        LEFT JOIN t_order o ON o.id = d.order_id
        LEFT JOIN t_aidea_product t ON d.drugs_id = t.id
        <include refid="sqlWhere"/>

    </select>

    <select id="fetchSearchByPage" parameterType="java.util.Map" resultMap="resultMap">
        SELECT
          <include refid="sqlColumns"/>
        FROM t_order_details d
        LEFT JOIN t_order o ON o.id = d.order_id
        LEFT JOIN t_aidea_product t ON d.drugs_id = t.id
        <include refid="sqlWhere"/>
        <if test="userId!=null"> AND (o.service_id = ${userId} OR o.clerk_id = ${userId} OR o.doctor_id = ${userId}) </if>
        <if test="sort!=null">ORDER BY ${sort.param} ${sort.type}</if>
        <if test="limit>0">limit #{offset},#{limit}</if>

    </select>

    <update id="update" parameterType="com.cftech.orderdetail.model.OrderDetails">
        update t_order
        <set>
            <if test="id != null">
                id = #{id, jdbcType=BIGINT},
            </if>
            <if test="orderId != null">
                order_id = #{orderId, jdbcType=BIGINT},
            </if>
            <if test="orderCode != null">
                order_code = #{orderCode, jdbcType=VARCHAR},
            </if>
            <if test="drugsId != null">
                drugs_id = #{drugsId, jdbcType=BIGINT},
            </if>
            <if test="drugsNum != null">
                drugs_num = #{drugsNum, jdbcType=BIGINT},
            </if>
            <if test="openid != null">
                openid = #{openid, jdbcType=VARCHAR},
            </if>
            <if test="price != null">
                price = #{price, jdbcType=CHAR},
            </if>
            <if test="amount != null">
                amount = #{amount, jdbcType=CHAR},
            </if>
            <if test="drugsCode != null">
                drugs_code = #{drugsCode, jdbcType=CHAR},
            </if>
            <if test="drugsSku != null">
                drugs_sku = #{drugsSku, jdbcType=VARCHAR},
            </if>
            <if test="drugsMateriel != null">
                drugs_materiel = #{drugsMateriel, jdbcType=VARCHAR},
            </if>
            <if test="accountsId != null">
                accounts_id = #{accountsId, jdbcType=BIGINT},
            </if>
            <if test="delFlag != null">
                del_flag = #{delFlag, jdbcType=TINYINT},
            </if>
            <if test="status != null">
                status = #{status, jdbcType=VARCHAR},
            </if>
            <if test="createTime != null">
                create_time = #{createTime, jdbcType=TIMESTAMP},
            </if>
            <if test="description != null">
                description = #{description, jdbcType=VARCHAR},
            </if>
            <if test="createBy != null">
                create_by = #{createBy, jdbcType=BIGINT},
            </if>
            <if test="updateBy != null">
                update_by = #{updateBy, jdbcType=BIGINT},
            </if>
            <if test="drugsName != null">
                drugs_name = #{drugsName, jdbcType=VARCHAR},
            </if>
        </set>
        where id=#{id,jdbcType=BIGINT}
    </update>

    <update id="delete" parameterType="java.lang.Long">
        update t_order set del_flag=1 where id=#{id,jdbcType=BIGINT}
    </update>
</mapper>