<?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.xxfc.platform.vehicle.mapper.BranchCompanyStockRightMapper">

    <update id="updateBalance">
        update branch_company_stock_info_right
        set balance = #{balance}
        where id = #{companyId} and balance = #{lastBalance}
    </update>

    <select id="search" resultType="com.xxfc.platform.vehicle.pojo.vo.BranchCompanyStockInfoRightVo">
        SELECT
        distinct r.company_id as companyId,
        r.id,
        r.balance,
        r.company_pic as companyPic,
        r.info,
        r.price,
        r.rank,
        r.total,
        r.stock_state,
        c.addr_city AS addrCity,
        c.addr_province AS addrProvince,
        c.`name` AS companyName
        FROM
        branch_company_stock_info_right r
        LEFT JOIN branch_company c ON r.company_id = c.id
        <trim prefix="where" suffixOverrides="and">
            c.is_del = 0 and
            <if test="companyName != null">
                c.name like CONCAT('%',#{companyName},'%')
            </if>
            <if test="addrProvince != null">
                c.addr_province = #{addrProvince} and
            </if>
            <if test="addrCity != null">
                c.addr_city = #{addrCity} and
            </if>
            <if test="priceStart">
                r.price >= #{priceStart} and
            </if>
            <if test="priceEnd">
                r.price &lt;= #{priceEnd} and
            </if>
        </trim>
        order by rank DESC,price DESC
    </select>
    <select id="selectAllPrice" resultType="integer">
        SELECT DISTINCT
	      `price`
         FROM
	  ( SELECT `price` / 10000 AS `price` FROM branch_company_stock_info_right WHERE is_del = 0 ) AS `bcsir` ORDER BY price DESC
    </select>
    <select id="selectInfoById" resultType="com.xxfc.platform.vehicle.pojo.vo.BranchCompanyStockInfoRightVo">
        SELECT
        distinct r.company_id as companyId,
        r.id,
        r.balance,
        r.company_pic as companyPic,
        r.info,
        r.price,
        r.rank,
        r.total,
        r.stock_state,
        c.addr_city AS addrCity,
        c.addr_province AS addrProvince,
        c.`name` AS companyName
        FROM
        branch_company_stock_info_right r
        LEFT JOIN branch_company c ON r.company_id = c.id
        where  c.is_del = 0 and r.id=#{id} and r.is_del=0
    </select>

    <select id="getCompanyInfo" resultType="Integer">
        SELECT id FROM branch_company WHERE `name`=#{name} and is_del=0 limit 1
    </select>

    <insert id="addCompamyList" parameterType="java.util.List">
        insert into branch_company_stock_info_right (
        company_id,balance,total,price,stock_state,company_pic,crt_time,upd_time
        ) VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.companyId},#{item.balance},#{item.total},#{item.price},#{item.stockState}
            ,#{item.companyPic},#{item.crtTime},#{item.updTime})
        </foreach>
    </insert>
    <select id="findWithPageByPriceOrCode"
            resultType="com.xxfc.platform.vehicle.pojo.dto.BranchCompanyStockRightDTO">
        SELECT
        bcsir.id AS `id`,
        cb.id AS `companyBaseId`,
        bcsir.balance,
        bcsir.total,
        bcsir.type,
        bcsir.price/10000 as `price`,
        cb.cover,
        cb.name as `companyName`
        FROM
        ( SELECT id, company_base_id, balance, total, price, type FROM `branch_company_stock_info_right` WHERE
        `stock_state` =
        2 AND `is_del`=0
        <if test="price != null">
            AND `price`=#{price}*10000
        </if>
        ) AS `bcsir`
        INNER JOIN ( SELECT id, `name`, cover FROM `company_base` WHERE 1=1
        <if test="provinceCode != null">
            AND `addr_province`=#{provinceCode}
        </if>
        <if test="cityCode != null">
            AND `addr_city`=#{cityCode}
        </if>
        ) AS `cb` ON cb.id = bcsir.company_base_id order by bcsir.price desc
    </select>
</mapper>