<?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.redBook.mapper.OARedBookMapper">
    <select id="productModelList" parameterType="Product" resultType="Product">
        select uid_0,name_0,isnull(example_0,'') example_0
        from rb_productType
        where  web_show_0=1
        <if test="uid_0 != null and uid_0 != ''">
            and puid_0 = #{uid_0}
        </if>
        <if test="uid_0 == null or uid_0 == ''">
            and puid_0 = 0
        </if>
        order by order_0
    </select>

    <select id="productRemarkList" parameterType="Product" resultType="Product">
        select type_uid_0,remark_0 from [rb_productRemark] where type_uid_0 = #{uid_0}
    </select>

    <select id="productExtList" parameterType="Product" resultType="Product">
        select model_ext_0 as name_0 from  rb_productType A
        inner join [rb_material_redbook_ext] B on A.name_0=B.model_0
        where A.uid_0 = #{uid_0} and B.show_0=1
    </select>

    <select id="productYsxhList" parameterType="Product" resultType="Product">
       select #{uid_0} uid_0,name_0 from (
         select name_0,1 order_0 from rb_productType where uid_0 = #{uid_0}
		 union all
         select model_ext_0 as name_0,B.order_0
		 from  rb_productType A
		 inner join [rb_material_redbook_ext] B on A.name_0=B.model_0
         where A.uid_0 = #{uid_0} and B.show_0=1 and isnull(B.type_uid_0,0)=0
		 union all
         select model_ext_0 as name_0,B.order_0
         from  rb_productType A
         inner join [rb_material_redbook_ext] B on A.uid_0=B.type_uid_0
         where A.uid_0 = #{uid_0}  and B.show_0=1
       ) A order by order_0
    </select>

    <select id="judgesection" parameterType="Product" resultType="Product">
        select A.uid_0,A.name_0,cast(cast(B.截面  as float) as nvarchar(20)) 截面
        from rb_productType A
        left join p_section B on A.uid_0=B.type_uid_0
        where A.uid_0 = #{uid_0}  and 截面 is not null order by A.name_0,B.截面
    </select>

    <select id="productJmList" parameterType="Product" resultType="Product">
        select A.uid_0,A.name_0,cast(cast(B.截面  as float) as nvarchar(20)) section
        from rb_productType A
        left join p_section B on A.uid_0=B.type_uid_0
		where A.uid_0 = #{uid_0} and B.model_0 = #{name_0}  and 截面 is not null order by A.name_0,B.截面
    </select>

    <select id="searchData" parameterType="Product" resultType="Product">
        select B.uid_0,A.namevoltage name_0,isnull(A.电压等级,N' - ') voltage ,A.单位 stu,A.型号 model,isnull(A.规格,'') spec ,
		convert(decimal,convert(float,A.红本价格)) price,convert(varchar(10),B.date_0,23) pricedate
		from [rb_product_price] A
		left join rb_productVersion B on A.version_uid_0=B.uid_0
		where B.sta_0=1 and A.type_uid_0 = #{uid_0}
        <if test="section != null and section != ''">
            and A.截面 = #{section}
        </if>
		    and A.型号 = #{name_0} order by A.order_0
    </select>

    <select id="handleSearchData" resultType="Product" parameterType="String">
        select B.uid_0,A.namevoltage name_0,isnull(A.电压等级,N' - ') voltage ,A.单位 stu,A.型号 model,isnull(A.规格,'') spec ,
        convert(decimal,convert(float,A.红本价格)) price,convert(varchar(10),B.date_0,23) pricedate
        from [rb_product_price] A
        left join rb_productVersion B on A.version_uid_0=B.uid_0
        where B.sta_0=1
        <if test="name_0 != null and name_0 != ''">
            and A.型号 like '%${name_0}%'
        </if>
        <if test="model != null and model != ''">
            and A.规格 = #{model}
        </if>
        order by len(A.namevoltage)
    </select>


    <insert id="insertOAQuot" parameterType="OAQuot">
        insert into OAQuot
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="quot_id != null">quot_id,</if>
            <if test="quotCode != null and quotCode != ''">quotCode,</if>
            <if test="quotCustomer != null and quotCustomer != ''">quotCustomer,</if>
            <if test="quotProject != null and quotProject != ''">quotProject,</if>
            <if test="quotLxr != null and quotLxr != ''">quotLxr,</if>
            <if test="quotLxrdh != null and quotLxrdh != ''">quotLxrdh,</if>
            <if test="totalPrice != null and totalPrice != ''">totalPrice,</if>
            <if test="rbDateUid != null and rbDateUid != ''">rbDateUid,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="quot_id != null">#{quot_id},</if>
            <if test="quotCode != null and quotCode != ''">#{quotCode},</if>
            <if test="quotCustomer != null and quotCustomer != ''">#{quotCustomer},</if>
            <if test="quotProject != null and quotProject != ''">#{quotProject},</if>
            <if test="quotLxr != null and quotLxr != ''">#{quotLxr},</if>
            <if test="quotLxrdh != null and quotLxrdh != ''">#{quotLxrdh},</if>
            <if test="totalPrice != null and totalPrice != ''">#{totalPrice},</if>
            <if test="rbDateUid != null and rbDateUid != ''">#{rbDateUid},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>

    <update id="updateOAQuot" parameterType="OAQuot">
        update OAQuot
        <trim prefix="SET" suffixOverrides=",">
            <if test="quotCode != null">quotCode = #{quotCode},</if>
            <if test="quotCustomer != null and quotCustomer != ''">quotCustomer = #{quotCustomer},</if>
            <if test="quotProject != null and quotProject != ''">quotProject = #{quotProject},</if>
            <if test="quotLxr != null and quotLxr != ''">quotLxr = #{quotLxr},</if>
            <if test="quotLxrdh != null and quotLxrdh != ''">quotLxrdh = #{quotLxrdh},</if>
            <if test="totalPrice != null and totalPrice != ''">totalPrice = #{totalPrice},</if>
            <if test="rbDateUid != null and rbDateUid != ''">rbDateUid = #{rbDateUid},</if>
            <if test="quotApprovalStatus != null and quotApprovalStatus != ''">quotApprovalStatus = #{quotApprovalStatus},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where quot_id = #{quot_id}
    </update>

    <insert id="batchOAQuotProduct">
        insert into OAQuotProduct(uid_0,quot_product_id, name_0, name_1, spec, voltage,stu,price,setPrice,count,allPrice,per,per2,quot_id,number) values
        <foreach item="item" index="index" collection="list" separator=",">
            (#{item.uid_0}, #{item.quot_product_id}, #{item.name_0}, #{item.name_1}, #{item.spec}, #{item.voltage}, #{item.stu},cast(#{item.price,jdbcType=DECIMAL} as decimal(18,2)),cast(#{item.setPrice,jdbcType=DECIMAL} as decimal(18,2)), cast(#{item.count,jdbcType=DECIMAL} as decimal(18,3)), cast(#{item.allPrice,jdbcType=DECIMAL} as decimal(18,2)),cast(#{item.per,jdbcType=DECIMAL} as decimal(18,2)),cast(#{item.per2,jdbcType=DECIMAL} as decimal(18,2)), #{item.quot_id}, #{item.index})
        </foreach>
    </insert>

    <select id="versionList" resultType="RbVersionDateResult">
        select uid_0 value,
               convert(varchar(10),[date_0],23)+' 铜:'+convert(varchar(10),[tong_price_0])+' 铝:'+convert(varchar(10),[lv_price_0]) label
        from [rb_productVersion] A
        inner join (select  max(uid_0) muid from rb_productVersion
        where sta_0=1 or sta_0=0 group by date_0 )B on A.uid_0=B.muid
        order by sta_0 desc,uid_0 desc
    </select>

    <select id="queryRedBPriceByParams" resultType="OAQuotProduct">
        SELECT a.红本价格 AS setPrice, a.namevoltage AS name_0, a.规格 AS spec, a.电压等级 AS voltage
        FROM rb_product_price a
        JOIN (VALUES
        <foreach collection="params" item="param" open="(" separator="),(" close=")">
            #{param.uid_0}, #{param.name_0}, #{param.spec}, #{param.voltage}
        </foreach>
        ) AS v(version_uid_0,产品型号, 规格, 电压等级)
        ON a.version_uid_0 = v.version_uid_0
        AND a.namevoltage = v.产品型号
        AND a.规格 = v.规格
        AND a.电压等级 = v.电压等级
    </select>

    <select id="getFixDatePrice" resultType="String">
        select top 1 红本价格  from rb_product_price A
        inner join [rb_productVersion] B on A.version_uid_0=B.uid_0
        where A.[namevoltage] = #{name_0} and A.规格 = #{spec}
        and A.电压等级 = #{voltage} and B.uid_0 = #{uid_0}
        and (B.sta_0=1 or sta_0=0)
    </select>

    <select id="getFixDatePrice2" resultType="OAQuotProduct">
        select top 1 B.uid_0,A.namevoltage name_0,isnull(A.电压等级,N' - ') voltage ,A.单位 stu,
               convert(decimal,convert(float,A.红本价格)) price,convert(varchar(10),B.date_0,23) pricedate
        from [rb_product_price] A
        left join rb_productVersion B on A.version_uid_0=B.uid_0
        where A.型号 = #{name_1} and A.规格 = #{spec}  and UPPER(A.电压等级) = #{voltage}
        and B.uid_0 = #{uid_0}
        and (B.sta_0=1 or sta_0=0)
    </select>

    <select id="getFixDatePrice3" resultType="OAQuotProduct">
        select top 1 B.uid_0,A.namevoltage name_0,isnull(A.电压等级,N' - ') voltage,A.单位 stu,
               A.型号 name_1,isnull(A.规格,'') spec
               ,convert(decimal,convert(float,A.红本价格)) price,convert(varchar(10),B.date_0,23) pricedate
        from [rb_product_price] A
        left join rb_productVersion B on A.version_uid_0=B.uid_0
        where (B.sta_0=1 or sta_0=0) and A.namevoltage=#{name_0}
        and B.uid_0 = #{uid_0}
    </select>

    <sql id="quotsJoins">
        LEFT JOIN sys_user u on u.user_name=a.create_by
        LEFT JOIN sys_dept d on u.dept_id = d.dept_id
    </sql>
    <select id="listQuots" parameterType="OAQuot" resultType="OAQuot">
        select a.quot_id,a.quotCode,a.quotCustomer,a.quotProject,a.quotLxr,a.quotLxrdh,a.totalPrice,a.quotApprovalStatus,
               a.create_by,u.nick_name createName,a.create_time createTime,
               a.update_by,a.update_time updateTime
        from OAQuot a
        <include refid="quotsJoins"/>
        <where>
            <if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
                and datediff(d, a.create_time, #{params.beginTime}) <![CDATA[<=]]> 0
            </if>
            <if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
                and datediff(d, a.create_time, #{params.endTime}) <![CDATA[>=]]> 0
            </if>
            <if test="quotCustomer != null and quotCustomer != ''"><!-- 客户检索 -->
                and a.quotCustomer like '%${quotCustomer}%'
            </if>
            <if test="quotApprovalStatus != null and quotApprovalStatus != ''"><!-- 提交状态 -->
                and a.quotApprovalStatus = #{quotApprovalStatus}
            </if>
            <!-- 数据范围过滤 -->
            ${params.dataScope}
        </where>
        order by a.quotCode desc
    </select>

    <resultMap type="OAQuot" id="QuotResult">
        <result property="quot_id"    column="quot_id"    />
        <result property="quotCode"    column="quotCode"    />
        <result property="quotCustomer"    column="quotCustomer"    />
        <result property="quotProject"    column="quotProject"    />
        <result property="quotLxr"    column="quotLxr"    />
        <result property="quotLxrdh"    column="quotLxrdh"    />
        <result property="totalPrice"    column="totalPrice"    />
        <result property="rbDateUid"    column="rbDateUid"    />
        <result property="quotApprovalStatus"    column="quotApprovalStatus"    />
    </resultMap>

    <resultMap id="QuotQuotMaterialResult" type="OAQuot" extends="QuotResult">
        <collection property="selectedResultData" notNullColumn="quot_product_id" javaType="java.util.List" resultMap="QuotMaterialResult" />
    </resultMap>

    <resultMap type="OAQuotProduct" id="QuotMaterialResult">
        <result property="uid_0"    column="uid_0"    />
        <result property="quot_product_id"    column="quot_product_id"    />
        <result property="name_0"    column="name_0"    />
        <result property="name_1"    column="name_1"    />
        <result property="spec"    column="spec"    />
        <result property="voltage"    column="voltage"    />
        <result property="stu"    column="stu"    />
        <result property="per"    column="per"    />
        <result property="per2"    column="per2"    />
        <result property="price"    column="price"    />
        <result property="setPrice"    column="setPrice"    />
        <result property="count"    column="count"    />
        <result property="allPrice"    column="allPrice"    />
    </resultMap>

    <select id="selectQuotByQuotId" parameterType="String" resultMap="QuotQuotMaterialResult">
        select a.quot_id, a.quotCode, a.quotCustomer, a.quotProject, a.quotLxr,
        a.quotLxrdh, a.totalPrice,a.rbDateUid,a.quotApprovalStatus,

        b.uid_0,b.quot_product_id, b.name_0, b.name_1,
        b.spec, b.voltage, b.stu,
        isnull(b.per,1) per,isnull(b.per2,1) per2,b.price,b.setPrice,b.count,b.allPrice,b.quot_id
        from OAQuot a
        left join OAQuotProduct b on b.quot_id = a.quot_id
        where a.quot_id = #{quotId}  order by b.number
    </select>

    <delete id="deleteQuotsByQuotId" parameterType="java.lang.String">
        delete from OAQuot where quot_id = #{quotId}
    </delete>
    <delete id="deleteQuotProductsByCusId" parameterType="java.lang.String">
        delete from OAQuotProduct where quot_id = #{quotId}
    </delete>

    <select id="rb_price_version" resultType="String">
        select top 1 muid from
        (
        select max(uid_0) muid,date_0 from rb_productVersion
        where sta_0=1 or sta_0=0 group by date_0
        )a order by date_0 desc
    </select>

    <select id="getCode" resultType="String" statementType="CALLABLE">
        {call GetSerialNo(#{type,mode=IN,jdbcType=VARCHAR})}
    </select>

</mapper>