<?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.jeeplus.modules.workclientinfo.dao.WorkClientInfoDao">
    
	<sql id="workClientInfoColumnsJoinLink">
		a.id AS "id",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		a.del_flag AS "delFlag",
		a.name AS "name",
		a.name_for_short AS "nameForShort",
		a.or_unicode AS "orUnicode",
		a.company_type AS "companyType",
		a.company_industry AS "companyIndustry",
		a.client_type AS "clientType",
		a.credit_rank AS "creditRank",
		a.area_id AS "area.id",
		a.zip_code AS "zipCode",
		a.fax AS "fax",
		a.lawer_presint AS "lawerPresint",
		a.tax_id AS "taxId",
		a.email AS "email",
		a.address AS "address",
		a.register_address AS "registerAddress",
		a.company_url AS "companyUrl",
		a.remarks AS "unitIntroduction",
		a.has_uscc AS "hasUscc",
		a.usc_code AS "uscCode",
		area.name AS "area.name",
		b.id AS "workClientLinkman.id",
		b.name AS "workClientLinkman.name",
		b.link_mobile AS "workClientLinkman.linkMobile",
		b.link_phone AS "workClientLinkman.linkPhone",
		a.company_id AS "companyId",
		a.telephone AS "telephone"
	</sql>

	<sql id="workClientInfoColumns">
		a.id AS "id",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		a.del_flag AS "delFlag",
		a.name AS "name",
		a.name_for_short AS "nameForShort",
		a.or_unicode AS "orUnicode",
		a.company_type AS "companyType",
		a.company_industry AS "companyIndustry",
		a.client_type AS "clientType",
		a.credit_rank AS "creditRank",
		a.area_id AS "area.id",
		a.zip_code AS "zipCode",
		a.fax AS "fax",
		a.lawer_presint AS "lawerPresint",
		a.tax_id AS "taxId",
		a.email AS "email",
		a.address AS "address",
		a.register_address AS "registerAddress",
		a.company_url AS "companyUrl",
		a.remarks AS "unitIntroduction",
		area.name AS "area.name",
		a.company_id AS "companyId",
		a.telephone AS "telephone",
		a.has_uscc AS "hasUscc",
		a.usc_code AS "uscCode",
		a.number AS "number"
	</sql>
	
	<sql id="workClientInfoJoins">
		LEFT JOIN sys_area area ON area.id = a.area_id
	</sql>

	<sql id="workClientInfoJoinsLink">
		LEFT JOIN sys_area area ON area.id = a.area_id
	</sql>

	<select id="getBeUsedCount1" resultType="java.lang.Integer">
		SELECT  COUNT(*) FROM work_bid_project
		WHERE bid_company = #{clientId}
	</select>
	<select id="getBeUsedCount2" resultType="java.lang.Integer">
		SELECT COUNT(*) FROM work_biding_project
		WHERE client_id = #{clientId}
	</select>
	<select id="getBeUsedCount3" resultType="java.lang.Integer">
		SELECT count(*) FROM `work_engineering_project`
		WHERE client_id = #{clientId}
	</select>
	<select id="getBeUsedCount4" resultType="java.lang.Integer">
		SELECT COUNT(*) FROM work_full_manage
		WHERE client_id = #{clientId}
		OR build_id = #{clientId}
	</select>
	<select id="getBeUsedCount5" resultType="java.lang.Integer">
		SELECT COUNT(*) FROM work_contract_info
		WHERE client_id = #{clientId}
	</select>

	<select id="get" resultType="WorkClientInfo" >
		SELECT 
			<include refid="workClientInfoColumns"/>
		FROM work_client_info a
		<include refid="workClientInfoJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkClientInfo" >
		SELECT 
			<include refid="workClientInfoColumns"/>
		FROM work_client_info a
		<include refid="workClientInfoJoinsLink"/>
		<if test="workClientLinkman != null and workClientLinkman.name !=null and workClientLinkman.name !=''">
			LEFT JOIN work_client_linkman b ON a.id = b.client_id
		</if>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<!--<if test="workClientLinkman != null and workClientLinkman.id != '' and workClientLinkman.id != null">-->
				<!--LEFT JOIN work_client_linkman b ON a.id = b.client_id-->
				<!--and b.id = #{workClientLinkman.id}-->
			<!--</if>-->
			<if test="name != null and name != ''">
				AND a.name LIKE 
					<if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{name}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{name},'%')</if>
			</if>
			<if test="companyType != null and companyType != ''">
				AND a.company_type = #{companyType}
			</if>
			<if test="companyIndustry != null and companyIndustry != ''">
				AND a.company_industry = #{companyIndustry}
			</if>
			<if test="area != null and area.id != null and area.id != ''">
				AND (a.area_id = #{area.id} OR find_in_set(#{area.id},a.area_parent_ids))
			</if>
            <if test="workClientLinkman != null and workClientLinkman.name != null and workClientLinkman.name !=''">
				AND b.name LIKE concat('%',#{workClientLinkman.name},'%')
            </if>
            <if test="workClientLinkman != null and workClientLinkman.linkPhone != null and workClientLinkman.linkPhone !=''">
				AND b.link_phone LIKE concat('%',#{workClientLinkman.linkPhone},'%')
            </if>
			<if test="address != null and address != ''">
				AND a.address LIKE 
					<if test="dbName == 'oracle'">'%'||#{address}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{address}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{address},'%')</if>
			</if>
			<if test="clientType != null and clientType != ''">
				AND a.client_type like concat('%',#{clientType},'%')
			</if>
			<if test="uscCode != null and uscCode != ''">
				AND a.usc_code like concat('%',#{uscCode},'%')
			</if>
            <if test="beginDate !=null">
                AND a.create_date >= #{beginDate}
            </if>
            <if test="endDate !=null">
                AND a.create_date &lt; #{endDate}
            </if>
            AND a.company_id = #{currentUser.company.id}
		</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="queryCount" resultType="int" >
        SELECT
        count(1)
        FROM work_client_info a
        <if test="workClientLinkman != null and workClientLinkman.name !=null and workClientLinkman.name !=''">
            LEFT JOIN work_client_linkman b ON a.id = b.client_id
        </if>
        <where>
            a.del_flag = #{DEL_FLAG_NORMAL}
            <if test="name != null and name != ''">
                AND a.name LIKE
                <if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
                <if test="dbName == 'mssql'">'%'+#{name}+'%'</if>
                <if test="dbName == 'mysql'">concat('%',#{name},'%')</if>
            </if>
            <if test="companyType != null and companyType != ''">
                AND a.company_type = #{companyType}
            </if>
            <if test="companyIndustry != null and companyIndustry != ''">
                AND a.company_industry = #{companyIndustry}
            </if>
            <if test="area != null and area.id != null and area.id != ''">
                AND (a.area_id = #{area.id} OR find_in_set(#{area.id},a.area_parent_ids))
            </if>
            <if test="workClientLinkman != null and workClientLinkman.name != null and workClientLinkman.name !=''">
                AND b.name LIKE concat('%',#{workClientLinkman.name},'%')
            </if>
            <if test="workClientLinkman != null and workClientLinkman.linkPhone != null and workClientLinkman.linkPhone !=''">
                AND b.link_phone LIKE concat('%',#{workClientLinkman.linkPhone},'%')
            </if>
            <if test="address != null and address != ''">
                AND a.address LIKE
                <if test="dbName == 'oracle'">'%'||#{address}||'%'</if>
                <if test="dbName == 'mssql'">'%'+#{address}+'%'</if>
                <if test="dbName == 'mysql'">concat('%',#{address},'%')</if>
            </if>
            <if test="clientType != null and clientType != ''">
                AND a.client_type like concat('%',#{clientType},'%')
            </if>
            <if test="uscCode != null and uscCode != ''">
                AND a.usc_code like concat('%',#{uscCode},'%')
            </if>
            <if test="beginDate !=null">
                AND a.create_date >= #{beginDate}
            </if>
            <if test="endDate !=null">
                AND a.create_date &lt; #{endDate}
            </if>
            AND a.company_id = #{currentUser.company.id}
        </where>
    </select>
	
	<select id="findAllList" resultType="WorkClientInfo" >
		SELECT 
			<include refid="workClientInfoColumns"/>
		FROM work_client_info a
		<include refid="workClientInfoJoins"/>
		<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 work_client_info(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			del_flag,
			name,
			name_for_short,
			or_unicode,
			company_type,
			company_industry,
			client_type,
			credit_rank,
			area_id,
			area_parent_ids,
			zip_code,
			fax,
            lawer_presint,
			<!-- our_bank,
			bank_number, -->
			tax_id,
			email,
			address,
			register_address,
			company_url,
			remarks,
			company_id,
			office_id,
			telephone,
			has_uscc,
			usc_code,
			number
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{delFlag},
			#{name},
			#{nameForShort},
			#{orUnicode},
			#{companyType},
			#{companyIndustry},
			#{clientType},
			#{creditRank},
			#{area.id},
			#{area.parentIds},
			#{zipCode},
			#{fax},
            #{lawerPresint},
        <!--#{ourBank},
            #{bankNumber}, -->
			#{taxId},
			#{email},
			#{address},
			#{registerAddress},
			#{companyUrl},
			#{unitIntroduction},
			#{companyId},
			#{officeId},
			#{telephone},
			#{hasUscc},
			#{uscCode},
			#{number}
		)
	</insert>
	
	<update id="update">
		UPDATE work_client_info SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			name = #{name},
			name_for_short = #{nameForShort},
			or_unicode = #{orUnicode},
			company_type = #{companyType},
			company_industry = #{companyIndustry},
			client_type = #{clientType},
			credit_rank = #{creditRank},
			area_id = #{area.id},
			area_parent_ids = #{area.parentIds},
			zip_code = #{zipCode},
			fax = #{fax},
            lawer_presint = #{lawerPresint},
        <!--our_bank = #{ourBank},
        bank_number = #{bankNumber}, -->
			tax_id = #{taxId},
			email = #{email},
			address = #{address},
			register_address = #{registerAddress},
			company_url = #{companyUrl},
			remarks = #{unitIntroduction},
			company_id = #{companyId},
			office_id = #{officeId},
			telephone = #{telephone},
			has_uscc = #{hasUscc},
			usc_code = #{uscCode}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_client_info
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_client_info SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkClientInfo" statementType="STATEMENT">
		select * FROM work_client_info  where ${propertyName} = '${value}'
	</select>
	
    <select id="countByUscCode" resultType="int">
        SELECT count(1) from work_client_info WHERE  usc_code = #{uscCode}
    </select>

	<select id="findByName" resultType="WorkClientInfo">
		select a.id
		from work_client_info a
		<where>
			name = #{name}
			<if test="orUnicode != null and orUnicode != ''">
				AND or_unicode = #{orUnicode}
			</if>
			<if test="id != null and id != ''">
				AND id != #{id}
			</if>
		</where>
	</select>
</mapper>