<?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.workoutsignin.dao.WorkOutSignInDao">
    
	<sql id="workOutSignInColumns">
		a.id AS "id",
		a.location_name AS "locationName",
		a.location_info AS "locationInfo",
		a.longitude AS "longitude",
		a.latitude AS "latitude",
		a.user_id AS "user.id",
		a.company_id AS "companyId",
		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",
		user.name AS "user.name"
	</sql>
	
	<sql id="workOutSignInJoins">
		LEFT JOIN sys_user user ON user.id = a.user_id
	</sql>
	
    <select id="getCreateDateList" resultType="string">
		SELECT create_date FROM work_workout_signin
		WHERE  #{date} LIKE  LEFT (create_date,7)
		<if test="companyId != null and companyId !=''">
			AND    company_id = #{companyId}
		</if>
		ORDER BY  create_date asc
	</select>

	<select id="get" resultType="WorkOutSignIn" >
		SELECT 
			<include refid="workOutSignInColumns"/>
		FROM work_workout_signin a
		<include refid="workOutSignInJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkOutSignIn" >
		SELECT 
			<include refid="workOutSignInColumns"/>
		FROM work_workout_signin a
		<include refid="workOutSignInJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="user != null and user.name != null and user.name != ''">
				AND user.name LIKE
				<if test="dbName == 'oracle'">'%'||#{user.name}||'%'</if>
				<if test="dbName == 'mysql'">CONCAT('%', #{user.name}, '%')</if>
			</if>
			<if test="companyId != null and companyId != ''">
				AND a.company_id = #{companyId}
			</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="WorkOutSignIn" >
		SELECT 
			<include refid="workOutSignInColumns"/>
		FROM work_workout_signin a
		<include refid="workOutSignInJoins"/>
		<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_workout_signin(
			id,
			location_name,
			location_info,
			longitude,
			latitude,
			user_id,
			company_id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag
		) VALUES (
			#{id},
			#{locationName},
			#{locationInfo},
			#{longitude},
			#{latitude},
			#{user.id},
			#{companyId},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag}
		)
	</insert>
	
	<update id="update">
		UPDATE work_workout_signin SET 	
			location_name = #{locationName},
			location_info = #{locationInfo},
			longitude = #{longitude},
			latitude = #{latitude},
			user_id = #{user.id},
			company_id = #{companyId},
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_workout_signin
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_workout_signin SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkOutSignIn" statementType="STATEMENT">
		select * FROM work_workout_signin  where ${propertyName} = '${value}'
	</select>
	
	<!--获取最后一条记录  -->
	<select id="findLast" resultType="WorkOutSignIn">
		select * FROM work_workout_signin a ORDER BY  a.create_date DESC  LIMIT 1
	</select>
	<select id="findCount" resultType="Long">
		select count(1) FROM work_workout_signin a where date(a.create_date) = #{createDate}
	</select>

	<select id="findByCompany2" resultType="WorkOutSignIn" parameterType="java.util.Map">
		SELECT
		<include refid="workOutSignInColumns"/>
		FROM work_workout_signin a
		<include refid="workOutSignInJoins"/>
		<where>
			a.del_flag = #{sign.DEL_FLAG_NORMAL}
			<if test="date != null and date != ''">
				AND #{date} LIKE  LEFT (a.create_date,7)
			</if>
			<if test="sign.companyId != null and sign.companyId != ''">
				AND a.company_id = #{sign.companyId}
			</if>
		</where>
		<choose>
			<when test="sign.page !=null and sign.page.orderBy != null and sign.page.orderBy != ''">
				ORDER BY ${sign.page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>
	<select id="findByCompany" resultType="WorkOutSignIn" >
		SELECT
		<include refid="workOutSignInColumns"/>
		FROM work_workout_signin a
		<include refid="workOutSignInJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="createDate != null and createDate != ''">
				AND Date(a.create_date) = #{createDate}
			</if>
			<if test="companyId != null and companyId != ''">
				AND a.company_id = #{companyId}
			</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="findListByUser" resultType="WorkOutSignIn" >
		SELECT
		<include refid="workOutSignInColumns"/>
		FROM work_workout_signin a
		<include refid="workOutSignInJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test=" companyId != null and companyId != '' ">
				AND a.company_id = #{companyId}
			</if>
			<if test="user != null and user.id != null and user.id !=''">
				AND a.user_id =  #{user.id}
			</if>
			<if test="user != null and user.name != null and user.name !=''">
				AND user.name LIKE
				<if test="dbName == 'oracle'">'%'||#{user.name}||'%'</if>
				<if test="dbName == 'mysql'">CONCAT('%', #{user.name}, '%')</if>
			</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="getCreateDateList1" resultType="string">
		SELECT a.create_date FROM work_workout_signin a
		WHERE   DATE_FORMAT(a.create_date, '%Y-%m') = #{time}
	       and a.create_by=#{userId}
	       ORDER BY  a.create_date asc
	</select>
	<select id="findByCompany1" resultType="WorkOutSignIn">
		SELECT
		<include refid="workOutSignInColumns"/>
		FROM work_workout_signin a
		<include refid="workOutSignInJoins"/>
		where
		a.del_flag = '0'
		and DATE_FORMAT(a.create_date, '%Y-%m') = #{time}
		and a.create_by=#{userId}
		ORDER BY a.update_date DESC
	</select>

	<select id="getCreateDateListAll" resultType="string">
		SELECT a.create_date
		FROM work_workout_signin a
		LEFT JOIN sys_user u on u.id = a.create_by
		LEFT JOIN sys_office o ON o.id = a.office_id
		LEFT JOIN sys_office s ON s.id = a.company_id
		WHERE   DATE_FORMAT(a.create_date, '%Y-%m') = #{time}
	      ${sqlStr}
	       ORDER BY  a.create_date asc
	</select>
	<select id="findByCompanyAll" resultType="WorkOutSignIn">
		SELECT
		a.*
		FROM work_workout_signin a
		LEFT JOIN sys_user u on u.id = a.create_by
		LEFT JOIN sys_office o ON o.id = a.office_id
		LEFT JOIN sys_office s ON s.id = a.company_id
		where
		a.del_flag = '0'
		and DATE_FORMAT(a.create_date, '%Y-%m') = #{time}
		${sqlStr}
		ORDER BY a.update_date DESC
	</select>

	<select id="findByCompanySelf" resultType="WorkOutSignIn">
		SELECT
		<include refid="workOutSignInColumns"/>
		FROM work_workout_signin a
		<include refid="workOutSignInJoins"/>
		where
		a.del_flag = '0'
		and DATE_FORMAT(a.create_date, '%Y-%m-%d') = #{time}
		and a.create_by=#{userId}
		ORDER BY a.update_date DESC
	</select>

	<select id="findByManage" resultType="WorkOutSignIn">
		SELECT
		a.id AS "id",
		a.location_name AS "locationName",
		a.location_info AS "locationInfo",
		a.longitude AS "longitude",
		a.latitude AS "latitude",
		a.user_id AS "user.id",
		a.company_id AS "companyId",
		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",
		u.name AS "user.name"
		FROM work_workout_signin a
		LEFT JOIN sys_user u on u.id = a.create_by
		LEFT JOIN sys_office o ON o.id = a.office_id
		LEFT JOIN sys_office s ON s.id = a.company_id
		where
		a.del_flag = '0'
		and DATE_FORMAT(a.create_date, '%Y-%m-%d') = #{time}
		${sqlStr}
		ORDER BY a.update_date DESC
	</select>
</mapper>