<?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.benyun.mapper.WdInfoMapper">
    <resultMap type="com.ruoyi.benyun.entity.bo.WdAddrTypeDistribution" id="WdAddrDisResult">
        <result property="addrCode" column="addr_code"/>
        <result property="typeCode" column="type_code_by"/>
        <result property="count" column="count"/>
    </resultMap>
    <resultMap type="com.ruoyi.benyun.entity.bo.WdAddrTypeDistribution" id="WdTypeDisResult">
        <result property="addrCode" column="addr_code"/>
        <result property="typeCode" column="wd_type_code"/>
        <result property="count" column="count"/>
    </resultMap>
    <resultMap type="com.ruoyi.benyun.entity.bo.WdAddrTypeDistribution" id="WdTotalDisResult">
        <result property="typeCode" column="wd_type_code"/>
        <result property="count" column="count"/>
    </resultMap>
    <resultMap type="com.ruoyi.benyun.entity.vo.AddStatusVo" id="AddStatusResult">
        <result property="typeCode" column="wd_type_code"/>
        <result property="count" column="count"/>
        <result property="time" column="time"/>
    </resultMap>
    <select id="searchAddrDisByMulti" resultMap="WdAddrDisResult">
        SELECT
            addr_code,
            type_code_by,
            COUNT(*) count
        FROM
            `ddt_wd_info`
        WHERE
            type_code_by IS NOT NULL
        <if test="addrCodes != null">
            <foreach collection="addrCodes" item="addrCode" open="AND addr_code IN (" separator="," close=")">
                #{addrCode,jdbcType=VARCHAR}
            </foreach>
        </if>
        <if test="typeCodes != null">
            <foreach collection="typeCodes" item="typeCode" open="AND type_code_by IN (" separator="," close=")">
                #{typeCode,jdbcType=VARCHAR}
            </foreach>
        </if>
        GROUP BY
            addr_code,
            type_code_by
        ORDER BY count DESC;
    </select>
    <select id="searchWdTypeDisByMulti" resultMap="WdTypeDisResult">
        SELECT
            addr_code,
            wd_type_code,
            COUNT(*) count
        FROM
            `ddt_wd_info`
        <trim prefix="WHERE" prefixOverrides="AND">
            <if test="addrCodes != null">
                <foreach collection="addrCodes" item="addrCode" open="AND addr_code IN (" separator="," close=")">
                    #{addrCode,jdbcType=VARCHAR}
                </foreach>
            </if>
            <if test="typeCodes != null">
                <foreach collection="typeCodes" item="typeCode" open="AND wd_type_code IN (" separator="," close=")">
                    #{typeCode,jdbcType=VARCHAR}
                </foreach>
            </if>
        </trim>
        GROUP BY
            addr_code,
            wd_type_code
        ORDER BY count DESC
    </select>
    <select id="searchWdTotal" resultMap="WdTotalDisResult">
        SELECT
            wd_type_code,
            COUNT(*) count
        FROM
            `ddt_wd_info`
        GROUP BY
            wd_type_code
        ORDER BY count DESC
    </select>

    <select id="list" parameterType="com.ruoyi.benyun.entity.vo.WdInfoVo" resultType="com.ruoyi.benyun.entity.WdInfo">
        select * from ddt_wd_info
        <where>
            <if test="wdName != null and wdName != ''">
                and wd_name like concat("%",#{wdName},"%")
            </if>
            <if test="typeCodeBy != null and typeCodeBy != ''">
                and type_code_by = #{typeCodeBy}
            </if>
            <if test="addrCode != null and addrCode != ''">
                and addr_code = #{addrCode}
            </if>
            <if test="showDelete != -1">
                and show_delete = #{showDelete}
            </if>
        </where>
    </select>
    <select id="searchAddStatusByMulti" resultMap="AddStatusResult">
        SELECT
            wd_type_code,
            COUNT(*) count,
	        DATE_FORMAT(collect_time, "%Y-%m") time
        FROM
            `ddt_wd_info`
        <trim prefix="where" prefixOverrides="and">
            <if test="times != null">
                <foreach collection="times" item="time" open="and DATE_FORMAT(collect_time, '%Y-%m') IN (" separator="," close=")">
                    #{time,jdbcType=VARCHAR}
                </foreach>
            </if>
            <if test="addrCodes != null">
                <foreach collection="addrCodes" item="addrCode" open="and addr_code IN (" separator="," close=")">
                    #{addrCode,jdbcType=VARCHAR}
                </foreach>
            </if>
            <if test="wdTypeCodes != null">
                <foreach collection="wdTypeCodes" item="type" open="and wd_type_code IN (" separator="," close=")">
                    #{type,jdbcType=VARCHAR}
                </foreach>
            </if>
        </trim>
        GROUP BY
            wd_type_code,
            time
    </select>

    <update id="recoverWd" parameterType="java.lang.String">
        update ddt_wd_info set show_delete = 0 where wd_id = #{wdId}
    </update>
</mapper>
