<?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.sign.mapper.SignMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="SignResultMap" type="org.springblade.modules.sign.entity.SignEntity">
        <result column="id" property="id"/>
        <result column="user_id" property="userId"/>
        <result column="qd_time" property="qdTime"/>
        <result column="qd_jd" property="qdJd"/>
        <result column="qd_wd" property="qdWd"/>
        <result column="qddz_mc" property="qddzMc"/>
        <result column="type" property="type"/>
        <result column="create_time" property="createTime"/>
        <result column="rq" property="rq"/>
    </resultMap>


    <select id="selectSignPage" resultType="org.springblade.modules.sign.vo.SignVO">
        SELECT distinct
            si.user_id,
            bu.real_name
        FROM
            sign_info si
        LEFT JOIN blade_user bu ON bu.id = si.user_id
        LEFT JOIN blade_dept dept ON bu.dept_id = dept.id :: text
        <where>
            <if test="signVOa.id != null and signVOa.id != ''">
                dept.id = #{signVOa.id}
            </if>
            <if test="signVOa.Mathvalue != null and signVOa.Mathvalue != ''">
                AND si.rq like '%' || #{signVOa.Mathvalue} || '%'
            </if>
        </where>
    </select>

    <select id="selectSignUser" resultType="org.springblade.modules.sign.vo.SignVO">
        SELECT
            bu.id AS userId,
            bu.real_name,
            COALESCE ( pcs.dept_name, sq.dept_name ) AS deptName,
            br.role_name
        FROM
            blade_user bu
            JOIN "unnest" ( string_to_array( role_id, ',' ) ) role ON role IN ( '1677215233615192065', '1677215613229064194' )
            LEFT JOIN blade_role br ON br.id :: text = role
            LEFT JOIN blade_dept sq ON sq.id :: text = ( string_to_array( bu.dept_id, ',' ) ) [1]
            LEFT JOIN blade_dept pcs ON ( pcs.id = sq.parent_id AND pcs.dept_category = 6 )
        WHERE
            bu.is_deleted = 0
        <if test="null != signVOa.id">
        AND COALESCE ( pcs.id, sq.id ) = #{signVOa.id}
        </if>
    </select>

    <select id="exportSign" resultType="org.springblade.modules.sign.excel.SignExcel">
        SELECT * FROM sign_info ${ew.customSqlSegment}
    </select>

    <select id="getKgCs" resultType="int">
        SELECT
            COUNT( id )
        FROM
            sign_info
        WHERE
            user_id :: text = #{params.userId} :: text
          AND to_char( rq :: date, 'yyyy-MM' ) = substring(#{params.yf}, 1, 7)
        AND type :: text = #{params.type} :: text
    </select>

    <select id="selectDkjlByUserid" resultMap="SignResultMap">
        SELECT DISTINCT ON ( rq )
            *
        FROM
            sign_info
        WHERE
            user_id = #{userId}
          AND to_char( rq :: DATE, 'yyyy-MM' ) = #{yf}
        ORDER BY
            rq ASC
    </select>

    <select id="getSignUser" resultType="org.springblade.modules.sign.dto.SignDTO">
        SELECT
            *
        FROM
            (
                SELECT
                    bu.id AS userId,
                    ( SELECT COUNT ( si.id ) FROM sign_info si WHERE si.create_user = bu.id :: TEXT AND si.create_time >= CURRENT_DATE ) AS time
                FROM
                    blade_user bu
                    JOIN "unnest" ( string_to_array( role_id, ',' ) ) ROLE ON ROLE IN ( '1677215233615192065', '1677215613229064194' )
                WHERE
                    bu.is_deleted = 0
            ) AS tem
        WHERE
            tem.time = 0
    </select>

    <insert id="autoAbsent">
        INSERT INTO sign_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="null != type and '' != lb">lb,</if>
            <if test="null != gznr and '' != gznr">gznr,</if>
            <if test="null != zp0 and '' != zp0">zp0,</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>
        </trim>
    </insert>

    <select id="selectSign" resultType="org.springblade.modules.sign.vo.SignVOS">
        select array_to_string(array_agg(type),',') as type,rq from sign_info where user_id = #{userId} and rq like '%' || #{Mathvalue} || '%'
        group by rq
        order by rq
    </select>

    <select id="selectBm" resultType="org.springblade.modules.sign.vo.BmVO">
        SELECT
            id AS value,
	        dept_name AS label
        FROM
            blade_dept
        WHERE
            dept_category = 6
        AND (ancestors like '%' || #{deptId} || '%' OR id :: text = #{deptId})
        ORDER BY
            dept_name DESC
    </select>

    <select id="getUserListByRoleId" resultType="org.springblade.modules.system.entity.User">
        SELECT
            *
        FROM
            blade_user
        WHERE
            role_id = '1677215233615192065'
    </select>

    <select id="getSignException" resultType="org.springblade.modules.sign.vo.SignExceptionVO">
        SELECT
            si.id,
            bd."dept_name分局名" AS fjMc,
            bd."dept_name派出所名" AS pcsMc,
            bd."dept_name社区名" AS sqMc,
            bu."name",
            br.role_name,
            si.create_time,
            si.type
        FROM
            sign_info si
        LEFT JOIN blade_user bu ON bu.id :: TEXT = si.create_user
	    LEFT JOIN blade_role br ON br.id :: TEXT = bu.role_id
        LEFT JOIN v_dept_all bd ON bd.id社区 :: TEXT = si.create_dept
        WHERE
            si.is_deleted = 0
        AND si.bdjg = '失败'
        AND (bd.ancestors社区祖级 LIKE '%' || #{params.deptId} || '%' OR #{params.deptId} LIKE '%' || si.create_dept || '%')
        <if test="null != params.fjMc and '' != params.fjMc">
        AND bd."dept_name分局名" = #{params.fjMc}
        </if>
        <if test="null != params.pcsMc and '' != params.pcsMc">
        AND bd."dept_name派出所名" = #{params.pcsMc}
        </if>
        <if test="null != params.sqMc and '' != params.sqMc">
        AND bd."dept_name社区名" = #{params.sqMc}
        </if>
        <if test="null != params.month and '' != params.month">
        AND si.create_time :: TEXT LIKE '%' || #{params.month} || '%'
        </if>
        ORDER BY si.create_time DESC
    </select>

    <select id="exportInfo" resultType="org.springblade.modules.sign.excel.SignExceptionExcel">
        SELECT
            si.id,
            bd."dept_name分局名" AS fjMc,
            bd."dept_name派出所名" AS pcsMc,
            bd."dept_name社区名" AS sqMc,
            bu."name",
            br.role_name,
            si.create_time,
            si.type
        FROM
            sign_info si
        LEFT JOIN blade_user bu ON bu.id :: TEXT = si.create_user
        LEFT JOIN blade_role br ON br.id :: TEXT = bu.role_id
        LEFT JOIN v_dept_all bd ON bd.id社区 :: TEXT = si.create_dept
        WHERE
            si.is_deleted = 0
        AND si.bdjg = '失败'
        AND (bd.ancestors社区祖级 LIKE '%' || #{params.deptId} || '%' OR #{params.deptId} LIKE '%' || si.create_dept || '%')
        <if test="null != params.fjMc and '' != params.fjMc">
        AND bd."dept_name分局名" = #{params.fjMc}
        </if>
        <if test="null != params.pcsMc and '' != params.pcsMc">
        AND bd."dept_name派出所名" = #{params.pcsMc}
        </if>
        <if test="null != params.sqMc and '' != params.sqMc">
        AND bd."dept_name社区名" = #{params.sqMc}
        </if>
        <if test="null != params.month and '' != params.month">
        AND si.create_time :: TEXT LIKE '%' || #{params.month} || '%'
        </if>
        ORDER BY si.create_time DESC
    </select>

    <select id="getSignZpById" resultMap="SignResultMap">
        SELECT
            qdzp
        FROM
            sign_info
        WHERE
            id :: TEXT = #{params.id}
    </select>

    <select id="getRqType" resultType="java.lang.String">
        SELECT
            rq_type
        FROM
            sign_date_conf
        WHERE
            rq = CURRENT_DATE :: TEXT
    </select>
</mapper>
