<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//oos.juyouhx.com//DTD Mapper 3.0//EN" "http://oss.juyouhx.com/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.scpyun.platform.standard.jilinsscgsdp.eventApprove">

    <select id="getSysOfficeParentId" parameterType="String" resultType="String">
        select parent_id from sys_office where id=#{id}
    </select>

    <select id="getFax" parameterType="String" resultType="String">
        select fax from sys_office where id=#{id}
    </select>

    <select id="getOfficeId" parameterType="String" resultType="String">
        select office_id from sys_user
        where id=(select create_by from sys_office where id=#{id})
    </select>

    <insert id="insertEventApprove" parameterType="com.scpyun.platform.jilinsscgsdp.bean.vo.EventApproveVo">
      insert into jl_event_approve
      (
        id,
        <if test="event_id != null and event_id != ''">event_id,</if>
        <if test="approve_type != null and approve_type != ''">approve_type,</if>
        <if test="approve_desc != null and approve_desc != ''">approve_desc,</if>
        <if test="approve_days != null and approve_days != ''">approve_days,</if>
        deal_user,
        create_by,
        approve_time
      )
      values
      (
        uuid(),
        <if test="event_id != null and event_id != ''">#{event_id},</if>
        <if test="approve_type != null and approve_type != ''">#{approve_type},</if>
        <if test="approve_desc != null and approve_desc != ''">#{approve_desc},</if>
        <if test="approve_days != null and approve_days != ''">
          <!--#{approve_days}
          +
          ifnull((select approve_days from jl_event_approve where del_flag='0' and event_id=#{event_id} order by approve_time desc limit 1),0),-->
          (
            #{approve_days} + ifnull(
              (select t.approve_days from
                (select approve_days from jl_event_approve where del_flag='0' and event_id=#{event_id} order by approve_time desc limit 1) t
              ), 0)
          ),
        </if>
        #{deal_user},
        #{_user.id},
        now()
      )
    </insert>

    <update id="updateEventApprove" parameterType="com.scpyun.platform.jilinsscgsdp.bean.vo.EventApproveVo">
      update jl_event_approve
      <set>
        <if test="deal_result != null and deal_result != ''">deal_result = #{deal_result},</if>
        <if test="deal_desc != null and deal_desc != ''">deal_desc = #{deal_desc},</if>
          deal_user_id = #{_user.id},
          deal_time=now()
      </set>
      where id = #{id}
    </update>

    <select id="eventApproveList" parameterType="com.scpyun.platform.jilinsscgsdp.bean.vo.EventApproveVo" resultType="com.scpyun.platform.jilinsscgsdp.bean.vo.EventApproveVo">
      select
        a.id,
        a.event_id,
        a.approve_type,
        a.approve_time,
        DATE_FORMAT(a.approve_time,'%Y-%m-%d %T') approve_time,
        a.approve_desc,
        a.approve_days,
        b.name as create_by,
        a.deal_result,
        c.name as deal_user_id,
        DATE_FORMAT(a.deal_time,'%Y-%m-%d %T') deal_time,
        a.deal_desc,
        d.event_code,
        e.name as company_name
      from jl_event_approve a
      join sys_user b on a.create_by=b.id
      left join sys_user c on a.deal_user_id=c.id
      left join jl_event d on a.event_id=d.id
      left join sys_office e on a.deal_user=e.id
      where a.del_flag='0'
      <if test="approve_type != null and approve_type != ''">
        and a.approve_type = #{approve_type}
      </if>
      <if test="deal_result != null and deal_result != ''">
        and a.deal_result = #{deal_result}
      </if>
      <if test="data_type != null and data_type !='' and data_type=='1'.toString()">
        and a.create_by=#{_user.id}
      </if>
      <if test="data_type != null and data_type !='' and data_type=='2'.toString()">
        and a.deal_user=#{_user.company_id}
      </if>
      order by a.approve_time desc,a.deal_result asc
  </select>

</mapper>
