<?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="MdeInvoiceTitleMapper">
    
	<sql id="mdeInvoiceTitleColumns">
		a.inv_id AS "invId",
		a.user_id AS "userCode",
		a.inv_title AS "invTitle",
		a.taxation_no AS "taxationNo",
		a.ltd_add AS "ltdAdd",
		a.ltd_tel AS "ltdTel",
		a.opening_bank AS "openingBank",
		a.account_number AS "accountNumber",
		a.inv_content AS "invContent",
		a.inv_money AS "invMoney",
		a.recipient_name AS "recipientName",
		a.recipient_tel AS "recipientTel",
		a.recipient_add AS "recipientAdd",
		a.inv_type AS "invType",
		a.version_no AS "versionNo",
		a.remarks AS "remarks",
		a.del_flag AS "delFlag",
		a.create_by AS "createBy",
		a.create_date AS "createDate",
		a.update_by AS "updateBy",
		a.update_date AS "updateDate" 
	</sql>
	
	<sql id="mdeInvoiceTitleJoins">
	</sql>
    
	<select id="get" resultType="com.wechat.model.dbEntity.MdeInvoiceTitle">
		SELECT 
			<include refid="mdeInvoiceTitleColumns"/>
		FROM mde_invoice_title a
		<include refid="mdeInvoiceTitleJoins"/>
		WHERE a.inv_id = #{invId}
	</select>
	
	<select id="getInvoiceByUserCode" resultType="com.wechat.model.dbEntity.MdeInvoiceTitle">
		SELECT 
			<include refid="mdeInvoiceTitleColumns"/>
		FROM mde_invoice_title a
		<include refid="mdeInvoiceTitleJoins"/>
		WHERE a.user_id = #{userCode} limit 1
	</select>
	
	<select id="findList" resultType="com.wechat.model.dbEntity.MdeInvoiceTitle">
		SELECT 
			<include refid="mdeInvoiceTitleColumns"/>
		FROM mde_invoice_title a
		<include refid="mdeInvoiceTitleJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY #{page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>
	
	<select id="findAllList" resultType="com.wechat.model.dbEntity.MdeInvoiceTitle">
		SELECT 
			<include refid="mdeInvoiceTitleColumns"/>
		FROM mde_invoice_title a
		<include refid="mdeInvoiceTitleJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
		</where>		
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY #{page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>
	
	<insert id="insert">
		INSERT INTO mde_invoice_title(
			user_id,
			inv_title,
			taxation_no,
			ltd_add,
			ltd_tel,
			opening_bank,
			account_number,
			inv_content,
			inv_money,
			recipient_name,
			recipient_tel,
			recipient_add,
			inv_type,
			remarks,
			create_by,
			create_date,
			update_by,
			update_date
		) 
		select 
			#{userCode},
			#{invTitle},
			#{taxationNo},
			#{ltdAdd},
			#{ltdTel},
			#{openingBank},
			#{accountNumber},
			#{invContent},
			price,
			#{recipientName},
			#{recipientTel},
			#{recipientAdd},
			#{invType},
			#{remarks},
			#{createBy},
			now(),
			#{updateBy},
			now()
			from mde_orders where order_id = #{orderId}
		
	</insert>
	 
	<update id="update">
		UPDATE mde_invoice_title SET 	
			user_id = #{userCode},
			inv_title = #{invTitle},
			taxation_no = #{taxationNo},
			ltd_add = #{ltdAdd},
			ltd_tel = #{ltdTel},
			opening_bank = #{openingBank},
			account_number = #{accountNumber},
			inv_content = #{invContent},
			inv_money = #{invMoney},
			recipient_name = #{recipientName},
			recipient_tel = #{recipientTel},
			recipient_add = #{recipientAdd},
			inv_type = #{invType},
			version_no = version_no+1,
			remarks = #{remarks},
			update_by = #{updateBy},
			update_date = #{updateDate}
		WHERE inv_id = #{invId} and version_no = #{versionNo}
	</update>
	
	<update id="delete">
		UPDATE mde_invoice_title SET 
			del_flag = #{DEL_FLAG_DELETE},
			version_no = version_no+1
		WHERE inv_id = #{invId} and version_no = #{versionNo}
	</update>
	<update id="deleteAllByUserCode">
		delete from mde_invoice_title WHERE user_id = #{userCode}
	</update> 
	
</mapper>