<?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.oqpo.api.mapper.stockmng.DmandFrcstMapper">

    <select id="selectDmandFrcstCndtInfo" resultType="com.oqpo.api.entity.stockmng.DmandFrcstCndtInfoEntity">
        /* DmandFrcstMapper.selectDmandFrcstCndtInfo */
        SELECT A.brand_id, A.term_cnt, A.weight_val1, A.weight_val2, A.weight_val3, A.weight_val4, A.weight_val5,
            A.weight_val6, A.coefficient, DATE_FORMAT(A.sys_reg_dttm,'%Y.%m.%d %H:%i:%s') AS sys_reg_dttm,
            A.sys_reg_id, DATE_FORMAT(A.sys_chg_dttm,'%Y.%m.%d %H:%i:%s') AS sys_chg_dttm, A.sys_chg_id
        FROM dmand_frcst_cndt_info A
        WHERE A.brand_id = #{brandId}
    </select>

    <insert id="insertDmandFrcstCndtInfo" >
        /* DmandFrcstMapper.insertDmandFrcstCndtInfo */
        INSERT INTO dmand_frcst_cndt_info
            (brand_id, term_cnt, weight_val1, weight_val2, weight_val3, weight_val4, weight_val5, weight_val6,
             coefficient, sys_reg_dttm, sys_reg_id, sys_chg_dttm, sys_chg_id)
        VALUES
            (#{entity.brandId}, #{entity.termCnt}, #{entity.weightVal1}, #{entity.weightVal2}, #{entity.weightVal3},
             #{entity.weightVal4}, #{entity.weightVal5}, #{entity.weightVal6}, #{entity.coefficient},
             NOW(), #{userId}, NOW(), #{userId})
    </insert>

    <update id="updateDmandFrcstCndtInfo" >
        /* DmandFrcstMapper.updateDmandFrcstCndtInfo */
        UPDATE dmand_frcst_cndt_info SET
            term_cnt = #{entity.termCnt},
            weight_val1 = #{entity.weightVal1},
            weight_val2 = #{entity.weightVal2},
            weight_val3 = #{entity.weightVal3},
            weight_val4 = #{entity.weightVal4},
            weight_val5 = #{entity.weightVal5},
            weight_val6 = #{entity.weightVal6},
            coefficient = #{entity.coefficient},
            sys_chg_dttm = NOW(),
            sys_chg_id = #{userId}
        WHERE brand_id = #{entity.brandId}
    </update>

    <select id="PC_DMAND_FRCST_VAL" parameterType="com.oqpo.api.web.dto.request.stockmng.StatusDmandFrcstRequest" statementType="CALLABLE">
        <![CDATA[
            {call PC_DMAND_FRCST_VAL(
                #{brandId, javaType=string, jdbcType=VARCHAR, mode=IN},
                #{itemId, javaType=string, jdbcType=VARCHAR, mode=IN},
                #{termCnt, javaType=integer, jdbcType=INTEGER, mode=IN},
                #{method, javaType=string, jdbcType=VARCHAR, mode=IN},
                #{target, javaType=string, jdbcType=VARCHAR, mode=IN},
                #{baseDate, javaType=string, jdbcType=VARCHAR, mode=IN},
                #{rcVal1, javaType=integer, jdbcType=INTEGER, mode=OUT},
                #{rcVal2, javaType=integer, jdbcType=INTEGER, mode=OUT},
                #{rcVal3, javaType=integer, jdbcType=INTEGER, mode=OUT}
            )}
        ]]>
    </select>

    <select id="selectBrandItemOdrDataMmList" resultType="com.oqpo.api.entity.stockmng.BrandItemOdrDataMmEntity">
        /* DmandFrcstMapper.selectBrandItemOdrDataMmList */
        SELECT CONCAT(SUBSTR(A.base_month, 1, 4),'.',SUBSTR(A.base_month, 5)) AS base_month, A.pch_req_cnt
        FROM brand_item_odr_data_mm A
        WHERE A.brand_id = #{brandId}
        AND A.item_id = #{itemId}
        <![CDATA[
        AND A.BASE_MONTH >= DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 9 MONTH), '%Y%m')
        AND A.BASE_MONTH <= DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 1 MONTH), '%Y%m')
        ]]>
    </select>

</mapper>
