<?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.fjhx.mapper.supplier.SupplierMapper">

    <select id="getPage" resultType="java.util.LinkedHashMap">
        select distinct
        s.id,
        s.code supplierCoded,
        s.name supplierName,
        mc.name malssifyName,
        s.account_date accountDate,
        s.account_date_remark accountDateRemark,
        sc.link_men linkMen,
        sc.telephone,
        m.price
        from supplier s
        left join supplier_price sp on s.id = sp.supplier_id
        left join material m on m.id = sp.material_id
        left join material_classify mc on m.material_classify_id = mc.id
        left join supplier_contacts sc on sc.supplier_id = s.id
        where s.del_flag = 0
        <include refid="condition"/>
        <if test="condition.type neq null and condition.type neq ''">
            and m.technology_type = #{condition.type}
        </if>
    </select>

    <select id="getAccountDate" resultType="java.util.Map">
        select s.account_date        value,
               count(s.account_date) count
        from supplier s
        where s.del_flag = 0
        group by s.account_date
        order by s.account_date
    </select>

    <select id="getSafetyWarnDay" resultType="java.util.Map">
        select count(0) count, t.value value
        from (
                 select distinct sp.supplier_id,
                                 case
                                     when m.safety_warn_day &lt; 6 then 1
                                     when m.safety_warn_day &lt; 16 then 2
                                     when m.safety_warn_day &lt; 31 then 3
                                     when m.safety_warn_day &lt; 46 then 4
                                     else 5
                                     end value
                 from supplier_price sp
                          inner join material m on sp.material_id = m.id
                 where sp.del_flag = 0
             ) t
        group by t.value
        order by t.value
    </select>

    <select id="getStatistics" resultType="java.util.Map">
        select t.technology_type type, count(0) count
        from (
        select distinct s.id, m.technology_type
        from supplier_price sp
        inner join supplier s on s.id = sp.supplier_id
        inner join material m on m.id = sp.material_id
        where s.del_flag = 0
        <include refid="condition"/>
        ) t
        group by t.technology_type
        order by t.technology_type
    </select>

    <select id="allCount" resultType="java.lang.Integer">
        select count(distinct s.id)
        from supplier s
        left join supplier_price sp on s.id = sp.supplier_id
        left join material m on m.id = sp.material_id
        where s.del_flag = 0
        <include refid="condition"/>
    </select>

    <sql id="condition">
        <if test="condition.keyword neq null and condition.keyword neq ''">
            and instr(s.name, #{condition.keyword}) > 0
        </if>
        <if test="condition.accountDate neq null and condition.accountDate neq ''">
            and s.account_date = #{accountDate}
        </if>
        <if test="condition.purpose neq null and condition.purpose neq ''">
            and m.purpose = #{condition.purpose}
        </if>
        <if test="condition.materialId neq null and condition.materialId neq ''">
            and m.id = #{condition.materialId}
        </if>
        <choose>
            <when test="condition.safetyWarnDay eq 1">
                AND m.safety_warn_day &lt; 5
            </when>
            <when test="condition.safetyWarnDay eq 2">
                AND m.safety_warn_day between 6 and 15
            </when>
            <when test="condition.safetyWarnDay eq 3">
                AND m.safety_warn_day between 16 and 30
            </when>
            <when test="condition.safetyWarnDay eq 4">
                AND m.safety_warn_day between 31 and 45
            </when>
            <when test="condition.safetyWarnDay eq 5">
                AND m.safety_warn_day &gt; 45
            </when>
        </choose>
    </sql>

</mapper>
