<?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.app.mapper.PurchaserMapper">
    
    <resultMap type="Purchaser" id="PurchaserResult">
        <result property="id"    column="id"    />
        <result property="purchaserName"    column="purchaser_name"    />
        <result property="idNumber"    column="id_number"    />
        <result property="phone"    column="phone"    />
        <result property="businessPlace"    column="business_place"    />
        <result property="salePlace"    column="sale_place"    />
        <result property="region"    column="region"    />
        <result property="remark"    column="remark"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="delFlag"    column="del_flag"    />
    </resultMap>
    <resultMap type="com.ruoyi.app.DTO.PurchaserAmountRankDTO" id="PurchaserAmountRankDTOResult">
        <result property="totalAmount"    column="total_amount"    />
        <result property="purchaserName"    column="purchaser_name"    />
    </resultMap>

    <resultMap type="com.ruoyi.app.DTO.PurchaserSaleAmountDTO" id="PurchaserSaleAmountDTOResult">
        <result property="totalAmount"    column="total_amount"    />
        <result property="salePlace"    column="sale_place"    />
    </resultMap>
    <sql id="selectPurchaserVo">
        select id, purchaser_name, id_number, phone, business_place, sale_place, region, remark, create_time, create_by, update_time, update_by, del_flag from purchaser
    </sql>

    <select id="selectPurchaserList" parameterType="Purchaser" resultMap="PurchaserResult">
        <include refid="selectPurchaserVo"/>
        <where>
            del_flag = '0'
            <if test="purchaserName != null  and purchaserName != ''"> and purchaser_name like concat('%', #{purchaserName}, '%')</if>
            <if test="idNumber != null  and idNumber != ''"> and id_number = #{idNumber}</if>
            <if test="phone != null  and phone != ''"> and phone = #{phone}</if>
            <if test="businessPlace != null  and businessPlace != ''"> and business_place = #{businessPlace}</if>
            <if test="salePlace != null  and salePlace != ''"> and sale_place = #{salePlace}</if>
            <if test="region != null  and region != ''"> and region = #{region}</if>
        </where>
    </select>

    <select id="selectPurchaserAll" resultMap="PurchaserResult">
        <include refid="selectPurchaserVo"/>
        where del_flag = '0'
    </select>
    
    <select id="selectPurchaserById" parameterType="Long" resultMap="PurchaserResult">
        <include refid="selectPurchaserVo"/>
        where id = #{id} and del_flag = '0'
    </select>

    <insert id="insertPurchaser" parameterType="Purchaser" useGeneratedKeys="true" keyProperty="id">
        insert into purchaser
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="purchaserName != null">purchaser_name,</if>
            <if test="idNumber != null">id_number,</if>
            <if test="phone != null">phone,</if>
            <if test="businessPlace != null">business_place,</if>
            <if test="salePlace != null">sale_place,</if>
            <if test="region != null">region,</if>
            <if test="remark != null">remark,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            del_flag
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="purchaserName != null">#{purchaserName},</if>
            <if test="idNumber != null">#{idNumber},</if>
            <if test="phone != null">#{phone},</if>
            <if test="businessPlace != null">#{businessPlace},</if>
            <if test="salePlace != null">#{salePlace},</if>
            <if test="region != null">#{region},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            '0'
         </trim>
    </insert>

    <update id="updatePurchaser" parameterType="Purchaser">
        update purchaser
        <trim prefix="SET" suffixOverrides=",">
            <if test="purchaserName != null">purchaser_name = #{purchaserName},</if>
            <if test="idNumber != null">id_number = #{idNumber},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="businessPlace != null">business_place = #{businessPlace},</if>
            <if test="salePlace != null">sale_place = #{salePlace},</if>
            <if test="region != null">region = #{region},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePurchaserById" parameterType="Long">
        update purchaser set del_flag = '2' where id = #{id}
    </delete>

    <delete id="deletePurchaserByIds" parameterType="String">
        update purchaser set del_flag = '2' where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <!--根据时间范围获取肉商数量-->
    <select id="selectCountByTime" parameterType="com.ruoyi.app.domain.request.TimeRangeParam" resultType="Long">
        select count(1) from purchaser
        <where>
            del_flag = '0'
<!--            <if test="startTime != null "> and create_time <![CDATA[ >= ]]> #{startTime}</if>-->
<!--            <if test="endTime != null "> and create_time <![CDATA[ <= ]]> #{endTime}</if>-->
        </where>
    </select>

    <!--根据时间范围获取肉商接收数量前五排名-->
    <select id="selectAmountRankByTime" parameterType="com.ruoyi.app.domain.request.TimeRangeParam" resultMap="PurchaserAmountRankDTOResult">
        select a.purchaser_name,IFNULL(SUM(b.amount), 0) AS total_amount
        from purchaser as a
        left join distribute_batch as b on b.purchaser_id = a.id and b.del_flag = '0'
        <where>
            a.del_flag = '0'
            <if test="startTime != null "> and b.create_time <![CDATA[ >= ]]> #{startTime}</if>
            <if test="endTime != null "> and b.create_time <![CDATA[ <= ]]> #{endTime}</if>
        </where>
        group by a.id,a.purchaser_name
        order by total_amount desc
        limit 5
    </select>

    <!--根据时间范围获取肉商不同销售地的数量-->
    <select id="selectSaleAmountByTime" parameterType="com.ruoyi.app.domain.request.TimeRangeParam" resultMap="PurchaserSaleAmountDTOResult">
        select dict.dict_label as sale_place,IFNULL(SUM(b.amount), 0) AS total_amount
        from sys_dict_data as dict
        left join purchaser as a on dict.dict_value = a.sale_place and a.del_flag = '0'
        left join distribute_batch as b on b.purchaser_id = a.id and b.del_flag = '0'
        <if test="startTime != null "> and b.create_time <![CDATA[ >= ]]> #{startTime}</if>
        <if test="endTime != null "> and b.create_time <![CDATA[ <= ]]> #{endTime}</if>
        <where>
            dict.dict_type = 'app_origin_type' and dict.status = '0'
        </where>
        group by dict.dict_value,dict.dict_label
    </select>
</mapper>