<?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.aegis.resource.supplier.mapper.ResourceSupplierMapper">

    <resultMap type="Supplier" id="SupplierResult">
        <id     property="id"              column="id"               />
        <result property="supplierCode"    column="supplier_code"     />
        <result property="supplierName"    column="supplier_name"     />
        <result property="creditCode"      column="credit_code"       />
        <result property="category"        column="category"          />
        <result property="area"            column="area"              />
        <result property="contactPerson"   column="contact_person"    />
        <result property="contactPhone"    column="contact_phone"     />
        <result property="email"           column="email"              />
        <result property="address"         column="address"            />
        <result property="status"          column="status"             />
        <result property="rating"          column="rating"             />
        <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="remark"          column="remark"             />
    </resultMap>

    <sql id="selectSupplierVo">
        select id, supplier_code, supplier_name, credit_code, category, area, contact_person,
               contact_phone, email, address, status, rating, del_flag, create_by, create_time,
               update_by, update_time, remark
        from resource_supplier
    </sql>

    <select id="selectSupplierList" parameterType="Supplier" resultMap="SupplierResult">
        <include refid="selectSupplierVo"/>
        <where>
            del_flag = '0'
            <if test="supplierCode != null and supplierCode != ''">
                AND supplier_code like concat('%', #{supplierCode}, '%')
            </if>
            <if test="supplierName != null and supplierName != ''">
                AND supplier_name like concat('%', #{supplierName}, '%')
            </if>
            <if test="category != null and category != ''">
                AND category = #{category}
            </if>
            <if test="area != null and area != ''">
                AND area like concat('%', #{area}, '%')
            </if>
            <if test="status != null and status != ''">
                AND status = #{status}
            </if>
            <if test="rating != null">
                AND rating = #{rating}
            </if>
            <if test="contactPerson != null and contactPerson != ''">
                AND contact_person like concat('%', #{contactPerson}, '%')
            </if>
            <if test="contactPhone != null and contactPhone != ''">
                AND contact_phone like concat('%', #{contactPhone}, '%')
            </if>
            <if test="params.beginTime != null and params.beginTime != ''">
                AND date_format(create_time,'%Y%m%d') &gt;= date_format(#{params.beginTime},'%Y%m%d')
            </if>
            <if test="params.endTime != null and params.endTime != ''">
                AND date_format(create_time,'%Y%m%d') &lt;= date_format(#{params.endTime},'%Y%m%d')
            </if>
        </where>
        order by rating desc, create_time desc
    </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 resource_supplier(
            <if test="supplierCode != null and supplierCode != ''">supplier_code,</if>
            <if test="supplierName != null and supplierName != ''">supplier_name,</if>
            <if test="creditCode != null">credit_code,</if>
            <if test="category != null">category,</if>
            <if test="area != null">area,</if>
            <if test="contactPerson != null">contact_person,</if>
            <if test="contactPhone != null">contact_phone,</if>
            <if test="email != null">email,</if>
            <if test="address != null">address,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="rating != null">rating,</if>
            <if test="remark != null">remark,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
            create_time
        )values(
            <if test="supplierCode != null and supplierCode != ''">#{supplierCode},</if>
            <if test="supplierName != null and supplierName != ''">#{supplierName},</if>
            <if test="creditCode != null">#{creditCode},</if>
            <if test="category != null">#{category},</if>
            <if test="area != null">#{area},</if>
            <if test="contactPerson != null">#{contactPerson},</if>
            <if test="contactPhone != null">#{contactPhone},</if>
            <if test="email != null">#{email},</if>
            <if test="address != null">#{address},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="rating != null">#{rating},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            sysdate()
        )
    </insert>

    <update id="updateSupplier" parameterType="Supplier">
        update resource_supplier
        <set>
            <if test="supplierCode != null and supplierCode != ''">supplier_code = #{supplierCode},</if>
            <if test="supplierName != null and supplierName != ''">supplier_name = #{supplierName},</if>
            <if test="creditCode != null">credit_code = #{creditCode},</if>
            <if test="category != null">category = #{category},</if>
            <if test="area != null">area = #{area},</if>
            <if test="contactPerson != null">contact_person = #{contactPerson},</if>
            <if test="contactPhone != null">contact_phone = #{contactPhone},</if>
            <if test="email != null">email = #{email},</if>
            <if test="address != null">address = #{address},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="rating != null">rating = #{rating},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            <if test="delFlag != null and delFlag != ''">del_flag = #{delFlag},</if>
            update_time = sysdate()
        </set>
        where id = #{id} and del_flag = '0'
    </update>

    <update id="updateSupplierRating">
        update resource_supplier
        <set>
            rating = #{rating},
            update_time = sysdate()
        </set>
        where id = #{id} and del_flag = '0'
    </update>

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

    <update id="deleteSupplierByIds" parameterType="Long">
        update resource_supplier set del_flag = '2' where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <select id="checkSupplierCodeUnique" resultMap="SupplierResult">
        <include refid="selectSupplierVo"/>
        where supplier_code = #{supplierCode} and del_flag = '0'
        <if test="id != null">
            AND id != #{id}
        </if>
        limit 1
    </select>

    <select id="selectDeletedSupplierByCode" parameterType="String" resultMap="SupplierResult">
        <include refid="selectSupplierVo"/>
        where supplier_code = #{supplierCode} and del_flag = '2'
        limit 1
    </select>

    <update id="restoreSupplier" parameterType="Supplier">
        update resource_supplier
        <set>
            <if test="supplierCode != null and supplierCode != ''">supplier_code = #{supplierCode},</if>
            <if test="supplierName != null and supplierName != ''">supplier_name = #{supplierName},</if>
            <if test="creditCode != null">credit_code = #{creditCode},</if>
            <if test="category != null">category = #{category},</if>
            <if test="area != null">area = #{area},</if>
            <if test="contactPerson != null">contact_person = #{contactPerson},</if>
            <if test="contactPhone != null">contact_phone = #{contactPhone},</if>
            <if test="email != null">email = #{email},</if>
            <if test="address != null">address = #{address},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="rating != null">rating = #{rating},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createBy != null and createBy != ''">create_by = #{createBy},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            del_flag = '0',
            update_time = sysdate()
        </set>
        where id = #{id}
    </update>

</mapper>