<?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="org.springblade.modules.gzInfo.mapper.InfoMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="infoResultMap" type="org.springblade.modules.gzInfo.entity.InfoEntity">
        <result column="id" property="id"/>
        <result column="lb" property="lb"/>
        <result column="gznr" property="gznr"/>
        <result column="zp0" property="zp0"/>
        <result column="user_id" property="userId"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="is_deleted" property="isDeleted"/>
    </resultMap>


    <select id="selectInfoPage" resultType="org.springblade.modules.gzInfo.vo.InfoVO">
        SELECT
            gi.ID,
            gi.lb,
            gi.xm,
            gi.pfx,
            gi.gznr,
            bu.real_name,
            to_char( gi.create_time, 'yyyy-MM-dd HH24:mm:ss' ) AS create_time
        FROM
            gz_info gi
        LEFT JOIN blade_user bu ON bu.id = gi.user_id
        LEFT JOIN blade_dict_biz bdb ON bdb.dict_value = gi.pfx
        WHERE 1 = 1
        <if test="null != params.userId and '' != params.userId">
            AND gi.user_id :: text = #{params.userId}
        </if>
        <if test="null != params.realName and '' != params.realName">
            AND bu.real_name = #{params.realName}
        </if>
        <if test="null != params.timeBegin and '' != params.timeBegin and null != params.timeEnd and '' != params.timeEnd">
            AND gi.create_time :: text BETWEEN #{params.timeBegin} AND #{params.timeEnd}
        </if>
        <if test="null != params.yf and '' != params.yf">
            AND to_char(gi.create_time, 'yyyy-MM') = substring(#{params.yf}, 1, 7)
        </if>
        <if test="null != params.pfxId and '' != params.pfxId">
            AND bdb.id :: text = #{params.pfxId}
        </if>
        ORDER BY create_time DESC
    </select>

    <select id="getDetailById" resultType="org.springblade.modules.gzInfo.vo.InfoVO">
        SELECT
            gi.ID,
            gi.lb,
            gi.xm,
            gi.pfx,
            gi.gznr,
            gi.zp0,
            gi.zp1,
            gi.zp2,
            gi.zp3,
            bu.real_name,
            gi.create_time
        FROM
            gz_info gi
        LEFT JOIN blade_user bu ON bu.ID = gi.user_id
        WHERE gi.ID = #{id}
    </select>

    <select id="exportInfo" resultType="org.springblade.modules.gzInfo.excel.InfoExcel">
        SELECT * FROM gz_info ${ew.customSqlSegment}
    </select>

    <insert id="insertGz">
        insert into gz_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="null != lb and '' != lb">lb,</if>
            <if test="null != gznr and '' != gznr">gznr,</if>
            <if test="null != zp0 and '' != zp0">zp0,</if>
            <if test="null != userId and '' != userId">user_id,</if>
            <if test="null != zp1 and '' != zp1">zp1,</if>
            <if test="null != zp2 and '' != zp2">zp2,</if>
            <if test="null != zp3 and '' != zp3">zp3,</if>
            <if test="null != xm and '' != xm">xm,</if>
            <if test="null != pfx and '' != pfx">pfx,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides="," >
            <if test="null != lb and '' != lb">#{lb},</if>
            <if test="null != gznr and '' != gznr">'<![CDATA[${gznr}]]>',</if>
            <if test="null != zp0 and '' != zp0">#{zp0},</if>
            <if test="null != userId and '' != userId">#{userId},</if>
            <if test="null != zp1 and '' != zp1">#{zp1},</if>
            <if test="null != zp2 and '' != zp2">#{zp2},</if>
            <if test="null != zp3 and '' != zp3">#{zp3},</if>
            <if test="null != xm and '' != xm">#{xm},</if>
            <if test="null != pfx and '' != pfx">#{pfx},</if>
        </trim>
    </insert>

    <select id="getDzbhdCs" resultType="int">
        SELECT
            COUNT ( ID )
        FROM
            gz_info
        WHERE
            create_user :: text = #{params.userId} :: text
          AND to_char( create_time, 'yyyy-MM' ) = substring(#{params.yf}, 1, 7)
          AND pfx :: text = #{params.pfx} :: text
    </select>
</mapper>
