<?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.activityassignment.dao.ActivityAssignmentDao">
    
	<sql id="activityAssignmentColumns">
		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.remarks AS "remarks",
		a.del_flag AS "delFlag",
		a.type AS "type",
		a.user_id AS "user.id",
		a.start_date AS "startDate",
		a.end_date AS "endDate",
		a.company_id AS "companyId",
		u.name AS "user.name",
		c.name AS "createBy.name"
	</sql>
	
	<sql id="activityAssignmentJoins">
		LEFT JOIN sys_user u ON u.id = a.user_id
		LEFT JOIN sys_user c ON c.id = a.create_by
		LEFT JOIN sys_user_office uo ON uo.user_id = a.create_by and uo.company_id = a.company_id
		LEFT JOIN sys_office o ON o.id = uo.office_id
	</sql>
	
    
	<select id="get" resultType="ActivityAssignment" >
		SELECT 
			<include refid="activityAssignmentColumns"/>
		FROM sys_activity_assignment a
		<include refid="activityAssignmentJoins"/>
		WHERE a.id = #{id}
	</select>

	<select id="getByUserIdAndDate" resultType="ActivityAssignment" >
		SELECT
			<include refid="activityAssignmentColumns"/>
		FROM sys_activity_assignment a
		<include refid="activityAssignmentJoins"/>
		WHERE a.create_by = #{createBy.id}
		AND (a.type = #{type} OR a.type = '1')
		AND a.company_id = #{companyId}
		AND #{startDate} BETWEEN a.start_date AND a.end_date
	</select>

	<select id="findList" resultType="ActivityAssignment" >
		SELECT 
			<include refid="activityAssignmentColumns"/>
		FROM sys_activity_assignment a
		<include refid="activityAssignmentJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			AND a.company_id = #{companyId}
			<if test="type != null and type != ''">
				AND a.type = #{type}
			</if>
			<choose>
				<when test="user != null and user.id != null and user.id != ''">
					AND a.user_id = #{user.id}
				</when>
				<when test="createBy != null and createBy.id != null and createBy.id != ''">
					AND (a.user_id = #{createBy.id} OR a.create_by = #{createBy.id})
				</when>
				<when test="branchOffice != null and branchOffice != ''">
					AND find_in_set(o.branch_office ,#{branchOffice})
				</when>
			</choose>
			<if test="startDate != null and startDate != ''">
				AND #{startDate} BETWEEN a.start_date AND a.end_date
			</if>
		</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="findAllList" resultType="ActivityAssignment" >
		SELECT 
			<include refid="activityAssignmentColumns"/>
		FROM sys_activity_assignment a
		<include refid="activityAssignmentJoins"/>
		<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 sys_activity_assignment(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			type,
			user_id,
			start_date,
			end_date,
			company_id
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{type},
			#{user.id},
			#{startDate},
			#{endDate},
			#{companyId}
		)
	</insert>
	
	<update id="update">
		UPDATE sys_activity_assignment SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			type = #{type},
			user_id = #{user.id},
			start_date = #{startDate},
			end_date = #{endDate},
			company_id = #{companyId}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM sys_activity_assignment
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE sys_activity_assignment SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="ActivityAssignment" statementType="STATEMENT">
		select * FROM sys_activity_assignment  where ${propertyName} = '${value}'
	</select>
	
</mapper>