<?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.unis.basic.info.modular.grainPriceCollectProvinceSum.mapper.GrainPriceCollectProvinceSumMapper">
    
    <update id="updateGrainPriceCollectCitySum" >
       update
      biz_grain_price_collect_city_sum b
	  LEFT JOIN sys_org c ON b.sys_org_id = c.ID
		set b.grain_price_collect_province_sum_id = #{provincId}
    WHERE   c.PARENT_ID =  #{orgId}  and b.audit_status = 1
          and date_format(b.sum_time,'%Y-%m-%d %T') BETWEEN #{monday} and #{sunday}
    </update>
    <select id="getAvgPriceByCity" resultType="java.util.Map">
        SELECT
        a.price_type as "priceType",
        IFNULL(a.first_level, 0) as "firstLevel",
        IFNULL(a.second_level, 0) as "secondLevel",
        IFNULL(a.three_level, 0) as "threeLevel",
        AVG(price) AS "avgPrice"
    FROM
        biz_grain_price_collect_category a
    INNER JOIN biz_grain_price_collect_city_sum b ON a.type_id = b.id
       WHERE a.type = #{type}   and b.status = 1
          and date_format(b.sum_time,'%Y-%m-%d %T') BETWEEN #{monday} and #{sunday}
        GROUP BY
        IFNULL(a.first_level, 0),
        IFNULL(a.second_level, 0),
        IFNULL(a.three_level, 0),
        a.price_type
      ORDER BY
        a.price_type
    </select>
    <select id="getOffAvgPriceByProvince" resultType="java.util.Map">
        SELECT
            a.price AS "offAvgPrice"
        FROM
            biz_grain_price_collect_category a
        INNER JOIN biz_grain_price_collect_province_sum b ON a.type_id = b.id
        WHERE
            a.type =  #{type}
        AND date_format(
            b.sum_time,
            '%Y-%m-%d %T'
        ) BETWEEN #{monday} and #{sunday}
        and a.price_type = #{priceType}
        AND IFNULL(a.first_level, 0) = #{firstLevel}
        AND IFNULL(a.second_level, 0) = #{secondLevel}
        AND IFNULL(a.three_level, 0) = #{threeLevel}
    </select>
    <select id="selectMyMapsPage" resultType="java.util.Map">
        SELECT
        CAST(a.id AS CHAR) as "id",
        c.name as "userName",
        DATE_FORMAT(a.sum_time,'%Y-%m-%d %T') as "sumTime"
        FROM
        biz_grain_price_collect_province_sum a
        LEFT JOIN sys_user c on a.sys_user_id = c.ID
        <if test="ew.sqlSegment != null and ew.sqlSegment != ''">
            where  ${ew.sqlSegment}
        </if>
        order  by a.sum_time desc
    </select>
</mapper>