<?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.OperatingReportDao">
    <resultMap id="OperatingReport" type="com.ym.mec.biz.dal.entity.OperatingReport">
        <!--@mbg.generated-->
        <!--@Table operating_report-->
        <id column="id_" property="id"/>
        <result column="organ_id_" property="organId"/>
        <result column="organ_name_" property="organName"/>
        <result column="cooperation_organ_id_" property="cooperationOrganId"/>
        <result column="school_name_" property="schoolName"/>
        <result column="sell_amount_" property="sellAmount"/>
        <result column="service_amount_" property="serviceAmount"/>
        <result column="other_income_" property="otherIncome"/>
        <result column="refund_amount_" property="refundAmount"/>
        <result column="income_total_" property="incomeTotal"/>
        <result column="sell_cost_" property="sellCost"/>
        <result column="expenses_amount_" property="expensesAmount"/>
        <result column="variable_cost_" property="variableCost"/>
        <result column="distribution_amount_" property="distributionAmount"/>
        <result column="cost_amount_" property="costAmount"/>
        <result column="profit_" property="profit"/>
        <result column="month_" property="month"/>
        <result column="create_time_" property="createTime"/>
    </resultMap>
    <sql id="Base_Column_List">
        <!--@mbg.generated-->
        id_, organ_id_, cooperation_organ_id_, sell_amount_, service_amount_,other_income_, refund_amount_,
        income_total_,
        sell_cost_, expenses_amount_, variable_cost_, distribution_amount_, cost_amount_,
        profit_, month_, create_time_
    </sql>
    <select id="get" parameterType="java.lang.Integer" resultMap="OperatingReport">
        <!--@mbg.generated-->
        select
        <include refid="Base_Column_List"/>
        from operating_report
        where id_ = #{id}
    </select>
    <delete id="delete" parameterType="java.lang.Integer">
        <!--@mbg.generated-->
        delete from operating_report
        where id_ = #{id}
    </delete>
    <insert id="insert" keyColumn="id_" keyProperty="id" parameterType="com.ym.mec.biz.dal.entity.OperatingReport"
            useGeneratedKeys="true">
        <!--@mbg.generated-->
        insert into operating_report (organ_id_, cooperation_organ_id_, sell_amount_, service_amount_,other_income_,
        refund_amount_,
        income_total_, sell_cost_, expenses_amount_, variable_cost_, distribution_amount_,
        cost_amount_, profit_, month_, create_time_)
        values (#{organId}, #{cooperationOrganId}, #{sellAmount}, #{serviceAmount},#{otherIncome}, #{refundAmount},
        #{incomeTotal}, #{sellCost}, #{expensesAmount}, #{variableCost}, #{distributionAmount},
        #{costAmount}, #{profit}, #{month}, #{createTime})
    </insert>

    <update id="update" parameterType="com.ym.mec.biz.dal.entity.OperatingReport">
        <!--@mbg.generated-->
        update operating_report
        <set>
            <if test="organId != null">
                organ_id_ = #{organId},
            </if>
            <if test="cooperationOrganId != null">
                cooperation_organ_id_ = #{cooperationOrganId},
            </if>
            <if test="sellAmount != null">
                sell_amount_ = #{sellAmount},
            </if>
            <if test="serviceAmount != null">
                service_amount_ = #{serviceAmount},
            </if>
            <if test="otherIncome != null">
                other_income_ = #{otherIncome},
            </if>
            <if test="refundAmount != null">
                refund_amount_ = #{refundAmount},
            </if>
            <if test="incomeTotal != null">
                income_total_ = #{incomeTotal},
            </if>
            <if test="sellCost != null">
                sell_cost_ = #{sellCost},
            </if>
            <if test="expensesAmount != null">
                expenses_amount_ = #{expensesAmount},
            </if>
            <if test="variableCost != null">
                variable_cost_ = #{variableCost},
            </if>
            <if test="distributionAmount != null">
                distribution_amount_ = #{distributionAmount},
            </if>
            <if test="costAmount != null">
                cost_amount_ = #{costAmount},
            </if>
            <if test="profit != null">
                profit_ = #{profit},
            </if>
            <if test="month != null">
                month_ = #{month},
            </if>
            <if test="createTime != null">
                create_time_ = #{createTime},
            </if>
        </set>
        where id_ = #{id}
    </update>


    <!-- 分页查询 -->
    <select id="queryPage" resultMap="OperatingReport" parameterType="map">
        SELECT opr.*,o.name_ organ_name_,co.name_ school_name_ FROM operating_report opr
        LEFT JOIN organization o ON o.id_ = opr.organ_id_
        LEFT JOIN cooperation_organ co ON co.id_= opr.cooperation_organ_id_
        <include refid="queryPageSql"/>
        ORDER BY opr.month_ DESC,opr.organ_id_ ASC
        <include refid="global.limit"/>
    </select>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
        SELECT COUNT(*) FROM operating_report opr
        <include refid="queryPageSql"/>
    </select>

    <sql id="queryPageSql">
        <where>
            <if test="organIdList != null and organIdList != ''">
                AND FIND_IN_SET(opr.organ_id_,#{organIdList})
            </if>
            <if test="type != null and type=='organ'.toString()">
                AND opr.cooperation_organ_id_ IS NULL
            </if>
            <if test="type != null and type=='school'.toString()">
                AND opr.cooperation_organ_id_ IS NOT NULL
            </if>
            <if test="cooperationOrganId != null">
                AND opr.cooperation_organ_id_ = #{cooperationOrganId}
            </if>
            <if test="startTime != null">
                AND opr.month_ >= #{startTime}
            </if>
            <if test="endTime != null">
                AND opr.month_ &lt;= #{endTime}
            </if>
        </where>
    </sql>
    <select id="getReport" resultMap="OperatingReport" parameterType="map">
        SELECT * FROM operating_report
        <where>
            <if test="organId != null">
                AND organ_id_ = #{organId}
            </if>
            <if test="cooperationOrganId != null">
                AND cooperation_organ_id_ = #{cooperationOrganId}
            </if>
            <if test="cooperationOrganId == null">
                AND cooperation_organ_id_ IS NULL
            </if>
            <if test="startTime != null">
                AND month_ >= #{startTime}
            </if>
            <if test="endTime != null">
                AND month_ &lt;= #{endTime}
            </if>
        </where>
        LIMIT 1
    </select>
</mapper>