<?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.yunniu.farming.webadmin.dao.AccountalterrecDao">
    <resultMap id="BaseResultMap" type="com.yunniu.farming.webadmin.model.Accountalterrec">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="customerid" property="customerid" jdbcType="INTEGER"/>
        <result column="orderid" property="orderid" jdbcType="INTEGER"/>
        <result column="productid" property="productid" jdbcType="INTEGER"/>
        <result column="itype" property="itype" jdbcType="INTEGER"/>
        <result column="idetailtype" property="idetailtype" jdbcType="INTEGER"/>
        <result column="sdesc" property="sdesc" jdbcType="VARCHAR"/>
        <result column="soperation" property="soperation" jdbcType="VARCHAR"/>
        <result column="drmbin" property="drmbin" jdbcType="DECIMAL"/>
        <result column="drmbout" property="drmbout" jdbcType="DECIMAL"/>
        <result column="dcoinin" property="dcoinin" jdbcType="DECIMAL"/>
        <result column="dcoinout" property="dcoinout" jdbcType="DECIMAL"/>
        <result column="drate" property="drate" jdbcType="DECIMAL"/>
        <result column="damount" property="damount" jdbcType="DECIMAL"/>
        <result column="iarrivalway" property="iarrivalway" jdbcType="INTEGER"/>
        <result column="icashtype" property="icashtype" jdbcType="INTEGER"/>
        <result column="swxopenid" property="swxopenid" jdbcType="VARCHAR"/>
        <result column="swxnickname" property="swxnickname" jdbcType="VARCHAR"/>
        <result column="srealname" property="srealname" jdbcType="VARCHAR"/>
        <result column="salipayaccount" property="salipayaccount" jdbcType="VARCHAR"/>
        <result column="sbanktype" property="sbanktype" jdbcType="VARCHAR"/>
        <result column="sbankcardno" property="sbankcardno" jdbcType="VARCHAR"/>
        <result column="istatus" property="istatus" jdbcType="INTEGER"/>
        <result column="sreason" property="sreason" jdbcType="VARCHAR"/>
        <result column="taudittime" property="taudittime" jdbcType="TIMESTAMP"/>
        <result column="tcreatetime" property="tcreatetime" jdbcType="TIMESTAMP"/>
        <result column="sremark" property="sremark" jdbcType="VARCHAR"/>
        <result column="sdef1" property="sdef1" jdbcType="VARCHAR"/>
        <result column="sdef2" property="sdef2" jdbcType="VARCHAR"/>
        <result column="sdef3" property="sdef3" jdbcType="VARCHAR"/>
        <result column="sdef4" property="sdef4" jdbcType="VARCHAR"/>
        <result column="sdef5" property="sdef5" jdbcType="VARCHAR"/>
        <result column="sdef6" property="sdef6" jdbcType="VARCHAR"/>
        <result column="sdef7" property="sdef7" jdbcType="VARCHAR"/>
        <result column="sdef8" property="sdef8" jdbcType="VARCHAR"/>
        <result column="sdef9" property="sdef9" jdbcType="VARCHAR"/>
        <result column="sdef10" property="sdef10" jdbcType="VARCHAR"/>
        <result column="idef11" property="idef11" jdbcType="INTEGER"/>
        <result column="idef12" property="idef12" jdbcType="INTEGER"/>
        <result column="ddef13" property="ddef13" jdbcType="DECIMAL"/>
        <result column="ddef14" property="ddef14" jdbcType="DECIMAL"/>
        <result column="ddef15" property="ddef15" jdbcType="TIMESTAMP"/>

        <association property="customer" column="customerid" select="selectCustomer"/>
    </resultMap>


    <select id="selectCustomer" parameterType="Integer" resultType="com.yunniu.farming.webadmin.model.Customer">
      SELECT *
      FROM customer WHERE id = #{customerid}
    </select>


    <sql id="Base_Column_List">
        accountalterrec.id,  accountalterrec.customerid,  accountalterrec.orderid,
        accountalterrec.productid, accountalterrec.itype, accountalterrec.idetailtype, accountalterrec.sdesc,
        accountalterrec.soperation, accountalterrec.drmbin, accountalterrec.drmbout,
        accountalterrec.dcoinin, accountalterrec.dcoinout,
        accountalterrec.drate, accountalterrec.damount, accountalterrec.iarrivalway,accountalterrec.icashtype,
        accountalterrec.swxopenid, accountalterrec.swxnickname, accountalterrec.srealname, accountalterrec.salipayaccount,
        accountalterrec.sbanktype, accountalterrec.sbankcardno, accountalterrec.istatus, accountalterrec.sreason,
        accountalterrec.taudittime,accountalterrec.tcreatetime, accountalterrec.sremark, accountalterrec.sdef1, accountalterrec.sdef2, accountalterrec.sdef3,
        accountalterrec.sdef4, accountalterrec.sdef5, accountalterrec.sdef6, accountalterrec.sdef7, accountalterrec.sdef8,
        accountalterrec.sdef9, accountalterrec.sdef10, accountalterrec.idef11, accountalterrec.idef12, accountalterrec.ddef13,
        accountalterrec.ddef14, accountalterrec.ddef15
    </sql>

    <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer">
        select 'false' as QUERYID,
        <include refid="Base_Column_List"/>
        from accountalterrec
        where id = #{id,jdbcType=INTEGER}
    </select>

    <!-- 带参普通查询 -->
    <select id="selectByParam" resultMap="BaseResultMap" parameterType="com.yunniu.farming.webadmin.model.Accountalterrec">
        select

        <include refid="Base_Column_List"/>

        from accountalterrec

        <where>
            <if test="id != null">
                AND accountalterrec.id = #{id,jdbcType=INTEGER}
            </if>
            <if test="orderid != null">
                AND accountalterrec.orderid = #{orderid,jdbcType=INTEGER}
            </if>

            <if test="customerid != null">
                AND accountalterrec.customerid = #{customerid,jdbcType=INTEGER}
            </if>
            <if test="itype != null">
                AND accountalterrec.itype = #{itype,jdbcType=INTEGER}
            </if>
            <if test="productid != null">
                AND accountalterrec.productid = #{productid,jdbcType=INTEGER}
            </if>
            <if test="idetailtype != null">
                AND accountalterrec.idetailtype = #{idetailtype,jdbcType=INTEGER}
            </if>
            <if test="istatus != null">
                AND accountalterrec.istatus = #{istatus,jdbcType=INTEGER}
            </if>
            <if test="sdesc != null">
                AND accountalterrec.sdesc like '%${sdesc}%'
            </if>
            <if test="beginDate != null">
                AND accountalterrec.tcreatetime <![CDATA[>=]]> #{beginDate}
            </if>
            <if test="endDate != null">
                AND accountalterrec.tcreatetime <![CDATA[<=]]> #{endDate}
            </if>

        </where>
    </select>

    <!-- 带参分页查询 -->
    <select id="selectByParamPageList" resultMap="BaseResultMap"
            parameterType="com.yunniu.farming.webadmin.model.Accountalterrec">
        SELECT 'false' as QUERYID,

        <include refid="Base_Column_List"/>

        FROM accountalterrec

        <!-- 关联查询用户信息 -->
        LEFT JOIN customer as cus ON accountalterrec.customerid = cus.id

        <where>
            <if test="id != null">
                AND accountalterrec.id = #{id,jdbcType=INTEGER}
            </if>
            <if test="orderid != null">
                AND accountalterrec.orderid = #{orderid,jdbcType=INTEGER}
            </if>

            <if test="customerid != null">
                AND accountalterrec.customerid = #{customerid,jdbcType=INTEGER}
            </if>

            <if test="itype != null">
                AND accountalterrec.itype = #{itype,jdbcType=INTEGER}
            </if>
            <if test="idetailtype != null">
                AND accountalterrec.idetailtype = #{idetailtype,jdbcType=INTEGER}
            </if>

            <if test="itypecash != null">
                AND accountalterrec.idetailtype in (21,41)
            </if>

            <if test="nickname != null">
                AND cus.swxnick like '%${nickname}%'
            </if>

            <if test="istatus != null">
                AND accountalterrec.istatus = #{istatus,jdbcType=INTEGER}
            </if>
            <if test="sdesc != null">
                AND accountalterrec.sdesc like '%${sdesc}%'
            </if>

            <if test="method == 'shouyi'">
                AND accountalterrec.idetailtype in (11,12)
            </if>

            <if test="method == 'team'">
                AND accountalterrec.idetailtype in (31)
            </if>

            <if test="method == 'manage'">
                AND accountalterrec.idetailtype in (32,33)
            </if>

            <if test="method == 'cash'">
                AND accountalterrec.idetailtype in (41,21)
            </if>

            <if test="beginDate != null">
                AND DATE_FORMAT(accountalterrec.tcreatetime,'%Y-%m-%d') <![CDATA[>=]]> #{beginDate}
            </if>
            <if test="endDate != null">
                AND DATE_FORMAT(accountalterrec.tcreatetime,'%Y-%m-%d') <![CDATA[<=]]> #{endDate}
            </if>
        </where>

        ORDER BY accountalterrec.tcreatetime DESC

    </select>

    <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
        delete from accountalterrec
        where id = #{id,jdbcType=INTEGER}
    </delete>


    <insert id="insertSelective" parameterType="com.yunniu.farming.webadmin.model.Accountalterrec" useGeneratedKeys="true"
            keyProperty="id">
        insert into accountalterrec
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="customerid != null">
                customerid,
            </if>
            <if test="orderid != null">
                orderid,
            </if>
            <if test="productid != null">
                productid,
            </if>
            <if test="itype != null">
                itype,
            </if>
            <if test="idetailtype != null">
                idetailtype,
            </if>
            <if test="sdesc != null">
                sdesc,
            </if>
            <if test="soperation != null">
                soperation,
            </if>
            <if test="drmbin != null">
                drmbin,
            </if>
            <if test="drmbout != null">
                drmbout,
            </if>
            <if test="dcoinin != null">
                dcoinin,
            </if>
            <if test="dcoinout != null">
                dcoinout,
            </if>

            <if test="drate != null">
                drate,
            </if>
            <if test="damount != null">
                damount,
            </if>
            <if test="iarrivalway != null">
                iarrivalway,
            </if>
            <if test="icashtype != null">
                icashtype,
            </if>
            <if test="swxopenid != null">
                swxopenid,
            </if>
            <if test="swxnickname != null">
                swxnickname,
            </if>
            <if test="srealname != null">
                srealname,
            </if>
            <if test="salipayaccount != null">
                salipayaccount,
            </if>
            <if test="sbanktype != null">
                sbanktype,
            </if>
            <if test="sbankcardno != null">
                sbankcardno,
            </if>
            <if test="istatus != null">
                istatus,
            </if>
            <if test="sreason != null">
                sreason,
            </if>
            <if test="taudittime != null">
                taudittime,
            </if>
            <if test="tcreatetime != null">
                tcreatetime,
            </if>
            <if test="sremark != null">
                sremark,
            </if>
            <if test="sdef1 != null">
                sdef1,
            </if>
            <if test="sdef2 != null">
                sdef2,
            </if>
            <if test="sdef3 != null">
                sdef3,
            </if>
            <if test="sdef4 != null">
                sdef4,
            </if>
            <if test="sdef5 != null">
                sdef5,
            </if>
            <if test="sdef6 != null">
                sdef6,
            </if>
            <if test="sdef7 != null">
                sdef7,
            </if>
            <if test="sdef8 != null">
                sdef8,
            </if>
            <if test="sdef9 != null">
                sdef9,
            </if>
            <if test="sdef10 != null">
                sdef10,
            </if>
            <if test="idef11 != null">
                idef11,
            </if>
            <if test="idef12 != null">
                idef12,
            </if>
            <if test="ddef13 != null">
                ddef13,
            </if>
            <if test="ddef14 != null">
                ddef14,
            </if>
            <if test="ddef15 != null">
                ddef15,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id,jdbcType=INTEGER},
            </if>
            <if test="customerid != null">
                #{customerid,jdbcType=INTEGER},
            </if>
            <if test="orderid != null">
                #{orderid,jdbcType=INTEGER},
            </if>
            <if test="productid != null">
                #{productid,jdbcType=INTEGER},
            </if>
            <if test="itype != null">
                #{itype,jdbcType=INTEGER},
            </if>
            <if test="idetailtype != null">
                #{idetailtype,jdbcType=INTEGER},
            </if>
            <if test="sdesc != null">
                #{sdesc,jdbcType=VARCHAR},
            </if>
            <if test="soperation != null">
                #{soperation,jdbcType=VARCHAR},
            </if>
            <if test="drmbin != null">
                #{drmbin,jdbcType=DECIMAL},
            </if>
            <if test="drmbout != null">
                #{drmbout,jdbcType=DECIMAL},
            </if>

            <if test="dcoinin != null">
                #{dcoinin,jdbcType=DECIMAL},
            </if>
            <if test="dcoinout != null">
                #{dcoinout,jdbcType=DECIMAL},
            </if>

            <if test="drate != null">
                #{drate,jdbcType=DECIMAL},
            </if>
            <if test="damount != null">
                #{damount,jdbcType=DECIMAL},
            </if>
            <if test="iarrivalway != null">
                #{iarrivalway,jdbcType=INTEGER},
            </if>
            <if test="icashtype != null">
                #{icashtype,jdbcType=INTEGER},
            </if>
            <if test="swxopenid != null">
                #{swxopenid,jdbcType=VARCHAR},
            </if>
            <if test="swxnickname != null">
                #{swxnickname,jdbcType=VARCHAR},
            </if>
            <if test="srealname != null">
                #{srealname,jdbcType=VARCHAR},
            </if>
            <if test="salipayaccount != null">
                #{salipayaccount,jdbcType=VARCHAR},
            </if>
            <if test="sbanktype != null">
                #{sbanktype,jdbcType=VARCHAR},
            </if>
            <if test="sbankcardno != null">
                #{sbankcardno,jdbcType=VARCHAR},
            </if>
            <if test="istatus != null">
                #{istatus,jdbcType=INTEGER},
            </if>
            <if test="sreason != null">
                #{sreason,jdbcType=VARCHAR},
            </if>
            <if test="taudittime != null">
                #{taudittime,jdbcType=TIMESTAMP},
            </if>
            <if test="tcreatetime != null">
                #{tcreatetime,jdbcType=TIMESTAMP},
            </if>
            <if test="sremark != null">
                #{sremark,jdbcType=VARCHAR},
            </if>
            <if test="sdef1 != null">
                #{sdef1,jdbcType=VARCHAR},
            </if>
            <if test="sdef2 != null">
                #{sdef2,jdbcType=VARCHAR},
            </if>
            <if test="sdef3 != null">
                #{sdef3,jdbcType=VARCHAR},
            </if>
            <if test="sdef4 != null">
                #{sdef4,jdbcType=VARCHAR},
            </if>
            <if test="sdef5 != null">
                #{sdef5,jdbcType=VARCHAR},
            </if>
            <if test="sdef6 != null">
                #{sdef6,jdbcType=VARCHAR},
            </if>
            <if test="sdef7 != null">
                #{sdef7,jdbcType=VARCHAR},
            </if>
            <if test="sdef8 != null">
                #{sdef8,jdbcType=VARCHAR},
            </if>
            <if test="sdef9 != null">
                #{sdef9,jdbcType=VARCHAR},
            </if>
            <if test="sdef10 != null">
                #{sdef10,jdbcType=VARCHAR},
            </if>
            <if test="idef11 != null">
                #{idef11,jdbcType=INTEGER},
            </if>
            <if test="idef12 != null">
                #{idef12,jdbcType=INTEGER},
            </if>
            <if test="ddef13 != null">
                #{ddef13,jdbcType=DECIMAL},
            </if>
            <if test="ddef14 != null">
                #{ddef14,jdbcType=DECIMAL},
            </if>
            <if test="ddef15 != null">
                #{ddef15,jdbcType=TIMESTAMP},
            </if>
        </trim>
    </insert>


    <update id="updateByPrimaryKeySelective" parameterType="com.yunniu.farming.webadmin.model.Accountalterrec">
        update accountalterrec
        <set>


            <if test="customerid != null">
                customerid = #{customerid,jdbcType=INTEGER},
            </if>


            <if test="orderid != null">
                orderid = #{orderid,jdbcType=INTEGER},
            </if>
            <if test="productid != null">
                productid = #{productid,jdbcType=INTEGER},
            </if>
            <if test="itype != null">
                itype = #{itype,jdbcType=INTEGER},
            </if>
            <if test="idetailtype != null">
                idetailtype = #{idetailtype,jdbcType=INTEGER},
            </if>
            <if test="sdesc != null">
                sdesc = #{sdesc,jdbcType=VARCHAR},
            </if>
            <if test="soperation != null">
                soperation = #{soperation,jdbcType=VARCHAR},
            </if>
            <if test="drmbin != null">
                drmbin = #{drmbin,jdbcType=DECIMAL},
            </if>
            <if test="drmbout != null">
                drmbout = #{drmbout,jdbcType=DECIMAL},
            </if>

            <if test="dcoinin != null">
                dcoinin = #{dcoinin,jdbcType=DECIMAL},
            </if>
            <if test="dcoinout != null">
                dcoinout = #{dcoinout,jdbcType=DECIMAL},
            </if>

            <if test="drate != null">
                drate = #{drate,jdbcType=DECIMAL},
            </if>
            <if test="damount != null">
                damount = #{damount,jdbcType=DECIMAL},
            </if>
            <if test="iarrivalway != null">
                iarrivalway = #{iarrivalway,jdbcType=INTEGER},
            </if>
            <if test="icashtype != null">
                icashtype = #{icashtype,jdbcType=INTEGER},
            </if>
            <if test="icashtype != null">
                icashtype = #{icashtype,jdbcType=INTEGER},
            </if>
            <if test="swxopenid != null">
                swxopenid = #{swxopenid,jdbcType=VARCHAR},
            </if>
            <if test="swxnickname != null">
                swxnickname = #{swxnickname,jdbcType=VARCHAR},
            </if>
            <if test="srealname != null">
                srealname = #{srealname,jdbcType=VARCHAR},
            </if>
            <if test="salipayaccount != null">
                salipayaccount = #{salipayaccount,jdbcType=VARCHAR},
            </if>
            <if test="sbanktype != null">
                sbanktype = #{sbanktype,jdbcType=VARCHAR},
            </if>
            <if test="sbankcardno != null">
                sbankcardno = #{sbankcardno,jdbcType=VARCHAR},
            </if>
            <if test="istatus != null">
                istatus = #{istatus,jdbcType=INTEGER},
            </if>
            <if test="sreason != null">
                sreason = #{sreason,jdbcType=VARCHAR},
            </if>
            <if test="taudittime != null">
                taudittime = #{taudittime,jdbcType=TIMESTAMP},
            </if>
            <if test="tcreatetime != null">
                tcreatetime = #{tcreatetime,jdbcType=TIMESTAMP},
            </if>
            <if test="sremark != null">
                sremark = #{sremark,jdbcType=VARCHAR},
            </if>
            <if test="sdef1 != null">
                sdef1 = #{sdef1,jdbcType=VARCHAR},
            </if>
            <if test="sdef2 != null">
                sdef2 = #{sdef2,jdbcType=VARCHAR},
            </if>
            <if test="sdef3 != null">
                sdef3 = #{sdef3,jdbcType=VARCHAR},
            </if>
            <if test="sdef4 != null">
                sdef4 = #{sdef4,jdbcType=VARCHAR},
            </if>
            <if test="sdef5 != null">
                sdef5 = #{sdef5,jdbcType=VARCHAR},
            </if>
            <if test="sdef6 != null">
                sdef6 = #{sdef6,jdbcType=VARCHAR},
            </if>
            <if test="sdef7 != null">
                sdef7 = #{sdef7,jdbcType=VARCHAR},
            </if>
            <if test="sdef8 != null">
                sdef8 = #{sdef8,jdbcType=VARCHAR},
            </if>
            <if test="sdef9 != null">
                sdef9 = #{sdef9,jdbcType=VARCHAR},
            </if>
            <if test="sdef10 != null">
                sdef10 = #{sdef10,jdbcType=VARCHAR},
            </if>
            <if test="idef11 != null">
                idef11 = #{idef11,jdbcType=INTEGER},
            </if>
            <if test="idef12 != null">
                idef12 = #{idef12,jdbcType=INTEGER},
            </if>
            <if test="ddef13 != null">
                ddef13 = #{ddef13,jdbcType=DECIMAL},
            </if>
            <if test="ddef14 != null">
                ddef14 = #{ddef14,jdbcType=DECIMAL},
            </if>
            <if test="ddef15 != null">
                ddef15 = #{ddef15,jdbcType=TIMESTAMP},
            </if>
        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>





    <resultMap id="BalanceResultMap" type="com.yunniu.farming.webadmin.model.Accountalterrec">
        <result column="yjbalance" property="yjbalance" jdbcType="DECIMAL"/>
        <result column="jtbalance" property="jtbalance" jdbcType="DECIMAL"/>
    </resultMap>

    <!-- 查询余额  -->
    <select id="selectBalanceByid" resultMap="BalanceResultMap"
            parameterType="com.yunniu.farming.webadmin.model.Accountalterrec">
        SELECT

        <!-- 业绩余额 = 业绩收入汇总 - 业绩支出汇总 -->
        ( SUM( IFNULL( arc.drmbin, 0) ) - SUM( IFNULL( arc.drmbout, 0) ) ) AS yjbalance,
        <!-- 津贴余额 = 津贴收入汇总 - 津贴支出汇总 -->
        ( SUM( IFNULL( arc.dcoinin, 0) ) - SUM( IFNULL( arc.dcoinout, 0) ) ) AS jtbalance

        FROM accountalterrec arc

        <where>
            <if test="customerid != null">
                arc.customerid = #{customerid,jdbcType=INTEGER}
            </if>

        </where>

        GROUP BY

        <if test="customerid != null">
            arc.customerid
        </if>

    </select>




    <!-- 统计个人收益 -->
    <select id="countShouYi" resultType="double" parameterType="com.yunniu.farming.webadmin.model.Accountalterrec">
        select SUM( IFNULL( drmbin, 0) )
        from accountalterrec
        where
        idetailtype in (11,12)
        and customerid = #{customerid}
    </select>

    <!-- 统计团队津贴 -->
    <select id="countTeamJt" resultType="double" parameterType="com.yunniu.farming.webadmin.model.Accountalterrec">
        select SUM( IFNULL( dcoinin, 0) )
        from accountalterrec
        where
        idetailtype in (31)
        and customerid = #{customerid}
    </select>


    <!-- 统计管理津贴 -->
    <select id="countManageJt" resultType="double" parameterType="com.yunniu.farming.webadmin.model.Accountalterrec">
        select  SUM( IFNULL( dcoinin, 0) )
        from accountalterrec
        where
        idetailtype in (32,33)
        and customerid = #{customerid}
    </select>

</mapper>