<?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.SysEnterpriseMapperEx">

	<resultMap id="VirtSelectResultMap" type="cn.superdesk.uniorder.dto.vo._function.select.SelectOptionVirt">
		<result column="sys_virt_id" jdbcType="BIGINT" property="sysVirtId"/>
		<result column="organization_id" jdbcType="BIGINT" property="outOrgId"/>
		<result column="enterprise_id" jdbcType="BIGINT" property="virtId"/>
		<result column="enterprise_name" jdbcType="VARCHAR" property="virtName"/>

		<result column="source_from" jdbcType="VARCHAR" property="sourceFrom" />
		<result column="service_no" jdbcType="VARCHAR" property="serviceNo" />
	</resultMap>

	<select id="getVirtSelect" resultMap="VirtSelectResultMap">
		select
			id as sys_virt_id,
			organization_id,
			enterprise_id,
			enterprise_name,
			source_from,
			service_no
		from
			sys_enterprise
		<where>
		and 1=1
		<choose>
			<when test="deleted != null and deleted != ''">
				and deleted = #{deleted,jdbcType=TINYINT}
			</when>
			<otherwise>
				and deleted = 0
			</otherwise>
		</choose>

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

		<if test="virtName != null and virtName != ''">
			and enterprise_name like CONCAT('%',#{virtName,jdbcType=VARCHAR},'%')
		</if>
		<if test="projectId != null and projectId != ''">
			and organization_id = #{projectId,jdbcType=BIGINT}
		</if>
		<if test="notuse != null and notuse == 1 ">
			and enterprise_id not in (SELECT virt_id from unified_customer)
		</if>
		<if test="outVirtIds != null and outVirtIds != ''"><!-- 已修正 数据权限 by linjinyu at 2020090818 -->
			and enterprise_id in
			<foreach collection="outVirtIds" index="index" item="pid" open="(" close=")" separator=",">
				#{pid}
			</foreach>
		</if>
		<choose>
			<when test="orderByClause != null and orderByClause != '' ">
				order by ${orderByClause}

				<!--
				sync_time desc 同步时间 倒序
				id asc 结算中心Id 升序
				enterprise_id asc 第三方企业Id 升序
				-->
			</when>
			<otherwise>
				order by sync_time desc
			</otherwise>
		</choose>
		</where>
	</select>


	<resultMap id="SysOrganizationXEnterpriseResultMap" type="cn.superdesk.uniorder.dto.vo.sys.SysOrganizationXEnterpriseVo">

		<id column="sys_virt_id" jdbcType="BIGINT" property="sysVirtId" />

		<!-- 项目部分 -->

<!--		id as sys_org_id,-->
<!--		organization_id,-->
<!--		organization_no,-->
<!--		organization_name,-->
<!--		province_code,-->
<!--		province_name,-->
<!--		city_code,-->
<!--		city_name,-->
<!--		address,-->
<!--		lng,-->
<!--		lat,-->
<!--		sync_time as org_sync_time,-->
<!--		other_field1 as org_other_field1,-->
<!--		other_field2 as org_other_field2,-->
<!--		other_field3 as org_other_field3,-->
<!--		deleted as org_deleted,-->
		<result column="sys_org_id" jdbcType="BIGINT" property="sysOrgId" />
		<result column="organization_id" jdbcType="BIGINT" property="organizationId" />
		<result column="organization_no" jdbcType="VARCHAR" property="organizationNo" />
		<result column="organization_name" jdbcType="VARCHAR" property="organizationName" />
		<result column="province_code" jdbcType="VARCHAR" property="provinceCode" />
		<result column="province_name" jdbcType="VARCHAR" property="provinceName" />
		<result column="city_code" jdbcType="VARCHAR" property="cityCode" />
		<result column="city_name" jdbcType="VARCHAR" property="cityName" />
		<result column="address" jdbcType="VARCHAR" property="address" />
		<result column="lng" jdbcType="DECIMAL" property="lng" />
		<result column="lat" jdbcType="DECIMAL" property="lat" />
		<result column="org_sync_time" jdbcType="TIMESTAMP" property="orgSyncTime" />
		<result column="org_other_field1" jdbcType="VARCHAR" property="orgOtherField1" />
		<result column="org_other_field2" jdbcType="VARCHAR" property="orgOtherField2" />
		<result column="org_other_field3" jdbcType="VARCHAR" property="orgOtherField3" />
		<result column="org_deleted" jdbcType="TINYINT" property="orgDeleted" />

		<!-- 企业部分 -->

<!--		id as sys_virt_id,-->
<!--		enterprise_id,-->
<!--		enterprise_no,-->
<!--		enterprise_name,-->
<!--		&lt;!&ndash; organization_id, &ndash;&gt;-->
<!--		sync_time as virt_sync_time,-->
<!--		other_field1 as virt_other_field1,-->
<!--		other_field2 as virt_other_field2,-->
<!--		other_field3 as virt_other_field3,-->
<!--		enabled as virt_enabled,-->
<!--		deleted as virt_deleted-->

<!--		<id column="sys_virt_id" jdbcType="BIGINT" property="sysVirtId" />-->
		<result column="enterprise_id" jdbcType="BIGINT" property="enterpriseId" />
		<result column="enterprise_no" jdbcType="VARCHAR" property="enterpriseNo" />
		<result column="enterprise_name" jdbcType="VARCHAR" property="enterpriseName" />
<!--		<result column="organization_id" jdbcType="BIGINT" property="organizationId" />-->
		<result column="virt_sync_time" jdbcType="TIMESTAMP" property="virtSyncTime" />
		<result column="virt_other_field1" jdbcType="VARCHAR" property="virtOtherField1" />
		<result column="virt_other_field2" jdbcType="VARCHAR" property="virtOtherField2" />
		<result column="virt_other_field3" jdbcType="VARCHAR" property="virtOtherField3" />
		<result column="virt_enabled" jdbcType="INTEGER" property="virtEnabled" />
		<result column="virt_deleted" jdbcType="TINYINT" property="virtDeleted" />

		<result column="virt_is_has_order" jdbcType="TINYINT" property="virtIsHasOrder" />
		<result column="virt_is_has_ext_invoice" jdbcType="TINYINT" property="virtIsHasExtInvoice" />
		<result column="source_from" jdbcType="VARCHAR" property="sourceFrom" />
	</resultMap>

	<select id="findSysOrganizationXEnterprise" resultMap="SysOrganizationXEnterpriseResultMap">
		select

			so.id as sys_org_id,
			so.organization_id,
			so.organization_no,
			so.organization_name,
			so.province_code,
			so.province_name,
			so.city_code,
			so.city_name,
			so.address,
			so.lng,
			so.lat,
			so.sync_time as org_sync_time,
			so.other_field1 as org_other_field1,
			so.other_field2 as org_other_field2,
			so.other_field3 as org_other_field3,
			so.deleted as org_deleted,

			se.id as sys_virt_id,
			se.enterprise_id,
			se.enterprise_no,
			se.enterprise_name,
			<!-- se.organization_id, -->
			se.sync_time as virt_sync_time,
			se.other_field1 as virt_other_field1,
			se.other_field2 as virt_other_field2,
			se.other_field3 as virt_other_field3,
			se.enabled as virt_enabled,
			se.deleted as virt_deleted,

			se.is_has_order as virt_is_has_order,
			se.is_has_ext_invoice as virt_is_has_ext_invoice,
			se.source_from as source_from
		from
			sys_enterprise se
			left join  sys_organization so on se.organization_id = so.organization_id
		<where>
			and 1 = 1
		<choose>
			<when test="enabled != null and enabled != ''">
				and se.enabled = #{enabled,jdbcType=TINYINT}
			</when>
			<otherwise>
				<!-- and se.enabled = 1 -->
			</otherwise>
		</choose>
		<choose>
			<when test="deleted != null and deleted != ''">
				and se.deleted = #{enabled,jdbcType=TINYINT}
			</when>
			<otherwise>
				<!-- and se.deleted = 0 -->
			</otherwise>
		</choose>
		<if test="sourceFromsSelected != null and sourceFromsSelected != '' and sourceFromsSelected.size > 0">
			and se.source_from in
			<foreach collection="sourceFromsSelected" index="index" item="source_from" open="(" close=")" separator=",">
				#{source_from}
			</foreach>
		</if>

		<choose>
			<when test="isHasExtInvoice != null and isHasExtInvoice == 0">
				and se.is_has_ext_invoice = 0
			</when>
			<when test="isHasExtInvoice != null and isHasExtInvoice > 0">
				and se.is_has_ext_invoice > 0
			</when>
			<otherwise>

			</otherwise>
		</choose>

		<if test="enterpriseName != null and enterpriseName != '' ">
			and se.enterprise_name like CONCAT('%',#{enterpriseName,jdbcType=VARCHAR},'%')
		</if>

		<if test="outOrgName != null and outOrgName != '' ">
			and so.organization_name like CONCAT('%',#{outOrgName,jdbcType=VARCHAR},'%')
		</if>



		<if test="isgenbill != null and isgenbill == 1">
			and se.enterprise_id in (
				select enterprise_id from sys_order where is_gen_bill=0
			)
		</if>
		<if test="getDD != null and getDD != ''">
			and se.enterprise_id in (
				SELECT id from temp_daide
			)
		</if>
		<if test="getnotDD != null and getnotDD != ''">
			and se.enterprise_id not in (
				SELECT id from temp_daide
			)
		</if>

		<choose>
			<when test="orderByClause != null and orderByClause != '' ">
				order by ${orderByClause}
			</when>
			<otherwise>
				order by se.sync_time desc
			</otherwise>
		</choose>
		</where>
	</select>

	<select id="findByVirtId" parameterType="java.lang.Long" resultMap="cn.superdesk.uniorder.dao.mapper.organisation.SysEnterpriseMapper.BaseResultMap">
		select
		<include refid="cn.superdesk.uniorder.dao.mapper.organisation.SysEnterpriseMapper.Base_Column_List"/>
		from
			sys_enterprise
		<where>
			and 1=1
			and enterprise_id = #{virId,jdbcType=BIGINT}
		</where>
	</select>

	<select id="selectByEnterpriseId" parameterType="java.lang.Long" resultMap="cn.superdesk.uniorder.dao.mapper.organisation.SysEnterpriseMapper.BaseResultMap">
		select
		<include refid="cn.superdesk.uniorder.dao.mapper.organisation.SysEnterpriseMapper.Base_Column_List"/>
		from
			sys_enterprise
		<where>
			and 1=1
			and enterprise_id = #{id,jdbcType=BIGINT}
		</where>
	</select>
	<select id="findByVirtIdAndOrgId" resultType="cn.superdesk.uniorder.dao.entity.organisation.SysEnterprise">
		select
		<include refid="cn.superdesk.uniorder.dao.mapper.organisation.SysEnterpriseMapper.Base_Column_List"/>
		from
		sys_enterprise
		<where>
			and 1=1
			and enterprise_id = #{virtId,jdbcType=BIGINT}
			and organization_id = #{orgId,jdbcType=BIGINT}
		</where>
	</select>

</mapper>
