<?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.OrderTucEarlyWarningMapper">
    
    <resultMap type="com.dgtly.system.domain.OrderTucEarlyWarning" id="OrderTucEarlyWarningResult">
        <result property="earlyId"    column="early_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="salesLevel"    column="sales_level"    />
        <result property="orgCode"    column="org_code"    />
        <result property="orgName"    column="org_name"    />
        <result property="orgNetValue"    column="org_net_value"    />
        <result property="orgTarget"    column="org_target"    />
    </resultMap>

    <sql id="selectOrderTucEarlyWarningVo">
        select early_id, create_by, create_time, update_by, update_time, is_delete, sales_level, org_code, org_name, org_net_value, org_target from order_tuc_early_warning
    </sql>

    <select id="selectOrderTucEarlyWarningList" parameterType="OrderTucEarlyWarning" resultMap="OrderTucEarlyWarningResult">
        <include refid="selectOrderTucEarlyWarningVo"/>
        <where>  
            <if test="isDelete != null "> and is_delete = #{isDelete}</if>
            <if test="salesLevel != null  and salesLevel != ''"> and sales_level = #{salesLevel}</if>
            <if test="orgCode != null  and orgCode != ''"> and org_code = #{orgCode}</if>
            <if test="orgName != null  and orgName != ''"> and org_name like concat('%', #{orgName}, '%')</if>
            <if test="orgNetValue != null  and orgNetValue != ''"> and org_net_value = #{orgNetValue}</if>
            <if test="orgTarget != null  and orgTarget != ''"> and org_target = #{orgTarget}</if>
        </where>
    </select>
    
    <select id="selectOrderTucEarlyWarningById" parameterType="Long" resultMap="OrderTucEarlyWarningResult">
        <include refid="selectOrderTucEarlyWarningVo"/>
        where early_id = #{earlyId}
    </select>
        
    <insert id="insertOrderTucEarlyWarning" parameterType="OrderTucEarlyWarning" useGeneratedKeys="true" keyProperty="earlyId">
        insert into order_tuc_early_warning
        <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="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="orgNetValue != null  and orgNetValue != ''">org_net_value,</if>
            <if test="orgTarget != null  and orgTarget != ''">org_target,</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="salesLevel != null  and salesLevel != ''">#{salesLevel},</if>
            <if test="orgCode != null  and orgCode != ''">#{orgCode},</if>
            <if test="orgName != null  and orgName != ''">#{orgName},</if>
            <if test="orgNetValue != null  and orgNetValue != ''">#{orgNetValue},</if>
            <if test="orgTarget != null  and orgTarget != ''">#{orgTarget},</if>
         </trim>
    </insert>

    <insert id="batchInsert" parameterType="java.util.List">
        insert into order_tuc_early_warning (create_time, update_time,sales_level,org_code,org_name,org_net_value,org_target) values
        <foreach collection="list" item="model" index="index" separator=",">
            (#{model.createTime}, #{model.updateTime},#{model.salesLevel},#{model.orgCode},#{model.orgName},#{model.orgNetValue},#{model.orgTarget})
        </foreach>
    </insert>



    <update id="updateOrderTucEarlyWarning" parameterType="OrderTucEarlyWarning">
        update order_tuc_early_warning
        <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="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="orgNetValue != null  and orgNetValue != ''">org_net_value = #{orgNetValue},</if>
            <if test="orgTarget != null  and orgTarget != ''">org_target = #{orgTarget},</if>

        </trim>
        where org_code = #{orgCode}
    </update>

    <delete id="deleteOrderTucEarlyWarningById" parameterType="Long">
        delete from order_tuc_early_warning where early_id = #{earlyId}
    </delete>

    <delete id="deleteOrderTucEarlyWarningByIds" parameterType="String">
        delete from order_tuc_early_warning where early_id in 
        <foreach item="earlyId" collection="array" open="(" separator="," close=")">
            #{earlyId}
        </foreach>
    </delete>

    <select id="selectOrderTucEarlyWarning"  resultMap="OrderTucEarlyWarningResult">
        <include refid="selectOrderTucEarlyWarningVo"/>
        <where>
            <if test="orgCode != null  and orgCode != ''"> and org_code = #{orgCode}</if>
        </where>
        limit 1
    </select>

    <select id="selectTucGroupSales" resultType="com.dgtly.system.domain.OrderTucEarlyWarning">
        SELECT
            'TUC' AS orgCode,
            'TUC' AS orgName,
            'TUC' AS salesLevel,
            sum(
                    CONVERT (
                                now_year_act / 1000,
                                DECIMAL ( 18, 3 ))) AS orgNetValue,
            sum(
                    CONVERT (
                                NONTAX_DISCOUNT_TARGET / 1000,
                                DECIMAL ( 18, 3 ))) AS orgTarget

        FROM
            `order_sales_sum_tuc_group`
        WHERE
            cal_month BETWEEN #{startTime}
                AND  #{endTime}
          AND BUSINESS_UNIT NOT IN ( '其中-公装事业部', '其中-家装事业部' ) UNION ALL
        SELECT
            BUSINESS_UNIT AS orgCode,
            BUSINESS_UNIT AS orgName,
            BUSINESS_UNIT AS salesLevel,
            sum(
                    CONVERT (
                                now_year_act / 1000,
                                DECIMAL ( 18, 3 ))) AS orgNetValue,
            sum(
                    CONVERT (
                                NONTAX_DISCOUNT_TARGET / 1000,
                                DECIMAL ( 18, 3 ))) AS orgTarget

        FROM
            `order_sales_sum_tuc_group`
        WHERE
            cal_month BETWEEN #{startTime}
                AND  #{endTime}
        GROUP BY
            BUSINESS_UNIT UNION ALL
        SELECT
            company_code AS orgCode,
            company_name AS orgName,
            'company_level' AS salesLevel,
            sum(
                    CONVERT (
                                now_year_act / 1000,
                                DECIMAL ( 18, 3 ))) AS orgNetValue,
            sum(
                    CONVERT (
                                NONTAX_DISCOUNT_TARGET / 1000,
                                DECIMAL ( 18, 3 ))) AS orgTarget

        FROM
            `order_sales_sum_tuc_group`
        WHERE
            cal_month BETWEEN #{startTime}
                AND  #{endTime}
          AND BUSINESS_UNIT = '零售'
        GROUP BY
            company_code
    </select>

    <delete id="deleteOrderTucEarlyWarning" parameterType="Long">
        delete from order_tuc_early_warning
    </delete>
    
</mapper>