<?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.OperatingReportCloudDao">
	
	<resultMap type="com.ym.mec.biz.dal.entity.OperatingReportCloud" id="OperatingReportCloud">
		<result column="id_" property="id" />
		<result column="organ_id_" property="organId" />
		<result column="calender_id_" property="calenderId" />
		<result column="month_flag_" property="monthFlag" />
		<result column="start_date_" property="startDate" />
		<result column="end_date_" property="endDate" />
		<result column="cloud_price_" property="cloudPrice" />
		<result column="course_price_" property="coursePrice" />
		<result column="settlement_flag_" property="settlementFlag" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
	</resultMap>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="OperatingReportCloud" >
		SELECT * FROM operating_report_cloud WHERE id_ = #{id} 
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="OperatingReportCloud">
		SELECT * FROM operating_report_cloud ORDER BY id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.OperatingReportCloud" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO operating_report_cloud (organ_id_,calender_id_,start_date_,end_date_,cloud_price_,course_price_,settlement_flag_,month_flag_,create_time_,update_time_)
		VALUES(#{organId},#{calenderId},#{startDate},#{endDate},#{cloudPrice},#{coursePrice},#{settlementFlag},#{monthFlag},NOW(),NOW())
	</insert>
	<insert id="batchInsert">
		INSERT INTO operating_report_cloud (organ_id_,calender_id_,month_flag_,create_time_,update_time_) VALUES
		<foreach collection="operatingReportClouds" separator="," item="item">
			(#{item.organId},#{item.calenderId},#{item.monthFlag},NOW(),NOW())
		</foreach>
	</insert>

	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.OperatingReportCloud">
		UPDATE operating_report_cloud <set>
		<if test="startDate != null">
		start_date_ = #{startDate},
		</if>
		<if test="endDate != null">
		end_date_ = #{endDate},
		</if>
		<if test="updateTime != null">
		update_time_ = #{updateTime},
		</if>
		<if test="cloudPrice != null">
		cloud_price_ = #{cloudPrice},
		</if>
		<if test="settlementFlag != null">
		settlement_flag_ = #{settlementFlag},
		</if>
		<if test="coursePrice != null">
		course_price_ = #{coursePrice},
		</if>
		</set> WHERE id_ = #{id}
	</update>
    <update id="settlementCalender">
		UPDATE operating_report_cloud orc
		LEFT JOIN cloud_teacher_free_course ctf ON ctf.calender_id_ = orc.calender_id_
		SET orc.settlement_flag_ = 1
		WHERE orc.month_flag_ = 0 AND orc.settlement_flag_ = 0 AND DATE_FORMAT(orc.start_date_,'%Y-%m') = #{month};
	</update>
	<update id="settlementCurrentCalender">
		UPDATE operating_report_cloud orc
		SET orc.settlement_flag_ = 1
		WHERE orc.month_flag_ = 1 AND orc.settlement_flag_ = 0 AND DATE_FORMAT(orc.create_time_,'%Y-%m') = #{month};
	</update>

	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM operating_report_cloud WHERE id_ = #{id} 
	</delete>
	
	<!-- 分页查询 -->
	<select id="queryPage" resultMap="OperatingReportCloud" parameterType="map">
		SELECT * FROM operating_report_cloud ORDER BY id_ <include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM operating_report_cloud
	</select>
	<select id="findByCalenderId" resultMap="OperatingReportCloud">
		SELECT * FROM operating_report_cloud WHERE calender_id_ = #{calenderId} AND month_flag_ = 0 LIMIT 1
	</select>
	<select id="findByCurrentCalenderId" resultMap="OperatingReportCloud">
		SELECT * FROM operating_report_cloud WHERE calender_id_ = #{calenderId} AND month_flag_ = 1 AND DATE_FORMAT(create_time_,'%Y-%m') = DATE_FORMAT(NOW(),'%Y-%m') LIMIT 1
	</select>
</mapper>
