<?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.cku.oa.trainschool.dao.TrainingInstitutionDao">
    
	<sql id="trainingInstitutionColumns">
		a.id AS "id",
		a.school_name_cn AS "schoolNameCn",
		a.school_name_en AS "schoolNameEn",
		a.school_short_name AS "schoolShortName",
		a.school_user_id AS "schoolUserId",
		a.school_member_code AS "schoolMemberCode",
		a.member_code AS "memberCode",
		a.responsibler AS "responsibler",
		a.telephone AS "telephone",
		a.province AS "province",
		a.city AS "city",
		a.street AS "street",
		a.address AS "address",
		a.start_date AS "startDate",
		a.end_date AS "endDate",
		a.school_type AS "schoolType",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		a.remarks AS "remarks",
		a.del_flag AS "delFlag",
		a.introduce AS "introduce",
		a.avatar AS "avatar",
		a.school_web AS "schoolWeb",
		a.qualifications AS "qualifications",
		a.display_onweb AS "displayOnWeb",
		a.first_school_type AS "firstSchoolType",
		b.id AS "user.id",
		b.login_flag AS "user.loginFlag",
		b.login_name AS "user.loginName",
		b.name AS "user.name",
		b.password AS "user.password",
		c.member_end_date AS "member.memberEndDate",
		c.account_balance AS "member.accountBalance"
	</sql>

	<sql id="trainingInstitutionJoins">
		LEFT JOIN jeesite_sys_user b ON a.school_user_id = b.id
		LEFT JOIN sys_member c ON a.school_member_code = c.member_code
	</sql>

	<select id="get" resultType="TrainingInstitution">
		SELECT
			<include refid="trainingInstitutionColumns"/>
		FROM training_institution a
		<include refid="trainingInstitutionJoins"/>
		WHERE a.id = #{id}
	</select>

	<select id="findList" resultType="TrainingInstitution">
		SELECT
			<include refid="trainingInstitutionColumns"/>
		FROM training_institution a
		<include refid="trainingInstitutionJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="schoolType != null and schoolType != ''">
				AND a.school_type = #{schoolType}
			</if>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>

	<select id="findGroomerList" resultType="TrainingInstitution">
		SELECT
			<include refid="trainingInstitutionColumns"/>
		,d.member_end_date AS "responsiblerMemberEndDate"
		,DATE(d.groomer_end_date) AS "responsiblerGroomerEndDate"
		FROM training_institution a
		<include refid="trainingInstitutionJoins"/>
		LEFT JOIN sys_member d ON a.member_code = d.member_code
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			AND  a.school_type in('7','1')
			<if test="schoolNameCn != null and schoolNameCn != ''">
				AND a.school_name_cn  LIKE CONCAT('%',#{schoolNameCn},'%')
			</if>
			<if test="schoolShortName != null and schoolShortName != ''">
				AND a.school_short_name = #{schoolShortName}
			</if>
			<if test="schoolMemberCode != null and schoolMemberCode != ''">
				AND a.school_member_code = #{schoolMemberCode}
			</if>
			<if test='user != null and user.loginFlag != null and user.loginFlag == "1"'>
				AND b.login_flag = '1'
			</if>
			<if test="user != null and user.id != null and user.id != ''">
				AND b.id = #{user.id}
			</if>
			<if test='user != null and user.loginFlag != null and user.loginFlag == "0"'>
				AND (b.login_flag = '0' OR b.login_flag IS NULL)
			</if>
			<if test='schoolMemberPayFlag != null and schoolMemberPayFlag == "1"'>
				AND c.member_end_date <![CDATA[>]]> date_sub(NOW(),interval 1 day)
			</if>
			<if test='schoolMemberPayFlag != null and schoolMemberPayFlag == "0"'>
				AND (c.member_end_date IS NULL OR c.member_end_date <![CDATA[<]]> date_sub(NOW(),interval 1 day))
			</if>
			<if test='responsiblerMemberPayFlag != null and responsiblerMemberPayFlag == "1"'>
				AND (
					d.member_end_date <![CDATA[>]]> date_sub(NOW(),interval 1 day)
					OR
					date_add(DATE(d.groomer_end_date),interval 1 day) <![CDATA[>]]> NOW()
				)
			</if>
			<if test='responsiblerMemberPayFlag != null and responsiblerMemberPayFlag == "0"'>
				AND (
					(d.member_end_date IS NULL OR d.member_end_date <![CDATA[<]]> date_sub(NOW(),interval 1 day))
					AND
					(d.groomer_end_date IS NULL OR date_add(DATE(d.groomer_end_date),interval 1 day) <![CDATA[<]]> NOW())
				)
			</if>
			<if test="displayOnWeb != null and displayOnWeb != ''">
				AND a.display_onweb = #{displayOnWeb}
			</if>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.end_date
			</otherwise>
		</choose>
	</select>

	<select id="findGroomerListAPP" resultType="TrainingInstitution">
		SELECT
		<include refid="trainingInstitutionColumns"/>
		,d.member_end_date AS "responsiblerMemberEndDate"
		,DATE(d.groomer_end_date) AS "responsiblerGroomerEndDate",

		(select count(*) from sys_member where registrant=a.school_name_cn )  AS "student",

		(select count(*) from groomer_course where del_flag='0' AND launch_time IS NOT null and (states='3' or states='6') and  school_id=a.id) AS "curriculumCount"
		FROM training_institution a
		<include refid="trainingInstitutionJoins"/>
		LEFT JOIN sys_member d ON a.member_code = d.member_code
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			AND  b.login_flag='1'
			AND  a.school_type in('7','1')
			<if test="schoolNameCn != null and schoolNameCn != ''">
				AND a.school_name_cn  LIKE CONCAT('%',#{schoolNameCn},'%')
			</if>
			<if test="schoolShortName != null and schoolShortName != ''">
				AND a.school_short_name = #{schoolShortName}
			</if>
			<if test="schoolMemberCode != null and schoolMemberCode != ''">
				AND a.school_member_code = #{schoolMemberCode}
			</if>
			<if test='user != null and user.loginFlag != null and user.loginFlag == "1"'>
				AND b.login_flag = '1'
			</if>
			<if test="user != null and user.id != null and user.id != ''">
				AND b.id = #{user.id}
			</if>
			<if test='user != null and user.loginFlag != null and user.loginFlag == "0"'>
				AND (b.login_flag = '0' OR b.login_flag IS NULL)
			</if>
			<if test='schoolMemberPayFlag != null and schoolMemberPayFlag == "1"'>
				AND c.member_end_date <![CDATA[>]]> date_sub(NOW(),interval 1 day)
			</if>
			<if test='schoolMemberPayFlag != null and schoolMemberPayFlag == "0"'>
				AND (c.member_end_date IS NULL OR c.member_end_date <![CDATA[<]]> date_sub(NOW(),interval 1 day))
			</if>
			<if test='responsiblerMemberPayFlag != null and responsiblerMemberPayFlag == "1"'>
				AND (
				d.member_end_date <![CDATA[>]]> date_sub(NOW(),interval 1 day)
				OR
				date_add(DATE(d.groomer_end_date),interval 1 day) <![CDATA[>]]> NOW()
				)
			</if>
			<if test='responsiblerMemberPayFlag != null and responsiblerMemberPayFlag == "0"'>
				AND (
				(d.member_end_date IS NULL OR d.member_end_date <![CDATA[<]]> date_sub(NOW(),interval 1 day))
				AND
				(d.groomer_end_date IS NULL OR date_add(DATE(d.groomer_end_date),interval 1 day) <![CDATA[<]]> NOW())
				)
			</if>
			<if test="displayOnWeb != null and displayOnWeb != ''">
				AND a.display_onweb = #{displayOnWeb}
			</if>
		</where>
		ORDER BY   (CASE WHEN a.start_date is null then '9999-12-31' else a.start_date end ) ASC

	</select>
	<!--FIELD((case  when  a.qualifications is NULL then '0' else a.qualifications end),'2','3','1','0') ASC,curriculumCount DESC,a.create_date DESC-->

	<select id="getHandlerOrgList" resultType="TrainingInstitution">
		SELECT
		<include refid="trainingInstitutionColumns"/>
		,d.member_end_date AS "responsiblerMemberEndDate"
		,DATE(d.groomer_end_date) AS "responsiblerGroomerEndDate",
		(select count(*) from sys_member where registrant=a.school_name_cn )  AS "student"
		FROM training_institution a
		<include refid="trainingInstitutionJoins"/>
		LEFT JOIN sys_member d ON a.member_code = d.member_code
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			AND  a.end_date>NOW()
			AND  a.first_school_type=#{firstSchoolType}
		</where>
		ORDER BY a.create_date
	</select>


	<!--FIELD((case  when  a.qualifications is NULL then '0' else a.qualifications end),'2','3','1','0') ASC,curriculumCount DESC,a.create_date DESC-->





	<!-- 培训的合作学校 -->
	<select id="findHandlerList" resultType="TrainingInstitution">
		SELECT 
			<include refid="trainingInstitutionColumns"/>
		FROM training_institution a
		<include refid="trainingInstitutionJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			AND (a.school_type in ('2','3','4','5','6') or a.first_school_type in ('1','2','3','4','5'))
			<if test="schoolNameCn != null and schoolNameCn != ''">
				AND a.school_name_cn  LIKE CONCAT('%',#{schoolNameCn},'%')
			</if>
			<if test="displayOnWeb != null and displayOnWeb != ''">
				AND a.display_onweb = #{displayOnWeb}
			</if>
			<if test=" firstSchoolType!= null and  firstSchoolType!= ''">
				AND a.first_school_type = #{firstSchoolType}
			</if>

		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>
	
	<select id="findAllList" resultType="TrainingInstitution">
		SELECT 
			<include refid="trainingInstitutionColumns"/>
		FROM training_institution a
		<include refid="trainingInstitutionJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
		</where>		
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>
	
	<insert id="insert">
		INSERT INTO training_institution(
			id,
			school_name_cn,
			school_name_en,
			school_short_name,
		<if test="schoolUserId != null and schoolUserId != ''">
			school_user_id,
		</if>
		<if test="schoolMemberCode != null and schoolMemberCode != ''">
			school_member_code,
		</if>
			member_code,
			responsibler,
			telephone,
		<if test="province != null and province != ''">
			province,
		</if>
		<if test="city != null and city != ''">
			city,
		</if>
		<if test="street != null and street != ''">
			street,
		</if>
		<if test="qualifications != null and qualifications != ''">
			qualifications,
		</if>
		<if test="firstSchoolType != null and firstSchoolType != ''">
			first_school_type,
		</if>
		avatar,
		introduce,
			address,
			start_date,
			end_date,
			school_type,
			school_web,
			display_onweb,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag
		) VALUES (
			#{id},
			#{schoolNameCn},
			#{schoolNameEn},
			#{schoolShortName},
		<if test="schoolUserId != null and schoolUserId != ''">
			#{schoolUserId},
		</if>
		<if test="schoolMemberCode != null and schoolMemberCode != ''">
			#{schoolMemberCode},
		</if>
			#{memberCode},
			#{responsibler},
			#{telephone},
		<if test="province != null and province != ''">
			#{province},
		</if>
		<if test="city != null and city != ''">
			#{city},
		</if>
		<if test="street != null and street != ''">
			#{street},
		</if>
		<if test="qualifications != null and qualifications != ''">
			#{qualifications},
		</if>
		<if test="firstSchoolType != null and firstSchoolType != ''">
			#{firstSchoolType},
		</if>
			#{avatar},
			#{introduce},
			#{address},
			#{startDate},
			#{endDate},
			#{schoolType},
			#{schoolWeb},
			#{displayOnWeb},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag}
		)
	</insert>
	
	<update id="update">
		UPDATE training_institution SET 	
			school_name_cn = #{schoolNameCn},
			school_name_en = #{schoolNameEn},
			school_short_name = #{schoolShortName},
			member_code = #{memberCode},
			responsibler = #{responsibler},
			telephone = #{telephone},
			province = #{province},
			city = #{city},
			street = #{street},
		<if test="qualifications != null and qualifications != ''">
			qualifications = #{qualifications},
		</if>
		<if test="firstSchoolType != null and firstSchoolType != ''">
			first_school_type = #{firstSchoolType},
		</if>
		<if test="firstSchoolType != null and firstSchoolType != ''">
			first_school_type = #{firstSchoolType},
		</if>
			avatar = #{avatar},
		introduce= #{introduce},
			address = #{address},
			start_date = #{startDate},
			end_date = #{endDate},
			school_type = #{schoolType},
			school_web = #{schoolWeb},
			display_onweb = #{displayOnWeb},
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks}
		WHERE id = #{id}
	</update>
	
	<update id="delete">
		UPDATE training_institution SET 
			remarks = concat(IFNULL(remarks,''),#{remarks}),
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	<update id="updateIntroduce">
		UPDATE training_institution SET
			introduce = &apos;${introduce}&apos;,
			avatar = #{avatar}
		WHERE id = #{id}
	</update>

    <select id="getByMemberCode" resultType="TrainingInstitution">
        SELECT 
        a.id AS "id",
		a.school_name_cn AS "schoolNameCn",
		a.school_name_en AS "schoolNameEn",
		a.school_short_name AS "schoolShortName",
		a.school_user_id AS "schoolUserId",
		a.school_member_code AS "schoolMemberCode",
		a.member_code AS "memberCode",
		a.responsibler AS "responsibler",
		a.telephone AS "telephone",
		a.province AS "province",
		a.city AS "city",
		a.street AS "street",
		a.address AS "address",
		a.start_date AS "startDate",
		a.end_date AS "endDate",
		a.school_type AS "schoolType",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		a.remarks AS "remarks",
		a.del_flag AS "delFlag"
        FROM training_institution a WHERE a.member_code = #{_parameter} limit 1
    </select>
    
    	
	<select id="getQusByMemberCode" resultType="TrainingInstitution">
	
	 	SELECT 
        a.id AS "id",
		a.school_name_cn AS "schoolNameCn",
		a.school_name_en AS "schoolNameEn",
		a.school_short_name AS "schoolShortName",
		a.school_user_id AS "schoolUserId",
		a.school_member_code AS "schoolMemberCode",
		a.member_code AS "memberCode",
		a.responsibler AS "responsibler",
		a.telephone AS "telephone",
		a.province AS "province",
		a.city AS "city",
		a.street AS "street",
		a.address AS "address",
		a.start_date AS "startDate",
		a.end_date AS "endDate",
		a.school_type AS "schoolType",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		a.remarks AS "remarks",
		a.del_flag AS "delFlag"
        FROM training_institution a 
        WHERE
		a.end_date > NOW( ) 
		AND a.del_flag = '0' 
		AND ( a.school_type IN ( '2', '3', '4', '5', '6' ) OR a.first_school_type IN ( '1', '2', '3', '4' ) )
		AND a.member_code = #{memberCode} 
        limit 1
	 </select>
    
     <select id="getBySchoolMemberCode" resultType="TrainingInstitution">
        SELECT 
        a.id AS "id",
		a.school_name_cn AS "schoolNameCn",
		a.school_name_en AS "schoolNameEn",
		a.school_short_name AS "schoolShortName",
		a.school_user_id AS "schoolUserId",
		a.school_member_code AS "schoolMemberCode",
		a.member_code AS "memberCode",
		a.responsibler AS "responsibler",
		a.telephone AS "telephone",
		a.province AS "province",
		a.city AS "city",
		a.street AS "street",
		a.address AS "address",
		a.start_date AS "startDate",
		a.end_date AS "endDate",
		a.school_type AS "schoolType",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		a.remarks AS "remarks",
		a.del_flag AS "delFlag"
        FROM training_institution a WHERE a.school_member_code = #{_parameter} limit 1
    </select>

	<select id="getTrainingInstitution" resultType="TrainingInstitution">
		SELECT
			a.school_name_cn AS "schoolNameCn",
			a.telephone AS "telephone",
			a.address AS "address"
		FROM training_institution a
		WHERE a.del_flag = '0'
		AND a.end_date > date_sub(NOW(),interval 1 day)
		AND a.school_type IN ${schoolType}
		<if test="province != null and province != ''">
			AND a.address LIKE CONCAT('%',#{province},'%')
		</if>
		GROUP BY school_name_cn,a.telephone,a.address
		limit #{pageNo},#{pageSize}
	</select>

	<select id="getTrainingInstitutionSchoolTypeList" resultType="string">
		SELECT
			a.school_type
		FROM training_institution a
		WHERE a.del_flag = '0'
		AND a.end_date > date_sub(NOW(),interval 1 day)
		<if test="province != null and province != ''">
			AND a.address LIKE CONCAT('%',#{province},'%')
		</if>
		AND a.school_name_cn = #{schoolNameCn}
		AND a.telephone = #{telephone}
		AND a.address = #{address}
	</select>

	<select id="getTrainingInstitutionCount" resultType="long">
		SELECT
			COUNT(b.countNum)
		FROM (
			SELECT
				COUNT(1) as countNum
			FROM training_institution a
			WHERE a.del_flag = '0'
			AND a.end_date > date_sub(NOW(),interval 1 day)
			AND a.school_type IN ${schoolType}
		<if test="province != null and province != ''">
			AND a.address LIKE CONCAT('%',#{province},'%')
		</if>
			GROUP BY school_name_cn,a.telephone,a.address
		)b
	</select>

	<select id="getExpireGroomerSchoolUserId" resultType="string">
		SELECT
			a.school_user_id
		FROM
		(
			SELECT
				school_user_id,
				school_member_code,
				member_code
			FROM
				training_institution
			WHERE
				school_type IN ('1', '7')
			AND del_flag = '0'
		) a
		LEFT JOIN sys_member c ON a.school_member_code = c.member_code
		LEFT JOIN sys_member d ON a.member_code = d.member_code
		LEFT JOIN jeesite_sys_user u ON a.school_user_id = u.id
		WHERE a.school_user_id != ''
		AND u.login_flag = '1'
		AND
		(
			(
				c.member_end_date IS NULL
				OR c.member_end_date <![CDATA[<]]> date_sub(NOW(), INTERVAL 1 DAY)
			)
			OR
			(
				(d.member_end_date IS NULL OR d.member_end_date <![CDATA[<]]> date_sub(NOW(),interval 1 day))
				AND
				(d.groomer_end_date IS NULL OR date_add(DATE(d.groomer_end_date),interval 1 day) <![CDATA[<]]> NOW())
			)
		)
	</select>

	<update id="updateBySchoolNameCn">
		UPDATE training_institution SET
			telephone = #{telephone},
			province = #{province},
			city = #{city},
			street = #{street},
			address = #{address}
		WHERE school_name_cn = #{schoolNameCn}
		AND del_flag = '0'
	</update>

	<!-- 查询伴侣犬合作学校 -->
	<select id="findPartnerDogSchoolList" resultType="TrainingInstitution">
		SELECT
		<include refid="trainingInstitutionColumns"/>
		FROM training_institution a
		<include refid="trainingInstitutionJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			AND  a.first_school_type = '5'
			<if test="schoolNameCn != null and schoolNameCn != ''">
				AND a.school_name_cn  LIKE CONCAT('%',#{schoolNameCn},'%')
			</if>
			<if test="displayOnWeb != null and displayOnWeb != ''">
				AND a.display_onweb = #{displayOnWeb}
			</if>
			<if test=" firstSchoolType!= null and  firstSchoolType!= ''">
				AND a.first_school_type = #{firstSchoolType}
			</if>

		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>

	<!-- 查询伴侣犬合作学校 -->
	<select id="findHandlerSchoolList" resultType="TrainingInstitution">
		SELECT
		<include refid="trainingInstitutionColumns"/>
		FROM training_institution a
		<include refid="trainingInstitutionJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			AND (a.school_type in ('2','3','4','5','6') or a.first_school_type in ('1','2','3','4'))
			<if test="schoolNameCn != null and schoolNameCn != ''">
				AND a.school_name_cn  LIKE CONCAT('%',#{schoolNameCn},'%')
			</if>
			<if test="displayOnWeb != null and displayOnWeb != ''">
				AND a.display_onweb = #{displayOnWeb}
			</if>
			<if test=" firstSchoolType!= null and  firstSchoolType!= ''">
				AND a.first_school_type = #{firstSchoolType}
			</if>

		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>
</mapper>