<?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.supplier.mapper.PersonalMapper">
    <!-- 查询个人信息 -->
    <select id="getPresonalMessage" parameterType="String" resultType="Supplier">
        select * from sys_supplier where code = #{code}
    </select>
    <!-- 修改手机号 -->
    <update id="updatePhone" parameterType="Supplier">
        update sys_supplier
        <set>
            <trim suffixOverrides=",">
                <if test="phone != null and phone != ''">
                    phone = #{phone},
                </if>
                <if test="name != null and name != ''">
                    name = #{name},
                </if>
                <if test="wxOpenid != null and wxOpenid != ''">
                    wx_openid = #{wxOpenid},
                </if>
                <if test="wxRefreshToken != null and wxRefreshToken != ''">
                    wx_refresh_token = #{wxRefreshToken},
                </if>
                <if test="wxid != null and wxid != ''">
                    wxid = #{wxid},
                </if>
                <if test="wxtoken != null and wxtoken != ''">
                    wxtoken = #{wxtoken},
                </if>
                <if test="headpir != null and headpir != ''">
                    headpir = #{headpir},
                </if>
            </trim>
        </set>
        where code = #{code}
    </update>
    <!-- 手机号登录验证是否第一次登录 -->
    <select id="checkUser" resultType="Supplier">
        select id,code,nature_supplier as natureSupplier,contact,phone,language,certificate_type as certificateType,certificate,headpir,jdrq,flag,captcha,invoice,name
        from sys_supplier
        where phone=#{phone} and captcha=#{captcha}
    </select>
    <!-- 校验验证码 -->
    <select id="checkCaptcha" resultType="int">
        select count(*) from sys_supplier where code=#{code} and captcha=#{captcha}
    </select>
    <!-- 查询是否存在此微信用户 -->
    <select id="seleceWxUserCount" resultType="User">
        select a.*,
               b.fans,
               c.focus
        from
            sys_users a
            LEFT JOIN (select count(*) as fans,be_code from bus_focus_on GROUP BY be_code) b on a.code = b.be_code
            LEFT JOIN (select count(*) as focus,code from bus_focus_on GROUP BY code) c on a.code = c.code
        where wxid = #{wxid}
    </select>
    <!-- 新增微信用户 -->
    <insert id="addWXUser" parameterType="User">
        insert into sys_users(name,sex,jdrq,headpir,wxid,wxtoken,flag,city,wx_refresh_token,wx_openid)
        values(#{name},#{sex},SYSDATETIME(),#{headpir},#{wxid},#{wxtoken},0,#{city},#{wxRefreshToken},#{wxOpenid})
    </insert>
    <!-- 查询指定微信id的用户的id -->
    <select id="geThisUserID" resultType="int">
        select id from sys_users where wxid = #{wxid}
    </select>
    <!-- 添加指定用户的code -->
    <update id="updateUserWx" parameterType="User">
        update sys_users
        <set>
            <trim suffixOverrides=",">
                <if test="code != null and code !=''">
                    code = #{code},
                </if>
                <if test="name != null and name !=''">
                    name = #{name},
                </if>
                <if test="city != null and city !=''">
                    city = #{city},
                </if>
                <if test="wxOpenid != null and wxOpenid !=''">
                    wx_openid = #{wxOpenid},
                </if>
                <if test="sex != null and sex !=''">
                    sex = #{sex},
                </if>
                <if test="headpir != null and headpir !=''">
                    headpir = #{headpir},
                </if>
                <if test="wxtoken != null and wxtoken !=''">
                    wxtoken = #{wxtoken},
                </if>
                <if test="wxRefreshToken != null and wxRefreshToken !=''">
                    wx_refresh_token = #{wxRefreshToken},
                </if>
            </trim>
        </set>
        where wxid = #{wxid}
    </update>
    <!-- 供应商报价单次数减一 -->
    <update id="updateOffNum">
        update sys_supplier set offer_num = (select offer_num - 1 from sys_supplier where code = #{code}) where code = #{code}
    </update>
    <!-- 查询报价单单价 -->
    <select id="getByNumber" resultType="Map">
        select * from tuya_sites_info where site_id = 1
    </select>
    <!-- 修改供应商金额 -->
    <update id="minusBalanceOf">
        update sys_supplier set balance_of = (select balance_of - ${offerVal} from sys_supplier where code = #{code}) where code = #{code}
    </update>
    <!-- 增加购买的报价单次数 -->
    <update id="addOfferNum">
        update sys_supplier set offer_num = (select offer_num + ${offerNum} from sys_supplier where code = #{code}) where code = #{code}
    </update>
    <!-- 添加购买次数记录 -->
    <insert id="addBuyOfferNum" parameterType="BuyOfferNum">
        insert into bus_buy_offer_num(uuid,jdrq,pay_amount,offer_num,code)
        values(#{uuid},CONVERT(VARCHAR(100), SYSDATETIME(), 20),#{payAmount},#{offerNum},#{code})
    </insert>
    <!-- 查询收支明细 -->
    <select id="balanceOfPayments" resultType="Map" parameterType="String">
        SELECT
            a.uuid,
            a.release_uuid,
            a.total_fee,
            d.refund_fee,
            b.offer_name,
            CONVERT(date, a.jdrq) as jdrq
        FROM
            bus_pay a
            JOIN ( SELECT uuid,offer_name,demand_uuid FROM bus_supplier_release WHERE code = #{code} GROUP BY uuid, code, offer_name,demand_uuid) b ON a.release_uuid = b.uuid
            JOIN ( SELECT demand_uuid,order_type from bus_private_ordering_type where order_type = '5' GROUP BY demand_uuid,order_type) c on b.demand_uuid = c.demand_uuid
            LEFT JOIN (SELECT release_uuid,refund_fee FROM bus_refund GROUP BY release_uuid,refund_fee) d on a.release_uuid = d.release_uuid
        order by a.jdrq desc
    </select>
    <!-- 查询充值次数 -->
    <select id="topUpNum" resultType="Map" parameterType="String">
        SELECT
            a.balance_of,a.code,b.topUp,c.offerNum
        FROM
            sys_supplier a
            LEFT JOIN (select count(*) as topUp,code from bus_top_up_record GROUP BY code) b on a.code = b.code
            LEFT JOIN ( SELECT COUNT ( * ) AS offerNum, code FROM bus_buy_offer_num GROUP BY code ) c ON a.code = c.code
        WHERE
            a.code = #{code}
    </select>
</mapper>