<?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.SporadicChargeInfoDao">

    <resultMap type="com.ym.mec.biz.dal.entity.SporadicChargeInfo" id="SporadicChargeInfo">
        <result column="id_" property="id"/>
        <result column="title_" property="title"/>
        <result column="charge_type_" property="chargeType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="organ_id_" property="organId"/>
        <result column="amount_" property="amount"/>
        <result column="discount_amount_" property="discountAmount"/>
        <result column="detail_" property="detail"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="operator_id_" property="operatorId"/>
        <result column="del_flag_" property="delFlag"/>
        <result column="open_flag_" property="openFlag"/>
        <result column="organ_name_" property="organName"/>
        <result column="user_id_" property="userId"/>
        <result column="music_group_id_" property="musicGroupId"/>
        <result column="max_num_" property="maxNum"/>
        <result column="max_amount_" property="maxAmount"/>
        <result column="paid_amount_" property="paidAmount"/>
        <result column="open_max_term_" property="openMaxTerm" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="version_" property="version"/>
    </resultMap>

    <!-- 根据主键查询一条记录 -->
    <select id="get" resultMap="SporadicChargeInfo">
        SELECT *
        FROM sporadic_charge_info
        WHERE id_ = #{id}
    </select>

    <!-- 全查询 -->
    <select id="findAll" resultMap="SporadicChargeInfo">
        SELECT *
        FROM sporadic_charge_info
        ORDER BY id_
    </select>

    <!-- 向数据库增加一条记录 -->
    <insert id="insert" parameterType="com.ym.mec.biz.dal.entity.SporadicChargeInfo"
            useGeneratedKeys="true" keyColumn="id" keyProperty="id">
        INSERT INTO sporadic_charge_info
        (title_, charge_type_, organ_id_, amount_, discount_amount_, detail_, create_time_, update_time_, operator_id_,
         user_id_, music_group_id_, max_num_,max_amount_,open_max_term_)
        VALUES (#{title}, #{chargeType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
                #{organId}, #{amount}, #{discountAmount}, #{detail}, now(), now(), #{operatorId}, #{userId},
                #{musicGroupId}, #{maxNum},#{maxAmount},#{openMaxTerm,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler})
    </insert>

    <!-- 根据主键查询一条记录 -->
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.SporadicChargeInfo">
        UPDATE sporadic_charge_info
        <set>
            <if test="musicGroupId != null">
                music_group_id_ = #{musicGroupId},
            </if>
            <if test="title != null">
                title_ = #{title},
            </if>
            <if test="openFlag != null">
                open_flag_ = #{openFlag},
            </if>
            <if test="chargeType != null">
                charge_type_ = #{chargeType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="organId != null">
                organ_id_ = #{organId},
            </if>
            <if test="amount != null">
                amount_ = #{amount},
            </if>
            <if test="discountAmount != null">
                discount_amount_ = #{discountAmount},
            </if>
            <if test="detail != null">
                detail_ = #{detail},
            </if>
            <if test="updateTime != null">
                update_time_ = now(),
            </if>
            <if test="operatorId != null">
                operator_id_ = #{operatorId},
            </if>
            <if test="userId != null">
                user_id_ = #{userId},
            </if>
            <if test="maxNum != null">
                max_num_ = #{maxNum},
            </if>
            <if test="paidNum != null">
                paid_num_ = #{paidNum},
            </if>
            <if test="maxAmount != null">
                max_amount_ = #{maxAmount},
            </if>
            <if test="paidAmount != null">
                paid_amount_ = #{paidAmount},
            </if>
            <if test="openMaxTerm != null">
                open_max_term_ = #{openMaxTerm,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="version != null">
                version_ = version_+1,
            </if>
        </set>
        WHERE id_ = #{id} AND version_ = #{version}
    </update>

    <!-- 根据主键删除一条记录 -->
    <update id="delete">
        UPDATE sporadic_charge_info
        SET del_flag_ = 1
        WHERE id_ = #{id}
    </update>

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="SporadicChargeInfo" parameterType="map">
        SELECT sci.* FROM sporadic_charge_info sci
        LEFT JOIN sys_user su ON su.id_ = sci.user_id_
        <include refid="queryPageSql"/>
        <include refid="global.orderby"/>
        <include refid="global.limit"/>
    </select>

    <sql id="queryPageSql">
        <where>
            sci.del_flag_ != 1
            <if test="organId != null">
                AND FIND_IN_SET(sci.organ_id_,#{organId})
            </if>
            <if test="chargeType != null">
                AND sci.charge_type_ = #{chargeType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
            <if test="type == 'personal'">
                AND sci.user_id_ IS NOT NULL
            </if>
            <if test="type == 'common'">
                AND sci.user_id_ IS NULL
            </if>
            <if test="openFlag != null">
                AND sci.open_flag_ = #{openFlag}
            </if>
            <if test="search != null">
                AND (sci.title_ LIKE CONCAT('%',#{search},'%') OR su.username_ LIKE CONCAT('%',#{search},'%'))
            </if>
        </where>
    </sql>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
        SELECT COUNT(sci.id_) FROM sporadic_charge_info sci
        LEFT JOIN sys_user su ON su.id_ = sci.user_id_
        <include refid="queryPageSql"/>
    </select>
    <select id="findInfoById" resultMap="SporadicChargeInfo">
        SELECT *
        FROM sporadic_charge_info
        where id_ = #{id}
    </select>
    <!-- 根据类型和分部id查询活动 -->
    <select id="findByOrganIdAndType" resultMap="SporadicChargeInfo">
        SELECT *
        FROM sporadic_charge_info
        where organ_id_ = #{organId}
          AND charge_type_ = #{chargeType}
          AND del_flag_ = 0
    </select>
    <select id="getOrganActiveInfo" resultMap="SporadicChargeInfo">
        SELECT *
        FROM sporadic_charge_info
        where organ_id_ = #{organId}
          AND charge_type_ = #{chargeType}
          AND del_flag_ = 0
    </select>

    <select id="getActiveOrgans" resultMap="com.ym.mec.biz.dal.dao.OrganizationDao.Organization">
        SELECT o.*
        FROM sporadic_charge_info sci
                 LEFT JOIN organization o ON o.id_ = sci.organ_id_
        WHERE sci.charge_type_ = #{type}
          AND sci.del_flag_ = 0
        GROUP BY o.id_
    </select>

    <select id="getActiveOrgansMaxAmount" resultType="com.ym.mec.biz.dal.dto.OrganMaxAmountDto">
        SELECT o.id_ id,o.name_ name ,(SUM(sci.max_amount_)/COUNT(*)) maxAmount
        FROM sporadic_charge_info sci
        LEFT JOIN organization o ON o.id_ = sci.organ_id_
        WHERE sci.charge_type_ = #{type}
        AND sci.del_flag_ = 0
        GROUP BY o.id_
    </select>

    <sql id="findSporadicChargeInfosCondition">
        <where>
            del_flag_ = 0
            <if test="organId!=null">
                AND organ_id_=#{organId}
            </if>
            <if test="organIds!=null">
                AND organ_id_ IN
                <foreach collection="organIds" item="organId" open="(" close=")" separator=",">
                    #{organId}
                </foreach>
            </if>
            <if test="chargeType!=null">
                AND charge_type_ = #{chargeType}
            </if>
        </where>
    </sql>

    <select id="findSporadicChargeInfos" resultMap="SporadicChargeInfo">
        SELECT *
        FROM sporadic_charge_info
        <include refid="findSporadicChargeInfosCondition" />
    </select>
</mapper>
