<?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.OperatingReportNewDao">
	
	<resultMap type="com.ym.mec.biz.dal.entity.OperatingReportNew" id="OperatingReportNew">
		<result column="id_" property="id" />
		<result column="principal_" property="principal" />
		<result column="organ_name_" property="organName" />
		<result column="organ_id_" property="organId" />
		<result column="sale_amount_" property="saleAmount" />
		<result column="service_amount_" property="serviceAmount" />
		<result column="cloud_amount_" property="cloudAmount" />
		<result column="course_amount_" property="courseAmount" />
		<result column="maintenance_amount_" property="maintenanceAmount" />
		<result column="business_refund_" property="businessRefund" />
		<result column="total_income_" property="totalIncome" />
		<result column="sale_cost_" property="saleCost" />
		<result column="fixed_costs_" property="fixedCosts" />
		<result column="variable_costs_" property="variableCosts" />
		<result column="internal_settlement_" property="internalSettlement" />
		<result column="total_cost_" property="totalCost" />
		<result column="quasi_discretionary_profit_" property="quasiDiscretionaryProfit" />
		<result column="prepaid_fee_" property="prepaidFee" />
		<result column="cloud_prepaid_fee_" property="cloudPrepaidFee" />
		<result column="course_prepaid_fee_" property="coursePrepaidFee" />
		<result column="sale_prepaid_fee_" property="salePrepaidFee" />
		<result column="other_prepaid_fee_" property="otherPrepaidFee" />
		<result column="prepayments_" property="prepayments" />
		<result column="receivables_" property="receivables" />
		<result column="payable_" property="payable" />
		<result column="month_" property="month" />
	</resultMap>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="OperatingReportNew" >
		SELECT * FROM operating_report_new WHERE id_ = #{id} 
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="OperatingReportNew">
		SELECT * FROM operating_report_new ORDER BY id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.OperatingReportNew" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO operating_report_new (id_,principal_,organ_id_,organ_name_,sale_amount_,service_amount_,cloud_amount_,course_amount_,maintenance_amount_,
		                                  business_refund_,total_income_,sale_cost_,fixed_costs_,variable_costs_,internal_settlement_,
		                                  total_cost_,quasi_discretionary_profit_,prepaid_fee_,cloud_prepaid_fee_,course_prepaid_fee_,sale_prepaid_fee_,other_prepaid_fee_
		                                  ,prepayments_,receivables_,payable_,month_)
		VALUES(#{id},#{principal},#{organId},#{organName},#{saleAmount},#{serviceAmount},#{cloudAmount},#{courseAmount},#{maintenanceAmount},
		       #{businessRefund},#{totalIncome},#{saleCost},#{fixedCosts},#{variableCosts},#{internalSettlement},
		       #{totalCost},#{quasiDiscretionaryProfit},#{prepaidFee},#{cloudPrepaidFee},#{coursePrepaidFee},#{salePrepaidFee},#{otherPrepaidFee},
		       #{prepayments},#{receivables},#{payable},#{month})
	</insert>
	<insert id="batchInsert">
		INSERT INTO operating_report_new (principal_,organ_id_,organ_name_,sale_amount_,service_amount_,cloud_amount_,course_amount_,maintenance_amount_,business_refund_,total_income_,sale_cost_,
		                                  fixed_costs_,variable_costs_,internal_settlement_,total_cost_,quasi_discretionary_profit_,
		                                  prepaid_fee_,cloud_prepaid_fee_,course_prepaid_fee_,sale_prepaid_fee_,other_prepaid_fee_,prepayments_,receivables_,payable_,month_)
		VALUES
		<foreach collection="operatingList" item="bean" separator=",">
			(#{bean.principal},#{bean.organId},#{bean.organName},#{bean.saleAmount},#{bean.serviceAmount},#{bean.cloudAmount},#{bean.courseAmount},#{bean.maintenanceAmount},#{bean.businessRefund},#{bean.totalIncome},
			 #{bean.saleCost},#{bean.fixedCosts},#{bean.variableCosts},#{bean.internalSettlement},#{bean.totalCost},#{bean.quasiDiscretionaryProfit},
			 #{bean.prepaidFee},#{bean.cloudPrepaidFee},#{bean.coursePrepaidFee},#{bean.salePrepaidFee},#{bean.otherPrepaidFee},
			 #{bean.prepayments},#{bean.receivables},#{bean.payable},#{bean.month})
		</foreach>
	</insert>

	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.OperatingReportNew">
		UPDATE operating_report_new <set>
		<if test="saleAmount != null">
		sale_amount_ = #{saleAmount},
		</if>
		<if test="prepaidFee != null">
		prepaid_fee_ = #{prepaidFee},
		</if>
		<if test="serviceAmount != null">
		service_amount_ = #{serviceAmount},
		</if>
		<if test="payable != null">
		payable_ = #{payable},
		</if>
		<if test="businessRefund != null">
		business_refund_ = #{businessRefund},
		</if>
		<if test="totalCost != null">
		total_cost_ = #{totalCost},
		</if>
		<if test="totalIncome != null">
		total_income_ = #{totalIncome},
		</if>
		<if test="quasiDiscretionaryProfit != null">
		quasi_discretionary_profit_ = #{quasiDiscretionaryProfit},
		</if>
		<if test="fixedCosts != null">
		fixed_costs_ = #{fixedCosts},
		</if>
		<if test="internalSettlement != null">
		internal_settlement_ = #{internalSettlement},
		</if>
		<if test="prepayments != null">
		prepayments_ = #{prepayments},
		</if>
		<if test="month != null">
		month_ = #{month},
		</if>
		<if test="variableCosts != null">
		variable_costs_ = #{variableCosts},
		</if>
		<if test="organName != null">
		organ_name_ = #{organName},
		</if>
		<if test="saleCost != null">
		sale_cost_ = #{saleCost},
		</if>
		<if test="receivables != null">
		receivables_ = #{receivables},
		</if>
		<if test="principal != null">
		principal_ = #{principal},
		</if>
		</set> WHERE id_ = #{id}
	</update>
    <update id="initCloudStartTime">
		update operating_report_cloud orc
		left join (
		select orc.calender_id_,MIN(cs.class_date_) start_date_ from operating_report_cloud orc
		LEFT JOIN cloud_teacher_free_course ctf ON ctf.calender_id_ = orc.calender_id_
		LEFT JOIN course_schedule cs ON cs.id_ = ctf.course_id_
		where orc.month_flag_ = false AND orc.settlement_flag_ = false AND orc.start_date_ IS NULL
		AND cs.class_date_ &lt;= #{lastDay}
		group by orc.calender_id_) mc ON orc.calender_id_ = mc.calender_id_
		LEFT JOIN (select calender_id_,DATE_ADD(
		DATE_ADD(create_time_, interval CASE WHEN period_ = 'YEAR_HALF' THEN 6
		WHEN period_ = 'YEAR' THEN 12
		WHEN period_ = 'QUARTERLY' THEN 3 ELSE num_ END MONTH),interval 1 DAY) end_date_
		from music_group_payment_calender_member order by id_ DESC) mgpcm
		ON mc.calender_id_ = mgpcm.calender_id_
		set orc.start_date_ = mc.start_date_,orc.end_date_ = mgpcm.end_date_
		where orc.calender_id_ = mc.calender_id_ AND orc.month_flag_ = 0
	</update>

    <!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM operating_report_new WHERE id_ = #{id} 
	</delete>
	<delete id="deleteByMonth">
		DELETE FROM operating_report_new WHERE month_ = #{month}
	</delete>
	<sql id="queryPageSql">
		<where>
			<if test="organId != null and organId != ''">
				AND FIND_IN_SET(organ_id_,#{organId})
			</if>
			<if test="date != null and date != ''">
				AND month_ = #{date}
			</if>
		</where>
	</sql>

	<!-- 分页查询 -->
	<select id="queryPage" resultMap="OperatingReportNew" parameterType="map">
		SELECT * FROM operating_report_new
		<include refid="queryPageSql"/>
		ORDER BY id_
		<include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(id_) FROM operating_report_new
		<include refid="queryPageSql"/>
	</select>
	<select id="initOperatingReport" resultMap="OperatingReportNew">
		select id_ organ_id_,name_ organ_name_,#{month} month_ from organization where del_flag_ = 0 and tenant_id_ = 1
	</select>
	<select id="sumSellAmount" resultMap="OperatingReportNew">
		select so.organ_id_,SUM(so.actual_amount_) sale_amount_,SUM(CASE WHEN so.status_ = 1 THEN 0 ELSE so.sell_cost_ * so.num_ END) sale_cost_ from sell_order so
		left join student_payment_order spo ON spo.order_no_ = so.order_no_
		left join music_group mg ON mg.id_ = spo.music_group_id_ AND spo.group_type_ = 'MUSIC'
		where (mg.musical_instruments_provide_status_ = 1 AND mg.musical_instruments_provide_time_ = #{month}) OR
		      (DATE_FORMAT(so.create_ime_,'%Y-%m') = #{month} AND (mg.musical_instruments_provide_status_ IS NULL OR mg.musical_instruments_provide_status_ = 1))
		group by so.organ_id_
	</select>
	<select id="sumCloudCourseAmount" resultType="java.util.Map">
		SELECT cs.organ_id_ 'key',SUM(ctf.cost_) 'value' FROM cloud_teacher_free_course ctf
		LEFT JOIN course_schedule cs ON cs.id_ = ctf.course_id_
		where cs.del_flag_ = 0 AND cs.is_lock_ = 0 AND cs.pre_course_flag_ = 0 AND (cs.new_course_id_ IS NULL OR cs.new_course_id_ = cs.id_)
		AND cs.class_date_ BETWEEN #{firstDay} AND #{lastDay} group by cs.organ_id_;
	</select>
	<select id="sumCourseAmount" resultType="java.util.Map">
		SELECT cs.organ_id_ 'key',SUM(cssp.actual_price_) 'value' FROM course_schedule cs
		LEFT JOIN course_schedule_student_payment cssp ON cssp.course_schedule_id_ = cs.id_
		where cs.del_flag_ = 0 AND cs.is_lock_ = 0 AND cs.pre_course_flag_ = 0 AND (cs.new_course_id_ IS NULL OR cs.new_course_id_ = cs.id_)
		AND cssp.actual_price_ > 0 AND cs.class_date_ BETWEEN #{firstDay} AND #{lastDay} group by cs.organ_id_;
	</select>
	<select id="sumPersonalCloudAmount" resultType="java.util.Map">
		select cto.organ_id_ 'key',TRUNCATE(SUM(operating_amount_ / (TIMESTAMPDIFF(DAY,cto.start_time_,cto.end_time_) + 1) *
		(TIMESTAMPDIFF(DAY,CASE WHEN start_time_ &lt; #{firstDay} THEN #{firstDay} ELSE DATE_FORMAT(start_time_,'%Y-%m-%d') END,
		CASE WHEN DATE_FORMAT(end_time_,'%Y-%m') > #{month} THEN #{lastDay} ELSE DATE_FORMAT(end_time_,'%Y-%m-%d') END) + 1)),2) 'value'
		from cloud_teacher_order cto
		where operating_amount_ > 0 AND #{month} BETWEEN DATE_FORMAT(start_time_,'%Y-%m')
		AND DATE_FORMAT(end_time_,'%Y-%m') group by cto.organ_id_;
	</select>
	<select id="sumGroupCloudAmount" resultType="java.util.Map">
		select orc.organ_id_ 'key',TRUNCATE(SUM(orc.cloud_price_ / (TIMESTAMPDIFF(DAY,orc.start_date_,orc.end_date_) + 1) *
		(TIMESTAMPDIFF(DAY,CASE WHEN orc.start_date_ &lt; #{firstDay} THEN #{firstDay} ELSE orc.start_date_ END,
		CASE WHEN DATE_FORMAT(orc.end_date_,'%Y-%m') > #{month} THEN #{lastDay} ELSE orc.end_date_ END) + 1) ),2) 'value' from operating_report_cloud orc
		where #{month} BETWEEN DATE_FORMAT(orc.start_date_,'%Y-%m') AND DATE_FORMAT(orc.end_date_,'%Y-%m') AND orc.cloud_price_ > 0 AND orc.start_date_  IS NOT NULL;
	</select>
	<select id="sumRefundAmount" resultType="java.util.Map">
		select suc.organ_id_ 'key',SUM(amount_) 'value' from sys_user_cash_account_log suc
		where DATE_FORMAT(suc.create_time_,'%Y-%m') = #{month} AND suc.amount_ > 0 group by suc.organ_id_;
	</select>
	<select id="sumVariableCosts" resultType="java.util.Map">
		select fe.organ_id_ 'key',SUM(amount_) 'value' from financial_expenditure fe
		where DATE_FORMAT(fe.create_time_,'%Y-%m') = #{month} AND fee_project_ BETWEEN 6 AND 21 AND process_id_ = 28 group by fe.organ_id_;
	</select>
	<select id="sumFixedCosts" resultType="java.util.Map">
		select fe.organ_id_ 'key',SUM(amount_) 'value' from financial_expenditure fe
		where DATE_FORMAT(fe.create_time_,'%Y-%m') = #{month} AND fee_project_ IN (1,2,4,5) AND process_id_ = 28 group by fe.organ_id_;
	</select>
	<select id="sumCloudPreAmount" resultType="java.util.Map">
		select cto.organ_id_ 'key',TRUNCATE(SUM(operating_amount_ / (TIMESTAMPDIFF(DAY,cto.start_time_,cto.end_time_) + 1) *
		(TIMESTAMPDIFF(DAY,CASE WHEN start_time_ &lt; #{lastDay} THEN #{lastDay} ELSE start_time_ END,end_time_) + 1) ),2) 'value'
		from cloud_teacher_order cto
		where operating_amount_ > 0 AND DATE_FORMAT(end_time_,'%Y-%m') > #{month} group by cto.organ_id_;
	</select>
	<select id="sumGroupCloudPreAmount" resultType="java.util.Map">
		select orc.organ_id_ 'key',TRUNCATE(SUM(orc.cloud_price_ / (TIMESTAMPDIFF(DAY,orc.start_date_,orc.end_date_) + 1) * (TIMESTAMPDIFF(DAY,#{lastDay},orc.end_date_) + 1) ),2) 'value'
		from operating_report_cloud orc
		where DATE_FORMAT(orc.end_date_,'%Y-%m') > #{month} AND orc.cloud_price_ > 0 AND orc.month_flag_ = false group by orc.organ_id_;
	</select>
	<select id="sumGroupAmount" resultType="java.util.Map">
		select orc.organ_id_ 'key',SUM(orc.cloud_price_) 'value'
		from operating_report_cloud orc
		where orc.cloud_price_ > 0 AND orc.month_flag_ = true AND DATE_FORMAT(orc.create_time_,'%Y-%m') = #{month} group by orc.organ_id_;
	</select>
	<select id="sumCloudCoursePreAmount" resultType="java.util.Map">
		SELECT cs.organ_id_ 'key',SUM(ctf.cost_) 'value' FROM cloud_teacher_free_course ctf
		LEFT JOIN course_schedule cs ON cs.id_ = ctf.course_id_
		where cs.del_flag_ = 0 AND cs.is_lock_ = 0 AND cs.pre_course_flag_ = 0
		AND (cs.new_course_id_ IS NULL OR cs.new_course_id_ = cs.id_) AND cs.class_date_ > #{lastDay}
		group by cs.organ_id_;
	</select>
	<select id="sumCoursePreAmount" resultType="java.util.Map">
		SELECT cs.organ_id_ 'key',SUM(cssp.actual_price_) 'value' FROM course_schedule cs
		LEFT JOIN course_schedule_student_payment cssp ON cssp.course_schedule_id_ = cs.id_
		where cs.del_flag_ = 0 AND cs.is_lock_ = 0 AND cs.pre_course_flag_ = 0
		AND (cs.new_course_id_ IS NULL OR cs.new_course_id_ = cs.id_) AND cs.class_date_ > #{lastDay}
		AND cs.group_type_ != 'MUSIC'
		group by cs.organ_id_;
	</select>
	<select id="sumOtherPreAmount" resultType="java.util.Map">
		select orc.organ_id_ 'key',SUM(orc.cloud_price_) 'value' from operating_report_cloud orc
		where orc.course_price_ = 0 AND orc.month_flag_ = false AND orc.settlement_flag_ = false GROUP BY orc.organ_id_;
	</select>
	<select id="sumSalePreAmount" resultType="java.util.Map">
		select mg.organ_id_ 'key',SUM(so.actual_amount_) 'value' from sell_order so
		left join student_payment_order spo ON spo.order_no_ = so.order_no_
		left join music_group mg ON mg.id_ = spo.music_group_id_ AND spo.group_type_ = 'MUSIC'
		where mg.musical_instruments_provide_status_ = 0 AND so.actual_amount_ > 0
		group by mg.organ_id_;
	</select>
	<select id="sumArrearsAmount" resultType="java.util.Map">
		select a.organ_id_ 'key',SUM(a.amount_) 'value' FROM (
		select SUM(mgpc.current_total_amount_) amount_,mgpc.organ_id_ from music_group_payment_calender_detail mgpcd
		LEFT JOIN (select * from music_group_payment_student_course_detail where used_course_minutes_ > 0 group by music_group_payment_calender_id_,user_id_) mgpscd 			ON mgpscd.music_group_payment_calender_detail_id_ = mgpcd.id_
		LEFT JOIN music_group_payment_calender mgpc ON mgpc.id_ = mgpscd.music_group_payment_calender_id_
		WHERE mgpcd.payment_status_ = 'NON_PAYMENT' AND mgpscd.used_course_minutes_ > 0 AND mgpc.current_total_amount_ > 0
		GROUP BY mgpscd.music_group_payment_calender_id_,mgpscd.user_id_) a
		group by organ_id_
	</select>
	<select id="sumSchoolAmount" resultType="java.util.Map">
		select mg.organ_id_ 'key',SUM(mg.amount_) 'value' from (
		select mgpc.organ_id_,mgpc.current_total_amount_ -
		CASE WHEN SUM(spro.service_amount_) IS NULL THEN 0 ELSE SUM(spro.service_amount_) END
			- CASE WHEN SUM(spro.sale_amount_) IS NULL THEN 0 ELSE SUM(spro.sale_amount_) END amount_ from (
		select mgpc.* from music_group_payment_calender mgpc
		left join music_group_payment_calender_detail mgpcd ON mgpc.id_ = mgpcd.music_group_payment_calender_id_
		where mgpcd.id_ IS NOT NULL AND mgpc.pay_user_type_ = 'SCHOOL'
		group by mgpc.id_) mgpc
		left join student_payment_route_order spro ON spro.calender_id_ = mgpc.id_ AND spro.audit_status_ = 'PASS'
		group by mgpc.id_) mg group by mg.organ_id_
	</select>
	<select id="sumMusicMargin" resultType="java.util.Map">
		select mm.organ_id_ 'key',SUM(mm.balance_) 'value' from music_margin mm where mm.refund_flag_ = 0 group by mm.organ_id_;
	</select>
	<select id="sumInternalSettlement" resultType="java.util.Map">
		select fe.organ_id_ 'key',SUM(amount_) 'value' from financial_expenditure fe
		where DATE_FORMAT(fe.create_time_,'%Y-%m') = #{month} AND fee_project_ = 22 AND process_id_ = 28 group by fe.organ_id_;
	</select>
	<select id="sumPrepayments" resultType="java.util.Map">
		select fe.organ_id_ 'key',SUM(amount_) 'value' from financial_expenditure fe
		where DATE_FORMAT(fe.create_time_,'%Y-%m') = #{month} AND fee_project_ = 3 AND process_id_ = 28 group by fe.organ_id_;
	</select>
	<select id="sumActivityUserMapperAmount" resultType="java.util.Map">
		SELECT mgpc.organ_id_ 'key',(SUM(CASE WHEN aum.sub_course_num_ > 0 AND aum.category_id_ != #{categoryId} THEN aum.sub_course_num_ ELSE 0 END) +
							   SUM(CASE WHEN aum.sub_give_course_num_ > 0 AND aum.give_category_id_ != #{categoryId} THEN aum.sub_give_course_num_ ELSE 0 END)) *
									CASE WHEN oct.id_ IS NULL THEN oct1.price_ ELSE oct.price_ END 'value'
		FROM activity_user_mapper aum
				 LEFT JOIN music_group_payment_calender mgpc ON mgpc.id_ = aum.calender_id_
				 left join organ_course_type_original_cost oct ON oct.course_type_ = 'VIP' AND oct.organ_id_ = mgpc.organ_id_
				 left join organ_course_type_original_cost oct1 ON oct1.course_type_ = 'VIP' AND oct1.organ_id_ = 0
		WHERE aum.calender_id_ IS NOT NULL AND aum.return_fee_ = 0
		  AND ((aum.sub_course_num_ > 0 AND aum.category_id_ != #{categoryId}) OR (aum.sub_give_course_num_ > 0 AND aum.give_category_id_ != #{categoryId}))
		GROUP BY mgpc.organ_id_
	</select>
	<select id="sumMusicCourseAmount" resultType="java.util.Map">
		select mg.organ_id_ 'key',SUM(sr.surplus_course_fee_) 'value' from student_registration sr
		left join music_group mg ON mg.id_ = sr.music_group_id_
		where sr.surplus_course_fee_ > 0
		group by mg.organ_id_;
	</select>
	<select id="sumSubActivityUserMapperAmount" resultType="java.util.Map">
		select su.organ_id_ 'key',SUM(aum.sub_no_course_price_) 'value' from activity_user_mapper aum
		LEFT JOIN sys_user su ON su.id_ = aum.user_id_
		where aum.return_fee_ = 0 AND aum.sub_no_course_price_ > 0
		group by su.organ_id_
	</select>
	<select id="sumImportServerOrderAmount" resultType="java.util.Map">
		select spo.organ_id_ 'key',SUM(spro.service_amount_) 'value' from student_payment_order spo
		LEFT JOIN student_payment_route_order spro ON spro.order_no_ = spo.order_no_
		where spo.group_type_ = 'OUTORDER' AND DATE_FORMAT(spo.create_time_,'%Y-%m') = #{month}
		GROUP BY spo.organ_id_;
	</select>
	<select id="sumWaitCloudPreAmount" resultType="java.util.Map">
		select cto.organ_id_ 'key',SUM(cto.operating_amount_) 'value' from cloud_teacher_order cto
		where cto.status_ = 1 AND cto.operating_amount_ > 0
		group by cto.organ_id_;
	</select>
	<select id="sumMaintenanceAmount" resultType="java.util.Map">
		select spo.organ_id_ 'key',SUM(spod.price_) 'value' from student_payment_order_detail spod
		left join student_payment_order spo ON spo.id_ = spod.payment_order_id_
		where spod.type_ = 'MAINTENANCE' AND DATE_FORMAT(spod.create_time_,'%Y-%m') = #{month} AND spo.status_ = 'SUCCESS'
		group by spo.organ_id_
	</select>
	<select id="sumLossCloudAmount" resultType="java.util.Map">
		select organ_id_ 'key',SUM(cloud_price_) 'value' from operating_report_cloud where cloud_price_ &lt; 0 AND DATE_FORMAT(start_date_,'%Y-%m') = #{month}
		group by organ_id_;
	</select>
	<select id="sumNotStartCloudPreAmount" resultType="java.util.Map">
		select orc.organ_id_ 'key',SUM(orc.cloud_price_) 'value' from operating_report_cloud orc
		where orc.start_date_ IS NULL AND orc.course_price_ > 0
		group by orc.organ_id_
	</select>
</mapper>
