<?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.ruoyi.project.benyi.mapper.BySchoolchargeMapper">

    <resultMap type="BySchoolcharge" id="BySchoolchargeResult">
        <result property="id" column="id"/>
        <result property="deptId" column="dept_id"/>
        <result property="byf" column="byf"/>
        <result property="hsf" column="hsf"/>
        <result property="createUserid" column="create_userid"/>
        <result property="createTime" column="create_time"/>
        <result property="byfX" column="byf_x"/>
        <result property="hsfX" column="hsf_x"/>
        <result property="byfT" column="byf_t"/>
        <result property="hsfT" column="hsf_t"/>
        <result property="byfZ" column="byf_z"/>
        <result property="hsfZ" column="hsf_z"/>
        <result property="month" column="month"/>
        <result property="name" column="name"/>
        <result property="classid" column="classid"/>
        <result property="days" column="days"/>
        <result property="bjtype" column="bjtype"/>
        <association property="dept" column="dept_id" javaType="SysDept" resultMap="deptResult"/>
    </resultMap>

    <resultMap id="deptResult" type="SysDept">
        <id property="deptId" column="dept_id"/>
        <result property="parentId" column="parent_id"/>
        <result property="deptName" column="dept_name"/>
        <result property="orderNum" column="order_num"/>
        <result property="leader" column="leader"/>
        <result property="status" column="dept_status"/>
    </resultMap>

    <sql id="selectBySchoolchargeVo">
        select a.id, a.dept_id, a.byf, a.hsf, a.create_userid, a.create_time,a.byf_x, a.hsf_x, a.byf_t, a.hsf_t, a.byf_z, a.hsf_z,b.dept_name from by_schoolcharge a
        right join sys_dept b on a.dept_id=b.dept_id
    </sql>

    <select id="selectBySchoolchargeList" parameterType="BySchoolcharge" resultMap="BySchoolchargeResult">
        <include refid="selectBySchoolchargeVo"/>
        where school_id is not null and del_flag=0
        <if test="byf != null ">and byf = #{byf}</if>
        <if test="hsf != null ">and hsf = #{hsf}</if>
        <if test="createUserid != null ">and create_userid = #{createUserid}</if>
        <!-- 数据范围过滤 -->
        ${dataScope}
    </select>

    <select id="selectByChildchargeList" parameterType="BySchoolcharge" resultMap="BySchoolchargeResult">
        select a.name,a.classid,
        c.bjtype,
        (select count(*) from by_child_checkin_detail where childid=a.id and type='01' and
        date_format(create_time,'%Y-%m')=concat(#{month})) as days ,
        b.id, b.dept_id, b.byf, b.hsf, b.create_userid, b.create_time,b.byf_x, b.hsf_x, b.byf_t, b.hsf_t, b.byf_z,
        b.hsf_z
        from by_child a
        left join by_class c on a.classid=c.bjbh
        left join by_schoolcharge b on a.schoolid=b.dept_id
        where c.bjbh = #{classid}
        <!-- 数据范围过滤 -->
        ${dataScope}
    </select>

    <select id="selectBySchoolchargeById" parameterType="Long" resultMap="BySchoolchargeResult">
        <include refid="selectBySchoolchargeVo"/>
        where a.id = #{id}
    </select>

    <insert id="insertBySchoolcharge" parameterType="BySchoolcharge" useGeneratedKeys="true" keyProperty="id">
        insert into by_schoolcharge
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deptId != null ">dept_id,</if>
            <if test="byf != null  and byf != ''">byf,</if>
            <if test="hsf != null ">hsf,</if>
            <if test="createUserid != null ">create_userid,</if>
            <if test="createTime != null ">create_time,</if>
            <if test="byfX != null  and byfX != ''">byf_x,</if>
            <if test="hsfX != null ">hsf_x,</if>
            <if test="byfT != null  and byfT != ''">byf_t,</if>
            <if test="hsfT != null ">hsf_t,</if>
            <if test="byfZ != null  and byfZ != ''">byf_z,</if>
            <if test="hsfZ != null ">hsf_z,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deptId != null ">#{deptId},</if>
            <if test="byf != null  and byf != ''">#{byf},</if>
            <if test="hsf != null ">#{hsf},</if>
            <if test="createUserid != null ">#{createUserid},</if>
            <if test="createTime != null ">#{createTime},</if>
            <if test="byfX != null  and byfX != ''">#{byfX},</if>
            <if test="hsfX != null ">#{hsfX},</if>
            <if test="byfT != null  and byfT != ''">#{byfT},</if>
            <if test="hsfT != null ">#{hsfT},</if>
            <if test="byfZ != null  and byfZ != ''">#{byfZ},</if>
            <if test="hsfZ != null ">#{hsfZ},</if>
        </trim>
    </insert>

    <update id="updateBySchoolcharge" parameterType="BySchoolcharge">
        update by_schoolcharge
        <trim prefix="SET" suffixOverrides=",">
            <if test="deptId != null ">dept_id = #{deptId},</if>
            <if test="byf != null  and byf != ''">byf = #{byf},</if>
            <if test="hsf != null ">hsf = #{hsf},</if>
            <if test="createUserid != null ">create_userid = #{createUserid},</if>
            <if test="createTime != null ">create_time = #{createTime},</if>
            <if test="byfX != null  and byfX != ''">byf_x = #{byfX},</if>
            <if test="hsfX != null ">hsf_x = #{hsfX},</if>
            <if test="byfT != null  and byfT != ''">byf_t = #{byfT},</if>
            <if test="hsfT != null ">hsf_t = #{hsfT},</if>
            <if test="byfZ != null  and byfZ != ''">byf_z = #{byfZ},</if>
            <if test="hsfZ != null ">hsf_z = #{hsfZ},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteBySchoolchargeById" parameterType="Long">
        delete from by_schoolcharge where id = #{id}
    </delete>

    <delete id="deleteBySchoolchargeByIds" parameterType="String">
        delete from by_schoolcharge where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

</mapper>