<?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.factory.mapper.CFactoryMapper">

    <resultMap type="CFactory" id="CFactoryResult">
        <result property="factoryId"    column="factory_id"    />
        <result property="factoryNo"    column="factory_no"    />
        <result property="factoryName"    column="factory_name"    />
        <result property="factoryRgRatio"    column="factory_rg_ratio"    />
        <result property="factoryWjRatio"    column="factory_wj_ratio"    />
        <result property="factoryFlRatio"    column="factory_fl_ratio"    />
        <result property="factoryDfRatio"    column="factory_df_ratio"    />
        <result property="factoryTrqRatio"    column="factory_trq_ratio"    />
        <result property="factoryYsRatio"    column="factory_ys_ratio"    />
        <result property="factoryPjRatio"    column="factory_pj_ratio"    />
        <result property="factoryTotalRatio"    column="factory_total_ratio"    />
    </resultMap>

    <resultMap id="CFactoryCMaterialTypeResult" type="CFactory" extends="CFactoryResult">
        <collection property="cMaterialTypeList" notNullColumn="sub_type_id" javaType="java.util.List" resultMap="CMaterialTypeResult" />
    </resultMap>

    <resultMap type="CMaterialTypeFactory" id="CMaterialTypeResult">
        <result property="typeId"    column="sub_type_id"    />
        <result property="typeNo"    column="sub_type_no"    />
        <result property="typeName"    column="sub_type_name"    />
        <result property="factoryId"    column="sub_factory_id"    />
    </resultMap>

    <sql id="selectCFactoryVo">
        select factory_id, factory_no, factory_name, factory_rg_ratio, factory_wj_ratio, factory_fl_ratio, factory_df_ratio, factory_trq_ratio, factory_ys_ratio, factory_pj_ratio, factory_total_ratio from c_factory
    </sql>

    <select id="selectCFactoryList" parameterType="CFactory" resultMap="CFactoryResult">
        <include refid="selectCFactoryVo"/>
        <where>
            <if test="factoryNo != null  and factoryNo != ''"> and factory_no = #{factoryNo}</if>
            <if test="factoryName != null  and factoryName != ''"> and factory_name like concat('%', #{factoryName}, '%')</if>
        </where>
    </select>

    <select id="selectCTypelist" resultType="CMaterialTypeFactory">
        select type_no as typeNo,type_name as typeName from c_material_type
    </select>

    <select id="selectCFactoryByFactoryId" parameterType="Long" resultMap="CFactoryCMaterialTypeResult">
        select a.factory_id, a.factory_no, a.factory_name, a.factory_rg_ratio, a.factory_wj_ratio, a.factory_fl_ratio,
               a.factory_df_ratio, a.factory_trq_ratio, a.factory_ys_ratio, a.factory_pj_ratio, a.factory_total_ratio,
               b.type_id as sub_type_id, case when c.type_state!='0' then '' else b.type_no end sub_type_no,
               case when c.type_state!='0' then b.type_name+'(停用)' else b.type_name end sub_type_name, b.factory_id as sub_factory_id
        from c_factory a
        left join c_material_type_factory b on b.factory_id = a.factory_no
        left join c_material_type c on c.type_no = b.type_no
        where a.factory_id = #{factoryId}
    </select>

    <insert id="insertCFactory" parameterType="CFactory">
        insert into c_factory
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="factoryId != null">factory_id,</if>
            <if test="factoryNo != null">factory_no,</if>
            <if test="factoryName != null">factory_name,</if>
            <if test="factoryRgRatio != null">factory_rg_ratio,</if>
            <if test="factoryWjRatio != null">factory_wj_ratio,</if>
            <if test="factoryFlRatio != null">factory_fl_ratio,</if>
            <if test="factoryDfRatio != null">factory_df_ratio,</if>
            <if test="factoryTrqRatio != null">factory_trq_ratio,</if>
            <if test="factoryYsRatio != null">factory_ys_ratio,</if>
            <if test="factoryPjRatio != null">factory_pj_ratio,</if>
            <if test="factoryTotalRatio != null">factory_total_ratio,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="factoryId != null">#{factoryId},</if>
            <if test="factoryNo != null">#{factoryNo},</if>
            <if test="factoryName != null">#{factoryName},</if>
            <if test="factoryRgRatio != null">#{factoryRgRatio},</if>
            <if test="factoryWjRatio != null">#{factoryWjRatio},</if>
            <if test="factoryFlRatio != null">#{factoryFlRatio},</if>
            <if test="factoryDfRatio != null">#{factoryDfRatio},</if>
            <if test="factoryTrqRatio != null">#{factoryTrqRatio},</if>
            <if test="factoryYsRatio != null">#{factoryYsRatio},</if>
            <if test="factoryPjRatio != null">#{factoryPjRatio},</if>
            <if test="factoryTotalRatio != null">#{factoryTotalRatio},</if>
         </trim>
    </insert>

    <update id="updateCFactory" parameterType="CFactory">
        update c_factory
        <trim prefix="SET" suffixOverrides=",">
            <if test="factoryNo != null">factory_no = #{factoryNo},</if>
            <if test="factoryName != null">factory_name = #{factoryName},</if>
            <if test="factoryRgRatio != null">factory_rg_ratio = #{factoryRgRatio},</if>
            <if test="factoryWjRatio != null">factory_wj_ratio = #{factoryWjRatio},</if>
            <if test="factoryFlRatio != null">factory_fl_ratio = #{factoryFlRatio},</if>
            <if test="factoryDfRatio != null">factory_df_ratio = #{factoryDfRatio},</if>
            <if test="factoryTrqRatio != null">factory_trq_ratio = #{factoryTrqRatio},</if>
            <if test="factoryYsRatio != null">factory_ys_ratio = #{factoryYsRatio},</if>
            <if test="factoryPjRatio != null">factory_pj_ratio = #{factoryPjRatio},</if>
            <if test="factoryTotalRatio != null">factory_total_ratio = #{factoryTotalRatio},</if>
        </trim>
        where factory_id = #{factoryId}
    </update>

    <delete id="deleteCFactoryByFactoryId" parameterType="Long">
        delete from c_factory where factory_id = #{factoryId}
    </delete>

    <delete id="deleteCFactoryByFactoryIds" parameterType="Long">
        delete from c_factory where factory_id in
        <foreach item="factoryId" collection="array" open="(" separator="," close=")">
            #{factoryId}
        </foreach>
    </delete>

    <delete id="deleteCMaterialTypeByFactoryIds" parameterType="String">
        delete from c_material_type_factory where factory_id in
        <foreach item="factoryId" collection="array" open="(" separator="," close=")">
            #{factoryId}
        </foreach>
    </delete>

    <delete id="deleteCMaterialTypeByFactoryId" parameterType="String">
        delete from c_material_type_factory where factory_id = #{factoryId}
    </delete>

    <insert id="batchCMaterialType">
        insert into c_material_type_factory(type_no, type_name, factory_id) values
		<foreach item="item" index="index" collection="list" separator=",">
            (#{item.typeNo}, #{item.typeName}, #{item.factoryId})
        </foreach>
    </insert>

    <select id="checkFactoryNoUnique" parameterType="String" resultMap="CFactoryResult">
        select top(1) factory_id, factory_no from c_factory where factory_no = #{factoryNo}
    </select>
</mapper>
