<?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="cn.superdesk.uniorder.dao.mapper.organisation.SysEnterpriseExtInvoiceMapperEx">

	<insert id="insertSelective" parameterType="cn.superdesk.uniorder.dao.entity.organisation.SysEnterpriseExtInvoice">

		<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
			SELECT LAST_INSERT_ID()
		</selectKey>
		insert into sys_enterprise_ext_invoice
		<trim prefix="(" suffix=")" suffixOverrides=",">

			<if test="id != null">
				id,
			</if>
			<if test="serviceNo != null">
				service_no,
			</if>
			<if test="serialNumber != null">
				serial_number,
			</if>
			<if test="sysOrgId != null">
				sys_org_id,
			</if>
			<if test="sysVirtId != null">
				sys_virt_id,
			</if>
			<if test="outOrgId != null">
				out_org_id,
			</if>
			<if test="outVirtId != null">
				out_virt_id,
			</if>
			<if test="invoiceState != null">
				invoice_state,
			</if>
			<if test="invoiceType != null">
				invoice_type,
			</if>
			<if test="selectedInvoiceTitle != null">
				selected_invoice_title,
			</if>
			<if test="realname != null">
				realname,
			</if>
			<if test="companyName != null">
				company_name,
			</if>
			<if test="taxpayersIDCode != null">
				taxpayers_i_d_code,
			</if>
			<if test="invoiceContent != null">
				invoice_content,
			</if>
			<if test="invoiceBankAccountNumber != null">
				invoice_bank_account_number,
			</if>
			<if test="invoiceBank != null">
				invoice_bank,
			</if>
			<if test="invoiceAccount != null">
				invoice_account,
			</if>
			<if test="invoiceAddressPhone != null">
				invoice_address_phone,
			</if>
			<if test="invoiceAddress != null">
				invoice_address,
			</if>
			<if test="invoicePhone != null">
				invoice_phone,
			</if>
			<if test="invoiceProvice != null">
				invoice_provice,
			</if>
			<if test="invoiceCity != null">
				invoice_city,
			</if>
			<if test="invoiceCounty != null">
				invoice_county,
			</if>
			<if test="invoiceName != null">
				invoice_name,
			</if>
			<if test="invoiceEmail != null">
				invoice_email,
			</if>
			<if test="contactsName != null">
				contacts_name,
			</if>
			<if test="contactsEmail != null">
				contacts_email,
			</if>
			<if test="contactsPhone != null">
				contacts_phone,
			</if>
			<if test="remark != null">
				remark,
			</if>
			<if test="isDefault != null">
				is_default,
			</if>
			<if test="deleted != null">
				deleted,
			</if>
			<if test="enabled != null">
				enabled,
			</if>
			<if test="createTime != null">
				create_time,
			</if>
			<if test="createUserId != null">
				create_user_id,
			</if>
			<if test="updateTime != null">
				update_time,
			</if>
			<if test="updateUserId != null">
				update_user_id,
			</if>
		</trim>
		<trim prefix="values (" suffix=")" suffixOverrides=",">
			<if test="id != null">
				#{id,jdbcType=BIGINT},
			</if>
			<if test="serviceNo != null">
				#{serviceNo,jdbcType=INTEGER},
			</if>
			<if test="serialNumber != null">
				#{serialNumber,jdbcType=VARCHAR},
			</if>
			<if test="sysOrgId != null">
				#{sysOrgId,jdbcType=BIGINT},
			</if>
			<if test="sysVirtId != null">
				#{sysVirtId,jdbcType=BIGINT},
			</if>
			<if test="outOrgId != null">
				#{outOrgId,jdbcType=BIGINT},
			</if>
			<if test="outVirtId != null">
				#{outVirtId,jdbcType=BIGINT},
			</if>
			<if test="invoiceState != null">
				#{invoiceState,jdbcType=INTEGER},
			</if>
			<if test="invoiceType != null">
				#{invoiceType,jdbcType=INTEGER},
			</if>
			<if test="selectedInvoiceTitle != null">
				#{selectedInvoiceTitle,jdbcType=INTEGER},
			</if>
			<if test="realname != null">
				#{realname,jdbcType=VARCHAR},
			</if>
			<if test="companyName != null">
				#{companyName,jdbcType=VARCHAR},
			</if>
			<if test="taxpayersIDCode != null">
				#{taxpayersIDCode,jdbcType=VARCHAR},
			</if>
			<if test="invoiceContent != null">
				#{invoiceContent,jdbcType=INTEGER},
			</if>
			<if test="invoiceBankAccountNumber != null">
				#{invoiceBankAccountNumber,jdbcType=VARCHAR},
			</if>
			<if test="invoiceBank != null">
				#{invoiceBank,jdbcType=VARCHAR},
			</if>
			<if test="invoiceAccount != null">
				#{invoiceAccount,jdbcType=VARCHAR},
			</if>
			<if test="invoiceAddressPhone != null">
				#{invoiceAddressPhone,jdbcType=VARCHAR},
			</if>
			<if test="invoiceAddress != null">
				#{invoiceAddress,jdbcType=VARCHAR},
			</if>
			<if test="invoicePhone != null">
				#{invoicePhone,jdbcType=VARCHAR},
			</if>
			<if test="invoiceProvice != null">
				#{invoiceProvice,jdbcType=INTEGER},
			</if>
			<if test="invoiceCity != null">
				#{invoiceCity,jdbcType=INTEGER},
			</if>
			<if test="invoiceCounty != null">
				#{invoiceCounty,jdbcType=INTEGER},
			</if>
			<if test="invoiceName != null">
				#{invoiceName,jdbcType=VARCHAR},
			</if>
			<if test="invoiceEmail != null">
				#{invoiceEmail,jdbcType=VARCHAR},
			</if>
			<if test="contactsName != null">
				#{contactsName,jdbcType=VARCHAR},
			</if>
			<if test="contactsEmail != null">
				#{contactsEmail,jdbcType=VARCHAR},
			</if>
			<if test="contactsPhone != null">
				#{contactsPhone,jdbcType=VARCHAR},
			</if>
			<if test="remark != null">
				#{remark,jdbcType=VARCHAR},
			</if>
			<if test="isDefault != null">
				#{isDefault,jdbcType=TINYINT},
			</if>
			<if test="deleted != null">
				#{deleted,jdbcType=TINYINT},
			</if>
			<if test="enabled != null">
				#{enabled,jdbcType=TINYINT},
			</if>
			<if test="createTime != null">
				#{createTime,jdbcType=TIMESTAMP},
			</if>
			<if test="createUserId != null">
				#{createUserId,jdbcType=BIGINT},
			</if>
			<if test="updateTime != null">
				#{updateTime,jdbcType=TIMESTAMP},
			</if>
			<if test="updateUserId != null">
				#{updateUserId,jdbcType=BIGINT},
			</if>
		</trim>
	</insert>

	<resultMap id="SysEnterpriseExtInvoiceExtPathVoResultMap" type="cn.superdesk.uniorder.dto.vo.organisation.SysEnterpriseExtInvoiceExtPathVo"
			   extends="cn.superdesk.uniorder.dao.mapper.organisation.SysEnterpriseExtInvoiceMapper.BaseResultMap">
		<result column="path" jdbcType="VARCHAR" property="path" />
	</resultMap>

	<select id="pageQuerySysCliqueXInvoice" resultMap="SysEnterpriseExtInvoiceExtPathVoResultMap">

		SELECT
			<!--
			<include refid="cn.superdesk.uniorder.dao.mapper.organisation.SysEnterpriseExtInvoiceMapper.Base_Column_List"/>
			-->
			seei.*,
			scxi.path,
			scxi.effect
		FROM
			sys_clique_x_invoice scxi
			LEFT JOIN sys_enterprise_ext_invoice seei ON scxi.sys_enterprise_ext_invoice_id = seei.id
		<where>

			<if test="path != null and path != '' ">
				and scxi.path LIKE CONCAT(#{path},'%')
			</if>
			<!--  and enabled != '' -->
			<choose>
				<when test="enabled != null">
					and seei.enabled = #{enabled,jdbcType=TINYINT}
				</when>
				<otherwise>
					and seei.enabled = 1
				</otherwise>
			</choose>
			<!--  and deleted != '' -->
			<choose>
				<when test="deleted != null">
					and seei.deleted = #{deleted,jdbcType=TINYINT}
				</when>
				<otherwise>
					and seei.deleted = 0
				</otherwise>
			</choose>

		</where>

	</select>

	<select id="pageQueryInvoiceXSysClique" resultMap="SysEnterpriseExtInvoiceExtPathVoResultMap">

		SELECT
			seei.*,
			GROUP_CONCAT(scxi.path) as path,
			GROUP_CONCAT(scxi.effect) as effect
		FROM
			sys_enterprise_ext_invoice seei
			LEFT JOIN sys_clique_x_invoice scxi ON scxi.sys_enterprise_ext_invoice_id = seei.id
		<where>

			<if test="path != null and path != '' ">
				and scxi.path LIKE CONCAT(#{path},'%')
			</if>
			<!--  and enabled != '' -->
			<choose>
				<when test="enabled != null">
					and seei.enabled = #{enabled,jdbcType=TINYINT}
				</when>
				<otherwise>
					and seei.enabled = 1
				</otherwise>
			</choose>

			<!--  and deleted != '' -->
			<choose>
				<when test="deleted != null">
					and seei.deleted = #{deleted,jdbcType=TINYINT}
				</when>
				<otherwise>
					and seei.deleted = 0
				</otherwise>
			</choose>

			<if test="companyName != null and companyName != '' ">
				and seei.company_name LIKE CONCAT('%',#{companyName},'%')
			</if>

			<if test="taxpayersIDCode != null and taxpayersIDCode != '' ">
				and seei.taxpayers_i_d_code LIKE CONCAT('%',#{taxpayersIDCode},'%')
			</if>

		</where>

		GROUP BY seei.id

	</select>

	<select id="findAll" resultMap="SysEnterpriseExtInvoiceExtPathVoResultMap">
		select
			id, service_no, serial_number, sys_org_id, sys_virt_id, out_org_id, out_virt_id,
			invoice_state, invoice_type, selected_invoice_title, realname, company_name, taxpayers_i_d_code,
			invoice_content, invoice_name, invoice_phone, invoice_email, contacts_name, contacts_email,
			contacts_phone, invoice_provice, invoice_city, invoice_county, invoice_address, invoice_address_phone,
			invoice_bank_account_number, invoice_bank, invoice_account, remark, is_default, deleted,
			enabled, create_time, create_user_id, update_time, update_user_id
		from
			sys_enterprise_ext_invoice
		<where>
			and 1 = 1
			<if test="sysEnterpriseExtInvoiceId != null and sysEnterpriseExtInvoiceId != '' ">
				and id = #{sysEnterpriseExtInvoiceId}
			</if>
			<if test="sysEnterpriseExtInvoiceIdNotIn != null and sysEnterpriseExtInvoiceIdNotIn != ''">
				and id not in
				<foreach collection="sysEnterpriseExtInvoiceIdNotIn" index="index" item="id" open="(" close=")" separator=",">
					#{id}
				</foreach>
			</if>
			<if test="companyName != null and companyName != '' ">
				and company_name like CONCAT('%',#{companyName},'%')
			</if>
			<if test="companyNameEq != null and companyNameEq != '' ">
				and company_name = #{companyNameEq}
			</if>
			<if test="outVirtId != null and outVirtId != '' ">
				and out_virt_id = #{outVirtId}
			</if>

			<choose>
				<when test="enabled != null">
					and enabled = #{enabled,jdbcType=TINYINT}
				</when>
				<otherwise>
					<!-- and enabled = 1 -->
				</otherwise>
			</choose>
			<choose>
				<when test="deleted != null">
					and deleted = #{deleted,jdbcType=TINYINT}
				</when>
				<otherwise>
					<!-- and deleted = 0 -->
				</otherwise>
			</choose>

			<choose>
				<when test="orderBy != null and orderBy != '' ">
					order by ${orderBy}
				</when>
				<otherwise>
					order by is_default desc, create_time asc
				</otherwise>
			</choose>
		</where>
	</select>

</mapper>
