<?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.AccountDetailMapper" >
  <resultMap id="BaseResultMap" type="com.th.demo.model.finance.AccountDetail" >
    <id column="id" property="id" jdbcType="VARCHAR" />
    <result column="f_id" property="fId" jdbcType="VARCHAR" />
    <result column="record_type" property="recordType" jdbcType="VARCHAR" />
    <result column="change_time" property="changeTime" jdbcType="INTEGER" />
    <result column="plate_no" property="plateNo" jdbcType="VARCHAR" />
    <result column="ware_name" property="wareName" jdbcType="VARCHAR" />
    <result column="stack_name" property="stackName" jdbcType="VARCHAR" />
    <result column="layer" property="layer" jdbcType="INTEGER" />
    <result column="material_name" property="materialName" jdbcType="VARCHAR" />
    <result column="standard" property="standard" jdbcType="VARCHAR" />
    <result column="model" property="model" jdbcType="VARCHAR" />
    <result column="customer_name" property="customerName" jdbcType="VARCHAR" />
    <result column="customer_no" property="customerNo" jdbcType="VARCHAR" />
    <result column="count" property="count" jdbcType="DOUBLE" />
    <result column="weight" property="weight" jdbcType="DOUBLE" />
    <result column="suttle" property="suttle" jdbcType="DOUBLE" />
    <result column="receive_address" property="receiveAddress" jdbcType="VARCHAR" />
    <result column="storage_unit_price" property="storageUnitPrice" jdbcType="DOUBLE" />
    <result column="storage_price" property="storagePrice" jdbcType="DOUBLE" />
    <result column="weight_unit_price" property="weightUnitPrice" jdbcType="DOUBLE" />
    <result column="weight_price" property="weightPrice" jdbcType="DOUBLE" />
    <result column="reduce_unit_price" property="reduceUnitPrice" jdbcType="DOUBLE" />
    <result column="reduce_price" property="reducePrice" jdbcType="DOUBLE" />
    <result column="total_price" property="totalPrice" jdbcType="DOUBLE" />
    <result column="account_flag" property="accountFlag" jdbcType="VARCHAR" />

    <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="RcvMoneyMap" type="com.th.demo.model.finance.RcvMoney" >
    <result column="id"  property="id" jdbcType="VARCHAR" />
    <result column="customer_name"  property="customerName" jdbcType="VARCHAR" />
    <result column="prod_nm"  property="prodNm" jdbcType="VARCHAR" />
    <result column="ord_no"  property="ordNo" jdbcType="VARCHAR" />
    <result column="weight"  property="weight" jdbcType="VARCHAR" />
    <result column="price"  property="price" jdbcType="VARCHAR" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, f_id, record_type, change_time, plate_no, ware_name, stack_name, layer, material_name,
    standard, model, customer_name, customer_no, count, weight, suttle, receive_address,
    storage_unit_price, storage_price, weight_unit_price, weight_price, reduce_unit_price,
    reduce_price, total_price, account_flag, 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_detail
    where id = #{id,jdbcType=VARCHAR}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
    delete from t_finance_account_detail
    where id = #{id,jdbcType=VARCHAR}
  </delete>
  <insert id="insert" parameterType="com.th.demo.model.finance.AccountDetail" >
    <selectKey keyProperty="id" order="BEFORE" resultType="java.lang.String">
      SELECT  REPLACE(UUID(),'-','') as id from dual
    </selectKey>

    insert into t_finance_account_detail (id, f_id, record_type,
    change_time, plate_no, ware_name,
    stack_name, layer, material_name,
    standard, model, customer_name,
    customer_no, count, weight,
    suttle, receive_address, storage_unit_price,
    storage_price, weight_unit_price, weight_price,
    reduce_unit_price, reduce_price, total_price,
    account_flag, add_id, add_time,
    modify_id, modify_time, cancel_id,
    cancel_time, cancel_flag, belong_id
    )
    values (#{id,jdbcType=VARCHAR}, #{fId,jdbcType=VARCHAR}, #{recordType,jdbcType=VARCHAR},
    #{changeTime,jdbcType=INTEGER}, #{plateNo,jdbcType=VARCHAR}, #{wareName,jdbcType=VARCHAR},
    #{stackName,jdbcType=VARCHAR}, #{layer,jdbcType=INTEGER}, #{materialName,jdbcType=VARCHAR},
    #{standard,jdbcType=VARCHAR}, #{model,jdbcType=VARCHAR}, #{customerName,jdbcType=VARCHAR},
    #{customerNo,jdbcType=VARCHAR}, #{count,jdbcType=DOUBLE}, #{weight,jdbcType=DOUBLE},
    #{suttle,jdbcType=DOUBLE}, #{receiveAddress,jdbcType=VARCHAR}, #{storageUnitPrice,jdbcType=DOUBLE},
    #{storagePrice,jdbcType=DOUBLE}, #{weightUnitPrice,jdbcType=DOUBLE}, #{weightPrice,jdbcType=DOUBLE},
    #{reduceUnitPrice,jdbcType=DOUBLE}, #{reducePrice,jdbcType=DOUBLE}, #{totalPrice,jdbcType=DOUBLE},
    #{accountFlag,jdbcType=VARCHAR}, #{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.AccountDetail" >
    insert into t_finance_account_detail
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="fId != null" >
        f_id,
      </if>
      <if test="recordType != null" >
        record_type,
      </if>
      <if test="changeTime != null" >
        change_time,
      </if>
      <if test="plateNo != null" >
        plate_no,
      </if>
      <if test="wareName != null" >
        ware_name,
      </if>
      <if test="stackName != null" >
        stack_name,
      </if>
      <if test="layer != null" >
        layer,
      </if>
      <if test="materialName != null" >
        material_name,
      </if>
      <if test="standard != null" >
        standard,
      </if>
      <if test="model != null" >
        model,
      </if>
      <if test="customerName != null" >
        customer_name,
      </if>
      <if test="customerNo != null" >
        customer_no,
      </if>
      <if test="count != null" >
        count,
      </if>
      <if test="weight != null" >
        weight,
      </if>
      <if test="suttle != null" >
        suttle,
      </if>
      <if test="receiveAddress != null" >
        receive_address,
      </if>
      <if test="storageUnitPrice != null" >
        storage_unit_price,
      </if>
      <if test="storagePrice != null" >
        storage_price,
      </if>
      <if test="weightUnitPrice != null" >
        weight_unit_price,
      </if>
      <if test="weightPrice != null" >
        weight_price,
      </if>
      <if test="reduceUnitPrice != null" >
        reduce_unit_price,
      </if>
      <if test="reducePrice != null" >
        reduce_price,
      </if>
      <if test="totalPrice != null" >
        total_price,
      </if>
      <if test="accountFlag != null" >
        account_flag,
      </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="fId != null" >
        #{fId,jdbcType=VARCHAR},
      </if>
      <if test="recordType != null" >
        #{recordType,jdbcType=VARCHAR},
      </if>
      <if test="changeTime != null" >
        #{changeTime,jdbcType=INTEGER},
      </if>
      <if test="plateNo != null" >
        #{plateNo,jdbcType=VARCHAR},
      </if>
      <if test="wareName != null" >
        #{wareName,jdbcType=VARCHAR},
      </if>
      <if test="stackName != null" >
        #{stackName,jdbcType=VARCHAR},
      </if>
      <if test="layer != null" >
        #{layer,jdbcType=INTEGER},
      </if>
      <if test="materialName != null" >
        #{materialName,jdbcType=VARCHAR},
      </if>
      <if test="standard != null" >
        #{standard,jdbcType=VARCHAR},
      </if>
      <if test="model != null" >
        #{model,jdbcType=VARCHAR},
      </if>
      <if test="customerName != null" >
        #{customerName,jdbcType=VARCHAR},
      </if>
      <if test="customerNo != null" >
        #{customerNo,jdbcType=VARCHAR},
      </if>
      <if test="count != null" >
        #{count,jdbcType=DOUBLE},
      </if>
      <if test="weight != null" >
        #{weight,jdbcType=DOUBLE},
      </if>
      <if test="suttle != null" >
        #{suttle,jdbcType=DOUBLE},
      </if>
      <if test="receiveAddress != null" >
        #{receiveAddress,jdbcType=VARCHAR},
      </if>
      <if test="storageUnitPrice != null" >
        #{storageUnitPrice,jdbcType=DOUBLE},
      </if>
      <if test="storagePrice != null" >
        #{storagePrice,jdbcType=DOUBLE},
      </if>
      <if test="weightUnitPrice != null" >
        #{weightUnitPrice,jdbcType=DOUBLE},
      </if>
      <if test="weightPrice != null" >
        #{weightPrice,jdbcType=DOUBLE},
      </if>
      <if test="reduceUnitPrice != null" >
        #{reduceUnitPrice,jdbcType=DOUBLE},
      </if>
      <if test="reducePrice != null" >
        #{reducePrice,jdbcType=DOUBLE},
      </if>
      <if test="totalPrice != null" >
        #{totalPrice,jdbcType=DOUBLE},
      </if>
      <if test="accountFlag != null" >
        #{accountFlag,jdbcType=VARCHAR},
      </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.AccountDetail" >
    update t_finance_account_detail
    <set >
      <if test="fId != null" >
        f_id = #{fId,jdbcType=VARCHAR},
      </if>
      <if test="recordType != null" >
        record_type = #{recordType,jdbcType=VARCHAR},
      </if>
      <if test="changeTime != null" >
        change_time = #{changeTime,jdbcType=INTEGER},
      </if>
      <if test="plateNo != null" >
        plate_no = #{plateNo,jdbcType=VARCHAR},
      </if>
      <if test="wareName != null" >
        ware_name = #{wareName,jdbcType=VARCHAR},
      </if>
      <if test="stackName != null" >
        stack_name = #{stackName,jdbcType=VARCHAR},
      </if>
      <if test="layer != null" >
        layer = #{layer,jdbcType=INTEGER},
      </if>
      <if test="materialName != null" >
        material_name = #{materialName,jdbcType=VARCHAR},
      </if>
      <if test="standard != null" >
        standard = #{standard,jdbcType=VARCHAR},
      </if>
      <if test="model != null" >
        model = #{model,jdbcType=VARCHAR},
      </if>
      <if test="customerName != null" >
        customer_name = #{customerName,jdbcType=VARCHAR},
      </if>
      <if test="customerNo != null" >
        customer_no = #{customerNo,jdbcType=VARCHAR},
      </if>
      <if test="count != null" >
        count = #{count,jdbcType=DOUBLE},
      </if>
      <if test="weight != null" >
        weight = #{weight,jdbcType=DOUBLE},
      </if>
      <if test="suttle != null" >
        suttle = #{suttle,jdbcType=DOUBLE},
      </if>
      <if test="receiveAddress != null" >
        receive_address = #{receiveAddress,jdbcType=VARCHAR},
      </if>
      <if test="storageUnitPrice != null" >
        storage_unit_price = #{storageUnitPrice,jdbcType=DOUBLE},
      </if>
      <if test="storagePrice != null" >
        storage_price = #{storagePrice,jdbcType=DOUBLE},
      </if>
      <if test="weightUnitPrice != null" >
        weight_unit_price = #{weightUnitPrice,jdbcType=DOUBLE},
      </if>
      <if test="weightPrice != null" >
        weight_price = #{weightPrice,jdbcType=DOUBLE},
      </if>
      <if test="reduceUnitPrice != null" >
        reduce_unit_price = #{reduceUnitPrice,jdbcType=DOUBLE},
      </if>
      <if test="reducePrice != null" >
        reduce_price = #{reducePrice,jdbcType=DOUBLE},
      </if>
      <if test="totalPrice != null" >
        total_price = #{totalPrice,jdbcType=DOUBLE},
      </if>
      <if test="accountFlag != null" >
        account_flag = #{accountFlag,jdbcType=VARCHAR},
      </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.AccountDetail" >
    update t_finance_account_detail
    set f_id = #{fId,jdbcType=VARCHAR},
      record_type = #{recordType,jdbcType=VARCHAR},
      change_time = #{changeTime,jdbcType=INTEGER},
      plate_no = #{plateNo,jdbcType=VARCHAR},
      ware_name = #{wareName,jdbcType=VARCHAR},
      stack_name = #{stackName,jdbcType=VARCHAR},
      layer = #{layer,jdbcType=INTEGER},
      material_name = #{materialName,jdbcType=VARCHAR},
      standard = #{standard,jdbcType=VARCHAR},
      model = #{model,jdbcType=VARCHAR},
      customer_name = #{customerName,jdbcType=VARCHAR},
      customer_no = #{customerNo,jdbcType=VARCHAR},
      count = #{count,jdbcType=DOUBLE},
      weight = #{weight,jdbcType=DOUBLE},
      suttle = #{suttle,jdbcType=DOUBLE},
      receive_address = #{receiveAddress,jdbcType=VARCHAR},
      storage_unit_price = #{storageUnitPrice,jdbcType=DOUBLE},
      storage_price = #{storagePrice,jdbcType=DOUBLE},
      weight_unit_price = #{weightUnitPrice,jdbcType=DOUBLE},
      weight_price = #{weightPrice,jdbcType=DOUBLE},
      reduce_unit_price = #{reduceUnitPrice,jdbcType=DOUBLE},
      reduce_price = #{reducePrice,jdbcType=DOUBLE},
      total_price = #{totalPrice,jdbcType=DOUBLE},
      account_flag = #{accountFlag,jdbcType=VARCHAR},
      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="selectByFid" resultMap="BaseResultMap">
    select * from t_finance_account_detail
    where f_id = #{param1,jdbcType=VARCHAR}
    and cancel_flag = '0'
      and belong_id=#{param2,jdbcType=VARCHAR}
  </select>

  <select id="select" resultMap="BaseResultMap"  >
    select
       customer_name
		,record_type
	  ,'' as change_time
		,avg(t.storage_unit_price) as storage_unit_price
		,sum(t.storage_price) as storage_price
		,avg(t.weight_unit_price) as weight_unit_price
		,sum(t.weight_price) as weight_price
		,avg(t.reduce_unit_price) as reduce_unit_price
		,sum(t.reduce_price) as reduce_price
		,sum(t.total_price) as total_price
		,max(t.account_flag) as account_flag
		,max(t.ware_name) as ware_name
		,'' as stack_name
		,'' as layer
		,'' as material_name
		,'' as model
		,'' as standard
		,sum(t.count) as count
		,sum(t.weight) as weight
		,sum(t.suttle) as suttle
		,'' as plate_no
		,max(t.receive_address) as receive_address
		,'' as remark
		,CONVERT(@rowNum:=@rowNum + 1 , char) as id
    from t_finance_account_detail t,(SELECT  @rownum := 0) AS rn
    where customer_name like concat('%',#{param1,jdbcType=VARCHAR},'%')
    and account_flag = #{param2,jdbcType=VARCHAR}
    and cancel_flag = '0'
    and belong_id = #{param3,jdbcType=VARCHAR}
    group by t.customer_name,t.record_type
  </select>

  <select id="queryBycustomer" resultMap="BaseResultMap"  >
    select
        *
    from t_finance_account_detail t
    where customer_name like concat('%',#{param1,jdbcType=VARCHAR},'%')
    and account_flag = #{param2,jdbcType=VARCHAR}
    and cancel_flag = '0'
    and belong_id = #{param3,jdbcType=VARCHAR}

  </select>

  <select id="queryRcvMoneyAmt" resultMap="RcvMoneyMap" parameterType="java.lang.String" >
    select      max(t.id) as id
                 ,customer_name
                 ,'' as prod_nm
                 ,'' as ord_no
                 ,sum(t.count * t.weight) as weight
                 ,sum(t.count * t.weight * c.storage_price) as price
    from (select * from t_ware_out_record tt where  ifnull(tt.cancel_flag,'0') = '0'
    and date_format(tt.add_time , '%Y-%m-%d') between #{param1,jdbcType=VARCHAR} and #{param2,jdbcType=VARCHAR}) t
    left join
    (select a.storage_price,b.name,b.standard from t_maint_price a,t_maint_material b
    where a.material_id = b.id
    and a.cancel_flag = '0')  c
    on (t.material_name = c.name
    and t.standard = c.standard)
    group by t.customer_name
  </select>

  <select id="queryRcvMoneyBycustomer" resultMap="RcvMoneyMap" parameterType="java.lang.String" >
    select rst.*,CONVERT(@rowNum:=@rowNum + 1 , char) as id from (select customer_name
                 ,t.material_name as prod_nm
                 ,t.ord_no as ord_no
                 ,sum(t.count * t.weight) as weight
                 ,sum(t.count * t.weight * c.storage_price) as price
    from (select tt.*,tt2.ord_no from t_ware_out_record tt,t_ware_store tt2
             where tt.plate_no = tt2.plate_no and tt2.cancel_flag = '0' and   ifnull(tt.cancel_flag,'0') = '0' and tt2.out_flag = '1'
    and date_format(tt.add_time , '%Y-%m-%d') between #{param1,jdbcType=VARCHAR} and #{param2,jdbcType=VARCHAR}
    and tt.customer_name = #{param3,jdbcType=VARCHAR}) t
    left join
    (select a.storage_price,b.name,b.standard from t_maint_price a,t_maint_material b
    where a.material_id = b.id
    and a.cancel_flag = '0')  c
    on (t.material_name = c.name
    and t.standard = c.standard)
    group by t.customer_name,t.material_name,t.ord_no
    ) rst,(SELECT  @rownum := 0) AS rn
  </select>
</mapper>