<?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.th.demo.mapping.finance.AccountMapper">
    <resultMap id="BaseResultMap" type="com.th.demo.model.finance.Account">
        <id column="id" property="id" jdbcType="VARCHAR"/>
        <result column="customer_name" property="customerName" jdbcType="VARCHAR"/>
        <result column="weight" property="weight" jdbcType="DOUBLE"/>
        <result column="unit_price" property="unitPrice" jdbcType="DOUBLE"/>
        <result column="price" property="price" jdbcType="DOUBLE"/>

        <result column="add_time" property="addTime" jdbcType="TIMESTAMP"/>
        <result column="modify_id" property="modifyId" jdbcType="VARCHAR"/>
        <result column="modify_time" property="modifyTime" jdbcType="TIMESTAMP"/>
        <result column="cancel_id" property="cancelId" jdbcType="VARCHAR"/>
        <result column="cancel_time" property="cancelTime" jdbcType="TIMESTAMP"/>
        <result column="cancel_flag" property="cancelFlag" jdbcType="VARCHAR"/>
        <result column="belong_id" property="belongId" jdbcType="VARCHAR"/>
        <association column="add_id" property="addUser"
                     select="com.th.demo.mapping.system.SysUserMapper.selectByPrimaryKey"/>
    </resultMap>

    <resultMap id="AccountWithInvoice" type="com.th.demo.model.finance.AccountWithInvoice">
        <id column="id" property="id" jdbcType="VARCHAR"/>
        <result column="customer_name" property="customerName" jdbcType="VARCHAR"/>
        <result column="weight" property="weight" jdbcType="DOUBLE"/>
        <result column="unit_price" property="unitPrice" jdbcType="DOUBLE"/>
        <result column="price" property="price" jdbcType="DOUBLE"/>
        <result column="invoice_count" property="invoiceCount" jdbcType="DOUBLE"/>
        <result column="invoice_price" property="invoicePrice" jdbcType="DOUBLE"/>
        <result column="invoice_weight" property="invoiceWeight" jdbcType="DOUBLE"/>
        <result column="invoice_percent" property="invoicePercent" jdbcType="DOUBLE"/>
        <result column="add_time" property="addTime" jdbcType="TIMESTAMP"/>
        <result column="modify_id" property="modifyId" jdbcType="VARCHAR"/>
        <result column="modify_time" property="modifyTime" jdbcType="TIMESTAMP"/>
        <result column="cancel_id" property="cancelId" jdbcType="VARCHAR"/>
        <result column="cancel_time" property="cancelTime" jdbcType="TIMESTAMP"/>
        <result column="cancel_flag" property="cancelFlag" jdbcType="VARCHAR"/>
        <result column="belong_id" property="belongId" jdbcType="VARCHAR"/>
        <association column="add_id" property="addUser"
                     select="com.th.demo.mapping.system.SysUserMapper.selectByPrimaryKey"/>
    </resultMap>


    <sql id="Base_Column_List">
    id, customer_name, weight, unit_price, price, add_id, add_time, modify_id, modify_time, 
    cancel_id, cancel_time, cancel_flag, belong_id
  </sql>
    <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String">
        select
        <include refid="Base_Column_List"/>
        from t_finance_account
        where id = #{id,jdbcType=VARCHAR}
    </select>
    <delete id="deleteByPrimaryKey" parameterType="java.lang.String">
    delete from t_finance_account
    where id = #{id,jdbcType=VARCHAR}
  </delete>
    <insert id="insert" parameterType="com.th.demo.model.finance.Account">
    insert into t_finance_account (id, customer_name, weight,
      unit_price, price, add_id, 
      add_time, modify_id, modify_time, 
      cancel_id, cancel_time, cancel_flag, 
      belong_id)
    values (#{id,jdbcType=VARCHAR}, #{customerName,jdbcType=VARCHAR}, #{weight,jdbcType=DOUBLE},
      #{unitPrice,jdbcType=DOUBLE}, #{price,jdbcType=DOUBLE}, #{addUser.id,jdbcType=VARCHAR},
      #{addTime,jdbcType=TIMESTAMP}, #{modifyId,jdbcType=VARCHAR}, #{modifyTime,jdbcType=TIMESTAMP}, 
      #{cancelId,jdbcType=VARCHAR}, #{cancelTime,jdbcType=TIMESTAMP}, #{cancelFlag,jdbcType=VARCHAR}, 
      #{belongId,jdbcType=VARCHAR})
  </insert>
    <insert id="insertSelective" parameterType="com.th.demo.model.finance.Account">
        insert into t_finance_account
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="customerName != null">
                customer_name,
            </if>
            <if test="weight != null">
                weight,
            </if>
            <if test="unitPrice != null">
                unit_price,
            </if>
            <if test="price != null">
                price,
            </if>
            <if test="addId != null">
                add_id,
            </if>
            <if test="addTime != null">
                add_time,
            </if>
            <if test="modifyId != null">
                modify_id,
            </if>
            <if test="modifyTime != null">
                modify_time,
            </if>
            <if test="cancelId != null">
                cancel_id,
            </if>
            <if test="cancelTime != null">
                cancel_time,
            </if>
            <if test="cancelFlag != null">
                cancel_flag,
            </if>
            <if test="belongId != null">
                belong_id,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id,jdbcType=VARCHAR},
            </if>
            <if test="customerName != null">
                #{customerName,jdbcType=VARCHAR},
            </if>
            <if test="weight != null">
                #{weight,jdbcType=DOUBLE},
            </if>
            <if test="unitPrice != null">
                #{unitPrice,jdbcType=DOUBLE},
            </if>
            <if test="price != null">
                #{price,jdbcType=DOUBLE},
            </if>
            <if test="addId != null">
                #{addId,jdbcType=VARCHAR},
            </if>
            <if test="addTime != null">
                #{addTime,jdbcType=TIMESTAMP},
            </if>
            <if test="modifyId != null">
                #{modifyId,jdbcType=VARCHAR},
            </if>
            <if test="modifyTime != null">
                #{modifyTime,jdbcType=TIMESTAMP},
            </if>
            <if test="cancelId != null">
                #{cancelId,jdbcType=VARCHAR},
            </if>
            <if test="cancelTime != null">
                #{cancelTime,jdbcType=TIMESTAMP},
            </if>
            <if test="cancelFlag != null">
                #{cancelFlag,jdbcType=VARCHAR},
            </if>
            <if test="belongId != null">
                #{belongId,jdbcType=VARCHAR},
            </if>
        </trim>
    </insert>
    <update id="updateByPrimaryKeySelective" parameterType="com.th.demo.model.finance.Account">
        update t_finance_account
        <set>
            <if test="customerName != null">
                customer_name = #{customerName,jdbcType=VARCHAR},
            </if>
            <if test="weight != null">
                weight = #{weight,jdbcType=DOUBLE},
            </if>
            <if test="unitPrice != null">
                unit_price = #{unitPrice,jdbcType=DOUBLE},
            </if>
            <if test="price != null">
                price = #{price,jdbcType=DOUBLE},
            </if>
            <if test="addId != null">
                add_id = #{addId,jdbcType=VARCHAR},
            </if>
            <if test="addTime != null">
                add_time = #{addTime,jdbcType=TIMESTAMP},
            </if>
            <if test="modifyId != null">
                modify_id = #{modifyId,jdbcType=VARCHAR},
            </if>
            <if test="modifyTime != null">
                modify_time = #{modifyTime,jdbcType=TIMESTAMP},
            </if>
            <if test="cancelId != null">
                cancel_id = #{cancelId,jdbcType=VARCHAR},
            </if>
            <if test="cancelTime != null">
                cancel_time = #{cancelTime,jdbcType=TIMESTAMP},
            </if>
            <if test="cancelFlag != null">
                cancel_flag = #{cancelFlag,jdbcType=VARCHAR},
            </if>
            <if test="belongId != null">
                belong_id = #{belongId,jdbcType=VARCHAR},
            </if>
        </set>
        where id = #{id,jdbcType=VARCHAR}
    </update>
    <update id="updateByPrimaryKey" parameterType="com.th.demo.model.finance.Account">
    update t_finance_account
    set customer_name = #{customerName,jdbcType=VARCHAR},
      weight = #{weight,jdbcType=DOUBLE},
      unit_price = #{unitPrice,jdbcType=DOUBLE},
      price = #{price,jdbcType=DOUBLE},
      add_id = #{addUser.id,jdbcType=VARCHAR},
      add_time = #{addTime,jdbcType=TIMESTAMP},
      modify_id = #{modifyId,jdbcType=VARCHAR},
      modify_time = #{modifyTime,jdbcType=TIMESTAMP},
      cancel_id = #{cancelId,jdbcType=VARCHAR},
      cancel_time = #{cancelTime,jdbcType=TIMESTAMP},
      cancel_flag = #{cancelFlag,jdbcType=VARCHAR},
      belong_id = #{belongId,jdbcType=VARCHAR}
    where id = #{id,jdbcType=VARCHAR}
  </update>

    <select id="selectWithInvoice" resultMap="AccountWithInvoice">
     select  t.id, t.customer_name, t.weight, t.unit_price, t.price,
     t.add_id, t.add_time, t.modify_id, t.modify_time,
     t.cancel_id, t.cancel_time, t.cancel_flag, t.belong_id ,
     round(ifnull(count(d.id),0) ,2) as invoice_count,
     round(ifnull(sum(d.price),0) ,2 ) as invoice_price,
     round(ifnull(sum(d.weight),0), 3)  as invoice_weight,
     round(ifnull(sum(d.price) /t.price*100,0),2) as invoice_percent
     from   t_finance_account t  left join t_finance_invoice d
     on   t.id = d.account_id
     where   t.customer_name like concat('%',#{param1,jdbcType=VARCHAR},'%')
     and ifnull (t.cancel_flag,'0') = '0'
     and ifnull(d.cancel_flag,'0') = '0'
     and  t.belong_id = #{param2,jdbcType=VARCHAR}
     group by t.id, t.customer_name, t.weight, t.unit_price, t.price,
     t.add_id, t.add_time, t.modify_id, t.modify_time,
     t.cancel_id, t.cancel_time, t.cancel_flag, t.belong_id
  </select>


    <select id="queryGroup" resultMap="AccountWithInvoice">
        SELECT     CONVERT(@rowNum:=@rowNum + 1 , char) as id
                    ,t.customer_name
                    ,round(sum(weight)) as weight
                    ,round(avg(unit_price)) as unit_price
                    ,round(sum(price)) as price
                    ,round(sum(invoice_count)) as invoice_count
                    ,round(sum(invoice_price)) as invoice_price
                    ,round(sum(invoice_weight)) as invoice_weight
                    ,round(inv_price/amt_price * 100) as invoice_percent

        FROM (
             select  t.id, t.customer_name, t.weight, t.unit_price, t.price,
             t.add_id, t.add_time, t.modify_id, t.modify_time,
             t.cancel_id, t.cancel_time, t.cancel_flag, t.belong_id ,
             round(ifnull(count(d.id),0) ,2) as invoice_count,
             round(ifnull(sum(d.price),0) ,2 ) as invoice_price,
             round(ifnull(sum(d.weight),0), 3)  as invoice_weight,
             round(ifnull(sum(d.price) /t.price*100,0),2) as invoice_percent,
             round(sum(d.price),0) as inv_price,
		     round(sum(t.price),1) as amt_price
             from   t_finance_account t  left join t_finance_invoice d
             on   t.id = d.account_id
             where   t.customer_name like concat('%',#{param1,jdbcType=VARCHAR},'%')
             and ifnull (t.cancel_flag,'0') = '0'
             and ifnull(d.cancel_flag,'0') = '0'
             and  t.belong_id = #{param2,jdbcType=VARCHAR}
             group by t.id, t.customer_name, t.weight, t.unit_price, t.price,
             t.add_id, t.add_time, t.modify_id, t.modify_time,
             t.cancel_id, t.cancel_time, t.cancel_flag, t.belong_id
         ) t ,(SELECT  @rownum := 0) AS rn
      where  (case when invoice_count = 0 then 0 when invoice_count > 0 then 1 end ) = #{param3,jdbcType=VARCHAR}
      group by customer_name
  </select>

    <select id="queryBycustomer" resultMap="AccountWithInvoice">
     select  t.id, t.customer_name, t.weight, t.unit_price, t.price,
     t.add_id, t.add_time, t.modify_id, t.modify_time,
     t.cancel_id, t.cancel_time, t.cancel_flag, t.belong_id ,
     round(ifnull(count(d.id),0) ,2) as invoice_count,
     round(ifnull(sum(d.price),0) ,2 ) as invoice_price,
     round(ifnull(sum(d.weight),0), 3)  as invoice_weight,
     round(ifnull(sum(d.price) /t.price*100,0),2) as invoice_percent
     from   t_finance_account t  left join t_finance_invoice d
     on   t.id = d.account_id
     where   t.customer_name like concat('%',#{param1,jdbcType=VARCHAR},'%')
     and ifnull (t.cancel_flag,'0') = '0'
     and ifnull(d.cancel_flag,'0') = '0'
     and  t.belong_id = #{param2,jdbcType=VARCHAR}
     group by t.id, t.customer_name, t.weight, t.unit_price, t.price,
     t.add_id, t.add_time, t.modify_id, t.modify_time,
     t.cancel_id, t.cancel_time, t.cancel_flag, t.belong_id
  </select>


</mapper>