<?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="cn.timer.api.dao.xcgl.XcglAssoJsgzzxMapper">

    <resultMap id="BaseResultMap" type="cn.timer.api.bean.xcgl.XcglAssoJsgzzx" >
                <id column="id" property="id" />
                <result column="salaryitem" property="salaryitem" />
                <result column="yhmbzd" property="yhmbzd" />
                <result column="rule" property="rule" />
                <result column="type" property="type" />
                <result column="addtime" property="addtime" />
                <result column="adduserid" property="adduserid" />
                <result column="jxgzid" property="jxgzid" />
                <result column="remarks" property="remarks" />
                <result column="gsid" property="gsid" />
                <result column="gsgs" property="gsgs" />
                <result column="xzzid" property="xzzid" />
                <result column="rulestate" property="rulestate" />
                <result column="optionid" property="optionid" />
                <result column="is_custom" property="isCustom" />
                <result column="xzdazdyid" property="xzdazdyid" />
    </resultMap>
    
		    
	<resultMap id="PayTermDtoMap" type="cn.timer.api.dto.xcgl.PayTermDto" >
                <result column="xid" property="xid"/>
		   		<result column="xsalaryitem" property="xsalaryitem"/>
		   		<result column="xyhmbzd" property="xyhmbzd"/>
		   		<result column="xrule" property="xrule"/>
		   		<result column="xtype" property="xtype"/>
		   		<result column="xremarks" property="xremarks"/>
		   		<result column="xgsid" property="xgsid"/>
		   		<result column="xgsgs" property="xgsgs"/>
                <result column="xdygk" property="xdygk"/>
                <result column="xdegk" property="xdegk"/>
		   		<result column="xjxgzid" property="xjxgzid"/>
		   		<result column="xiscustom" property="xiscustom"/>
		   		<result column="xrulestate" property="xrulestate"/>
    </resultMap>
    
    
	<resultMap id="CalculationOptionsDtoMap" type="cn.timer.api.dto.xcgl.CalculationOptionsDto" >
                <result column="id" property="id"/>
		   		<result column="salaryitem" property="salaryitem"/>
		   		<result column="rsz" property="rsz"/>
    </resultMap>
    
    <select id="SalaryRuleGroupItem" resultMap="BaseResultMap">
    	select * from xcgl_asso_jsgzzx js
		where js.xzzid = #{xzzid}
		ORDER BY js.rulestate = 2 or js.rulestate = 3 ASC,
		js.id ASC
    </select>
    
    <select id="selectSalarySectionList" resultMap="BaseResultMap">
    		select jsgzzx.* from xcgl_asso_jsgzzx jsgzzx
				LEFT JOIN xcgl_asso_option as opt on opt.id = jsgzzx.optionid
				where opt.itemid = 2
				and jsgzzx.xzzid = #{xzzid}
    </select>
    
    
    <select id="selectxcglassojxgzList" resultMap="PayTermDtoMap">
			select gzzx.id as xid,
				gzzx.salaryitem as xsalaryitem,
				gzzx.yhmbzd as xyhmbzd,
				gzzx.rule as xrule,
				gzzx.type as xtype,
				gzzx.jxgzid as xjxgzid,
				gzzx.remarks as xremarks,
				gzzx.gsid as xgsid,
				gzzx.gsgs as xgsgs,
				gzzx.dygk as xdygk,
				gzzx.degk as xdegk,
				gzzx.is_custom as xiscustom,
				gzzx.rulestate as xrulestate
			from xcgl_asso_jsgzzx gzzx
		where gzzx.jxgzid = 0
		and gzzx.xzzid = #{xzzid}
	</select>
    
    <select id="selectCalculationOptionsList" resultMap="CalculationOptionsDtoMap">
    		select jsgzzx.id,
				jsgzzx.salaryitem,
				daz.rsz
			from xcgl_asso_jsgzzx jsgzzx
		LEFT JOIN xcgl_asso_xzdazdy as zdy on zdy.zdmc = jsgzzx.salaryitem
		LEFT JOIN xcgl_asso_xzdaz as daz on daz.xzdazdyid = zdy.id
		LEFT JOIN xcgl_asso_xzdadx as xzdadx on xzdadx.id = daz.xzdadxid and xzdadx.txzt = 0
		where jsgzzx.xzzid = #{xzzid}
		and jsgzzx.rulestate = 1
		and xzdadx.userid = #{userid}
    </select>
    
     <delete id="deleteByxzzid" >
        DELETE FROM xcgl_asso_jsgzzx
        WHERE xzzid = #{xzzid}
     </delete>
    <!-- 删除薪资档案值的同时删除已添加到薪资组(未锁定的组)中的薪资项 -->
    <delete id="deleteByxzdazdyid">
        delete jsgzzx from xcgl_asso_jsgzzx jsgzzx, xcgl_admin_xzz xxz
        where jsgzzx.xzdazdyid = #{xzdazdyid} and xxz.is_xzsd = 0
        and jsgzzx.xzzid = xxz.id
    </delete>
    
    <insert id="insertxcglassojsgzzxList" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id" >
	    insert into xcgl_asso_jsgzzx (salaryitem,yhmbzd,rule,type,addtime,adduserid,jxgzid,remarks,gsid,gsgs,dygk,degk,xzzid,rulestate,optionid)
	    <foreach collection="list" item="item" index="index" open="values " close="" separator=",">
	   	(
	      <if test="item.salaryitem != null" >
	        #{item.salaryitem,jdbcType=VARCHAR},
	      </if>
	      <if test="item.yhmbzd != null" >
	        #{item.yhmbzd,jdbcType=VARCHAR},
	      </if>
	      <if test="item.rule != null" >
	        #{item.rule,jdbcType=VARCHAR},
	      </if>
	      <if test="item.type != null" >
	        #{item.type,jdbcType=INTEGER},
	      </if>
	      <if test="item.addtime != null" >
	        #{item.addtime,jdbcType=BIGINT},
	      </if>
	      <if test="item.adduserid != null" >
	        #{item.adduserid,jdbcType=INTEGER},
	      </if>
	      <if test="item.jxgzid != null" >
	        #{item.jxgzid,jdbcType=INTEGER},
	      </if>
	      <if test="item.remarks != null" >
	        #{item.remarks,jdbcType=VARCHAR},
	      </if>
	      <if test="item.gsid != null" >
	        #{item.gsid,jdbcType=INTEGER},
	      </if>
	      <if test="item.gsgs != null" >
	        #{item.gsgs,jdbcType=VARCHAR},
	      </if>

            <if test="item.dygk != null" >
                #{item.dygk,jdbcType=DOUBLE},
            </if>
            <if test="item.degk != null" >
                #{item.degk,jdbcType=DOUBLE},
            </if>

	      <if test="item.xzzid != null" >
	        #{item.xzzid,jdbcType=INTEGER},
	      </if>
	      <if test="item.rulestate != null" >
	        #{item.rulestate,jdbcType=INTEGER},
	      </if>
	      <if test="item.optionid != null" >
	        #{item.optionid,jdbcType=INTEGER}
	      </if>
	      )
	    </foreach>
  	 </insert>
  	 
  	 <insert id="insertxcglassojsgzzxListTwo" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id" >
	    insert into xcgl_asso_jsgzzx (salaryitem,yhmbzd,rule,type,addtime,adduserid,jxgzid,remarks,gsid,gsgs,dygk,degk,xzzid,rulestate,optionid,xzdazdyid)
	    <foreach collection="list" item="item" index="index" open="values " close="" separator=",">
	   	(
	        #{item.salaryitem,jdbcType=VARCHAR},
	        #{item.yhmbzd,jdbcType=VARCHAR},
	        #{item.rule,jdbcType=VARCHAR},
	        #{item.type,jdbcType=INTEGER},
	        #{item.addtime,jdbcType=BIGINT},
	        #{item.adduserid,jdbcType=INTEGER},
	        #{item.jxgzid,jdbcType=INTEGER},
	        #{item.remarks,jdbcType=VARCHAR},
	        #{item.gsid,jdbcType=INTEGER},
	        #{item.gsgs,jdbcType=VARCHAR},
              #{item.dygk,jdbcType=DOUBLE},
              #{item.degk,jdbcType=DOUBLE},
	        #{item.xzzid,jdbcType=INTEGER},
	        #{item.rulestate,jdbcType=INTEGER},
	        #{item.optionid,jdbcType=INTEGER},
	        #{item.xzdazdyid,jdbcType=INTEGER}
	      )
	    </foreach>
  	 </insert>


    <insert id="jsgzzxinsert" useGeneratedKeys="true" keyProperty="id" parameterType="cn.timer.api.bean.xcgl.XcglAssoJsgzzx">
        INSERT INTO xcgl_asso_jsgzzx
        <trim prefix="(" suffix=")" suffixOverrides=",">
                        <if test ='null != salaryitem'>
                        salaryitem,
                        </if>
                        <if test ='null != yhmbzd'>
                        yhmbzd,
                        </if>
                        <if test ='null != rule'>
                        rule,
                        </if>
                        <if test ='null != type'>
                        type,
                        </if>
                        <if test ='null != addtime'>
                        addtime,
                        </if>
                        <if test ='null != adduserid'>
                        adduserid,
                        </if>
                        <if test ='null != jxgzid'>
                        jxgzid,
                        </if>
                        <if test ='null != remarks'>
                        remarks,
                        </if>
                        <if test ='null != gsid'>
                        gsid,
                        </if>
                        <if test ='null != gsgs'>
                        gsgs,
                        </if>
                        <if test ='null != xzzid'>
                        xzzid,
                        </if>
                        <if test ='null != rulestate'>
                        rulestate,
                        </if>
                        <if test ='null != optionid'>
                        optionid,
                        </if>
                        <if test ='null != isCustom'>
                        is_custom
                        </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
                        <if test ='null != salaryitem'>
                        #{salaryitem},
                        </if>
                        <if test ='null != yhmbzd'>
                        #{yhmbzd},
                        </if>
                        <if test ='null != rule'>
                        #{rule},
                        </if>
                        <if test ='null != type'>
                        #{type},
                        </if>
                        <if test ='null != addtime'>
                        #{addtime},
                        </if>
                        <if test ='null != adduserid'>
                        #{adduserid},
                        </if>
                        <if test ='null != jxgzid'>
                        #{jxgzid},
                        </if>
                        <if test ='null != remarks'>
                        #{remarks},
                        </if>
                        <if test ='null != gsid'>
                        #{gsid},
                        </if>
                        <if test ='null != gsgs'>
                        #{gsgs},
                        </if>
                        <if test ='null != xzzid'>
                        #{xzzid},
                        </if>
                        <if test ='null != rulestate'>
                        #{rulestate},
                        </if>
                        <if test ='null != optionid'>
                        #{optionid},
                        </if>
                        <if test ='null != isCustom'>
                        #{isCustom}
                        </if>
        </trim>
    </insert>

    <update id="jsgzzxupdate" parameterType="cn.timer.api.bean.xcgl.XcglAssoJsgzzx">
        UPDATE xcgl_asso_jsgzzx
        <set>
                    <if test ='null != salaryitem'>salaryitem = #{salaryitem},</if>
                    <if test ='null != yhmbzd'>yhmbzd = #{yhmbzd},</if>
                    <if test ='null != rule'>rule = #{rule},</if>
                    <if test ='null != type'>type = #{type},</if>
                    <if test ='null != addtime'>addtime = #{addtime},</if>
                    <if test ='null != adduserid'>adduserid = #{adduserid},</if>
                    <if test ='null != jxgzid'>jxgzid = #{jxgzid},</if>
                    <if test ='null != remarks'>remarks = #{remarks},</if>
                    <if test ='null != gsid'>gsid = #{gsid},</if>
                    <if test ='null != gsgs'>gsgs = #{gsgs},</if>
                    <if test ='null != xzzid'>xzzid = #{xzzid},</if>
                    <if test ='null != rulestate'>rulestate = #{rulestate},</if>
                    <if test ='null != optionid'>optionid = #{optionid},</if>
                    <if test ='null != optionid'>is_custom = #{isCustom}</if>
        </set>
        WHERE id = #{id}
    </update>



</mapper>