<?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.goafanti.common.dao.ExpenseAccountMapper">
  <resultMap id="BaseResultMap" type="com.goafanti.common.model.ExpenseAccount">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="prid" jdbcType="INTEGER" property="prid" />
    <result column="order_no" jdbcType="VARCHAR" property="orderNo" />
    <result column="aid" jdbcType="VARCHAR" property="aid" />
    <result column="aname" jdbcType="VARCHAR" property="aname" />
    <result column="type" jdbcType="INTEGER" property="type" />
    <result column="type_other" jdbcType="VARCHAR" property="typeOther" />
    <result column="status" jdbcType="INTEGER" property="status" />
    <result column="process_status" jdbcType="INTEGER" property="processStatus" />
    <result column="total_amount" jdbcType="DECIMAL" property="totalAmount" />
    <result column="attachment_url" jdbcType="VARCHAR" property="attachmentUrl" />
    <result column="apply_dep" jdbcType="VARCHAR" property="applyDep" />
    <result column="pay_dep" jdbcType="VARCHAR" property="payDep" />
    <result column="eaaid" jdbcType="INTEGER" property="eaaid" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="district_name" jdbcType="VARCHAR" property="districtName" />
    <result column="user_names" jdbcType="VARCHAR" property="userNames" />
    <result column="release_start" jdbcType="TIMESTAMP" property="releaseStart" />
    <result column="release_end" jdbcType="TIMESTAMP" property="releaseEnd" />
    <result column="duration" jdbcType="DOUBLE" property="duration" />
    <result column="examine_name" jdbcType="VARCHAR" property="examineName" />
    <result column="settlement_amount" jdbcType="DECIMAL" property="settlementAmount" />
    <result column="debit_id" jdbcType="INTEGER" property="debitId" />
    <result column="target_type" jdbcType="INTEGER" property="targetType" />
    <result column="liquidation_status" jdbcType="INTEGER" property="liquidationStatus" />
    <result column="remarks" jdbcType="VARCHAR" property="remarks" />
  </resultMap>
  <sql id="Base_Column_List">
    id, prid, order_no, aid, aname, `type`, type_other, `status`, process_status, total_amount,
    attachment_url, apply_dep, pay_dep, eaaid, create_time, district_name, user_names,
    release_start, release_end, duration, examine_name, settlement_amount, debit_id,
    target_type, liquidation_status, remarks
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from expense_account
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from expense_account
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.goafanti.common.model.ExpenseAccount" useGeneratedKeys="true">
    insert into expense_account (prid, order_no, aid,
      aname, `type`, type_other,
      `status`, process_status, total_amount,
      attachment_url, apply_dep, pay_dep,
      eaaid, create_time, district_name,
      user_names, release_start, release_end,
      duration, examine_name, settlement_amount,
      debit_id, target_type, liquidation_status,
      remarks)
    values (#{prid,jdbcType=INTEGER}, #{orderNo,jdbcType=VARCHAR}, #{aid,jdbcType=VARCHAR},
      #{aname,jdbcType=VARCHAR}, #{type,jdbcType=INTEGER}, #{typeOther,jdbcType=VARCHAR},
      #{status,jdbcType=INTEGER}, #{processStatus,jdbcType=INTEGER}, #{totalAmount,jdbcType=DECIMAL},
      #{attachmentUrl,jdbcType=VARCHAR}, #{applyDep,jdbcType=VARCHAR}, #{payDep,jdbcType=VARCHAR},
      #{eaaid,jdbcType=INTEGER}, #{createTime,jdbcType=TIMESTAMP}, #{districtName,jdbcType=VARCHAR},
      #{userNames,jdbcType=VARCHAR}, #{releaseStart,jdbcType=TIMESTAMP}, #{releaseEnd,jdbcType=TIMESTAMP},
      #{duration,jdbcType=DOUBLE}, #{examineName,jdbcType=VARCHAR}, #{settlementAmount,jdbcType=DECIMAL},
      #{debitId,jdbcType=INTEGER}, #{targetType,jdbcType=INTEGER}, #{liquidationStatus,jdbcType=INTEGER},
      #{remarks,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.goafanti.common.model.ExpenseAccount" useGeneratedKeys="true">
    insert into expense_account
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="prid != null">
        prid,
      </if>
      <if test="orderNo != null">
        order_no,
      </if>
      <if test="aid != null">
        aid,
      </if>
      <if test="aname != null">
        aname,
      </if>
      <if test="type != null">
        `type`,
      </if>
      <if test="typeOther != null">
        type_other,
      </if>
      <if test="status != null">
        `status`,
      </if>
      <if test="processStatus != null">
        process_status,
      </if>
      <if test="totalAmount != null">
        total_amount,
      </if>
      <if test="attachmentUrl != null">
        attachment_url,
      </if>
      <if test="applyDep != null">
        apply_dep,
      </if>
      <if test="payDep != null">
        pay_dep,
      </if>
      <if test="eaaid != null">
        eaaid,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="districtName != null">
        district_name,
      </if>
      <if test="userNames != null">
        user_names,
      </if>
      <if test="releaseStart != null">
        release_start,
      </if>
      <if test="releaseEnd != null">
        release_end,
      </if>
      <if test="duration != null">
        duration,
      </if>
      <if test="examineName != null">
        examine_name,
      </if>
      <if test="settlementAmount != null">
        settlement_amount,
      </if>
      <if test="debitId != null">
        debit_id,
      </if>
      <if test="targetType != null">
        target_type,
      </if>
      <if test="liquidationStatus != null">
        liquidation_status,
      </if>
      <if test="remarks != null">
        remarks,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="prid != null">
        #{prid,jdbcType=INTEGER},
      </if>
      <if test="orderNo != null">
        #{orderNo,jdbcType=VARCHAR},
      </if>
      <if test="aid != null">
        #{aid,jdbcType=VARCHAR},
      </if>
      <if test="aname != null">
        #{aname,jdbcType=VARCHAR},
      </if>
      <if test="type != null">
        #{type,jdbcType=INTEGER},
      </if>
      <if test="typeOther != null">
        #{typeOther,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        #{status,jdbcType=INTEGER},
      </if>
      <if test="processStatus != null">
        #{processStatus,jdbcType=INTEGER},
      </if>
      <if test="totalAmount != null">
        #{totalAmount,jdbcType=DECIMAL},
      </if>
      <if test="attachmentUrl != null">
        #{attachmentUrl,jdbcType=VARCHAR},
      </if>
      <if test="applyDep != null">
        #{applyDep,jdbcType=VARCHAR},
      </if>
      <if test="payDep != null">
        #{payDep,jdbcType=VARCHAR},
      </if>
      <if test="eaaid != null">
        #{eaaid,jdbcType=INTEGER},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="districtName != null">
        #{districtName,jdbcType=VARCHAR},
      </if>
      <if test="userNames != null">
        #{userNames,jdbcType=VARCHAR},
      </if>
      <if test="releaseStart != null">
        #{releaseStart,jdbcType=TIMESTAMP},
      </if>
      <if test="releaseEnd != null">
        #{releaseEnd,jdbcType=TIMESTAMP},
      </if>
      <if test="duration != null">
        #{duration,jdbcType=DOUBLE},
      </if>
      <if test="examineName != null">
        #{examineName,jdbcType=VARCHAR},
      </if>
      <if test="settlementAmount != null">
        #{settlementAmount,jdbcType=DECIMAL},
      </if>
      <if test="debitId != null">
        #{debitId,jdbcType=INTEGER},
      </if>
      <if test="targetType != null">
        #{targetType,jdbcType=INTEGER},
      </if>
      <if test="liquidationStatus != null">
        #{liquidationStatus,jdbcType=INTEGER},
      </if>
      <if test="remarks != null">
        #{remarks,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.goafanti.common.model.ExpenseAccount">
    update expense_account
    <set>
      <if test="prid != null">
        prid = #{prid,jdbcType=INTEGER},
      </if>
      <if test="orderNo != null">
        order_no = #{orderNo,jdbcType=VARCHAR},
      </if>
      <if test="aid != null">
        aid = #{aid,jdbcType=VARCHAR},
      </if>
      <if test="aname != null">
        aname = #{aname,jdbcType=VARCHAR},
      </if>
      <if test="type != null">
        `type` = #{type,jdbcType=INTEGER},
      </if>
      <if test="typeOther != null">
        type_other = #{typeOther,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        `status` = #{status,jdbcType=INTEGER},
      </if>
      <if test="processStatus != null">
        process_status = #{processStatus,jdbcType=INTEGER},
      </if>
      <if test="totalAmount != null">
        total_amount = #{totalAmount,jdbcType=DECIMAL},
      </if>
      <if test="attachmentUrl != null">
        attachment_url = #{attachmentUrl,jdbcType=VARCHAR},
      </if>
      <if test="applyDep != null">
        apply_dep = #{applyDep,jdbcType=VARCHAR},
      </if>
      <if test="payDep != null">
        pay_dep = #{payDep,jdbcType=VARCHAR},
      </if>
      <if test="eaaid != null">
        eaaid = #{eaaid,jdbcType=INTEGER},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="districtName != null">
        district_name = #{districtName,jdbcType=VARCHAR},
      </if>
      <if test="userNames != null">
        user_names = #{userNames,jdbcType=VARCHAR},
      </if>
      <if test="releaseStart != null">
        release_start = #{releaseStart,jdbcType=TIMESTAMP},
      </if>
      <if test="releaseEnd != null">
        release_end = #{releaseEnd,jdbcType=TIMESTAMP},
      </if>
      <if test="duration != null">
        duration = #{duration,jdbcType=DOUBLE},
      </if>
      <if test="examineName != null">
        examine_name = #{examineName,jdbcType=VARCHAR},
      </if>
      <if test="settlementAmount != null">
        settlement_amount = #{settlementAmount,jdbcType=DECIMAL},
      </if>
      <if test="debitId != null">
        debit_id = #{debitId,jdbcType=INTEGER},
      </if>
      <if test="targetType != null">
        target_type = #{targetType,jdbcType=INTEGER},
      </if>
      <if test="liquidationStatus != null">
        liquidation_status = #{liquidationStatus,jdbcType=INTEGER},
      </if>
      <if test="remarks != null">
        remarks = #{remarks,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.goafanti.common.model.ExpenseAccount">
    update expense_account
    set prid = #{prid,jdbcType=INTEGER},
      order_no = #{orderNo,jdbcType=VARCHAR},
      aid = #{aid,jdbcType=VARCHAR},
      aname = #{aname,jdbcType=VARCHAR},
      `type` = #{type,jdbcType=INTEGER},
      type_other = #{typeOther,jdbcType=VARCHAR},
      `status` = #{status,jdbcType=INTEGER},
      process_status = #{processStatus,jdbcType=INTEGER},
      total_amount = #{totalAmount,jdbcType=DECIMAL},
      attachment_url = #{attachmentUrl,jdbcType=VARCHAR},
      apply_dep = #{applyDep,jdbcType=VARCHAR},
      pay_dep = #{payDep,jdbcType=VARCHAR},
      eaaid = #{eaaid,jdbcType=INTEGER},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      district_name = #{districtName,jdbcType=VARCHAR},
      user_names = #{userNames,jdbcType=VARCHAR},
      release_start = #{releaseStart,jdbcType=TIMESTAMP},
      release_end = #{releaseEnd,jdbcType=TIMESTAMP},
      duration = #{duration,jdbcType=DOUBLE},
      examine_name = #{examineName,jdbcType=VARCHAR},
      settlement_amount = #{settlementAmount,jdbcType=DECIMAL},
      debit_id = #{debitId,jdbcType=INTEGER},
      target_type = #{targetType,jdbcType=INTEGER},
      liquidation_status = #{liquidationStatus,jdbcType=INTEGER},
      remarks = #{remarks,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>

  <sql id="selectDtailsSql">
    a.id, a.prid, a.order_no orderNo, o.contract_no contractNo ,tm.buyer_name buyerName, a.aid, a.aname, a.`type`,
        a.type_other typeOther , a.status,a.process_status processStatus,a.total_amount totalAmount,a.debit_id debitId,
        a.attachment_url attachmentUrl,a.apply_dep applyDep, a.pay_dep payDep, a.eaaid,a.district_name districtName,
        a.user_names userNames,a.create_time, a.release_start,a.release_end, a.duration,b.name appDepName,
        c.name payDepName,d.name ,d.bank ,d.accounts ,a.settlement_amount settlementAmount,
        a.liquidation_status liquidationStatus, a.remarks,
           date_format(a.create_time,'%Y-%m-%d %H:%i:%S')createTimeStr,
            date_format(a.release_start,'%Y-%m-%d %H:%i:%S')releaseStartStr,
            date_format(a.release_end,'%Y-%m-%d %H:%i:%S')releaseEndStr
  </sql>
  <select id="selectByid" resultType="com.goafanti.expenseAccount.bo.OutExpenseAccount">
    SELECT
    <include refid="selectDtailsSql"/>
    FROM expense_account a left join department b on a.apply_dep =b.id left join department c on a.pay_dep =c.id
    left join expense_account_private d on a.eaaid=d.id left join t_order_new o on a.order_no =o.order_no
    left join t_order_mid tm on a.order_no =tm.order_no
    WHERE a.id= #{id}
  </select>


  <select id="selectExpenseAccountList" resultType="com.goafanti.expenseAccount.bo.OutExpenseAccountBo">
    select a.id,a.total_amount totalAmount,a.`type`,a.process_status processStatus,a.status,a.examine_name examineName,a.type_other typeOther,
    a.attachment_url attachmentUrl,a.target_type targetType , e.contract_no contractNo,tm.buyer_name buyerName ,
    <if test="processStatus ==1">
      eae.type eaeType,
    </if>
    f.name depName,d2.name financeName, date_format(a.create_time,'%Y-%m-%d %H:%i:%S')createTimeStr,a.aname,
    b.name appDepName, c.name payDepName
    from expense_account a left join department b on a.apply_dep =b.id left join department c on a.pay_dep =c.id
    left join admin d on a.aid =d.id left join t_order_new e on a.order_no =e.order_no
    left join t_order_mid tm on a.order_no =tm.order_no left join department f on d.department_id =f.id
    left join admin d2 on f.finance_id =d2.id
    <if test="processStatus ==1">
    inner join (select eaid ,auditor,min(process_status)`type` from expense_account_examine
      where auditor= #{auditor} group by eaid) eae on a.id=eae.eaid
    </if>
    where 1=1
      <if test="processStatus ==2">
        and a.process_status>0
      </if>
    <if test=" aid !=null">
    and a.aid= #{aid}
    </if>
    <if test="processStatus ==1">
      and a.process_status>0
    <if test="examineStatus ==0">
      and a.process_status =eae.`type`
    </if>
      <if test="examineStatus ==1">
        and a.process_status >eae.`type`
      </if>
    </if>
    <if test="status != null">
      and a.status= #{status}
    </if>
    <if test="type != null">
      and a.type= #{type}
    </if>
    <if test="startTime !=null and endTime !=null">
      and a.create_time between #{startTime} and #{endTime}
    </if>
    <if test="depId != null">
      and d.department_id = #{depId}
    </if>
    <if test="contractNo != null">
      and e.contract_no = #{contractNo}
    </if>
    <if test="username != null">
      and tm.buyer_name  like concat('%',#{username},'%')
    </if>
    <if test="applyDep != null">
      and a.apply_dep  = #{applyDep}
    </if>
    <if test="payDep != null">
      and a.pay_dep = #{payDep}
    </if>
    order by a.status,a.create_time desc
    <if test="page_sql !=null">
      ${page_sql}
    </if>

  </select>
  <select id="selectExpenseAccountCount" resultType="java.lang.Integer">
    select count(*)
    from expense_account a left join department b on a.apply_dep =b.id left join department c on a.pay_dep =c.id
    left join admin d on a.aid =d.id left join t_order_new e on a.order_no =e.order_no
    left join t_order_mid tm on a.order_no =tm.order_no
    <if test="processStatus ==1">
      inner join (select eaid ,auditor,min(process_status)`type` from expense_account_examine
      where auditor= #{auditor} group by eaid) eae on a.id=eae.eaid
    </if>
    where 1=1
    <if test="processStatus ==2">
      and a.process_status>0
    </if>
    <if test=" aid !=null">
      and a.aid= #{aid}
    </if>
    <if test="processStatus ==1">
      and a.process_status>0
      <if test="examineStatus ==0">
        and a.process_status =eae.`type`
      </if>
      <if test="examineStatus ==1">
        and a.process_status >eae.`type`
      </if>
    </if>
    <if test="status != null">
      and a.status= #{status}
    </if>
    <if test="type != null">
      and a.type= #{type}
    </if>
    <if test="startTime !=null and endTime !=null">
      and a.create_time between #{startTime} and #{endTime}
    </if>
    <if test="depId != null">
      and d.department_id = #{depId}
    </if>
    <if test="contractNo != null">
      and e.contract_no = #{contractNo}
    </if>
    <if test="username != null">
      and tm.buyer_name  like concat('%',#{username},'%')
    </if>
    <if test="applyDep != null">
      and a.apply_dep  = #{applyDep}
    </if>
    <if test="payDep != null">
      and a.pay_dep = #{payDep}
    </if>
  </select>

  <select id="selectExpenseAccountTotalAmount" resultType="java.lang.Integer">
    select sum(a.total_amount) totalAmount
    from expense_account a left join department b on a.apply_dep =b.id left join department c on a.pay_dep =c.id
    left join admin d on a.aid =d.id left join t_order_new e on a.order_no =e.order_no
    left join t_order_mid tm on a.order_no =tm.order_no
    <if test="processStatus ==1">
      inner join (select eaid ,auditor,min(process_status)`type` from expense_account_examine
      where auditor= #{auditor} group by eaid) eae on a.id=eae.eaid
    </if>
    where 1=1
    <if test="processStatus ==2">
      and a.process_status>0
    </if>
    <if test=" aid !=null">
      and a.aid= #{aid}
    </if>
    <if test="processStatus ==1">
      and a.process_status>0
      <if test="examineStatus ==0">
        and a.process_status =eae.`type`
      </if>
      <if test="examineStatus ==1">
        and a.process_status >eae.`type`
      </if>
    </if>
    <if test="status != null">
      and a.status= #{status}
    </if>
    <if test="type != null">
      and a.type= #{type}
    </if>
    <if test="startTime !=null and endTime !=null">
      and a.create_time between #{startTime} and #{endTime}
    </if>
    <if test="depId != null">
      and d.department_id = #{depId}
    </if>
    <if test="contractNo != null">
      and e.contract_no = #{contractNo}
    </if>
    <if test="username != null">
      and tm.buyer_name  like concat('%',#{username},'%')
    </if>
    <if test="applyDep != null">
      and a.apply_dep  = #{applyDep}
    </if>
    <if test="payDep != null">
      and a.pay_dep = #{payDep}
    </if>
  </select>


  <select id="selectByaidAndPrid" resultType="com.goafanti.expenseAccount.bo.OutExpenseAccount">
    SELECT
    <include refid="selectDtailsSql"/>
    FROM expense_account a left join department b on a.apply_dep =b.id left join department c on a.pay_dep =c.id
    left join expense_account_private d on a.eaaid=d.id left join t_order_new o on a.order_no =o.order_no
    left join t_order_mid tm on a.order_no =tm.order_no
    WHERE a.status= #{status}
    and a.prid= #{prid} and a.aid = #{aid}
  </select>
    <select id="selectByaidAndType" resultType="com.goafanti.expenseAccount.bo.OutExpenseAccount">
      SELECT
        a.id, a.prid, a.order_no orderNo, o.contract_no contractNo ,tm.buyer_name buyerName, a.aid, a.aname, a.`type`,
        a.type_other typeOther , a.status,a.process_status processStatus,a.total_amount totalAmount,a.debit_id debitId,
        a.attachment_url attachmentUrl,a.apply_dep applyDep, a.pay_dep payDep, a.eaaid,a.district_name districtName,
        a.user_names userNames,a.create_time, a.release_start,a.release_end, a.duration,b.name appDepName,
        c.name payDepName,d.name ,d.bank ,d.accounts ,t0.amount settlementAmount,
        a.liquidation_status liquidationStatus, a.remarks,
        date_format(a.create_time,'%Y-%m-%d %H:%i:%S')createTimeStr,
        date_format(a.release_start,'%Y-%m-%d %H:%i:%S')releaseStartStr,
        date_format(a.release_end,'%Y-%m-%d %H:%i:%S')releaseEndStr
      FROM expense_account a left join department b on a.apply_dep =b.id left join department c on a.pay_dep =c.id
      left join expense_account_private d on a.eaaid=d.id left join t_order_new o on a.order_no =o.order_no
      left join t_order_mid tm on a.order_no =tm.order_no
      left join (select debit_id ,sum(settlement_amount) amount from expense_account where debit_id is not  null and
      id !=  #{id} group by debit_id )t0 on a.id =t0.debit_id
      WHERE a.status=2 and a.liquidation_status in (0,1,2)
      and a.aid= #{aid} and a.type= #{type} and a.pay_dep= #{depId}
        order by a.create_time desc
    </select>
  <select id="selectStatistics" resultType="com.goafanti.expenseAccount.bo.OutMyEAStatistics">
    select a.aid,a.aname ,a.type,a.status ,a.total_amount totalAmount ,ifnull(a.debit_id,0) debitId ,a.target_type targetType ,ifnull(a2.total_amount,0) bxdkAmount
    from expense_account a
    left join expense_account a2 on a.debit_id =a2.id left join admin ad on a.aid=ad.id
    where 1=1
    <if test="name != null">
      and a.aname like concat('%',#{name},'%')
    </if>
    <if test="depId != null">
      and ad.department_id = #{depId}
    </if>
    <if test="startTime != null and endTime != null">
      and a.create_time between #{startTime} and #{endTime}
    </if>

  </select>
  <select id="selectByDebitId" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from expense_account
    where debit_id = #{debitId}
  </select>

</mapper>
