<?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.ym.mec.biz.dal.dao.StudentOperatingVisitDao">
	
	<resultMap type="com.ym.mec.biz.dal.entity.StudentOperatingVisit" id="StudentOperatingVisit">
		<result column="id_" property="id" />
		<result column="user_id_" property="userId" />
		<result column="month_" property="month" />
		<result column="visit_flag_" property="visitFlag" />
		<result column="organ_id_" property="organId" />
	</resultMap>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="StudentOperatingVisit" >
		SELECT * FROM student_operating_visit WHERE id_ = #{id} 
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="StudentOperatingVisit">
		SELECT * FROM student_operating_visit ORDER BY id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.StudentOperatingVisit" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO student_operating_visit (user_id_,month_,visit_flag_,organ_id_) VALUES(#{userId},#{month},#{visitFlag},#{organId})
	</insert>
	<insert id="batchAdd">
		INSERT INTO student_operating_visit (user_id_,month_,visit_flag_,organ_id_)
		select su.id_,#{month},CASE WHEN sv.student_id_ IS NULL THEN 0 ELSE 1 END visit_flag_,su.organ_id_
		from sys_user su
		left join student_visit sv ON sv.student_id_ = su.id_ AND DATE_FORMAT(sv.visit_time_,'%Y-%m') = #{month}
		where su.id_ IN
		<foreach collection="userIds" item="studentId" open="(" separator="," close=")">
			#{studentId}
		</foreach>
		group by su.id_
	</insert>

	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.StudentOperatingVisit">
		UPDATE student_operating_visit <set>
<if test="userId != null">
user_id_ = #{userId},
</if>
<if test="organId != null">
organ_id_ = #{organId},
</if>
<if test="visitFlag != null">
visit_flag_ = #{visitFlag},
</if>
<if test="month != null">
month_ = #{month},
</if>
</set> WHERE id_ = #{id} 
	</update>
    <update id="updateVisit">
		update student_operating_visit set visit_flag_ = 1
		where user_id_ = #{studentId} and visit_flag_ = 0 and month_ = #{month}
	</update>

	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM student_operating_visit WHERE id_ = #{id} 
	</delete>
	<delete id="deleteByUserId">
		delete from student_operating_visit where id_ IN (
		SELECT a.id_ FROM (select sov.id_ from student_operating_visit sov
		left join student s ON s.user_id_ = sov.user_id_
		where s.operating_tag_ = 0 AND sov.month_ = #{month})a)
	</delete>

	<!-- 分页查询 -->
	<select id="queryPage" resultMap="StudentOperatingVisit" parameterType="map">
		SELECT * FROM student_operating_visit ORDER BY id_ <include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM student_operating_visit
	</select>
	<select id="queryUserIds" resultType="java.lang.Integer">
		SELECT DISTINCT user_id_ FROM student_operating_visit WHERE month_ = #{month}
	</select>
	<resultMap id="ExportOperatingVisitDto" type="com.ym.mec.biz.dal.dto.ExportOperatingVisitDto">
		<result property="organName" column="organ_name_" />
		<result property="expectVisitNum" column="expect_visit_num_" />
		<result property="visitNum" column="visit_num_" />
	</resultMap>
	<select id="sumVisit" resultMap="ExportOperatingVisitDto">
		SELECT o.name_ organ_name_,count(sov.user_id_) expect_visit_num_,COUNT(CASE WHEN sov.visit_flag_ = 1 THEN 1 ELSE NULL END) visit_num_
		FROM student_operating_visit sov
		left join organization o on sov.organ_id_ = o.id_
		WHERE sov.month_ = #{month}
		<if test="tenantId != null">
			AND o.tenant_id_ = #{tenantId}
		</if>
		group by sov.organ_id_
	</select>
</mapper>
