<?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.dgtly.system.mapper.AlertConfigurationMapper">
    
    <resultMap type="AlertConfiguration" id="AlertConfigurationResult">
        <result property="alertId"    column="alert_id"    />
        <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="isDelete"    column="is_delete"    />
        <result property="alertContent"    column="alert_content"    />
        <result property="alertStartTime"    column="alert_start_time"    />
        <result property="alertEndTime"    column="alert_end_time"    />
        <result property="alertNum"    column="alert_num"    />
        <result property="userType"    column="user_type"    />
        <result property="alertStatus"    column="alert_status"    />
        <result property="salesLevel"    column="sales_level"    />
        <result property="orgCode"    column="org_code"    />
        <result property="orgName"    column="org_name"    />
        <result property="alertType"    column="alert_type"    />
        <result property="photoUrl"    column="photo_url"    />
        <result property="alertTitle"    column="alert_title"    />
    </resultMap>

    <sql id="selectAlertConfigurationVo">
        select alert_id, create_by, create_time, update_by, update_time, is_delete,alert_title, alert_content, alert_start_time, alert_end_time, alert_num, user_type, alert_status,sales_level,org_code,org_name,alert_type,photo_url from alert_configuration
    </sql>

    <select id="selectAlertConfigurationList" parameterType="AlertConfiguration" resultMap="AlertConfigurationResult">
        <include refid="selectAlertConfigurationVo"/>
        where 1=1
            <if test="isDelete != null "> and is_delete = #{isDelete}</if>
            <if test="alertContent != null  and alertContent != ''"> and alert_content like concat('%', #{alertContent}, '%')</if>
            <if test="alertStartTime != null "> and alert_start_time &gt;= #{alertStartTime}</if>
            <if test="alertEndTime != null "> and alert_end_time &lt; #{alertEndTime}</if>
            <if test="alertNum != null "> and alert_num = #{alertNum}</if>
            <if test="userType != null  and userType != ''"> and user_type = #{userType}</if>
            <if test="alertStatus != null  and alertStatus != ''"> and alert_status = #{alertStatus}</if>
        order by create_time desc
    </select>

    <select id="selectAlertConfigurationListFilter" parameterType="Long" resultMap="AlertConfigurationResult">
        <include refid="selectAlertConfigurationVo"/>
        WHERE alert_status = 1
        <if test="userType !=null and userType !='' and userType !='2'.toString() ">
          and ( user_type =#{userType}  or user_type ='2')
        </if>


    </select>
    
    <select id="selectAlertConfigurationById" parameterType="Long" resultMap="AlertConfigurationResult">
        <include refid="selectAlertConfigurationVo"/>
        where alert_id = #{alertId}
    </select>

    <select id="selectAlertConfigurationByAlertStatus" parameterType="Long" resultMap="AlertConfigurationResult">
        <include refid="selectAlertConfigurationVo"/>
        WHERE alert_status = 1
        <if test="userType !=null and userType !=''  ">
            and ( user_type =#{userType}  or user_type ='2')
        </if>
        AND now() >=alert_start_time
        AND alert_end_time > now()
    </select>

    <insert id="insertAlertConfiguration" parameterType="AlertConfiguration" useGeneratedKeys="true" keyProperty="alertId">
        insert into alert_configuration
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="createBy != null  and createBy != ''">create_by,</if>
            <if test="createTime != null ">create_time,</if>
            <if test="updateBy != null  and updateBy != ''">update_by,</if>
            <if test="updateTime != null ">update_time,</if>
            <if test="isDelete != null ">is_delete,</if>
            <if test="alertContent != null  and alertContent != ''">alert_content,</if>
            <if test="alertStartTime != null ">alert_start_time,</if>
            <if test="alertEndTime != null ">alert_end_time,</if>
            <if test="alertNum != null ">alert_num,</if>
            <if test="userType != null  and userType != ''">user_type,</if>
            <if test="alertStatus != null  and alertStatus != ''">alert_status,</if>
            <if test="salesLevel != null  and salesLevel != ''">sales_level,</if>
            <if test="orgCode != null  and orgCode != ''">org_code,</if>
            <if test="orgName != null  and orgName != ''">org_name,</if>
            <if test="alertType != null  and alertType != ''">alert_type,</if>
            <if test="photoUrl != null  and photoUrl != ''">photo_url,</if>
            <if test="alertTitle != null  and alertTitle != ''">alert_title,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="createBy != null  and createBy != ''">#{createBy},</if>
            <if test="createTime != null ">#{createTime},</if>
            <if test="updateBy != null  and updateBy != ''">#{updateBy},</if>
            <if test="updateTime != null ">#{updateTime},</if>
            <if test="isDelete != null ">#{isDelete},</if>
            <if test="alertContent != null  and alertContent != ''">#{alertContent},</if>
            <if test="alertStartTime != null ">#{alertStartTime},</if>
            <if test="alertEndTime != null ">#{alertEndTime},</if>
            <if test="alertNum != null ">#{alertNum},</if>
            <if test="userType != null  and userType != ''">#{userType},</if>
            <if test="alertStatus != null  and alertStatus != ''">#{alertStatus},</if>
            <if test="salesLevel != null  and salesLevel != ''">#{salesLevel},</if>
            <if test="orgCode != null  and orgCode != ''">#{orgCode},</if>
            <if test="orgName != null  and orgName != ''">#{orgName},</if>
            <if test="alertType != null  and alertType != ''">#{alertType},</if>
            <if test="photoUrl != null  and photoUrl != ''">#{photoUrl},</if>
            <if test="alertTitle != null  and alertTitle != ''">#{alertTitle},</if>
         </trim>
    </insert>

    <update id="updateAlertConfiguration" parameterType="AlertConfiguration">
        update alert_configuration
        <trim prefix="SET" suffixOverrides=",">
            <if test="createBy != null  and createBy != ''">create_by = #{createBy},</if>
            <if test="createTime != null ">create_time = #{createTime},</if>
            <if test="updateBy != null  and updateBy != ''">update_by = #{updateBy},</if>
            <if test="updateTime != null ">update_time = #{updateTime},</if>
            <if test="isDelete != null ">is_delete = #{isDelete},</if>
            <if test="alertContent != null  and alertContent != ''">alert_content = #{alertContent},</if>
            <if test="alertStartTime != null ">alert_start_time = #{alertStartTime},</if>
            <if test="alertEndTime != null ">alert_end_time = #{alertEndTime},</if>
            <if test="alertNum != null ">alert_num = #{alertNum},</if>
            <if test="userType != null  and userType != ''">user_type = #{userType},</if>
            <if test="alertStatus != null  and alertStatus != ''">alert_status = #{alertStatus},</if>
            <if test="salesLevel != null  and salesLevel != ''">sales_level = #{salesLevel},</if>
            <if test="orgCode != null  and orgCode != ''">org_code = #{orgCode},</if>
            <if test="orgName != null  and orgName != ''">org_name = #{orgName},</if>
            <if test="alertType != null  and alertType != ''">alert_type = #{alertType},</if>
            <if test="photoUrl != null  and photoUrl != ''">photo_url = #{photoUrl},</if>
            <if test="alertTitle != null  and alertTitle != ''">alert_title = #{alertTitle},</if>
        </trim>
        where alert_id = #{alertId}
    </update>

    <delete id="deleteAlertConfigurationById" parameterType="Long">
        delete from alert_configuration where alert_id = #{alertId}
    </delete>

    <delete id="deleteAlertConfigurationByIds" parameterType="String">
        delete from alert_configuration where alert_id in 
        <foreach item="alertId" collection="array" open="(" separator="," close=")">
            #{alertId}
        </foreach>
    </delete>

    <sql id="orgListWhere">
        and cal_month = date_format( CURDATE(), '%Y%m' )
        <if test="parType != null and parType == 'company_level'">
            and company_code = #{parValue}
        </if>
        <if test="parType != null and parType == 'saledept_level'  ">
            and saledept_code = #{parValue}
        </if>
        <if test="parType != null and parType == 'office_level'">
            and office_code = #{parValue}
        </if>
        <if test="parType != null and parType == 'suboffice_level'">
            and suboffice_code = #{parValue}
        </if>
    </sql>
    <select id="selectOrgCodeByInterface" resultType="java.lang.String">
        <if test="parType != null and parType == 'company_level' or parType == 'diy_level' ">
        SELECT
        company_code AS orgCode
        FROM
        order_sales_sum_tuc_group
        WHERE
        BUSINESS_UNIT = '零售'
        <include refid="orgListWhere"/>
        GROUP BY
        company_code UNION
        </if>
        <if test="parType != null and parType == 'company_level' or parType == 'saledept_level' or parType == 'office_level' or parType == 'diy_level' ">
        SELECT
        saledept_code AS orgCode
        FROM
        order_sales_sum_tuc_group
        WHERE
        BUSINESS_UNIT = '零售'
        <include refid="orgListWhere"/>
        GROUP BY
        saledept_code UNION
        </if>
        <if test="parType != null and parType == 'company_level' or parType == 'suboffice_level' or parType == 'saledept_level' or parType == 'office_level' or  parType == 'diy_level'  ">
        SELECT
        suboffice_code AS orgCode
        FROM
        order_sales_sum_tuc_group
        WHERE
        BUSINESS_UNIT = '零售'
        <include refid="orgListWhere"/>
        GROUP BY
        suboffice_code UNION
        </if>
        SELECT
        customer_id AS orgCode
        FROM
        order_sales_sum_tuc_customer
        WHERE
        BUSINESS_UNIT = '零售'
        <include refid="orgListWhere"/>
        GROUP BY
        customer_id
    </select>
    <select id="selectOrgCode" resultType="java.lang.String">

        SELECT
        company_code AS orgCode
        FROM
        order_sales_sum_tuc_group
        WHERE
        BUSINESS_UNIT = '零售'
        <include refid="orgListWhere"/>
        GROUP BY
        company_code UNION
        SELECT
        saledept_code AS orgCode
        FROM
        order_sales_sum_tuc_group
        WHERE
        BUSINESS_UNIT = '零售'
        <include refid="orgListWhere"/>
        GROUP BY
        saledept_code UNION
        SELECT
        suboffice_code AS orgCode
        FROM
        order_sales_sum_tuc_group
        WHERE
        BUSINESS_UNIT = '零售'
        <include refid="orgListWhere"/>
        GROUP BY
        suboffice_code
    </select>

    <select id="searchProductParamByCustomerCode" resultType="java.util.Map">
        SELECT
        g.company_code,
        g.company_name,
        g.office_code,
        g.office_name,
        g.suboffice_code,
        g.suboffice_name
        FROM
        order_sales_sum_tuc_group g
        left join tuc_company_sort sort on g.company_code=sort.company_code
        WHERE
        1 = 1 AND g.BUSINESS_UNIT = '零售'
        <if test="salesLevel != null and salesLevel == 'company_level'  ">
            and g.office_code  is not null
            and g.office_code  !=''
            and g.company_code = #{orgCode}

        </if>
        <if test="salesLevel != null and salesLevel == 'office_level'">
            and g.suboffice_code  is not null
            and g.suboffice_code  !=''
            and g.office_code = #{orgCode}
        </if>
        <if test="salesLevel != null and salesLevel == 'suboffice_level' ">
            and g.suboffice_code  is not null
            and g.suboffice_code  !=''
            and g.suboffice_code = #{orgCode}
        </if>
        <if test="groupByStr != null and groupByStr != ''">
            GROUP BY g.${groupByStr}
        </if>
        <if test="salesLevel != null and  salesLevel == 'diy_level' ">
            ORDER BY sort.company_sort+0 asc
        </if>
    </select>
</mapper>