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

    <resultMap type="com.ym.mec.biz.dal.entity.CooperationOrgan"
               id="CooperationOrgan">
        <result column="id_" property="id"/>
        <result column="education_user_id_" property="educationUserId"/>
        <result column="real_name_" property="realName"/>
        <result column="name_" property="name"/>
        <result column="linkman_" property="linkman"/>
        <result column="organ_id_" property="organId"/>
        <result column="job_" property="job"/>
        <result column="mobile_no_" property="mobileNo"/>
        <result column="address_" property="address"/>
        <result column="is_enable_" property="isEnable"/>
        <result column="ownership_type_" property="ownershipType"
                typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="full_job_resource_" property="fullJobResource"/>
    </resultMap>

    <resultMap type="com.ym.mec.biz.dal.entity.CooperationOrgan" id="ExtCooperationOrgan" extends="CooperationOrgan">
        <result column="organ_id_" property="organization.id"/>
        <result column="organ_name_" property="organization.name"/>
    </resultMap>

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

    <!-- 全查询 -->
    <select id="findAll" resultMap="CooperationOrgan">
        SELECT *
        FROM cooperation_organ
        WHERE del_flag_ != 1
        ORDER BY id_
    </select>

    <!-- 向数据库增加一条记录 -->
    <insert id="insert" parameterType="com.ym.mec.biz.dal.entity.CooperationOrgan"
            useGeneratedKeys="true" keyColumn="id" keyProperty="id">
        INSERT INTO cooperation_organ
        (id_, name_, linkman_, job_, mobile_no_, address_, organ_id_, create_time_, update_time_, ownership_type_,
         is_enable_, full_job_resource_,education_user_id_)
        VALUES (#{id}, #{name}, #{linkman}, #{job}, #{mobileNo}, #{address}, #{organId}, now(), now(),
                #{ownershipType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}, #{isEnable},
                #{fullJobResource},#{educationUserId})
    </insert>

    <!-- 根据主键查询一条记录 -->
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.CooperationOrgan">
        UPDATE cooperation_organ
        <set>
            <if test="educationUserId != null">
                education_user_id_ = #{educationUserId},
            </if>
            <if test="fullJobResource != null">
                full_job_resource_ = #{fullJobResource},
            </if>
            <if test="address != null">
                address_ = #{address},
            </if>
            <if test="job != null">
                job_ = #{job},
            </if>
            <if test="linkman != null">
                linkman_ = #{linkman},
            </if>
            <if test="updateTime != null">
                update_time_ = NOW(),
            </if>
            <if test="mobileNo != null">
                mobile_no_ = #{mobileNo},
            </if>
            <if test="name != null">
                name_ = #{name},
            </if>
            <if test="ownershipType != null">
                ownership_type_ = #{ownershipType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="organId != null">
                organ_id_ = #{organId},
            </if>
            <if test="isEnable != null">
                is_enable_ = #{isEnable},
            </if>
        </set>
        WHERE id_ = #{id}
    </update>

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

    <update id="batchUpdateEduId">
        UPDATE cooperation_organ SET education_user_id_ = #{transferUserId} WHERE organ_id_ = #{organId} AND education_user_id_ = #{levelUserId}
    </update>

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="ExtCooperationOrgan"
            parameterType="map">
        SELECT co.*,o.name_ organ_name_,su.real_name_ FROM cooperation_organ co
        LEFT JOIN organization o ON co.organ_id_ = o.id_
        LEFT JOIN sys_user su ON su.id_ = co.education_user_id_
        <include refid="queryPageSql"/>
        ORDER BY co.update_time_ DESC
        <include refid="global.limit"/>
    </select>

    <sql id="queryPageSql">
        <where>
            co.del_flag_ != 1
            <if test="search != null">
                AND (co.education_user_id_ = #{search} OR su.real_name_ LIKE CONCAT('%',#{search},'%')
                OR co.id_ LIKE CONCAT('%',#{search},'%') OR co.name_ LIKE CONCAT('%',#{search},'%'))
            </if>
            <if test="organId != null">
                AND FIND_IN_SET(co.organ_id_,#{organId})
            </if>
            <if test="isEnable != null">
                AND co.is_enable_ = #{isEnable}
            </if>
            <if test="fullJobResource != null">
                AND co.full_job_resource_ = #{fullJobResource}
            </if>
        </where>
    </sql>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
        SELECT COUNT(co.id_) FROM cooperation_organ co
        LEFT JOIN sys_user su ON su.id_ = co.education_user_id_
        <include refid="queryPageSql"/>
    </select>

    <select id="queryByOrganId" resultMap="CooperationOrgan">
        SELECT *
        FROM cooperation_organ
        WHERE FIND_IN_SET(organ_id_, #{organId})
          AND del_flag_ != 1
          AND is_enable_ = 1
    </select>

    <select id="queryNameByIds" resultType="java.util.Map">
        SELECT co.id_ 'key', co.name_ 'value'
        FROM cooperation_organ co
        WHERE FIND_IN_SET(co.id_, #{organIds})
          AND co.del_flag_ != 1
    </select>

    <select id="getOrganFullJobResourceNum" resultType="com.ym.mec.biz.dal.dto.OrganFullJobResourceNumDto">
        SELECT organ_id_ organId, COUNT(*) num
        FROM cooperation_organ
        WHERE full_job_resource_ = 1
          AND del_flag_ = 0
        GROUP BY organ_id_
    </select>

    <!-- 获取全部使用中的合作单位 -->
    <select id="getAllCooperationOrgan" resultMap="CooperationOrgan">
        SELECT *
        FROM cooperation_organ
        WHERE del_flag_ = 0
    </select>

    <!-- 获取合作单位乐团的的学校缴费 -->
    <select id="getCooperationOrganCalender4School"
            resultMap="com.ym.mec.biz.dal.dao.MusicGroupPaymentCalenderDao.MusicGroupPaymentCalender">
        SELECT mgpc.*
        FROM music_group mg
                 LEFT JOIN music_group_payment_calender mgpc ON mgpc.music_group_id_ = mg.id_
        WHERE mg.cooperation_organ_id_ = #{cooperationOrganId}
          AND mgpc.pay_user_type_ = 'SCHOOL'
          AND mgpc.status_ IN ('NO', 'OPEN', 'OVER')
        ORDER BY mgpc.payment_valid_start_date_ ASC
    </select>

    <select id="getCooperationOrganByIds" resultMap="CooperationOrgan">
        SELECT * FROM cooperation_organ WHERE id_ IN
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>

    <select id="getCooperationOrganByEduTeacherId" resultMap="CooperationOrgan">
        SELECT * FROM cooperation_organ WHERE education_user_id_ = #{userId} and is_enable_ =1 and del_flag_ =0
    </select>
</mapper>
