<?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.WorkClientBankDao">
    
	<sql id="workClientBankColumns">
		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.remarks AS "remarks",
		a.our_bank AS "ourBank",
		a.bank_number AS "bankNumber",
		a.client_id AS "clientId.id"
	</sql>
	
	<sql id="workClientBankJoins">
        LEFT JOIN work_client_info b ON b.id = a.client_id
	</sql>
	
    
	<select id="get" resultType="WorkClientBank" >
		SELECT 
			<include refid="workClientBankColumns"/>
		FROM work_client_bank a
		<include refid="workClientBankJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkClientBank" >
		SELECT 
			<include refid="workClientBankColumns"/>
            ,b.name AS "clientId.name"
		FROM work_client_bank a
		<include refid="workClientBankJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
            <if test="clientId != null and clientId.id != null and clientId.id !=''">
                AND a.client_id = #{clientId.id}
            </if>
            <if test="clientId != null and clientId.name != null and clientId.name !=''">
                AND b.name like concat('%',#{clientId.name},'%')
            </if>
            AND b.company_id = #{currentUser.company.id} and b.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="findListByClient" resultType="WorkClientBank" >
        SELECT
        <include refid="workClientBankColumns"/>
        FROM work_client_bank a
        <where>
            a.del_flag = #{DEL_FLAG_NORMAL}
            <if test="clientId != null and clientId.id != null and clientId.id !=''">
                AND a.client_id = #{clientId.id}
            </if>
        </where>
            ORDER BY a.update_date DESC
    </select>
	
	<select id="findAllList" resultType="WorkClientBank" >
		SELECT 
			<include refid="workClientBankColumns"/>
		FROM work_client_bank a
		<include refid="workClientBankJoins"/>
		<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_bank(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			del_flag,
			remarks,
			our_bank,
			bank_number,
			client_id
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{delFlag},
			#{remarks},
			#{ourBank},
			#{bankNumber},
			#{clientId.id}
		)
	</insert>
	
	<update id="update">
		UPDATE work_client_bank SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			our_bank = #{ourBank},
			bank_number = #{bankNumber},
			client_id = #{clientId.id}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_client_bank
        <choose>
            <when test="id !=null and id != ''">
                WHERE id = #{id}
            </when>
            <otherwise>
                WHERE client_id = #{clientId.id}
            </otherwise>
        </choose>
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_client_bank SET 
			del_flag = #{DEL_FLAG_DELETE}
        <choose>
            <when test="id !=null and id != ''">
                WHERE id = #{id}
            </when>
            <otherwise>
                WHERE client_id = #{clientId.id}
            </otherwise>
        </choose>
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkClientBank" statementType="STATEMENT">
		select * FROM work_client_bank  where ${propertyName} = '${value}'
	</select>
	
</mapper>