<?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.xinelu.manage.mapper.propagandainfo.PropagandaInfoMapper">

    <resultMap type="com.xinelu.manage.domain.propagandainfo.PropagandaInfo" id="PropagandaInfoResult">
        <result property="id" column="id"/>
        <result property="hospitalAgencyId" column="hospital_agency_id"/>
        <result property="hospitalAgencyName" column="hospital_agency_name"/>
        <result property="departmentId" column="department_id"/>
        <result property="departmentName" column="department_name"/>
        <result property="propagandaTitle" column="propaganda_title"/>
        <result property="propagandaType" column="propaganda_type"/>
        <result property="propagandaCode" column="propaganda_code"/>
        <result property="propagandaStatus" column="propaganda_status"/>
        <result property="propagandaContent" column="propaganda_content"/>
        <result property="propagandaCoverPath" column="propaganda_cover_path"/>
        <result property="articleSummary" column="article_summary"/>
        <result property="voicebroadcast" column="voicebroadcast"/>
        <result property="diseaseTypeId" column="disease_type_id"/>
        <result property="diseaseTypeName" column="disease_type_name"/>
        <result property="propagandaLink" column="propaganda_link"/>
        <result property="propagandaBarcodePath" column="propaganda_barcode_path"/>
	    <result property="delFlag" column="del_flag"/>
	    <result property="sourceTemplateId" column="source_template_id"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectPropagandaInfoVo">
        select id, hospital_agency_id, hospital_agency_name, department_id, department_name, propaganda_title, propaganda_type, propaganda_code, propaganda_status, propaganda_content, propaganda_cover_path, article_summary, voicebroadcast, disease_type_id, disease_type_name, propaganda_link, propaganda_barcode_path,del_flag, source_template_id, create_by, create_time, update_by, update_time from propaganda_info
    </sql>

    <select id="selectPropagandaInfoList" parameterType="com.xinelu.manage.dto.propagandainfo.PropagandaInfoDto" resultMap="PropagandaInfoResult">
	    select p.id, p.hospital_agency_id, p.hospital_agency_name, p.department_id, p.department_name, p.propaganda_title, p.propaganda_type, p.propaganda_code, p.propaganda_status,
	    p.propaganda_content, p.propaganda_cover_path, p.article_summary, p.voicebroadcast, p.disease_type_id, p.disease_type_name,
	    p.propaganda_link, p.propaganda_barcode_path,p.del_flag,p.source_template_id, p.create_by, p.create_time, p.update_by, p.update_time from propaganda_info p
        <where>
	        p.del_flag = 0
            <if test="hospitalAgencyId != null and publicId == null">
                and p.hospital_agency_id = #{hospitalAgencyId}
            </if>
            <if test="hospitalAgencyId != null and publicId != null">
                and (p.hospital_agency_id = #{hospitalAgencyId} or p.hospital_agency_id = #{publicId})
            </if>
            <if test="departmentId != null ">
                and p.department_id = #{departmentId}
            </if>
            <if test="propagandaTitle != null  and propagandaTitle != ''">
                and p.propaganda_title like concat('%', #{propagandaTitle}, '%')
            </if>
	        <if test="propagandaType != null  and propagandaType != ''">
		        and p.propaganda_type = #{propagandaType}
	        </if>
	        <if test="propagandaCode != null  and propagandaCode != ''">
		        and p.propaganda_code = #{propagandaCode}
	        </if>
            <if test="propagandaStatus != null  and propagandaStatus != ''">
                and p.propaganda_status = #{propagandaStatus}
            </if>
            <if test="createBy != null and propagandaStatus != ''">
                and p.create_by = #{createBy}
            </if>
	        <if test="createTimeStart != null">
		        and date_format(p.create_time, '%y%m%d') >= date_format(#{createTimeStart}, '%y%m%d')
	        </if>
	        <if test="createTimeEnd != null">
		        and date_format(p.create_time, '%y%m%d') &lt;= date_format(#{createTimeEnd}, '%y%m%d')
	        </if>
        </where>
        order by p.update_time desc
    </select>

    <select id="selectPropagandaInfoById" parameterType="Long"
            resultMap="PropagandaInfoResult">
            <include refid="selectPropagandaInfoVo"/>
            where id = #{id}
    </select>

    <insert id="insertPropagandaInfo" parameterType="com.xinelu.manage.domain.propagandainfo.PropagandaInfo" useGeneratedKeys="true"
            keyProperty="id">
        insert into propaganda_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="hospitalAgencyId != null">hospital_agency_id,
            </if>
            <if test="hospitalAgencyName != null">hospital_agency_name,
            </if>
            <if test="departmentId != null">department_id,
            </if>
            <if test="departmentName != null">department_name,
            </if>
            <if test="propagandaTitle != null">propaganda_title,
            </if>
            <if test="propagandaType != null">propaganda_type,
            </if>
            <if test="propagandaCode != null">propaganda_code,
            </if>
            <if test="propagandaStatus != null">propaganda_status,
            </if>
            <if test="propagandaContent != null">propaganda_content,
            </if>
            <if test="propagandaCoverPath != null">propaganda_cover_path,
            </if>
            <if test="articleSummary != null">article_summary,
            </if>
            <if test="voicebroadcast != null">voicebroadcast,
            </if>
            <if test="diseaseTypeId != null">disease_type_id,
            </if>
            <if test="diseaseTypeName != null">disease_type_name,
            </if>
            <if test="propagandaLink != null">propaganda_link,
            </if>
            <if test="propagandaBarcodePath != null">propaganda_barcode_path,
            </if>
	        <if test="delFlag != null">del_flag,
	        </if>
	        <if test="sourceTemplateId != null">source_template_id,
	        </if>
            <if test="createBy != null">create_by,
            </if>
            <if test="createTime != null">create_time,
            </if>
            <if test="updateBy != null">update_by,
            </if>
            <if test="updateTime != null">update_time,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="hospitalAgencyId != null">#{hospitalAgencyId},
            </if>
            <if test="hospitalAgencyName != null">#{hospitalAgencyName},
            </if>
            <if test="departmentId != null">#{departmentId},
            </if>
            <if test="departmentName != null">#{departmentName},
            </if>
            <if test="propagandaTitle != null">#{propagandaTitle},
            </if>
            <if test="propagandaType != null">#{propagandaType},
            </if>
            <if test="propagandaCode != null">#{propagandaCode},
            </if>
            <if test="propagandaStatus != null">#{propagandaStatus},
            </if>
            <if test="propagandaContent != null">#{propagandaContent},
            </if>
            <if test="propagandaCoverPath != null">#{propagandaCoverPath},
            </if>
            <if test="articleSummary != null">#{articleSummary},
            </if>
            <if test="voicebroadcast != null">#{voicebroadcast},
            </if>
            <if test="diseaseTypeId != null">#{diseaseTypeId},
            </if>
            <if test="diseaseTypeName != null">#{diseaseTypeName},
            </if>
            <if test="propagandaLink != null">#{propagandaLink},
            </if>
            <if test="propagandaBarcodePath != null">#{propagandaBarcodePath},
            </if>
	        <if test="delFlag != null">#{delFlag},
	        </if>
	        <if test="sourceTemplateId != null">#{sourceTemplateId},
	        </if>
            <if test="createBy != null">#{createBy},
            </if>
            <if test="createTime != null">#{createTime},
            </if>
            <if test="updateBy != null">#{updateBy},
            </if>
            <if test="updateTime != null">#{updateTime},
            </if>
        </trim>
    </insert>

    <update id="updatePropagandaInfo" parameterType="com.xinelu.manage.domain.propagandainfo.PropagandaInfo">
        update propaganda_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="hospitalAgencyId != null">hospital_agency_id =
                #{hospitalAgencyId},
            </if>
            <if test="hospitalAgencyName != null">hospital_agency_name =
                #{hospitalAgencyName},
            </if>
            <if test="departmentId != null">department_id =
                #{departmentId},
            </if>
            <if test="departmentName != null">department_name =
                #{departmentName},
            </if>
            <if test="propagandaTitle != null">propaganda_title =
                #{propagandaTitle},
            </if>
            <if test="propagandaType != null">propaganda_type =
                #{propagandaType},
            </if>
            <if test="propagandaCode != null">propaganda_code =
                #{propagandaCode},
            </if>
            <if test="propagandaStatus != null">propaganda_status =
                #{propagandaStatus},
            </if>
            <if test="propagandaContent != null">propaganda_content =
                #{propagandaContent},
            </if>
            <if test="propagandaCoverPath != null">propaganda_cover_path =
                #{propagandaCoverPath},
            </if>
            <if test="articleSummary != null">article_summary =
                #{articleSummary},
            </if>
            <if test="voicebroadcast != null">voicebroadcast =
                #{voicebroadcast},
            </if>
            disease_type_id = #{diseaseTypeId},
            disease_type_name = #{diseaseTypeName},
            <if test="propagandaLink != null">propaganda_link =
                #{propagandaLink},
            </if>
            <if test="propagandaBarcodePath != null">propaganda_barcode_path =
                #{propagandaBarcodePath},
            </if>
	        <if test="delFlag != null">del_flag =
		        #{delFlag},
	        </if>
	        <if test="sourceTemplateId != null">source_template_id =
		        #{sourceTemplateId},
	        </if>
            <if test="createBy != null">create_by =
                #{createBy},
            </if>
            <if test="createTime != null">create_time =
                #{createTime},
            </if>
            <if test="updateBy != null">update_by =
                #{updateBy},
            </if>
            <if test="updateTime != null">update_time =
                #{updateTime},
            </if>
        </trim>
        where id = #{id}
    </update>

    <update id="deletePropagandaInfoById" parameterType="Long">
        update propaganda_info set del_flag = 1 where id = #{id}
    </update>

    <update id="deletePropagandaInfoByIds" parameterType="String">
	    update propaganda_info set del_flag = 1 where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>
	<select id="selectNumByDept" resultType="com.xinelu.manage.vo.department.DepartmentVO">
		select d.id,
		d.department_name,
		count(p.id) AS countNum
		from department d left join
		(select id, department_id, propaganda_status from propaganda_info
		<where>
			del_flag = 0
			<if test="propagandaStatus != null  and propagandaStatus != ''">
				and propaganda_status = #{propagandaStatus}
			</if>
			<if test="hospitalAgencyId != null ">
				and hospital_agency_id = #{hospitalAgencyId}
			</if>
			<if test="departmentId != null ">
				and department_id = #{departmentId}
			</if>
			<if test="propagandaType != null  and propagandaType != ''">
				and propaganda_type = #{propagandaType}
			</if>
			<if test="propagandaStatus != null  and propagandaStatus != ''">
				and propaganda_status = #{propagandaStatus}
			</if>
			<if test="createTimeStart != null">
				and date_format(create_time, '%y%m%d') >= date_format(#{createTimeStart}, '%y%m%d')
			</if>
			<if test="createTimeEnd != null">
				and date_format(create_time, '%y%m%d') &lt;= date_format(#{createTimeEnd}, '%y%m%d')
			</if>
		</where>
		) as p on d.id = p.department_id
		<where>
			<if test="departmentName != null  and departmentName != ''">
				and d.department_name like concat('%', #{departmentName}, '%')
			</if>
			<if test="hospitalAgencyId != null ">
				and d.hospital_agency_id = #{hospitalAgencyId}
			</if>
			<!-- 数据范围过滤 -->
			${params.dataScope}
		</where>
		GROUP BY d.id,
		d.department_name,
		d.department_code
		order by countNum desc
	</select>
</mapper>