<?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.dhcc.finance.main.dao.CwGlSettleMapper">
  <resultMap id="BaseResultMap" type="com.dhcc.finance.main.domain.CwGlSettle">
    <id column="PK_SETTLE" jdbcType="VARCHAR" property="pkSettle" />
    <result column="PK_GLORGBOOK" jdbcType="VARCHAR" property="pkGlorgbook" />
    <result column="PERIOD" jdbcType="VARCHAR" property="period" />
    <result column="ISSETTLE" jdbcType="VARCHAR" property="issettle" />
    <result column="SETTLE_USER" jdbcType="VARCHAR" property="settleUser" />
    <result column="SETTLE_TIME" jdbcType="DATE" property="settleTime" />
    <result column="USE_STATE" jdbcType="VARCHAR" property="useState" />
    <result column="CREATE_USER" jdbcType="VARCHAR" property="createUser" />
    <result column="CREATE_TIME" jdbcType="TIMESTAMP" property="createTime" />
    <result column="UPDATE_USER" jdbcType="VARCHAR" property="updateUser" />
    <result column="UPDATE_TIME" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="REMARK" jdbcType="VARCHAR" property="remark" />
  </resultMap>
  
  <resultMap id="BaseResultMap1" type="com.dhcc.finance.main.model.CwGlSettleAndName">
    <id column="PK_SETTLE" jdbcType="VARCHAR" property="pkSettle" />
    <result column="PK_GLORGBOOK" jdbcType="VARCHAR" property="pkGlorgbook" />
    <result column="NAME" jdbcType="VARCHAR" property="name" />
    <result column="PERIOD" jdbcType="VARCHAR" property="period" />
    <result column="ISSETTLE" jdbcType="VARCHAR" property="issettle" />
    <result column="SETTLE_USER" jdbcType="VARCHAR" property="settleUser" />
    <result column="SETTLE_TIME" jdbcType="DATE" property="settleTime" />
    <result column="USE_STATE" jdbcType="VARCHAR" property="useState" />
    <result column="CREATE_USER" jdbcType="VARCHAR" property="createUser" />
    <result column="CREATE_TIME" jdbcType="TIMESTAMP" property="createTime" />
    <result column="UPDATE_USER" jdbcType="VARCHAR" property="updateUser" />
    <result column="UPDATE_TIME" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="REMARK" jdbcType="VARCHAR" property="remark" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause">
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List">
    PK_SETTLE, PK_GLORGBOOK, PERIOD, ISSETTLE, SETTLE_USER, SETTLE_TIME, USE_STATE, CREATE_USER, 
    CREATE_TIME, UPDATE_USER, UPDATE_TIME, REMARK
  </sql>
  <select id="selectByExample" parameterType="com.dhcc.finance.main.domain.CwGlSettleExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from cw_gl_settle
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>
  <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from cw_gl_settle
    where PK_SETTLE = #{pkSettle,jdbcType=VARCHAR}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String">
    delete from cw_gl_settle
    where PK_SETTLE = #{pkSettle,jdbcType=VARCHAR}
  </delete>
  <delete id="deleteByExample" parameterType="com.dhcc.finance.main.domain.CwGlSettleExample">
    delete from cw_gl_settle
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.dhcc.finance.main.domain.CwGlSettle">
    insert into cw_gl_settle (PK_SETTLE, PK_GLORGBOOK, PERIOD, 
      ISSETTLE, SETTLE_USER, SETTLE_TIME, 
      USE_STATE, CREATE_USER, CREATE_TIME, 
      UPDATE_USER, UPDATE_TIME, REMARK
      )
    values (#{pkSettle,jdbcType=VARCHAR}, #{pkGlorgbook,jdbcType=VARCHAR}, #{period,jdbcType=VARCHAR}, 
      #{issettle,jdbcType=VARCHAR}, #{settleUser,jdbcType=VARCHAR}, #{settleTime,jdbcType=DATE}, 
      #{useState,jdbcType=VARCHAR}, #{createUser,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, 
      #{updateUser,jdbcType=VARCHAR}, #{updateTime,jdbcType=TIMESTAMP}, #{remark,jdbcType=VARCHAR}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.dhcc.finance.main.domain.CwGlSettle">
    insert into cw_gl_settle
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="pkSettle != null">
        PK_SETTLE,
      </if>
      <if test="pkGlorgbook != null">
        PK_GLORGBOOK,
      </if>
      <if test="period != null">
        PERIOD,
      </if>
      <if test="issettle != null">
        ISSETTLE,
      </if>
      <if test="settleUser != null">
        SETTLE_USER,
      </if>
      <if test="settleTime != null">
        SETTLE_TIME,
      </if>
      <if test="useState != null">
        USE_STATE,
      </if>
      <if test="createUser != null">
        CREATE_USER,
      </if>
      <if test="createTime != null">
        CREATE_TIME,
      </if>
      <if test="updateUser != null">
        UPDATE_USER,
      </if>
      <if test="updateTime != null">
        UPDATE_TIME,
      </if>
      <if test="remark != null">
        REMARK,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="pkSettle != null">
        #{pkSettle,jdbcType=VARCHAR},
      </if>
      <if test="pkGlorgbook != null">
        #{pkGlorgbook,jdbcType=VARCHAR},
      </if>
      <if test="period != null">
        #{period,jdbcType=VARCHAR},
      </if>
      <if test="issettle != null">
        #{issettle,jdbcType=VARCHAR},
      </if>
      <if test="settleUser != null">
        #{settleUser,jdbcType=VARCHAR},
      </if>
      <if test="settleTime != null">
        #{settleTime,jdbcType=DATE},
      </if>
      <if test="useState != null">
        #{useState,jdbcType=VARCHAR},
      </if>
      <if test="createUser != null">
        #{createUser,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateUser != null">
        #{updateUser,jdbcType=VARCHAR},
      </if>
      <if test="updateTime != null">
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="remark != null">
        #{remark,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.dhcc.finance.main.domain.CwGlSettleExample" resultType="java.lang.Long">
    select count(*) from cw_gl_settle
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    update cw_gl_settle
    <set>
      <if test="record.pkSettle != null">
        PK_SETTLE = #{record.pkSettle,jdbcType=VARCHAR},
      </if>
      <if test="record.pkGlorgbook != null">
        PK_GLORGBOOK = #{record.pkGlorgbook,jdbcType=VARCHAR},
      </if>
      <if test="record.period != null">
        PERIOD = #{record.period,jdbcType=VARCHAR},
      </if>
      <if test="record.issettle != null">
        ISSETTLE = #{record.issettle,jdbcType=VARCHAR},
      </if>
      <if test="record.settleUser != null">
        SETTLE_USER = #{record.settleUser,jdbcType=VARCHAR},
      </if>
      <if test="record.settleTime != null">
        SETTLE_TIME = #{record.settleTime,jdbcType=DATE},
      </if>
      <if test="record.useState != null">
        USE_STATE = #{record.useState,jdbcType=VARCHAR},
      </if>
      <if test="record.createUser != null">
        CREATE_USER = #{record.createUser,jdbcType=VARCHAR},
      </if>
      <if test="record.createTime != null">
        CREATE_TIME = #{record.createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="record.updateUser != null">
        UPDATE_USER = #{record.updateUser,jdbcType=VARCHAR},
      </if>
      <if test="record.updateTime != null">
        UPDATE_TIME = #{record.updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="record.remark != null">
        REMARK = #{record.remark,jdbcType=VARCHAR},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    update cw_gl_settle
    set PK_SETTLE = #{record.pkSettle,jdbcType=VARCHAR},
      PK_GLORGBOOK = #{record.pkGlorgbook,jdbcType=VARCHAR},
      PERIOD = #{record.period,jdbcType=VARCHAR},
      ISSETTLE = #{record.issettle,jdbcType=VARCHAR},
      SETTLE_USER = #{record.settleUser,jdbcType=VARCHAR},
      SETTLE_TIME = #{record.settleTime,jdbcType=DATE},
      USE_STATE = #{record.useState,jdbcType=VARCHAR},
      CREATE_USER = #{record.createUser,jdbcType=VARCHAR},
      CREATE_TIME = #{record.createTime,jdbcType=TIMESTAMP},
      UPDATE_USER = #{record.updateUser,jdbcType=VARCHAR},
      UPDATE_TIME = #{record.updateTime,jdbcType=TIMESTAMP},
      REMARK = #{record.remark,jdbcType=VARCHAR}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.dhcc.finance.main.domain.CwGlSettle">
    update cw_gl_settle
    <set>
      <if test="pkGlorgbook != null">
        PK_GLORGBOOK = #{pkGlorgbook,jdbcType=VARCHAR},
      </if>
      <if test="period != null">
        PERIOD = #{period,jdbcType=VARCHAR},
      </if>
      <if test="issettle != null">
        ISSETTLE = #{issettle,jdbcType=VARCHAR},
      </if>
      <if test="settleUser != null">
        SETTLE_USER = #{settleUser,jdbcType=VARCHAR},
      </if>
      <if test="settleTime != null">
        SETTLE_TIME = #{settleTime,jdbcType=DATE},
      </if>
      <if test="useState != null">
        USE_STATE = #{useState,jdbcType=VARCHAR},
      </if>
      <if test="createUser != null">
        CREATE_USER = #{createUser,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        CREATE_TIME = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateUser != null">
        UPDATE_USER = #{updateUser,jdbcType=VARCHAR},
      </if>
      <if test="updateTime != null">
        UPDATE_TIME = #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="remark != null">
        REMARK = #{remark,jdbcType=VARCHAR},
      </if>
    </set>
    where PK_SETTLE = #{pkSettle,jdbcType=VARCHAR}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.dhcc.finance.main.domain.CwGlSettle">
    update cw_gl_settle
    set PK_GLORGBOOK = #{pkGlorgbook,jdbcType=VARCHAR},
      PERIOD = #{period,jdbcType=VARCHAR},
      ISSETTLE = #{issettle,jdbcType=VARCHAR},
      SETTLE_USER = #{settleUser,jdbcType=VARCHAR},
      SETTLE_TIME = #{settleTime,jdbcType=DATE},
      USE_STATE = #{useState,jdbcType=VARCHAR},
      CREATE_USER = #{createUser,jdbcType=VARCHAR},
      CREATE_TIME = #{createTime,jdbcType=TIMESTAMP},
      UPDATE_USER = #{updateUser,jdbcType=VARCHAR},
      UPDATE_TIME = #{updateTime,jdbcType=TIMESTAMP},
      REMARK = #{remark,jdbcType=VARCHAR}
    where PK_SETTLE = #{pkSettle,jdbcType=VARCHAR}
  </update>
   <!-- 查询结账信息 -->
  <select id="selectAllDateByPeriodLike" resultMap="BaseResultMap1">
	select PK_SETTLE,PK_GLORGBOOK,
	(select NAME FROM cw_bd_glorgbook b where cgs.PK_GLORGBOOK=b.PK_GLORGBOOK) as NAME, 
	PERIOD, ISSETTLE,
	(select USER_NAME from sys_user_info b  where b.USER_ID = cgs.SETTLE_USER) AS SETTLE_USER,SETTLE_TIME, 
	USE_STATE,CREATE_USER,CREATE_TIME,UPDATE_USER,UPDATE_TIME, REMARK from cw_gl_settle  cgs
  	where cgs.USE_STATE='1'
  	<if test="periodLike != null">
       and cgs.PERIOD =  #{periodLike,jdbcType=VARCHAR}
      </if>
      <if test="nameLike != null">
       and cgs.PK_GLORGBOOK = #{nameLike,jdbcType=VARCHAR}
      </if>
      <if test="issettle != null">
       and cgs.ISSETTLE =  #{issettle,jdbcType=VARCHAR}
      </if>
      <if test="settleUser != null">
       and cgs.SETTLE_USER LIKE  "%"#{settleUser,jdbcType=VARCHAR}"%"
      </if>
      <if test="settleTime != null">
       and cgs.SETTLE_TIME =  #{settleTime,jdbcType=VARCHAR}
      </if>
      order by cgs.PERIOD desc
  </select>
    <select id="selectAllDateByName" resultMap="BaseResultMap1">
	select PK_SETTLE,PK_GLORGBOOK,
	(select NAME FROM cw_bd_glorgbook b where cgs.PK_GLORGBOOK=b.PK_GLORGBOOK) as NAME, 
	PERIOD, ISSETTLE,
	(select USER_NAME from sys_user_info b  where b.USER_ID = cgs.SETTLE_USER) AS SETTLE_USER,SETTLE_TIME, 
	USE_STATE,CREATE_USER,CREATE_TIME,UPDATE_USER,UPDATE_TIME, REMARK from cw_gl_settle  cgs
  	where cgs.USE_STATE='1'
      <if test="nameLike != null">
       and cgs.PK_GLORGBOOK = #{nameLike,jdbcType=VARCHAR}
      </if>
      order by cgs.PERIOD desc
  </select>
  <!-- 查询账套期间已记账的数量-->
  <select id="countIsSettle" resultType="java.lang.Long" parameterType="java.lang.String">
  select count(*) from cw_gl_settle
    where PK_GLORGBOOK=#{pkGlorgbook,jdbcType=VARCHAR} and PERIOD = #{period,jdbcType=VARCHAR}
     and ISSETTLE = '1' and USE_STATE='1'
  </select>
  <!--批量更新结账信息  -->
   <update id="updateCwGlSettleBatch" parameterType="java.util.Map">
    update cw_gl_settle
    set 
      UPDATE_USER = #{updateUser,jdbcType=VARCHAR},
      UPDATE_TIME = #{updateTime,jdbcType=TIMESTAMP},
      ISSETTLE = #{issettle,jdbcType=VARCHAR},
      SETTLE_USER = #{settleUser,jdbcType=VARCHAR},
      SETTLE_TIME = #{settleTime,jdbcType=DATE}
    where PK_SETTLE in
    <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
       #{item.pkSettle,jdbcType=VARCHAR}
   </foreach>
  </update>
  <!-- 查询是否结账数据 -->
  <select id="selectIssettle" parameterType="java.util.Map" resultType="java.util.Map">
  	select  (select count(*) from  CW_GL_SETTLE where  USE_STATE = '1' and PK_GLORGBOOK = #{pkGlorgbook,jdbcType=VARCHAR} and PERIOD = #{period,jdbcType=VARCHAR} ) as allCount ,
  	        (select count(*) from  CW_GL_SETTLE where  USE_STATE = '1' and ISSETTLE ='1' and PK_GLORGBOOK = #{pkGlorgbook,jdbcType=VARCHAR} and PERIOD = #{period,jdbcType=VARCHAR} ) as settleCount
    from dual
  </select>
<!--    获取当前账套最早未结账的期间 -->
  <select id="selectNoSettleForPeriod" parameterType="java.lang.String" resultType="java.lang.String">
  <![CDATA[	select 
  		case when right(max(period),'2')>='12' then concat(left(max(period),'4')+1,"-01") 
  		else (case when right(max(period),'2')>8 then concat(left(max(period),'4'),"-",right(max(period),'2')+1)  
  		else concat(left(max(period),'4'),"-0",right(max(period),'2')+1) end ) end as period  
  	from cw_gl_settle 
  	where PK_GLORGBOOK= #{pkGlorgbook,jdbcType=VARCHAR}
  	and ISSETTLE= '1' 
  	  and period >= #{period,jdbcType=VARCHAR} ]]>
  	order by period 
  </select>
  <!-- 记账数据统计 -->
  <select id="countSettleTotal" resultType="java.lang.Long" parameterType="java.lang.String">
    select count(PK_SETTLE) from cw_gl_settle cgs
  	where 
  	USE_STATE = '1'  and  ISSETTLE = #{issettle,jdbcType=VARCHAR}
  	<if test="periodLike != null">
       and cgs.PERIOD   = #{periodLike,jdbcType=VARCHAR}
      </if>
      <if test="nameLike != null">
       and cgs.PK_GLORGBOOK = #{nameLike,jdbcType=VARCHAR}
      </if>
      <if test="settleUser != null">
       and cgs.SETTLE_USER = #{settleUser,jdbcType=VARCHAR}
      </if>
      <if test="settleTime != null">
       and cgs.SETTLE_TIME  = #{settleTime,jdbcType=VARCHAR}
      </if>
  </select>
      <!-- 初始化结账数据 -->
   <insert id="insertForSettle" parameterType="java.util.Map" >
  	 insert into cw_gl_settle (PK_SETTLE,PK_GLORGBOOK, PERIOD, ISSETTLE, SETTLE_USER, SETTLE_TIME,USE_STATE,CREATE_USER,CREATE_TIME,UPDATE_USER)
    select MD5(uuid()) as PK_SETTLE, pk_glorgbook,#{period,jdbcType=VARCHAR} as PERIOD ,'0' as ISSETTLE,NULL as SETTLE_USER,NULL as SETTLE_TIME,
    '1' as USE_STATE, #{createUser,jdbcType=VARCHAR} as CREATE_USER ,sysdate() as CREATE_TIME,#{updateUser,jdbcType=VARCHAR} as UPDATE_USER from CW_BD_GLORGBOOK a where not exists
    	(select 1 from CW_GL_SETTLE b where a.pk_glorgbook=b.pk_glorgbook and  b.period=#{period,jdbcType=VARCHAR})
  </insert>
  <select id="selectMaxPeriod" resultType="java.lang.String" parameterType="java.lang.String">
      select max(PERIOD) from cw_gl_settle a  where USE_STATE='1' and PK_GLORGBOOK = #{pkGlorgbook,jdbcType=VARCHAR}
   and ISSETTLE = '1'
  </select>
    <select id="searchIsCount" resultType="java.lang.String">
     select  glorPeriod from (select  concat(PK_GLORGBOOK,',',PERIOD) as glorPeriod from cw_gl_settle where USE_STATE='1' and ISSETTLE = '1')   a
     where glorPeriod in
    <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
      concat(#{item.pkGlorgbook,jdbcType=VARCHAR},',',#{item.period,jdbcType=VARCHAR})
   </foreach> 
  </select>
</mapper>