<?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.CwGlFreevalueMapper">
    <resultMap id="BaseResultMap" type="com.dhcc.finance.main.domain.CwGlFreevalue">
        <id column="PK_FREEVALUE" jdbcType="VARCHAR" property="pkFreevalue"/>
        <result column="ASSID" jdbcType="VARCHAR" property="assid"/>
        <result column="ASSINDEX" jdbcType="INTEGER" property="assindex"/>
        <result column="CHECKTYPE" jdbcType="VARCHAR" property="checktype"/>
        <result column="CHECKVALUE" jdbcType="VARCHAR" property="checkvalue"/>
        <result column="VALUECODE" jdbcType="VARCHAR" property="valuecode"/>
        <result column="VALUENAME" jdbcType="VARCHAR" property="valuename"/>
        <result column="DEBITAMOUNT" jdbcType="DECIMAL" property="debitamount"/>
        <result column="CREDITAMOUNT" jdbcType="DECIMAL" property="creditamount"/>
        <result column="PK_SYSTEM" jdbcType="VARCHAR" property="pkSystem"/>
        <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="BaseResultMap2" type="com.dhcc.finance.main.model.CwGlFreevalueInfo">
        <result column="CHECKTYPE" jdbcType="VARCHAR" property="checktype"/>
        <result column="CHECKVALUE" jdbcType="VARCHAR" property="checkvalue"/>
        <result column="DEBITAMOUNT" jdbcType="DECIMAL" property="debitamount"/>
        <result column="PK_SYSTEM" jdbcType="VARCHAR" property="pkSystem"/>
    </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_FREEVALUE, ASSID, ASSINDEX, CHECKTYPE, CHECKVALUE, VALUECODE, VALUENAME, DEBITAMOUNT,
    CREDITAMOUNT, PK_SYSTEM, USE_STATE, CREATE_USER, CREATE_TIME, UPDATE_USER, UPDATE_TIME, 
    REMARK
    </sql>
    <select id="selectByExample" parameterType="com.dhcc.finance.main.domain.CwGlFreevalueExample"
            resultMap="BaseResultMap">
        select
        <if test="distinct">
            distinct
        </if>
        <include refid="Base_Column_List"/>
        from cw_gl_freevalue
        <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_freevalue
        where PK_FREEVALUE = #{pkFreevalue,jdbcType=VARCHAR}
    </select>
    <delete id="deleteByPrimaryKey" parameterType="java.lang.String">
        delete
        from cw_gl_freevalue
        where PK_FREEVALUE = #{pkFreevalue,jdbcType=VARCHAR}
    </delete>
    <delete id="deleteByExample" parameterType="com.dhcc.finance.main.domain.CwGlFreevalueExample">
        delete from cw_gl_freevalue
        <if test="_parameter != null">
            <include refid="Example_Where_Clause"/>
        </if>
    </delete>
    <insert id="insert" parameterType="com.dhcc.finance.main.domain.CwGlFreevalue">
        insert into cw_gl_freevalue (PK_FREEVALUE, ASSID, ASSINDEX,
                                     CHECKTYPE, CHECKVALUE, VALUECODE,
                                     VALUENAME, DEBITAMOUNT, CREDITAMOUNT,
                                     PK_SYSTEM, USE_STATE, CREATE_USER,
                                     CREATE_TIME, UPDATE_USER, UPDATE_TIME,
                                     REMARK)
        values (#{pkFreevalue,jdbcType=VARCHAR}, #{assid,jdbcType=VARCHAR}, #{assindex,jdbcType=INTEGER},
                #{checktype,jdbcType=VARCHAR}, #{checkvalue,jdbcType=VARCHAR}, #{valuecode,jdbcType=VARCHAR},
                #{valuename,jdbcType=VARCHAR}, #{debitamount,jdbcType=DECIMAL}, #{creditamount,jdbcType=DECIMAL},
                #{pkSystem,jdbcType=VARCHAR}, #{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.CwGlFreevalue">
        insert into cw_gl_freevalue
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="pkFreevalue != null">
                PK_FREEVALUE,
            </if>
            <if test="assid != null">
                ASSID,
            </if>
            <if test="assindex != null">
                ASSINDEX,
            </if>
            <if test="checktype != null">
                CHECKTYPE,
            </if>
            <if test="checkvalue != null">
                CHECKVALUE,
            </if>
            <if test="valuecode != null">
                VALUECODE,
            </if>
            <if test="valuename != null">
                VALUENAME,
            </if>
            <if test="debitamount != null">
                DEBITAMOUNT,
            </if>
            <if test="creditamount != null">
                CREDITAMOUNT,
            </if>
            <if test="pkSystem != null">
                PK_SYSTEM,
            </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="pkFreevalue != null">
                #{pkFreevalue,jdbcType=VARCHAR},
            </if>
            <if test="assid != null">
                #{assid,jdbcType=VARCHAR},
            </if>
            <if test="assindex != null">
                #{assindex,jdbcType=INTEGER},
            </if>
            <if test="checktype != null">
                #{checktype,jdbcType=VARCHAR},
            </if>
            <if test="checkvalue != null">
                #{checkvalue,jdbcType=VARCHAR},
            </if>
            <if test="valuecode != null">
                #{valuecode,jdbcType=VARCHAR},
            </if>
            <if test="valuename != null">
                #{valuename,jdbcType=VARCHAR},
            </if>
            <if test="debitamount != null">
                #{debitamount,jdbcType=DECIMAL},
            </if>
            <if test="creditamount != null">
                #{creditamount,jdbcType=DECIMAL},
            </if>
            <if test="pkSystem != null">
                #{pkSystem,jdbcType=VARCHAR},
            </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.CwGlFreevalueExample"
            resultType="java.lang.Long">
        select count(*) from cw_gl_freevalue
        <if test="_parameter != null">
            <include refid="Example_Where_Clause"/>
        </if>
    </select>
    <update id="updateByExampleSelective" parameterType="map">
        update cw_gl_freevalue
        <set>
            <if test="record.pkFreevalue != null">
                PK_FREEVALUE = #{record.pkFreevalue,jdbcType=VARCHAR},
            </if>
            <if test="record.assid != null">
                ASSID = #{record.assid,jdbcType=VARCHAR},
            </if>
            <if test="record.assindex != null">
                ASSINDEX = #{record.assindex,jdbcType=INTEGER},
            </if>
            <if test="record.checktype != null">
                CHECKTYPE = #{record.checktype,jdbcType=VARCHAR},
            </if>
            <if test="record.checkvalue != null">
                CHECKVALUE = #{record.checkvalue,jdbcType=VARCHAR},
            </if>
            <if test="record.valuecode != null">
                VALUECODE = #{record.valuecode,jdbcType=VARCHAR},
            </if>
            <if test="record.valuename != null">
                VALUENAME = #{record.valuename,jdbcType=VARCHAR},
            </if>
            <if test="record.debitamount != null">
                DEBITAMOUNT = #{record.debitamount,jdbcType=DECIMAL},
            </if>
            <if test="record.creditamount != null">
                CREDITAMOUNT = #{record.creditamount,jdbcType=DECIMAL},
            </if>
            <if test="record.pkSystem != null">
                PK_SYSTEM = #{record.pkSystem,jdbcType=VARCHAR},
            </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_freevalue
        set PK_FREEVALUE = #{record.pkFreevalue,jdbcType=VARCHAR},
        ASSID = #{record.assid,jdbcType=VARCHAR},
        ASSINDEX = #{record.assindex,jdbcType=INTEGER},
        CHECKTYPE = #{record.checktype,jdbcType=VARCHAR},
        CHECKVALUE = #{record.checkvalue,jdbcType=VARCHAR},
        VALUECODE = #{record.valuecode,jdbcType=VARCHAR},
        VALUENAME = #{record.valuename,jdbcType=VARCHAR},
        DEBITAMOUNT = #{record.debitamount,jdbcType=DECIMAL},
        CREDITAMOUNT = #{record.creditamount,jdbcType=DECIMAL},
        PK_SYSTEM = #{record.pkSystem,jdbcType=VARCHAR},
        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.CwGlFreevalue">
        update cw_gl_freevalue
        <set>
            <if test="assid != null">
                ASSID = #{assid,jdbcType=VARCHAR},
            </if>
            <if test="assindex != null">
                ASSINDEX = #{assindex,jdbcType=INTEGER},
            </if>
            <if test="checktype != null">
                CHECKTYPE = #{checktype,jdbcType=VARCHAR},
            </if>
            <if test="checkvalue != null">
                CHECKVALUE = #{checkvalue,jdbcType=VARCHAR},
            </if>
            <if test="valuecode != null">
                VALUECODE = #{valuecode,jdbcType=VARCHAR},
            </if>
            <if test="valuename != null">
                VALUENAME = #{valuename,jdbcType=VARCHAR},
            </if>
            <if test="debitamount != null">
                DEBITAMOUNT = #{debitamount,jdbcType=DECIMAL},
            </if>
            <if test="creditamount != null">
                CREDITAMOUNT = #{creditamount,jdbcType=DECIMAL},
            </if>
            <if test="pkSystem != null">
                PK_SYSTEM = #{pkSystem,jdbcType=VARCHAR},
            </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_FREEVALUE = #{pkFreevalue,jdbcType=VARCHAR}
    </update>
    <update id="updateByPrimaryKey" parameterType="com.dhcc.finance.main.domain.CwGlFreevalue">
        update cw_gl_freevalue
        set ASSID        = #{assid,jdbcType=VARCHAR},
            ASSINDEX     = #{assindex,jdbcType=INTEGER},
            CHECKTYPE    = #{checktype,jdbcType=VARCHAR},
            CHECKVALUE   = #{checkvalue,jdbcType=VARCHAR},
            VALUECODE    = #{valuecode,jdbcType=VARCHAR},
            VALUENAME    = #{valuename,jdbcType=VARCHAR},
            DEBITAMOUNT  = #{debitamount,jdbcType=DECIMAL},
            CREDITAMOUNT = #{creditamount,jdbcType=DECIMAL},
            PK_SYSTEM    = #{pkSystem,jdbcType=VARCHAR},
            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_FREEVALUE = #{pkFreevalue,jdbcType=VARCHAR}
    </update>

    <!--查询assid  -->
    <select id="selectAssid" flushCache="true" parameterType="java.lang.String" resultMap="BaseResultMap">
        select temp.ASSID
        from (
                 select freevalue.ASSID,
                        group_concat(freevalue.CHECKVALUE order by freevalue.CHECKTYPE separator '-') as CHECKVALUES,
                        group_concat(freevalue.CHECKTYPE order by freevalue.CHECKTYPE separator '-')  as CHECKTYPES
                 from (
                          select a.ASSID, a.CHECKTYPE, a.CHECKVALUE
                          from CW_GL_FREEVALUE a
                                   inner join (select ASSID
                                               from CW_GL_FREEVALUE
                                               where CHECKVALUE = #{checkValue,jdbcType=VARCHAR}) b on a.assid = b.assid
                          where a.USE_STATE = '1'
                      ) freevalue
                 group by freevalue.assid
             ) temp
        where temp.CHECKVALUES = #{checkValues,jdbcType=VARCHAR}
          and temp.CHECKTYPES = #{checkTypes,jdbcType=VARCHAR}
    </select>
    <!--批量新增数据  -->
    <insert id="insertCwGlFreevalueBatch" parameterType="java.util.List">
        insert into CW_GL_FREEVALUE (
        PK_FREEVALUE, ASSID, ASSINDEX, CHECKTYPE, CHECKVALUE, VALUECODE, VALUENAME, DEBITAMOUNT,
        CREDITAMOUNT, PK_SYSTEM, USE_STATE, CREATE_USER, CREATE_TIME, UPDATE_USER, UPDATE_TIME,
        REMARK
        )
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.pkFreevalue,jdbcType=VARCHAR}, #{item.assid,jdbcType=VARCHAR}, #{item.assindex,jdbcType=INTEGER},
            #{item.checktype,jdbcType=VARCHAR}, #{item.checkvalue,jdbcType=VARCHAR}, #{item.valuecode,jdbcType=VARCHAR},
            #{item.valuename,jdbcType=VARCHAR}, #{item.debitamount,jdbcType=DECIMAL},
            #{item.creditamount,jdbcType=DECIMAL},
            #{item.pkSystem,jdbcType=VARCHAR}, #{item.useState,jdbcType=VARCHAR}, #{item.createUser,jdbcType=VARCHAR},
            #{item.createTime,jdbcType=TIMESTAMP}, #{item.updateUser,jdbcType=VARCHAR},
            #{item.updateTime,jdbcType=TIMESTAMP},
            #{item.remark,jdbcType=VARCHAR}
            )
        </foreach>
    </insert>
    <!--   删除辅助表数据 -->
    <update id="updateUserStateByAssid" parameterType="java.util.List">
        update CW_GL_FREEVALUE set USE_STATE = '2' where ASSID in
        <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
            #{item,jdbcType=VARCHAR}
        </foreach>
    </update>
    <!--   根据assid查询辅助项信息 -->
    <select id="searchCwGlFreevalueByassid" parameterType="java.lang.String" resultMap="BaseResultMap">
        select PK_FREEVALUE,
               ASSID,
               ASSINDEX,
               CHECKTYPE,
               CHECKVALUE,
               VALUECODE,
               VALUENAME,
               if(DEBITAMOUNT is not null and DEBITAMOUNT != '0' and DEBITAMOUNT != '', DEBITAMOUNT,
                  CREDITAMOUNT) as DEBITAMOUNT,
               CREDITAMOUNT,
               PK_SYSTEM,
               USE_STATE,
               CREATE_USER,
               CREATE_TIME,
               UPDATE_USER,
               UPDATE_TIME,
               REMARK
        from cw_gl_freevalue
        where assid = #{assid,jdbcType=VARCHAR}

    </select>
    <!--   根据assid查询辅助项信息 -->
    <select id="searchByAssid" parameterType="java.lang.String" resultMap="BaseResultMap2">
        select CHECKTYPE,
               CHECKVALUE,
               if(DEBITAMOUNT is not null and DEBITAMOUNT != '0' and DEBITAMOUNT != '', DEBITAMOUNT,
                  CREDITAMOUNT) as DEBITAMOUNT,
               PK_SYSTEM
        from cw_gl_freevalue
        where assid = #{assid,jdbcType=VARCHAR}
          and USE_STATE = '1'
    </select>
    <delete id="deleteByAssid" parameterType="java.util.List">
        delete from cw_gl_freevalue where ASSID in
        <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
            #{item,jdbcType=VARCHAR}
        </foreach>
    </delete>
    <!--
    insert into cw_gl_freevalue (PK_FREEVALUE, ASSID, ASSINDEX, CHECKTYPE, CHECKVALUE, VALUECODE, VALUENAME,
    DEBITAMOUNT, CREDITAMOUNT, PK_SYSTEM, USE_STATE, CREATE_USER, CREATE_TIME, UPDATE_USER,
    REMARK)-->
    <select id="selectCwGlFreevalueBatchByTemp" resultMap="BaseResultMap">

        select MD5(uuid()) PK_FREEVALUE,b.ASSID,#{cwGlVoucherTempRequest.index,jdbcType=VARCHAR}
        ASSINDEX,#{cwBdVouchtempF.pkAccass,jdbcType=VARCHAR}
        CHECKTYPE,#{cwGlVoucherTempRequest.valueName,jdbcType=VARCHAR} VALUENAME
        <if test='cwGlVoucherTempRequest.direction =="1"'>
            ,b.LOCALDEBITAMOUNT DEBITAMOUNT,0 CREDITAMOUNT
        </if>
        <if test='cwGlVoucherTempRequest.direction =="2"'>
            ,0 DEBITAMOUNT ,b.LOCALCREDITAMOUNT CREDITAMOUNT
        </if>
        ,#{cwGlVoucherTempRequest.pkSystem,jdbcType=VARCHAR} PK_SYSTEM,'1' USE_STATE,a.CREATE_USER, a.CREATE_TIME,a.ID REMARK
        from cw_bd_manual_serial a
        left join cw_gl_detail b on b.REMARK = concat(a.ID ,#{cwBdVouchtempF.pkVouchtempB,jdbcType=VARCHAR})
        where BATCH_NO = #{cwGlVoucherTempRequest.batchNo,jdbcType=VARCHAR} and a.USE_STATE ='1' and b.USE_STATE ='1'
                and VOUCHER_TYPE = #{cwGlVoucherTempRequest.voucherType,jdbcType=VARCHAR}
    </select>

    <update id="updateCheckType" parameterType="String">
        update cw_gl_freevalue a, CW_GL_DETAIL b
        <set>
            <if test="checkTypeN != null">
                a.CHECKTYPE = #{checkTypeN,jdbcType=VARCHAR}
            </if>
        </set>
        where a.ASSID = b.ASSID and b.PK_GLORGBOOK=#{pkGlorgbook ,jdbcType=VARCHAR}
        AND  b.SUBJCODE=#{subjcodeO ,jdbcType=VARCHAR} and a.CHECKTYPE = #{checkTypeO ,jdbcType=VARCHAR}
    </update>
</mapper>