<?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.invoice.dao.InvoiceMapper">

    <resultMap id="resultMap" type="com.cftech.invoice.model.Invoice">
        <id column="id" property="id"/>
        <result column="invoice_title" property="invoiceTitle"/>
        <result column="invoice_content" property="invoiceContent"/>
        <result column="unit_tax_number" property="unitTaxNumber"/>
        <result column="invoice_type" property="invoiceType"/>
        <result column="invoice_form" property="invoiceForm"/>
        <result column="clerk_id" property="clerkId"/>
        <result column="order_id" property="orderId"/>
        <result column="push_mode" property="pushMode"/>
        <result column="email" property="email"/>
        <result column="invoice_serial_num" property="invoiceSerialNum"/>
        <result column="product_id" property="productId"/>
        <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="operator" property="operator"/>
        <result column="openId" property="openId"/>
        <result column="number" property="number"/>
        <result column="invoice_code" property="invoiceCode"/>
        <result column="invoice_num" property="invoiceNum"/>
    </resultMap>

    <resultMap id="resultMapDetail" type="com.cftech.invoice.model.InvoiceDetail">
        <id column="id" property="id"/>
        <result column="number" property="number"/>
        <result column="createTime" property="createTime"/>
        <result column="confirm" property="confirm"/>
        <result column="status" property="status"/>
        <result column="totalAmount" property="totalAmount"/>
        <result column="productName" property="productName"/>
        <result column="format" property="format"/>
        <result column="unit" property="unit"/>
        <result column="price" property="price"/>
        <result column="productImg" property="productImg"/>
        <result column="isRs" property="isRs"/>
        <result column="invoiceTitle" property="invoiceTitle"/>
        <result column="invoiceType" property="invoiceType"/>
        <result column="invoiceContent" property="invoiceContent"/>
        <result column="invoiceStatus" property="invoiceStatus"/>
        <result column="invoiceCreateTime" property="invoiceCreateTime"/>
        <result column="invoice_code" property="invoiceCode"/>
        <result column="invoice_num" property="invoiceNum"/>
    </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">
                                    id,
                                    number,
                                                invoice_title,
                                                invoice_content,
                                                unit_tax_number,
                                                invoice_type,
                                                invoice_form,
                                                clerk_id,
                                                order_id,
                                                push_mode,
                                                email,
                                                invoice_serial_num,
                                                product_id,
                                                accounts_id,
                                                del_flag,
                                                status,
                                                create_time,
                                                update_time,
                                                description,
                                                create_by,
                                                update_by,
                                                operator,
                                                open_id,
                                                invoice_code,
                                                invoice_num
                        </sql>


    <insert id="save" parameterType="com.cftech.invoice.model.Invoice" useGeneratedKeys="true"
            keyProperty="id">
        insert into t_aidea_invoice
        (
        <include refid="sqlColumns"/>
        )
        values
        (
        #{id, jdbcType=BIGINT},
        #{number, jdbcType=VARCHAR},
        #{invoiceTitle, jdbcType=VARCHAR},
        #{invoiceContent, jdbcType=VARCHAR},
        #{unitTaxNumber, jdbcType=VARCHAR},
        #{invoiceType, jdbcType=VARCHAR},
        #{invoiceForm, jdbcType=VARCHAR},
        #{clerkId, jdbcType=VARCHAR},
        #{orderId, jdbcType=VARCHAR},
        #{pushMode, jdbcType=VARCHAR},
        #{email, jdbcType=VARCHAR},
        #{invoiceSerialNum, jdbcType=VARCHAR},
        #{productId, jdbcType=VARCHAR},
        #{accountsId, jdbcType=BIGINT},
        #{delFlag, jdbcType=TINYINT},
        #{status, jdbcType=VARCHAR},
        now(),
        now(),
        #{description, jdbcType=VARCHAR},
        #{createBy, jdbcType=BIGINT},
        #{updateBy, jdbcType=BIGINT},
        #{operator, jdbcType=VARCHAR},
        #{openId,  jdbcType=VARCHAR},
        #{invoiceCode,  jdbcType=VARCHAR},
        #{invoiceNum,  jdbcType=VARCHAR}
        )
    </insert>

    <select id="fetchById" parameterType="java.lang.Long" resultMap="resultMap">
        SELECT
        <include refid="sqlColumns"/>
        FROM t_aidea_invoice t
        WHERE t.id=#{id}
    </select>

    <select id="count" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT COUNT(1) FROM t_aidea_invoice
        <include refid="sqlWhere"/>

    </select>

    <select id="fetchSearchByPage" parameterType="java.util.Map" resultMap="resultMap">
        SELECT
        <include refid="sqlColumns"/>
        FROM t_aidea_invoice
        <include refid="sqlWhere"/>
        <if test="sort!=null">ORDER BY ${sort.param} ${sort.type}</if>
        <if test="limit>0">limit #{offset},#{limit}</if>
    </select>

    <select id="selectInvoiceDetail" parameterType="java.util.Map" resultMap="resultMapDetail">
        SELECT
        i.id id,
        o.number number,
        o.create_time createTime,
        o.confirm confirm,
        o.`status` STATUS,
        o.order_amount totalAmount,
        p.product_name productName,
        p.format format,
        p.unit unit,
        p.price price,
        p.product_img productImg,
        p.is_rs isRs,
        i.invoice_title invoiceTitle,
        i.invoice_type invoiceType,
        i.invoice_content invoiceContent,
        i.`status` invoiceStatus,
        i.create_time invoiceCreateTime,
        i.invoice_code invoiceCode,
        i.invoice_num invoiceNum
        FROM
        t_order o
        LEFT JOIN t_order_details d ON d.order_id = o.id
        AND d.del_flag = 0
        LEFT JOIN t_aidea_product p ON d.drugs_id = p.id
        AND p.del_flag = 0
        LEFT JOIN t_aidea_invoice i ON i.order_id = o.number
        AND i.del_flag = 0
        <include refid="sqlWhere"/>
        <if test="sort!=null">ORDER BY ${sort.param} ${sort.type}</if>
    </select>

    <update id="update" parameterType="com.cftech.invoice.model.Invoice">
        update t_aidea_invoice
        <set>
            <if test="id != null">
                id = #{id, jdbcType=BIGINT},
            </if>
            <if test="number != null">
                number = #{number, jdbcType=VARCHAR},
            </if>
            <if test="invoiceTitle != null">
                invoice_title = #{invoiceTitle, jdbcType=VARCHAR},
            </if>
            <if test="invoiceContent != null">
                invoice_content = #{invoiceContent, jdbcType=VARCHAR},
            </if>
            <if test="unitTaxNumber != null">
                unit_tax_number = #{unitTaxNumber, jdbcType=VARCHAR},
            </if>
            <if test="invoiceType != null">
                invoice_type = #{invoiceType, jdbcType=VARCHAR},
            </if>
            <if test="invoiceForm != null">
                invoice_form = #{invoiceForm, jdbcType=VARCHAR},
            </if>
            <if test="clerkId != null">
                clerk_id = #{clerkId, jdbcType=VARCHAR},
            </if>
            <if test="orderId != null">
                order_id = #{orderId, jdbcType=VARCHAR},
            </if>
            <if test="pushMode != null">
                push_mode = #{pushMode, jdbcType=VARCHAR},
            </if>
            <if test="email != null">
                email = #{email, jdbcType=VARCHAR},
            </if>
            <if test="invoiceSerialNum != null">
                invoice_serial_num = #{invoiceSerialNum, jdbcType=VARCHAR},
            </if>
            <if test="productId != null">
                product_id = #{productId, 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="operator != null">
                operator = #{operator,jdbcType=VARCHAR},
            </if>
            <if test="openId != null">
                open_id = #{openId,jdbcType=VARCHAR},
            </if>
            <if test="invoiceCode != null">
                invoice_code = #{invoiceCode,jdbcType=VARCHAR},
            </if>
            <if test="invoiceNum != null">
                invoice_num = #{invoiceNum,jdbcType=VARCHAR}
            </if>
        </set>
        where id=#{id,jdbcType=BIGINT}
    </update>

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