<?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.wgr.info.mapper.WgrInfoMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="wgrinfoResultMap" type="org.springblade.modules.wgr.info.entity.WgrInfoEntity">
        <result column="id" property="id"/>
        <result column="zjhm" property="zjhm"/>
        <result column="zjzl" property="zjzl"/>
        <result column="gjdq" property="gjdq"/>
        <result column="ywxm" property="ywxm"/>
        <result column="xb" property="xb"/>
        <result column="csrq" property="csrq"/>
        <result column="jlsy" property="jlsy"/>
        <result column="jzddz" property="jzddz"/>
        <result column="jzdqx" property="jzdqx"/>
        <result column="lrsj" property="lrsj"/>
        <result column="lxdh" property="lxdh"/>
        <result column="qzhm" property="qzhm"/>
        <result column="gzdw" property="gzdw"/>
        <result column="create_user" property="createUser"/>
        <result column="create_dept" property="createDept"/>
        <result column="create_time" property="createTime"/>
        <result column="update_user" property="updateUser"/>
        <result column="update_time" property="updateTime"/>
        <result column="status" property="status"/>
        <result column="is_deleted" property="isDeleted"/>
    </resultMap>


    <select id="selectWgrInfoPage" resultType="org.springblade.modules.wgr.info.vo.WgrInfoVO">
        SELECT
            *
        FROM (
            SELECT
            <if test="null != params.deptCategory and 4 == params.deptCategory">
                CASE WHEN wgr.ssfj_bm IS NOT NULL THEN 1 ELSE 0 END AS anzt,
            </if>
            <if test="null != params.deptCategory and 5 == params.deptCategory">
                CASE WHEN wgr.sspcs_bm IS NOT NULL THEN 1 ELSE 0 END AS anzt,
            </if>
            <if test="null != params.deptCategory and 6 == params.deptCategory">
                CASE WHEN wgr.sssq_bm IS NOT NULL THEN 1 ELSE 0 END AS anzt,
            </if>
                rw.rwjssj,
                rw.rwzt,
                ROW_NUMBER () OVER ( PARTITION BY wgr.id ORDER BY rw.create_time DESC ) maxRow,
                wgr.*,
                ( SELECT create_time FROM wgr_check wgrc WHERE wgrc.wgr_id = wgr.ID AND is_deleted = 0 ORDER BY create_time DESC LIMIT 1 ) AS lastTime
            FROM
                wgr_info wgr
            LEFT JOIN rwxffkb rw ON rw.rwly = 'wgr_info' AND rw.rwid = wgr.id
            WHERE
                wgr.is_deleted = 0
        ) temp
        LEFT JOIN wgr_check wc ON wc.wgr_id = temp.id AND wc.create_time &gt; temp.rwjssj
        WHERE
            temp.maxRow = 1
        <if test="null != params.deptCategory and 5 == params.deptCategory">
            AND temp.ssfj_bm = #{params.deptId} :: int8
        </if>
        <if test="null != params.deptCategory and 6 == params.deptCategory">
            AND temp.sspcs_bm = #{params.deptId} :: int8
        </if>
        <if test="null != params.deptCategory and 7 == params.deptCategory">
            AND #{params.deptId} LIKE '%' || temp.sssq_bm || '%'
            AND temp.rwzt = '已签收'
        </if>
        <if test="null != params.hczt and 0 == params.hczt">
            AND temp.sssq_bm IS NOT NULL
        </if>
        <if test="null != params.hczt and 1 == params.hczt">
            AND temp.id IS NOT NULL
        </if>
    </select>


    <select id="exportWgrInfo" resultType="org.springblade.modules.wgr.info.excel.WgrInfoExcel">
        SELECT * FROM wgr_info ${ew.customSqlSegment}
    </select>

    <select id="getIsExist" resultType="int">
        SELECT
            COUNT(*)
        FROM
            wgr_info
        WHERE
            is_deleted = 0
          AND gjdq = #{gjdq}
          AND ywxm = #{ywxm}
          AND csrq = #{csrq}
    </select>

    <update id="updateWgrinfo" parameterType="org.springblade.modules.wgr.info.excel.WgrInfoExcel">
        UPDATE wgr_info
        <set>
            <if test="null != zjhm and '' != zjhm">zjhm = #{zjhm},</if>
            <if test="null != zjzl and '' != zjzl">zjzl = #{zjzl},</if>
            <if test="null != xb and '' != xb">xb = #{xb},</if>
            <if test="null != jlsy and '' != jlsy">jlsy = #{jlsy},</if>
            <if test="null != jzddz and '' != jzddz">jzddz = #{jzddz},</if>
            <if test="null != jzdqx and '' != jzdqx">jzdqx = #{jzdqx},</if>
            <if test="null != lrsj and '' != lrsj">lrsj = #{lrsj},</if>
            <if test="null != lxdh and '' != lxdh">lxdh = #{lxdh},</if>
            <if test="null != qzhm and '' != qzhm">qzhm = #{qzhm},</if>
            <if test="null != gzdw and '' != gzdw">gzdw = #{gzdw},</if>
            update_time = now()
        </set>
        WHERE
            is_deleted = 0
          AND gjdq = #{gjdq}
          AND ywxm = #{ywxm}
          AND csrq = #{csrq}
    </update>

    <update id="resetBm" parameterType="java.lang.Long">
        UPDATE wgr_info
        SET ssfj_bm = NULL, sspcs_bm = NULL, sssq_bm = NULL, update_time = now()
        WHERE
        is_deleted = 0
        AND id = #{wgrId}
    </update>

    <update id="plxf" parameterType="java.lang.String">
        UPDATE wgr_info wgr
        SET ssfj_bm = ( SELECT ID FROM blade_dept WHERE dept_name = wgr.jzdqx ), update_time = now()
        WHERE
            id = #{id} :: int8
    </update>

</mapper>
