<?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.SupplierMapper">
    
    <resultMap type="Supplier" id="SupplierResult">
        <result property="id"    column="id"    />
        <result property="supplierName"    column="supplier_name"    />
        <result property="phone"    column="phone"    />
        <result property="supplySpecies"    column="supply_species"    />
        <result property="businessPlace"    column="business_place"    />
        <result property="originPlace"    column="origin_place"    />
        <result property="region"    column="region"    />
        <result property="evaluate"    column="evaluate"    />
        <result property="remark"    column="remark"    />
        <result property="inBlacklist"    column="in_blacklist"    />
        <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.SupplierAmountRankDTO" id="SupplierAmountRankDTOResult">
        <result property="totalAmount"    column="total_amount"    />
        <result property="supplierName"    column="supplier_name"    />
    </resultMap>

    <resultMap type="com.ruoyi.app.DTO.SupplierOriginAmountDTO" id="SupplierOriginAmountDTOResult">
        <result property="totalAmount"    column="total_amount"    />
        <result property="originPlace"    column="origin_place"    />
    </resultMap>

    <sql id="selectSupplierVo">
        select id, supplier_name, phone, supply_species, business_place, origin_place, region, evaluate, remark, in_blacklist, create_time, create_by, update_time, update_by, del_flag from supplier
    </sql>

    <select id="selectSupplierList" parameterType="Supplier" resultMap="SupplierResult">
        <include refid="selectSupplierVo"/>
        <where>
            del_flag = '0'
            <if test="supplierName != null  and supplierName != ''"> and supplier_name like concat('%', #{supplierName}, '%')</if>
            <if test="phone != null  and phone != ''"> and phone = #{phone}</if>
            <if test="supplySpecies != null  and supplySpecies != ''"> and supply_species = #{supplySpecies}</if>
            <if test="businessPlace != null  and businessPlace != ''"> and business_place = #{businessPlace}</if>
            <if test="originPlace != null  and originPlace != ''"> and origin_place = #{originPlace}</if>
            <if test="region != null  and region != ''"> and region = #{region}</if>
            <if test="evaluate != null  and evaluate != ''"> and evaluate = #{evaluate}</if>
            <if test="inBlacklist != null  and inBlacklist != ''"> and in_blacklist = #{inBlacklist}</if>
        </where>
    </select>

    <select id="selectSupplierAll" resultMap="SupplierResult">
        <include refid="selectSupplierVo"/>
        where del_flag = '0'
    </select>
    
    <select id="selectSupplierById" parameterType="Long" resultMap="SupplierResult">
        <include refid="selectSupplierVo"/>
        where id = #{id} and del_flag = '0'
    </select>

    <insert id="insertSupplier" parameterType="Supplier" useGeneratedKeys="true" keyProperty="id">
        insert into supplier
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="supplierName != null">supplier_name,</if>
            <if test="phone != null">phone,</if>
            <if test="supplySpecies != null">supply_species,</if>
            <if test="businessPlace != null">business_place,</if>
            <if test="originPlace != null">origin_place,</if>
            <if test="region != null">region,</if>
            <if test="evaluate != null">evaluate,</if>
            <if test="remark != null">remark,</if>
            <if test="inBlacklist != null">in_blacklist,</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="supplierName != null">#{supplierName},</if>
            <if test="phone != null">#{phone},</if>
            <if test="supplySpecies != null">#{supplySpecies},</if>
            <if test="businessPlace != null">#{businessPlace},</if>
            <if test="originPlace != null">#{originPlace},</if>
            <if test="region != null">#{region},</if>
            <if test="evaluate != null">#{evaluate},</if>
            <if test="remark != null">#{remark},</if>
            <if test="inBlacklist != null">#{inBlacklist},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            '0'
         </trim>
    </insert>

    <update id="updateSupplier" parameterType="Supplier">
        update supplier
        <trim prefix="SET" suffixOverrides=",">
            <if test="supplierName != null">supplier_name = #{supplierName},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="supplySpecies != null">supply_species = #{supplySpecies},</if>
            <if test="businessPlace != null">business_place = #{businessPlace},</if>
            <if test="originPlace != null">origin_place = #{originPlace},</if>
            <if test="region != null">region = #{region},</if>
            <if test="evaluate != null">evaluate = #{evaluate},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="inBlacklist != null">in_blacklist = #{inBlacklist},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
        </trim>
        where id = #{id}
    </update>

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

    <delete id="deleteSupplierByIds" parameterType="String">
        update supplier 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 supplier
        <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="SupplierAmountRankDTOResult">
        select a.supplier_name,IFNULL(SUM(b.amount), 0) AS total_amount
        from supplier as a
        left join entrance_batch as b on b.supplier_id = a.id and b.del_flag = '0'
        <where>
            a.del_flag = '0'
            <if test="startTime != null "> and b.entrance_time <![CDATA[ >= ]]> #{startTime}</if>
            <if test="endTime != null "> and b.entrance_time <![CDATA[ <= ]]> #{endTime}</if>
        </where>
        group by a.id,a.supplier_name
        order by total_amount desc
        limit 5
    </select>

    <!--根据时间范围获取不同产地供应商供应数量-->
    <select id="selectOriginAmountByTime" parameterType="com.ruoyi.app.domain.request.TimeRangeParam" resultMap="SupplierOriginAmountDTOResult">
        select dict.dict_label as origin_place,IFNULL(SUM(b.amount), 0) AS total_amount
        from sys_dict_data as dict
        left join supplier as a on dict.dict_value = a.origin_place and a.del_flag = '0'
        left join entrance_batch as b on b.supplier_id = a.id and b.del_flag = '0'
        <if test="startTime != null "> and b.entrance_time <![CDATA[ >= ]]> #{startTime}</if>
        <if test="endTime != null "> and b.entrance_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>