<?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.web.v1.mapper.SlaughterRelationMapper">
    
    <resultMap type="com.ruoyi.web.v1.model.SlaughterRelation" id="SlaughterRelationResult">
        <result property="id"    column="id"    />
        <result property="slaughterCode"    column="slaughter_code"    />
        <result property="supplierId"    column="supplier_id"    />
        <result property="purchaserId"    column="purchaser_id"    />
        <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"    />
        <association property="supplier"   javaType="com.ruoyi.web.v1.model.Supplier"  resultMap="supplierResult" />
        <association property="purchaser"  javaType="com.ruoyi.web.v1.model.Purchaser"  resultMap="purchaserResult" />
    </resultMap>
    <resultMap id="supplierResult" type="com.ruoyi.web.v1.model.Supplier">
        <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 id="purchaserResult" type="com.ruoyi.web.v1.model.Purchaser" >
        <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.web.v1.model.response.RespGetSlaughterRelation" id="GetSlaughterRelationResult">
        <result property="id"    column="id"    />
        <result property="supplierNo"    column="supplier_no"    />
        <result property="supplierName"    column="supplier_name"    />
        <result property="slaughterCodeSum"    column="slaughter_code_sum"    />
    </resultMap>

    <resultMap type="com.ruoyi.web.v1.DTO.SlaughterRelationDTO" id="SlaughterRelationDTOResult">
        <result property="slaughterCode"    column="slaughter_code"    />
        <result property="supplierName"    column="supplier_name"    />
        <result property="purchaserName"    column="purchaser_name"    />
    </resultMap>

    <sql id="selectSlaughterRelationVo">
        select id, slaughter_code, supplier_id, purchaser_id, remark, create_time, create_by, update_time, update_by, del_flag from slaughter_relation
    </sql>

    <select id="selectSlaughterRelationList" parameterType="com.ruoyi.web.v1.model.request.ReqSlaughterRelation" resultMap="SlaughterRelationResult">
        select a.*,b.purchaser_name,c.supplier_name from slaughter_relation as a
        left join purchaser as b on b.id = a.purchaser_id
        left join supplier as c on c.id = a.supplier_id
        <where>
            a.del_flag = '0'
            <if test="keyword != null  and keyword != ''"> and (a.slaughter_code = #{keyword} or b.purchaser_name = #{keyword} or c.supplier_name = #{keyword})</if>
            <if test="slaughterCode != null  and slaughterCode != ''"> and a.slaughter_code = #{slaughterCode}</if>
            <if test="supplierId != null "> and a.supplier_id = #{supplierId}</if>
            <if test="purchaserId != null "> and a.purchaser_id = #{purchaserId}</if>
        </where>
        order by a.create_time desc
    </select>

    <!--导出血码清单-->
    <select id="exportSlaughterRelationList" parameterType="com.ruoyi.web.v1.model.request.ExportSlaughterRelation" resultMap="SlaughterRelationDTOResult">
        select a.slaughter_code,b.purchaser_name,c.supplier_name from slaughter_relation as a
        inner join purchaser as b on b.id = a.purchaser_id
        inner join supplier as c on c.id = a.supplier_id
        <where>
            a.del_flag = '0'
            <if test="ids != null ">
                and a.id in
                <foreach item="id" collection="ids" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
            <if test="supplierIds != null ">
                and c.id in
                <foreach item="supplierId" collection="supplierIds" open="(" separator="," close=")">
                    #{supplierId}
                </foreach>
            </if>
        </where>
        order by c.create_time desc
    </select>

    
    <select id="selectSlaughterRelationById" parameterType="Long" resultMap="SlaughterRelationResult">
        <include refid="selectSlaughterRelationVo"/>
        where id = #{id} and del_flag = '0'
    </select>

    <select id="selectSupplierList" parameterType="com.ruoyi.web.v1.model.request.ReqGetSlaughterRelation" resultMap="GetSlaughterRelationResult">
        select a.id,a.supplier_no,a.supplier_name,count(b.id) as slaughter_code_sum
        from supplier as a
        left join slaughter_relation as b on a.id = b.supplier_id and b.del_flag = '0'
        <where>
            a.del_flag = '0'
            <if test="supplierName != null  and supplierName != ''"> and a.supplier_name like concat('%', #{supplierName}, '%')</if>
        </where>
        group by a.id,a.supplier_no,a.supplier_name
        order by a.create_time desc
    </select>

    <!--判断血码是否已存在-->
    <select id="checkCodeUnique" parameterType="String"  resultMap="SlaughterRelationResult">
        select id, slaughter_code from slaughter_relation where slaughter_code = #{slaughterCode} and del_flag = '0' limit 1
    </select>

    <!--判断当前供应商下肉商是否已有血码-->
    <select id="checkPurchaserUnique" parameterType="com.ruoyi.web.v1.model.SlaughterRelation"  resultMap="SlaughterRelationResult">
        select a.id, a.purchaser_id,b.purchaser_name from slaughter_relation as a
        left join purchaser as b on b.id = a.purchaser_id
        where a.purchaser_id = #{purchaserId} and a.supplier_id = #{supplierId} and a.del_flag = '0'
        limit 1
    </select>

    <insert id="insertSlaughterRelation" parameterType="com.ruoyi.web.v1.model.SlaughterRelation" useGeneratedKeys="true" keyProperty="id">
        insert into slaughter_relation
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="slaughterCode != null and slaughterCode != ''">slaughter_code,</if>
            <if test="supplierId != null">supplier_id,</if>
            <if test="purchaserId != null">purchaser_id,</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="slaughterCode != null and slaughterCode != ''">#{slaughterCode},</if>
            <if test="supplierId != null">#{supplierId},</if>
            <if test="purchaserId != null">#{purchaserId},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            '0'
         </trim>
    </insert>

    <insert id="insertSlaughterRelationBatch" parameterType="list" >
        INSERT INTO slaughter_relation (slaughter_code, supplier_id, purchaser_id, create_time, create_by,del_flag)
        VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.slaughterCode}, #{item.supplierId}, #{item.purchaserId}, #{item.createTime}, #{item.createBy},'0')
        </foreach>
    </insert>

    <update id="updateSlaughterRelation" parameterType="com.ruoyi.web.v1.model.SlaughterRelation">
        update slaughter_relation
        <trim prefix="SET" suffixOverrides=",">
            <if test="slaughterCode != null and slaughterCode != ''">slaughter_code = #{slaughterCode},</if>
            <if test="supplierId != null">supplier_id = #{supplierId},</if>
            <if test="purchaserId != null">purchaser_id = #{purchaserId},</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="deleteSlaughterRelationById" parameterType="Long">
        update slaughter_relation set del_flag = '2' where id = #{id}
    </delete>

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

    <!--根据供应商ID统计血码数量-->
    <select id="countSlaughterRelationBySupplier" parameterType="Long" resultType="Integer">
        select count(1) from slaughter_relation
        where supplier_id = #{supplierId} and del_flag = '0'
    </select>

    <!--根据肉商ID统计血码数量-->
    <select id="countSlaughterRelationByPurchaser" parameterType="Long" resultType="Integer">
        select count(1) from slaughter_relation
        where purchaser_id = #{purchaserId} and del_flag = '0'
    </select>
</mapper>