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

	<sql id="workClientLinkmanColumns">
		a.id AS "id",
		a.name AS "name",
		a.position AS "position",
		a.qq AS "qq",
		a.link_phone AS "linkPhone",
		a.link_mobile AS "linkMobile",
		a.email AS "email",
		a.is_default AS "isDefault",
		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.client_id AS "clientId.id",
		a.office AS "office",
		b.name AS "clientId.name"
	</sql>
	<sql id="linkManColumn">
		a.id AS "id",
		a.name AS "name",
		a.position AS "position",
		a.qq AS "qq",
		a.link_phone AS "linkPhone",
		a.link_mobile AS "linkMobile",
		a.email AS "email",
		a.is_default AS "isDefault",
		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.client_id AS "clientId.id",
		a.office AS "office"
	</sql>

	<sql id="workClientLinkmanJoins">
		LEFT JOIN work_client_info b ON b.id = a.client_id
	</sql>


	<select id="get" resultType="WorkClientLinkman" >
		SELECT
			<include refid="workClientLinkmanColumns"/>
		FROM work_client_linkman a
		<include refid="workClientLinkmanJoins"/>
		WHERE a.id = #{id}
	</select>

	<select id="findByClientId" resultType="WorkClientLinkman">
		SELECT <include refid="workClientLinkmanColumns"/>
		FROM work_client_linkman a
		<include refid="workClientLinkmanJoins"/>
		<where>
			a.del_flag = '0'
			AND a.client_id = #{clientId}
		</where>
	</select>
	<select id="findByClientProject" resultType="WorkClientLinkman" >
		SELECT
			<include refid="workClientLinkmanColumns"/>,
			p.project_id AS "projectId"
		FROM work_client_linkman a
		<include refid="workClientLinkmanJoins"/>
		join work_project_client p ON a.id = p.linkman_id
		JOIN work_bid_project c ON c.id = p.project_id
		WHERE a.id = #{id} AND p.project_id = #{projectId}
	</select>

	<select id="findByClient" resultType="WorkClientLinkman" >
		SELECT
		<include refid="workClientLinkmanColumns"/>
		FROM work_client_linkman a
		<include refid="workClientLinkmanJoins"/>
		WHERE a.client_id = #{clientId.id}
	</select>

	<select id="findList" resultType="WorkClientLinkman" >
		SELECT
			<include refid="workClientLinkmanColumns"/>
		FROM work_client_linkman a
		<include refid="workClientLinkmanJoins"/>
		<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="clientId != null and clientId.id != null and clientId.id != ''">
				AND a.client_id = #{clientId.id}
			</if>
			<if test="clientIds != null and clientIds.size !=0">
				AND a.client_id in
                <foreach collection="clientIds" item="cid" open="(" close=")" separator="," >
                    #{cid}
                </foreach>
			</if>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.create_date ASC
			</otherwise>
		</choose>
	</select>

	<select id="findListByProjectId" resultType="WorkClientLinkman" >
		SELECT
		a.id AS "id",
		a.name AS "name",
		a.position AS "position",
		a.qq AS "qq",
		a.link_phone AS "linkPhone",
		a.link_mobile AS "linkMobile",
		a.email AS "email",
		a.is_default AS "isDefault",
		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.client_id AS "clientId.id",
		a.office AS "office",
		p.project_id AS "projectId"
		FROM work_client_linkman a
		join work_project_client p ON a.id = p.linkman_id
		JOIN work_bid_project c ON c.id = p.project_id
		<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="projectId != null and projectId != ''">
				AND p.project_id = #{projectId}
			</if>
			<if test="remarks != null and remarks != ''">
				AND p.remarks = #{remarks}
			</if>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.create_date ASC
			</otherwise>
		</choose>
	</select>

	<select id="findAllList" resultType="WorkClientLinkman" >
		SELECT
			<include refid="workClientLinkmanColumns"/>
		FROM work_client_linkman a
		<include refid="workClientLinkmanJoins"/>
		<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.create_date ASC
			</otherwise>
		</choose>
	</select>

	<!-- work_project_client 相关begin-->
	<insert id="insertProject">
		INSERT INTO work_project_client(
			linkman_id,
			remarks,
			project_id
		) VALUES (
			#{id},
			#{remarks},
			#{projectId}
		)
	</insert>
	<delete id="deleteProjectClient">
		DELETE FROM  work_project_client
		WHERE project_id = #{id}
	</delete>
	<!-- work_project_client 相关end-->

	<insert id="insert">
		INSERT INTO work_client_linkman(
			id,
			name,
			position,
			qq,
			link_phone,
			link_mobile,
			email,
			is_default,
			create_by,
			create_date,
			update_by,
			update_date,
			del_flag,
			client_id,
			office
		) VALUES (
			#{id},
			#{name},
			#{position},
			#{qq},
			#{linkPhone},
			#{linkMobile},
			#{email},
			#{isDefault},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{delFlag},
			#{clientId.id},
			#{office}
		)
	</insert>

	<update id="update">
		UPDATE work_client_linkman SET
			name = #{name},
			position = #{position},
			qq = #{qq},
			link_phone = #{linkPhone},
			link_mobile = #{linkMobile},
			email = #{email},
			is_default = #{isDefault},
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			client_id = #{clientId.id},
			office = #{office}
		WHERE id = #{id}
	</update>


	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_client_linkman
		<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_linkman 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="WorkClientLinkman" statementType="STATEMENT">
		select * FROM work_client_linkman  where ${propertyName} = '${value}'
	</select>

	<select id="queryProjectLinkmans" resultType="WorkClientLinkman" >
		SELECT
			<include refid="workClientLinkmanColumns"/>
		FROM project_client_linkman p,work_client_linkman a,work_client_info b
		WHERE p.linkman_id = a.id and b.id = a.client_id and p.project_id=#{projectId} and is_entrust = #{isEntrust}
	</select>

	<select id="queryLinkManByClientId" resultType="WorkClientLinkman">
		select <include refid="linkManColumn"/>
		from work_client_linkman a
		where a.client_id = #{id}
	</select>

	<select id="queryLinkManByCompany" parameterType="WorkClientLinkman" resultType="WorkClientLinkman">
		select <include refid="workClientLinkmanColumns"/>
		from work_client_linkman a
		<include refid="workClientLinkmanJoins"/>
		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="clientId != null and clientId.companyId != null and clientId.companyId != ''">
			AND b.company_id = #{clientId.companyId}
		</if>

	</select>

</mapper>