<?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.web.modules.trading.mapper.BizMarketDailyQuoteMapper">

    <resultMap type="com.ruoyi.web.modules.trading.domain.BizMarketDailyQuote" id="BizMarketDailyQuoteResult">
        <id     property="id"                column="id"/>
        <result property="quoteDate"         column="quote_date"/>
        <result property="provinceCode"      column="province_code"/>
        <result property="provinceName"      column="province_name"/>
        <result property="cityCode"        column="city_code"/>
        <result property="cityName"        column="city_name"/>
        <result property="countyCode"      column="county_code"/>
        <result property="countyName"      column="county_name"/>
        <result property="tradeMarketId"   column="trade_market_id"/>
        <result property="cattleGender"    column="cattle_gender"/>
        <result property="weightGrade"     column="weight_grade"/>
        <result property="growthStage"     column="growth_stage"/>
        <result property="ageMonthGrade"   column="age_month_grade"/>
        <result property="fatGrade"        column="fat_grade"/>
        <result property="priceMin"        column="price_min"/>
        <result property="priceMax"        column="price_max"/>
        <result property="priceUnit"       column="price_unit"/>
        <result property="avgPrice"        column="avg_price"/>
        <result property="supplyStatus"    column="supply_status"/>
        <result property="changeFactors"   column="change_factors"/>
        <result property="probRise"        column="prob_rise"/>
        <result property="probFlat"        column="prob_flat"/>
        <result property="probFall"        column="prob_fall"/>
        <result property="outboundAdvice"  column="outbound_advice"/>
        <result property="delFlag"         column="del_flag"/>
        <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="tradeMarketName" column="market_name"/>
    </resultMap>

    <sql id="selectVo">
        select q.id, q.quote_date, q.province_code, q.province_name, q.city_code, q.city_name,
               q.county_code, q.county_name, q.trade_market_id, q.cattle_gender, q.weight_grade,
               q.growth_stage, q.age_month_grade, q.fat_grade, q.price_min, q.price_max, q.price_unit,
               q.avg_price, q.supply_status, q.change_factors, q.prob_rise, q.prob_flat, q.prob_fall,
               q.outbound_advice, q.del_flag, q.create_by, q.create_time, q.update_by, q.update_time,
               m.market_name
        from biz_market_daily_quote q
        left join biz_trade_market m on q.trade_market_id = m.id and m.del_flag = '0'
    </sql>

    <select id="selectBizMarketDailyQuoteById" parameterType="long" resultMap="BizMarketDailyQuoteResult">
        <include refid="selectVo"/>
        where q.id = #{id} and q.del_flag = '0'
    </select>

    <select id="selectBizMarketDailyQuoteByIdAny" parameterType="long" resultMap="BizMarketDailyQuoteResult">
        <include refid="selectVo"/>
        where q.id = #{id}
    </select>

    <select id="selectBizMarketDailyQuoteList" parameterType="com.ruoyi.web.modules.trading.domain.BizMarketDailyQuote"
            resultMap="BizMarketDailyQuoteResult">
        <include refid="selectVo"/>
        <where>
            q.del_flag = '0'
            <if test="quoteDate != null">
                and q.quote_date = #{quoteDate}
            </if>
            <if test="quoteDate == null and beginQuoteDate != null">
                and q.quote_date &gt;= #{beginQuoteDate}
            </if>
            <if test="quoteDate == null and endQuoteDate != null">
                and q.quote_date &lt;= #{endQuoteDate}
            </if>
            <if test="tradeMarketId != null">
                and q.trade_market_id = #{tradeMarketId}
            </if>
            <if test="countyCode != null and countyCode != ''">
                and q.county_code = #{countyCode}
            </if>
        </where>
        order by q.quote_date desc, q.create_time desc, q.id desc
    </select>

    <select id="countDuplicateDimension" resultType="int">
        select count(1) from biz_market_daily_quote
        where del_flag = '0'
          and quote_date = #{quoteDate}
          and county_code = #{countyCode}
          and trade_market_id = #{tradeMarketId}
          and cattle_gender = #{cattleGender}
          and IFNULL(weight_grade, 0) = #{weightGradeNorm}
          and IFNULL(growth_stage, 0) = #{growthStageNorm}
          and IFNULL(age_month_grade, 0) = #{ageMonthGradeNorm}
          and IFNULL(fat_grade, 0) = #{fatGradeNorm}
        <if test="excludeId != null">
          and id != #{excludeId}
        </if>
    </select>

    <insert id="insertBizMarketDailyQuote" parameterType="com.ruoyi.web.modules.trading.domain.BizMarketDailyQuote"
            useGeneratedKeys="true" keyProperty="id">
        insert into biz_market_daily_quote (
            quote_date, province_code, province_name, city_code, city_name, county_code, county_name,
            trade_market_id, cattle_gender, weight_grade, growth_stage, age_month_grade, fat_grade,
            price_min, price_max, price_unit, avg_price, supply_status, change_factors,
            prob_rise, prob_flat, prob_fall, outbound_advice, del_flag,
            create_by, create_time, update_by, update_time
        ) values (
            #{quoteDate}, #{provinceCode}, #{provinceName}, #{cityCode}, #{cityName}, #{countyCode}, #{countyName},
            #{tradeMarketId}, #{cattleGender}, #{weightGrade}, #{growthStage}, #{ageMonthGrade}, #{fatGrade},
            #{priceMin}, #{priceMax}, #{priceUnit}, #{avgPrice}, #{supplyStatus}, #{changeFactors},
            #{probRise}, #{probFlat}, #{probFall}, #{outboundAdvice}, #{delFlag},
            #{createBy}, #{createTime}, #{updateBy}, #{updateTime}
        )
    </insert>

    <update id="updateBizMarketDailyQuote" parameterType="com.ruoyi.web.modules.trading.domain.BizMarketDailyQuote">
        update biz_market_daily_quote set
            quote_date = #{quoteDate},
            province_code = #{provinceCode},
            province_name = #{provinceName},
            city_code = #{cityCode},
            city_name = #{cityName},
            county_code = #{countyCode},
            county_name = #{countyName},
            trade_market_id = #{tradeMarketId},
            cattle_gender = #{cattleGender},
            weight_grade = #{weightGrade},
            growth_stage = #{growthStage},
            age_month_grade = #{ageMonthGrade},
            fat_grade = #{fatGrade},
            price_min = #{priceMin},
            price_max = #{priceMax},
            price_unit = #{priceUnit},
            avg_price = #{avgPrice},
            supply_status = #{supplyStatus},
            change_factors = #{changeFactors},
            prob_rise = #{probRise},
            prob_flat = #{probFlat},
            prob_fall = #{probFall},
            outbound_advice = #{outboundAdvice},
            update_by = #{updateBy},
            update_time = #{updateTime}
        where id = #{id} and del_flag = '0'
    </update>

    <update id="logicDeleteBizMarketDailyQuoteByIds">
        update biz_market_daily_quote
        set del_flag = '2', update_by = #{updateBy}, update_time = now()
        where id in
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
        and del_flag = '0'
    </update>
</mapper>
