<?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.cms.mapper.policy.PolicyInfoMapper">

    <resultMap type="com.ruoyi.cms.domain.policy.PolicyInfo" id="PolicyInfoResult">
        <result property="id" column="id"/>
        <result property="zcmc" column="zcmc"/>
        <result property="zclx" column="zclx"/>
        <result property="zcLevel" column="zc_level"/>
        <result property="sourceUnit" column="source_unit"/>
        <result property="acceptUnit" column="accept_unit"/>
        <result property="publishTime" column="publish_time"/>
        <result property="zcContent" column="zc_content"/>
        <result property="subsidyStandard" column="subsidy_standard"/>
        <result property="handleChannel" column="handle_channel"/>
        <result property="applyCondition" column="apply_condition"/>
        <result property="fileUrl" column="file_url"/>
        <result property="fileName" column="file_name"/>
        <result property="viewNum" column="view_num"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="delFlag" column="del_flag"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <sql id="selectPolicyInfoListVo">
        select id, zcmc, zclx, zc_level, source_unit, accept_unit, publish_time, view_num, create_time
        from policy_info
    </sql>

    <sql id="selectPolicyInfoDetailVo">
        select id, zcmc, zclx, zc_level, source_unit, accept_unit, publish_time, 
               zc_content, subsidy_standard, handle_channel, apply_condition,
               file_url, file_name, view_num, create_by, create_time, update_by, update_time, remark
        from policy_info
    </sql>

    <select id="selectPolicyInfoList" resultMap="PolicyInfoResult">
        <include refid="selectPolicyInfoListVo"/>
        <where>
            del_flag = '0'
            <if test="query.searchValue != null and query.searchValue != ''">
                and zcmc like concat('%', #{query.searchValue}, '%')
            </if>
        </where>
        order by publish_time desc, create_time desc
    </select>

    <select id="selectPolicyInfoById" resultMap="PolicyInfoResult">
        <include refid="selectPolicyInfoDetailVo"/>
        where id = #{id} and del_flag = '0'
    </select>

    <update id="updateViewNum">
        update policy_info set view_num = view_num + 1 where id = #{id}
    </update>

    <insert id="insertPolicyInfo" useGeneratedKeys="true" keyProperty="id">
        insert into policy_info (
            zcmc, zclx, zc_level, source_unit, accept_unit, publish_time,
            zc_content, subsidy_standard, handle_channel, apply_condition,
            file_url, file_name, view_num, create_by, create_time, del_flag, remark
        ) values (
            #{zcmc}, #{zclx}, #{zcLevel}, #{sourceUnit}, #{acceptUnit}, #{publishTime},
            #{zcContent}, #{subsidyStandard}, #{handleChannel}, #{applyCondition},
            #{fileUrl}, #{fileName}, #{viewNum}, #{createBy}, now(), '0', #{remark}
        )
    </insert>

    <update id="updatePolicyInfo">
        update policy_info
        <set>
            <if test="zcmc != null">zcmc = #{zcmc},</if>
            <if test="zclx != null">zclx = #{zclx},</if>
            <if test="zcLevel != null">zc_level = #{zcLevel},</if>
            <if test="sourceUnit != null">source_unit = #{sourceUnit},</if>
            <if test="acceptUnit != null">accept_unit = #{acceptUnit},</if>
            <if test="publishTime != null">publish_time = #{publishTime},</if>
            <if test="zcContent != null">zc_content = #{zcContent},</if>
            <if test="subsidyStandard != null">subsidy_standard = #{subsidyStandard},</if>
            <if test="handleChannel != null">handle_channel = #{handleChannel},</if>
            <if test="applyCondition != null">apply_condition = #{applyCondition},</if>
            <if test="fileUrl != null">file_url = #{fileUrl},</if>
            <if test="fileName != null">file_name = #{fileName},</if>
            <if test="remark != null">remark = #{remark},</if>
            update_by = #{updateBy},
            update_time = now()
        </set>
        where id = #{id}
    </update>

    <update id="deletePolicyInfoByIds">
        update policy_info set del_flag = '2'
        where id in
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

</mapper>
