<?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.yiqi.system.mapper.SysSettlementRateMapper">

        <resultMap type="SysSettlementRate" id="SysSettlementRateResult">
                <result property="id" column="id"/>
                <result property="sourceType" column="source_type"/>
                <result property="orgId" column="org_id"/>
                <result property="factoryRate" column="factory_rate"/>
                <result property="materialRate" column="material_rate"/>
                <result property="manageRate" column="manage_rate"/>
                <result property="depositRate" column="deposit_rate"/>
                <result property="otherStoreRate" column="other_store_rate"/>
                <result property="aliPayRate" column="ali_pay_rate"/>
                <result property="wxPayRate" column="wx_pay_rate"/>
        </resultMap>

        <sql id="selectSysSettlementRateVo">
                select id, source_type, org_id, factory_rate, material_rate, manage_rate, deposit_rate, other_store_rate, ali_pay_rate, wx_pay_rate
                from sys_settlement_rate
        </sql>

        <select id="selectSysSettlementRateListByStore" parameterType="SysSettlementRate" resultMap="SysSettlementRateResult">
                select ssr.id, ssr.source_type, ssr.org_id, ssr.factory_rate, ssr.material_rate, ssr.manage_rate, ssr.deposit_rate, ssr.other_store_rate, ssr.ali_pay_rate,ssr.wx_pay_rate
                from sys_store ss
                                left join sys_settlement_rate ssr on ss.id = ssr.org_id
                                and ss.status = 0 and ss.del_flag = 0
                                and ssr.source_type = '02'
                <where>
                <if test="orgId != null and orgId != ''">
                        and ss.id = #{orgId}
                </if>
                <if test="sourceType != null and sourceType != ''">
                        and ssr.source_type = #{sourceType}
                </if>
                </where>
        </select>

        <select id="selectSysSettlementRateListByFactory" parameterType="SysSettlementRate" resultMap="SysSettlementRateResult">
                select ssr.id, ssr.source_type, ssr.org_id, ssr.factory_rate, ssr.material_rate, ssr.manage_rate, ssr.deposit_rate, ssr.other_store_rate,  ssr.ali_pay_rate,ssr.wx_pay_rate
                from manage_factory mf
                left join sys_settlement_rate ssr on mf.id = ssr.org_id
                                                and mf.status = 0 and mf.del_flag = 0
                                                and ssr.source_type = '01'
                <where>
                        <if test="orgId != null and orgId != ''">
                                and mf.id = #{orgId}
                        </if>
                        <if test="sourceType != null and sourceType != ''">
                                and ssr.source_type = #{sourceType}
                        </if>
                </where>
        </select>

        <select id="selectSysSettlementRateById" parameterType="Long"
                resultMap="SysSettlementRateResult">
                <include refid="selectSysSettlementRateVo"/>
                where id = #{id}
        </select>

        <insert id="insertSysSettlementRate" parameterType="SysSettlementRate" useGeneratedKeys="true"
                keyProperty="id">
                insert into sys_settlement_rate
                <trim prefix="(" suffix=")" suffixOverrides=",">
                        <if test="sourceType != null">source_type,
                        </if>
                        <if test="orgId != null">org_id,
                        </if>
                        <if test="factoryRate != null">factory_rate,
                        </if>
                        <if test="materialRate != null">material_rate,
                        </if>
                        <if test="manageRate != null">manage_rate,
                        </if>
                        <if test="depositRate != null">deposit_rate,
                        </if>
                        <if test="otherStoreRate != null">other_store_rate,
                        </if>
                        <if test="aliPayRate != null">ali_pay_rate,
                        </if>
                        <if test="wxPayRate != null">wx_pay_rate,
                        </if>
                </trim>
                <trim prefix="values (" suffix=")" suffixOverrides=",">
                        <if test="sourceType != null">#{sourceType},
                        </if>
                        <if test="orgId != null">#{orgId},
                        </if>
                        <if test="factoryRate != null">#{factoryRate},
                        </if>
                        <if test="materialRate != null">#{materialRate},
                        </if>
                        <if test="manageRate != null">#{manageRate},
                        </if>
                        <if test="depositRate != null">#{depositRate},
                        </if>
                        <if test="otherStoreRate != null">#{otherStoreRate},
                        </if>
                        <if test="aliPayRate != null">#{aliPayRate},
                        </if>
                        <if test="wxPayRate != null">#{wxPayRate},
                        </if>
                </trim>
        </insert>
        <insert id="insertAllRateFactory" parameterType="SysSettlementRate">
                insert into sys_settlement_rate (source_type, org_id, material_rate, manage_rate, deposit_rate)
                select
                    "01" as source_type,
                    f.id as org_id,
                    #{materialRate} as material_rate,
                    #{manageRate} as manage_rate,
                    #{depositRate} as deposit_rate
                from (
                       select id from manage_factory where del_flag = '0'
                     ) as f
        </insert>
        <insert id="insertAllRateStore" parameterType="SysSettlementRate">
                insert into sys_settlement_rate (source_type, org_id, factory_rate, material_rate, manage_rate, deposit_rate, other_store_rate, ali_pay_rate, wx_pay_rate)
                select
                        "02" as source_type,
                        f.id as org_id,
                        #{factoryRate} as factory_rate,
                        #{materialRate} as material_rate,
                        #{manageRate} as manage_rate,
                        #{depositRate} as deposit_rate,
                        #{otherStoreRate} as other_store_rate,
                        #{aliPayRate} as ali_pay_rate,
                        #{wxPayRate} as wx_pay_rate
                from (
                             select id from sys_store where del_flag = '0'
                     ) as f
        </insert>

        <update id="updateSysSettlementRate" parameterType="SysSettlementRate">
                update sys_settlement_rate
                <trim prefix="SET" suffixOverrides=",">
                        <if test="sourceType != null">source_type =
                                #{sourceType},
                        </if>
                        <if test="orgId != null">org_id =
                                #{orgId},
                        </if>
                        <if test="factoryRate != null">factory_rate =
                                #{factoryRate},
                        </if>
                        <if test="materialRate != null">material_rate =
                                #{materialRate},
                        </if>
                        <if test="manageRate != null">manage_rate =
                                #{manageRate},
                        </if>
                        <if test="depositRate != null">deposit_rate =
                                #{depositRate},
                        </if>
                        <if test="otherStoreRate != null">other_store_rate =
                                #{otherStoreRate},
                        </if>
                        <if test="aliPayRate != null">ali_pay_rate =
                                #{aliPayRate},
                        </if>
                        <if test="wxPayRate != null">wx_pay_rate =
                                #{wxPayRate},
                        </if>
                </trim>
                where id = #{id}
        </update>

        <delete id="deleteSysSettlementRateById" parameterType="Long">
                delete from sys_settlement_rate where id = #{id}
        </delete>

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



</mapper>